Skip to content
Snippets Groups Projects
Commit f63c6728 authored by Florian Kargl's avatar Florian Kargl Committed by rahix
Browse files

feat(pb): Implement callbacks for portexpander buttons

parent c084a687
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,9 @@ ...@@ -40,6 +40,9 @@
#include "portexpander.h" #include "portexpander.h"
#include "MAX77650-Arduino-Library.h" #include "MAX77650-Arduino-Library.h"
#include <stddef.h> #include <stddef.h>
static const uint8_t expander_pins[] = { 5, 0x0, 3, 6 };
/******************************************************************************/ /******************************************************************************/
int PB_Init(void) int PB_Init(void)
{ {
...@@ -62,21 +65,51 @@ int PB_Init(void) ...@@ -62,21 +65,51 @@ int PB_Init(void)
/******************************************************************************/ /******************************************************************************/
int PB_RegisterCallback(unsigned int pb, pb_callback callback) 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) { if (callback) {
if (portexpander_detected()) {
// Register callback
portexpander_register_callback(
mask, callback, (void *)pb
);
// Configure and enable interrupt
portexpander_int_config(mask, GPIO_INT_FALLING);
portexpander_int_enable(mask);
} else {
// Register callback // Register callback
GPIO_RegisterCallback(&pb_pin[pb], callback, (void *)pb); GPIO_RegisterCallback(
&pb_pin[pb - 1], callback, (void *)pb
);
// Configure and enable interrupt // Configure and enable interrupt
GPIO_IntConfig(&pb_pin[pb], GPIO_INT_EDGE, GPIO_INT_FALLING); GPIO_IntConfig(
GPIO_IntEnable(&pb_pin[pb]); &pb_pin[pb - 1],
NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(pb_pin[pb].port)); 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 {
if (portexpander_detected()) {
// Disable interrupt and clear callback
portexpander_int_disable(mask);
portexpander_register_callback(mask, NULL, NULL);
} else { } else {
// Disable interrupt and clear callback // Disable interrupt and clear callback
GPIO_IntDisable(&pb_pin[pb]); GPIO_IntDisable(&pb_pin[pb - 1]);
GPIO_RegisterCallback(&pb_pin[pb], NULL, NULL); GPIO_RegisterCallback(&pb_pin[pb - 1], NULL, NULL);
}
} }
return E_NO_ERROR; return E_NO_ERROR;
...@@ -85,25 +118,46 @@ int PB_RegisterCallback(unsigned int pb, pb_callback callback) ...@@ -85,25 +118,46 @@ int PB_RegisterCallback(unsigned int pb, pb_callback callback)
//****************************************************************************** //******************************************************************************
void PB_IntEnable(unsigned int pb) void PB_IntEnable(unsigned int pb)
{ {
// TODO: portexpander support MXC_ASSERT((pb > 0) && (pb <= num_pbs));
MXC_ASSERT(pb < num_pbs); if (pb == 2) {
GPIO_IntEnable(&pb_pin[pb]); 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) void PB_IntDisable(unsigned int pb)
{ {
// TODO: portexpander support MXC_ASSERT((pb > 0) && (pb <= num_pbs));
MXC_ASSERT(pb < num_pbs); if (pb == 2) {
GPIO_IntDisable(&pb_pin[pb]); 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) void PB_IntClear(unsigned int pb)
{ {
// TODO: portexpander support MXC_ASSERT((pb > 0) && (pb <= num_pbs));
MXC_ASSERT(pb < num_pbs); if (pb == 2) {
GPIO_IntClr(&pb_pin[pb]); 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) ...@@ -116,8 +170,8 @@ int PB_Get(unsigned int pb)
case 3: case 3:
case 4: case 4:
if (portexpander_detected()) { if (portexpander_detected()) {
uint8_t port = portexpander_in_get(0xFF); return portexpander_in_get(
return (port & (1 << expander_pins[pb - 1])) == 0; (1 << expander_pins[pb - 1])) == 0;
} else { } else {
return GPIO_InGet(&pb_pin[pb - 1]) == 0; return GPIO_InGet(&pb_pin[pb - 1]) == 0;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment