Skip to content

Commit 1a3b094

Browse files
committed
fw/drivers/sf32lb52/exti: fix context switching, use HAL
Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
1 parent 8152c99 commit 1a3b094

File tree

1 file changed

+14
-35
lines changed

1 file changed

+14
-35
lines changed

src/fw/drivers/sf32lb52/exti.c

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct {
1919
} ExtiHandlerConfig_t;
2020

2121
static ExtiHandlerConfig_t s_exti_gpio1_handler_configs[EXTI_MAX_GPIO1_PIN_NUM];
22+
static bool s_should_context_switch;
2223

2324
static GPIO_TypeDef *prv_gpio_get_instance(GPIO_TypeDef *hgpio, uint16_t gpio_pin,
2425
uint16_t *offset) {
@@ -119,48 +120,26 @@ void exti_disable(ExtiConfig cfg) {
119120
}
120121

121122
void HAL_GPIO_EXTI_Callback(GPIO_TypeDef *hgpio, uint16_t GPIO_Pin) {
122-
int index = 0;
123123
ExtiHandlerCallback cb = NULL;
124-
if (hgpio == hwp_gpio1) {
125-
while (index < EXTI_MAX_GPIO1_PIN_NUM && s_exti_gpio1_handler_configs[index].callback != NULL) {
126-
if (s_exti_gpio1_handler_configs[index].gpio_pin == GPIO_Pin) {
127-
cb = s_exti_gpio1_handler_configs[index].callback;
128-
break;
129-
}
130-
index++;
131-
}
132-
}
133124

134-
if (cb != NULL) {
135-
bool should_context_switch = false;
136-
cb(&should_context_switch);
137-
if (should_context_switch) {
138-
portEND_SWITCHING_ISR(should_context_switch);
125+
for (uint8_t index = 0; index < EXTI_MAX_GPIO1_PIN_NUM; index++) {
126+
if (s_exti_gpio1_handler_configs[index].callback != NULL &&
127+
s_exti_gpio1_handler_configs[index].gpio_pin == GPIO_Pin) {
128+
bool should_context_switch = false;
129+
130+
s_exti_gpio1_handler_configs[index].callback(&should_context_switch);
131+
s_should_context_switch |= should_context_switch;
132+
return;
139133
}
140134
}
135+
136+
PBL_LOG(LOG_LEVEL_WARNING, "No handler found for GPIO pin %u", GPIO_Pin);
141137
}
142138

143139
void GPIO1_IRQHandler(void) {
144-
// Optimized interrupt handler to avoid looping through all 78 pins
145-
// which causes an interrupt storm and blocks other tasks (e.g. I2C).
146-
GPIO_TypeDef *base = hwp_gpio1;
147-
// GPIO1 has pins 0-78, spanning 3 banks (32 pins each)
148-
for (int i = 0; i < 3; i++) {
149-
GPIO_TypeDef *gpiox = base + i;
150-
uint32_t isr = gpiox->ISR;
151-
uint32_t ier = gpiox->IER;
152-
uint32_t pending = isr & ier;
153-
154-
while (pending) {
155-
uint32_t bit = __builtin_ctz(pending);
156-
uint32_t pin = (i * 32) + bit;
157-
158-
// Always call the HAL handler which will clear ISR and invoke callback
159-
HAL_GPIO_EXTI_IRQHandler(hwp_gpio1, pin);
160-
161-
pending &= ~(1UL << bit);
162-
}
163-
}
140+
s_should_context_switch = false;
141+
HAL_GPIO_IRQHandler(hwp_gpio1);
142+
portEND_SWITCHING_ISR(s_should_context_switch);
164143
}
165144

166145
void exti_configure_other(ExtiLineOther exti_line, ExtiTrigger trigger) {}

0 commit comments

Comments
 (0)