diff --git a/lib/card10/pb.c b/lib/card10/pb.c index 727882ddd57a724d25180bea1ae835b2008932c6..b69b834b4e15cb62d884b75c0b24842cd91ab58c 100644 --- a/lib/card10/pb.c +++ b/lib/card10/pb.c @@ -40,6 +40,9 @@ #include "portexpander.h" #include "MAX77650-Arduino-Library.h" #include <stddef.h> + +static const uint8_t expander_pins[] = { 5, 0x0, 3, 6 }; + /******************************************************************************/ int PB_Init(void) { @@ -62,21 +65,51 @@ int PB_Init(void) /******************************************************************************/ int PB_RegisterCallback(unsigned int pb, pb_callback callback) { - MXC_ASSERT(pb < num_pbs); + MXC_ASSERT((pb > 0) && (pb <= num_pbs)); + + if (pb == 2) { + return E_INVALID; + } + + uint8_t mask = (1 << expander_pins[pb - 1]); - // TODO: portexpander support if (callback) { - // Register callback - GPIO_RegisterCallback(&pb_pin[pb], callback, (void *)pb); + if (portexpander_detected()) { + // Register callback + portexpander_register_callback( + mask, callback, (void *)pb + ); - // Configure and enable interrupt - GPIO_IntConfig(&pb_pin[pb], GPIO_INT_EDGE, GPIO_INT_FALLING); - GPIO_IntEnable(&pb_pin[pb]); - NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(pb_pin[pb].port)); + // Configure and enable interrupt + portexpander_int_config(mask, GPIO_INT_FALLING); + portexpander_int_enable(mask); + } else { + // Register callback + GPIO_RegisterCallback( + &pb_pin[pb - 1], callback, (void *)pb + ); + + // Configure and enable interrupt + GPIO_IntConfig( + &pb_pin[pb - 1], + GPIO_INT_EDGE, + GPIO_INT_FALLING + ); + GPIO_IntEnable(&pb_pin[pb - 1]); + NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ( + pb_pin[pb - 1].port) + ); + } } else { - // Disable interrupt and clear callback - GPIO_IntDisable(&pb_pin[pb]); - GPIO_RegisterCallback(&pb_pin[pb], NULL, NULL); + if (portexpander_detected()) { + // Disable interrupt and clear callback + portexpander_int_disable(mask); + portexpander_register_callback(mask, NULL, NULL); + } else { + // Disable interrupt and clear callback + GPIO_IntDisable(&pb_pin[pb - 1]); + GPIO_RegisterCallback(&pb_pin[pb - 1], NULL, NULL); + } } return E_NO_ERROR; @@ -85,25 +118,46 @@ int PB_RegisterCallback(unsigned int pb, pb_callback callback) //****************************************************************************** void PB_IntEnable(unsigned int pb) { - // TODO: portexpander support - MXC_ASSERT(pb < num_pbs); - GPIO_IntEnable(&pb_pin[pb]); + MXC_ASSERT((pb > 0) && (pb <= num_pbs)); + if (pb == 2) { + return; + } + + if (portexpander_detected()) { + portexpander_int_enable((1 << expander_pins[pb - 1])); + } else { + GPIO_IntEnable(&pb_pin[pb - 1]); + } } //****************************************************************************** void PB_IntDisable(unsigned int pb) { - // TODO: portexpander support - MXC_ASSERT(pb < num_pbs); - GPIO_IntDisable(&pb_pin[pb]); + MXC_ASSERT((pb > 0) && (pb <= num_pbs)); + if (pb == 2) { + return; + } + + if (portexpander_detected()) { + portexpander_int_disable((1 << expander_pins[pb - 1])); + } else { + GPIO_IntDisable(&pb_pin[pb - 1]); + } } //****************************************************************************** void PB_IntClear(unsigned int pb) { - // TODO: portexpander support - MXC_ASSERT(pb < num_pbs); - GPIO_IntClr(&pb_pin[pb]); + MXC_ASSERT((pb > 0) && (pb <= num_pbs)); + if (pb == 2) { + return; + } + + if (portexpander_detected()) { + portexpander_int_clr((1 << expander_pins[pb - 1])); + } else { + GPIO_IntClr(&pb_pin[pb - 1]); + } } //****************************************************************************** @@ -116,8 +170,8 @@ int PB_Get(unsigned int pb) case 3: case 4: if (portexpander_detected()) { - uint8_t port = portexpander_in_get(0xFF); - return (port & (1 << expander_pins[pb - 1])) == 0; + return portexpander_in_get( + (1 << expander_pins[pb - 1])) == 0; } else { return GPIO_InGet(&pb_pin[pb - 1]) == 0; }