44#include "em_gpio.h"
55#include "gpiointerrupt.h"
66#include <stdbool.h>
7+ #include <stddef.h>
78#include <string.h>
89
910#include "zigbee_app_framework_event.h"
1011
1112#include "hal/gpio.h"
13+ #include <stdio.h>
14+
15+ // Get container structure from embedded member pointer
16+ #define container_of (ptr , type , member ) \
17+ ((type *)((char *)(ptr) - offsetof(type, member)))
1218
1319// ------ Encoding helpers ------
1420// hal_gpio_pin_t: upper byte = port index (A=0, B=1, ...), lower byte = pin
@@ -47,7 +53,6 @@ static void hal_gpio_ensure_gpioint(void) {
4753// ------ Per-interrupt bookkeeping ------
4854typedef struct {
4955 bool in_use ;
50- uint8_t em4wu_int_no ;
5156 hal_gpio_pin_t hal_pin ;
5257 uint8_t pull_dir ; // for EM4WU polarity selection
5358 gpio_callback_t user_cb ;
@@ -83,16 +88,9 @@ static void _dispatch_regular(uint8_t intNo, void *ctx) {
8388 }
8489}
8590
86- static void _dispatch_em4wu (uint8_t intNo , void * ctx ) {
87- (void )intNo ;
88- int_slot_t * slot = (int_slot_t * )ctx ;
89- if (slot ) {
90- sl_zigbee_af_event_set_active (& slot -> af_event );
91- }
92- }
93-
94- static void _af_event_handler (sli_zigbee_event_t * event ) {
95- int_slot_t * slot = (int_slot_t * )event -> data ;
91+ static void _af_event_handler (sl_zigbee_af_event_t * event ) {
92+ // Get int_slot_t from embedded af_event field
93+ int_slot_t * slot = container_of (event , int_slot_t , af_event );
9694 if (slot -> user_cb ) {
9795 slot -> user_cb (slot -> hal_pin , slot -> arg );
9896 }
@@ -172,7 +170,6 @@ void hal_gpio_callback(hal_gpio_pin_t gpio_pin, gpio_callback_t callback,
172170 slot -> user_cb = callback ;
173171 slot -> arg = arg ;
174172 sl_zigbee_af_isr_event_init (& slot -> af_event , _af_event_handler );
175- slot -> af_event .data = (uint32_t )slot ;
176173
177174 GPIO_Mode_TypeDef mode = GPIO_PinModeGet (port , pin_num );
178175 if (mode == gpioModeInputPull ) {
@@ -185,25 +182,11 @@ void hal_gpio_callback(hal_gpio_pin_t gpio_pin, gpio_callback_t callback,
185182 slot -> pull_dir = HAL_GPIO_PULL_NONE ;
186183 }
187184
188- // 1) Register regular edge-sensitive callback (both edges)
185+ // Register regular edge-sensitive callback (both edges)
189186 unsigned int reg_int = GPIOINT_CallbackRegisterExt (
190187 line , (GPIOINT_IrqCallbackPtrExt_t )_dispatch_regular , slot );
191188 EFM_ASSERT (reg_int != INTERRUPT_UNAVAILABLE );
192189 GPIO_ExtIntConfig (port , pin_num , line , true, true, true);
193-
194- // 2) Try to also register an EM4WU (level-sensitive) wake-up on this pin.
195- // If unsupported, GPIOINT_EM4WUCallbackRegisterExt returns
196- // INTERRUPT_UNAVAILABLE. Polarity: wake on the *active* level.
197- bool active_is_high = (slot -> pull_dir == HAL_GPIO_PULL_DOWN );
198- unsigned int em4_line = GPIOINT_EM4WUCallbackRegisterExt (
199- port , pin_num , (GPIOINT_IrqCallbackPtrExt_t )_dispatch_em4wu , slot );
200- if (em4_line != INTERRUPT_UNAVAILABLE ) {
201- GPIO_EM4WUExtIntConfig (port , pin_num , em4_line , active_is_high ? 1U : 0U ,
202- true);
203- slot -> em4wu_int_no = em4_line ;
204- } else {
205- slot -> em4wu_int_no = LINE_MISSING ;
206- }
207190}
208191
209192// (Optional) helper to unregister an interrupt if you add
@@ -214,19 +197,12 @@ void hal_gpio_unreg_callback(hal_gpio_pin_t gpio_pin) {
214197 GPIO_Port_TypeDef port = hal_port_from_index (port_idx );
215198
216199 uint8_t int_no = LINE_MISSING ;
217- uint8_t em4wu_int_no = LINE_MISSING ;
218200 for (uint8_t i = 0 ; i < MAX_INT_LINES ; i ++ ) {
219201 if (s_slots [i ].in_use && s_slots [i ].hal_pin == gpio_pin ) {
220202 int_no = i ;
221- em4wu_int_no = s_slots [i ].em4wu_int_no ;
222203 }
223204 }
224205
225- if (em4wu_int_no != LINE_MISSING ) {
226- GPIO_EM4WUExtIntConfig (port , pin_num , em4wu_int_no , 0 , false);
227- GPIOINT_EM4WUCallbackUnRegister (em4wu_int_no );
228- }
229-
230206 if (int_no != LINE_MISSING ) {
231207 GPIO_ExtIntConfig (port , pin_num , int_no , false, false, false);
232208 GPIOINT_CallbackUnRegister (int_no );
0 commit comments