EFM32 Jade Gecko 12 Software Documentation  efm32jg12-doc-5.1.2
gpiointerrupt.c
Go to the documentation of this file.
1 /***************************************************************************/
17 #include "em_gpio.h"
18 #include "em_core.h"
19 #include "gpiointerrupt.h"
20 #include "em_assert.h"
21 #include "em_common.h"
22 
23 /*******************************************************************************
24  ******************************** MACROS ***********************************
25  ******************************************************************************/
26 
27 /*******************************************************************************
28  ******************************* STRUCTS ***********************************
29  ******************************************************************************/
30 
33 typedef struct
34 {
35  /* Pin number in range of 0 to 15 */
36  uint32_t pin;
37 
38  /* Pointer to the callback function */
39  GPIOINT_IrqCallbackPtr_t callback;
40 
41 } GPIOINT_CallbackDesc_t;
42 
43 
44 /*******************************************************************************
45  ******************************** GLOBALS **********************************
46  ******************************************************************************/
47 
48 /* Array of user callbacks. One for each pin. */
49 static GPIOINT_IrqCallbackPtr_t gpioCallbacks[16] = {0};
50 
51 /*******************************************************************************
52  ****************************** PROTOTYPES *********************************
53  ******************************************************************************/
54 static void GPIOINT_IRQDispatcher(uint32_t iflags);
55 
58 /*******************************************************************************
59  *************************** GLOBAL FUNCTIONS ******************************
60  ******************************************************************************/
61 
62 /***************************************************************************/
67 void GPIOINT_Init(void)
68 {
69  NVIC_ClearPendingIRQ(GPIO_ODD_IRQn);
70  NVIC_EnableIRQ(GPIO_ODD_IRQn);
71  NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn);
72  NVIC_EnableIRQ(GPIO_EVEN_IRQn);
73 }
74 
75 
76 /***************************************************************************/
91 void GPIOINT_CallbackRegister(uint8_t pin, GPIOINT_IrqCallbackPtr_t callbackPtr)
92 {
94  /* Dispatcher is used */
95  gpioCallbacks[pin] = callbackPtr;
96  )
97 }
98 
101 /***************************************************************************/
114 static void GPIOINT_IRQDispatcher(uint32_t iflags)
115 {
116  uint32_t irqIdx;
117  GPIOINT_IrqCallbackPtr_t callback;
118 
119  /* check for all flags set in IF register */
120  while(iflags)
121  {
122  irqIdx = SL_CTZ(iflags);
123 
124  /* clear flag*/
125  iflags &= ~(1 << irqIdx);
126 
127  callback = gpioCallbacks[irqIdx];
128  if (callback)
129  {
130  /* call user callback */
131  callback(irqIdx);
132  }
133  }
134 }
135 
136 /***************************************************************************/
142 void GPIO_EVEN_IRQHandler(void)
143 {
144  uint32_t iflags;
145 
146  /* Get all even interrupts. */
147  iflags = GPIO_IntGetEnabled() & 0x00005555;
148 
149  /* Clean only even interrupts. */
150  GPIO_IntClear(iflags);
151 
152  GPIOINT_IRQDispatcher(iflags);
153 }
154 
155 
156 /***************************************************************************/
162 void GPIO_ODD_IRQHandler(void)
163 {
164  uint32_t iflags;
165 
166  /* Get all odd interrupts. */
167  iflags = GPIO_IntGetEnabled() & 0x0000AAAA;
168 
169  /* Clean only odd interrupts. */
170  GPIO_IntClear(iflags);
171 
172  GPIOINT_IRQDispatcher(iflags);
173 }
174 
181 /******** THE REST OF THE FILE IS DOCUMENTATION ONLY !**********************/
Emlib peripheral API "assert" implementation.
__STATIC_INLINE uint32_t GPIO_IntGetEnabled(void)
Get enabled and pending GPIO interrupt flags. Useful for handling more interrupt sources in the same ...
Definition: em_gpio.h:735
__STATIC_INLINE void GPIO_IntClear(uint32_t flags)
Clear one or more pending GPIO interrupts.
Definition: em_gpio.h:674
void GPIOINT_CallbackRegister(uint8_t pin, GPIOINT_IrqCallbackPtr_t callbackPtr)
Registers user callback for given pin number.
Definition: gpiointerrupt.c:91
General purpose utilities.
#define CORE_ATOMIC_SECTION(yourcode)
Definition: em_core.h:126
General Purpose IO (GPIO) peripheral API.
Core interrupt handling API.
void(* GPIOINT_IrqCallbackPtr_t)(uint8_t pin)
GPIO interrupt callback function pointer.
Definition: gpiointerrupt.h:46
__STATIC_INLINE uint32_t SL_CTZ(uint32_t value)
Count trailing number of zeros. Use CLZ instruction if available.
Definition: em_common.h:194
void GPIOINT_Init(void)
Initialization of GPIOINT module.
Definition: gpiointerrupt.c:67
GPIOINT API definition.