@@ -79,17 +79,17 @@ static uint32_t gpioIds[NUMBER_OF_GPIO] = {0};
7979
8080/** Main GPIO IRQ handler called from vector table handler
8181 *
82- * @param gpioBase The GPIO register base address
83- * @return void
82+ * @param gpioBase The GPIO register base address
83+ * @return void
8484 */
8585void fGpioHandler (void )
8686{
8787 uint8_t index ;
8888 uint32_t active_interrupts = 0 ;
89- gpio_irq_event event ;
89+ gpio_irq_event event = IRQ_NONE ;
9090 GpioReg_pt gpioBase ;
9191
92- /* Enable the GPIO clock */
92+ /* Enable the GPIO clock which may have been switched off by other drivers */
9393 CLOCK_ENABLE (CLOCK_GPIO );
9494
9595 gpioBase = GPIOREG ;
@@ -106,15 +106,12 @@ void fGpioHandler(void)
106106 if ((gpioBase -> IRQ_POLARITY_SET >> index ) & 0x01 ) {
107107 /* Edge triggered high */
108108 event = IRQ_RISE ;
109- } else if (( gpioBase -> IRQ_POLARITY_CLEAR >> index ) & 0x01 ) {
109+ } else {
110110 /* Edge triggered low */
111111 event = IRQ_FALL ;
112- } else {
113- /* Edge none */
114- event = IRQ_NONE ;
115112 }
116113 }
117- gpioBase -> IRQ_CLEAR | = (0x1 << index );
114+ gpioBase -> IRQ_CLEAR = (0x1 << index );
118115
119116 /* Call the handler registered to the pin */
120117 irq_handler (gpioIds [index ], event );
@@ -145,22 +142,16 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
145142 /* Store the ID, this is required by registered handler function */
146143 gpioIds [pin ] = id ;
147144
148- /* Enable the GPIO clock */
145+ /* Enable the GPIO clock which may have been switched off by other drivers */
149146 CLOCK_ENABLE (CLOCK_GPIO );
150147
151148 /* Initialize the GPIO membase */
152149 obj -> GPIOMEMBASE = GPIOREG ;
153150
154151 /* Set default values for the pin interrupt */
155- /* TODO: Only one DIO line is configured using this function; overrides other DIO line setting
156- * If mbed layer wants to call this function repeatedly for setting multiple DIO lines as input
157- * then change this setting to obj->GPIOMEMBASE->W_IN |= obj->pinMask. All parameter setting needs to change from = to |=
158- */
159152 obj -> GPIOMEMBASE -> W_IN = obj -> pinMask ;
160- obj -> GPIOMEMBASE -> IRQ_ENABLE_SET = obj -> pinMask ;
161153 obj -> GPIOMEMBASE -> IRQ_EDGE = obj -> pinMask ;
162- obj -> GPIOMEMBASE -> IRQ_POLARITY_SET = (obj -> pinMask );
163- obj -> GPIOMEMBASE -> ANYEDGE_SET = IO_NONE ;
154+ obj -> GPIOMEMBASE -> IRQ_POLARITY_SET = obj -> pinMask ;
164155
165156 /* Register the handler for this pin */
166157 irq_handler = handler ;
@@ -178,10 +169,11 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
178169 */
179170void gpio_irq_free (gpio_irq_t * obj )
180171{
181- /* Enable the GPIO clock */
172+ /* Enable the GPIO clock which may have been switched off by other drivers */
182173 CLOCK_ENABLE (CLOCK_GPIO );
183174
184- obj -> GPIOMEMBASE -> W_IN = (IO_ALL ^ (obj -> pinMask ));
175+ /* Disable IRQs to indicate that it is now free */
176+ obj -> GPIOMEMBASE -> IRQ_ENABLE_CLEAR = obj -> pinMask ;
185177 gpioIds [obj -> pin ] = 0 ;
186178}
187179
@@ -193,42 +185,35 @@ void gpio_irq_free(gpio_irq_t *obj)
193185 */
194186void gpio_irq_set (gpio_irq_t * obj , gpio_irq_event event , uint32_t enable )
195187{
196-
197- /* Enable the GPIO clock */
188+ /* Enable the GPIO clock which may have been switched off by other drivers */
198189 CLOCK_ENABLE (CLOCK_GPIO );
199-
190+ obj -> GPIOMEMBASE -> IRQ_EDGE = obj -> pinMask ;
191+
200192 switch (event ) {
201- case IRQ_RISE :
202- obj -> GPIOMEMBASE -> IRQ_EDGE = (obj -> pinMask );
203- obj -> GPIOMEMBASE -> IRQ_LEVEL = (IO_ALL ^ (obj -> pinMask ));
204- /* Enable is an integer; hence checking for 1 or 0*/
205- if (enable == 1 ) {
206- /* Enable rising edge */
207- obj -> GPIOMEMBASE -> IRQ_POLARITY_SET = (obj -> pinMask );
208- } else if (enable == 0 ) {
209- /* Disable rising edge */
210- obj -> GPIOMEMBASE -> IRQ_POLARITY_SET = (IO_ALL ^ (obj -> pinMask ));
211- }
212- break ;
193+ case IRQ_RISE :
194+
195+ /* Enable rising edge */
196+ obj -> GPIOMEMBASE -> IRQ_POLARITY_SET = obj -> pinMask ;
197+ break ;
213198
214199 case IRQ_FALL :
215- obj -> GPIOMEMBASE -> IRQ_EDGE = (obj -> pinMask );
216- obj -> GPIOMEMBASE -> IRQ_LEVEL = (IO_ALL ^ (obj -> pinMask ));
217- /* Enable is an integer; hence checking for 1 or 0*/
218- if (enable == 1 ) {
219- /* Enable falling edge */
220- obj -> GPIOMEMBASE -> IRQ_POLARITY_CLEAR = (obj -> pinMask );
221- } else if (enable == 0 ) {
222- /* Disable falling edge */
223- obj -> GPIOMEMBASE -> IRQ_POLARITY_CLEAR = (IO_ALL ^ (obj -> pinMask ));
224- }
200+
201+ /* Enable falling edge */
202+ obj -> GPIOMEMBASE -> IRQ_POLARITY_CLEAR = obj -> pinMask ;
225203 break ;
226204
227205 default :
228206 /* No event is set */
229207 break ;
230208 }
209+ /* Enable the IRQ based on enable parameter */
210+ if (enable ) {
211+
212+ obj -> GPIOMEMBASE -> IRQ_ENABLE_SET = obj -> pinMask ;
213+ } else {
231214
215+ obj -> GPIOMEMBASE -> IRQ_ENABLE_CLEAR = obj -> pinMask ;
216+ }
232217}
233218
234219/** Enable GPIO IRQ
@@ -238,10 +223,10 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
238223 */
239224void gpio_irq_enable (gpio_irq_t * obj )
240225{
241- /* Enable the GPIO clock */
226+ /* Enable the GPIO clock which may have been switched off by other drivers */
242227 CLOCK_ENABLE (CLOCK_GPIO );
243228
244- obj -> GPIOMEMBASE -> IRQ_ENABLE_SET = ( obj -> pinMask ) ;
229+ obj -> GPIOMEMBASE -> IRQ_ENABLE_SET = obj -> pinMask ;
245230}
246231
247232/** Disable GPIO IRQ
@@ -251,10 +236,11 @@ void gpio_irq_enable(gpio_irq_t *obj)
251236 */
252237void gpio_irq_disable (gpio_irq_t * obj )
253238{
254- /* Enable the GPIO clock */
239+
240+ /* Enable the GPIO clock which may have been switched off by other drivers */
255241 CLOCK_ENABLE (CLOCK_GPIO );
256242
257- obj -> GPIOMEMBASE -> IRQ_ENABLE_CLEAR = ( obj -> pinMask ) ;
243+ obj -> GPIOMEMBASE -> IRQ_ENABLE_CLEAR = obj -> pinMask ;
258244}
259245
260246#endif //DEVICE_INTERRUPTIN
0 commit comments