xref: /btstack/port/stm32-f4discovery-usb/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c (revision a8f7f3fcbcd51f8d2e92aca076b6a9f812db358c)
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hal_exti.c
4   * @author  MCD Application Team
5   * @brief   EXTI HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Extended Interrupts and events controller (EXTI) peripheral:
8   *           + Initialization and de-initialization functions
9   *           + IO operation functions
10   *
11   @verbatim
12   ==============================================================================
13                     ##### EXTI Peripheral features #####
14   ==============================================================================
15   [..]
16     (+) Each Exti line can be configured within this driver.
17 
18     (+) Exti line can be configured in 3 different modes
19         (++) Interrupt
20         (++) Event
21         (++) Both of them
22 
23     (+) Configurable Exti lines can be configured with 3 different triggers
24         (++) Rising
25         (++) Falling
26         (++) Both of them
27 
28     (+) When set in interrupt mode, configurable Exti lines have two different
29         interrupts pending registers which allow to distinguish which transition
30         occurs:
31         (++) Rising edge pending interrupt
32         (++) Falling
33 
34     (+) Exti lines 0 to 15 are linked to gpio pin number 0 to 15. Gpio port can
35         be selected through multiplexer.
36 
37                      ##### How to use this driver #####
38   ==============================================================================
39   [..]
40 
41     (#) Configure the EXTI line using HAL_EXTI_SetConfigLine().
42         (++) Choose the interrupt line number by setting "Line" member from
43              EXTI_ConfigTypeDef structure.
44         (++) Configure the interrupt and/or event mode using "Mode" member from
45              EXTI_ConfigTypeDef structure.
46         (++) For configurable lines, configure rising and/or falling trigger
47              "Trigger" member from EXTI_ConfigTypeDef structure.
48         (++) For Exti lines linked to gpio, choose gpio port using "GPIOSel"
49              member from GPIO_InitTypeDef structure.
50 
51     (#) Get current Exti configuration of a dedicated line using
52         HAL_EXTI_GetConfigLine().
53         (++) Provide exiting handle as parameter.
54         (++) Provide pointer on EXTI_ConfigTypeDef structure as second parameter.
55 
56     (#) Clear Exti configuration of a dedicated line using HAL_EXTI_GetConfigLine().
57         (++) Provide exiting handle as parameter.
58 
59     (#) Register callback to treat Exti interrupts using HAL_EXTI_RegisterCallback().
60         (++) Provide exiting handle as first parameter.
61         (++) Provide which callback will be registered using one value from
62              EXTI_CallbackIDTypeDef.
63         (++) Provide callback function pointer.
64 
65     (#) Get interrupt pending bit using HAL_EXTI_GetPending().
66 
67     (#) Clear interrupt pending bit using HAL_EXTI_GetPending().
68 
69     (#) Generate software interrupt using HAL_EXTI_GenerateSWI().
70 
71   @endverbatim
72   ******************************************************************************
73   * @attention
74   *
75   * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
76   * All rights reserved.</center></h2>
77   *
78   * This software component is licensed by ST under BSD 3-Clause license,
79   * the "License"; You may not use this file except in compliance with the
80   * License. You may obtain a copy of the License at:
81   *                        opensource.org/licenses/BSD-3-Clause
82   *
83   ******************************************************************************
84   */
85 
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32f4xx_hal.h"
88 
89 /** @addtogroup STM32F4xx_HAL_Driver
90   * @{
91   */
92 
93 /** @addtogroup EXTI
94   * @{
95   */
96 /** MISRA C:2012 deviation rule has been granted for following rule:
97   * Rule-18.1_b - Medium: Array `EXTICR' 1st subscript interval [0,7] may be out
98   * of bounds [0,3] in following API :
99   * HAL_EXTI_SetConfigLine
100   * HAL_EXTI_GetConfigLine
101   * HAL_EXTI_ClearConfigLine
102   */
103 
104 #ifdef HAL_EXTI_MODULE_ENABLED
105 
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private defines -----------------------------------------------------------*/
108 /** @defgroup EXTI_Private_Constants EXTI Private Constants
109   * @{
110   */
111 
112 /**
113   * @}
114   */
115 
116 /* Private macros ------------------------------------------------------------*/
117 /* Private variables ---------------------------------------------------------*/
118 /* Private function prototypes -----------------------------------------------*/
119 /* Exported functions --------------------------------------------------------*/
120 
121 /** @addtogroup EXTI_Exported_Functions
122   * @{
123   */
124 
125 /** @addtogroup EXTI_Exported_Functions_Group1
126   *  @brief    Configuration functions
127   *
128 @verbatim
129  ===============================================================================
130               ##### Configuration functions #####
131  ===============================================================================
132 
133 @endverbatim
134   * @{
135   */
136 
137 /**
138   * @brief  Set configuration of a dedicated Exti line.
139   * @param  hexti Exti handle.
140   * @param  pExtiConfig Pointer on EXTI configuration to be set.
141   * @retval HAL Status.
142   */
HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef * hexti,EXTI_ConfigTypeDef * pExtiConfig)143 HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
144 {
145   uint32_t regval;
146   uint32_t linepos;
147   uint32_t maskline;
148 
149   /* Check null pointer */
150   if ((hexti == NULL) || (pExtiConfig == NULL))
151   {
152     return HAL_ERROR;
153   }
154 
155   /* Check parameters */
156   assert_param(IS_EXTI_LINE(pExtiConfig->Line));
157   assert_param(IS_EXTI_MODE(pExtiConfig->Mode));
158 
159   /* Assign line number to handle */
160   hexti->Line = pExtiConfig->Line;
161 
162   /* Compute line mask */
163   linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
164   maskline = (1uL << linepos);
165 
166   /* Configure triggers for configurable lines */
167   if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
168   {
169     assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
170 
171     /* Configure rising trigger */
172     /* Mask or set line */
173     if ((pExtiConfig->Trigger & EXTI_TRIGGER_RISING) != 0x00u)
174     {
175       EXTI->RTSR |= maskline;
176     }
177     else
178     {
179       EXTI->RTSR &= ~maskline;
180     }
181 
182     /* Configure falling trigger */
183     /* Mask or set line */
184     if ((pExtiConfig->Trigger & EXTI_TRIGGER_FALLING) != 0x00u)
185     {
186       EXTI->FTSR |= maskline;
187     }
188     else
189     {
190       EXTI->FTSR &= ~maskline;
191     }
192 
193 
194     /* Configure gpio port selection in case of gpio exti line */
195     if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
196     {
197       assert_param(IS_EXTI_GPIO_PORT(pExtiConfig->GPIOSel));
198       assert_param(IS_EXTI_GPIO_PIN(linepos));
199 
200       regval = SYSCFG->EXTICR[linepos >> 2u];
201       regval &= ~(SYSCFG_EXTICR1_EXTI0 << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
202       regval |= (pExtiConfig->GPIOSel << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
203       SYSCFG->EXTICR[linepos >> 2u] = regval;
204     }
205   }
206 
207   /* Configure interrupt mode : read current mode */
208   /* Mask or set line */
209   if ((pExtiConfig->Mode & EXTI_MODE_INTERRUPT) != 0x00u)
210   {
211     EXTI->IMR |= maskline;
212   }
213   else
214   {
215     EXTI->IMR &= ~maskline;
216   }
217 
218   /* Configure event mode : read current mode */
219   /* Mask or set line */
220   if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0x00u)
221   {
222     EXTI->EMR |= maskline;
223   }
224   else
225   {
226     EXTI->EMR &= ~maskline;
227   }
228 
229   return HAL_OK;
230 }
231 
232 /**
233   * @brief  Get configuration of a dedicated Exti line.
234   * @param  hexti Exti handle.
235   * @param  pExtiConfig Pointer on structure to store Exti configuration.
236   * @retval HAL Status.
237   */
HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef * hexti,EXTI_ConfigTypeDef * pExtiConfig)238 HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
239 {
240   uint32_t regval;
241   uint32_t linepos;
242   uint32_t maskline;
243 
244   /* Check null pointer */
245   if ((hexti == NULL) || (pExtiConfig == NULL))
246   {
247     return HAL_ERROR;
248   }
249 
250   /* Check the parameter */
251   assert_param(IS_EXTI_LINE(hexti->Line));
252 
253   /* Store handle line number to configuration structure */
254   pExtiConfig->Line = hexti->Line;
255 
256   /* Compute line mask */
257   linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
258   maskline = (1uL << linepos);
259 
260   /* 1] Get core mode : interrupt */
261 
262   /* Check if selected line is enable */
263   if ((EXTI->IMR & maskline) != 0x00u)
264   {
265     pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
266   }
267   else
268   {
269     pExtiConfig->Mode = EXTI_MODE_NONE;
270   }
271 
272   /* Get event mode */
273   /* Check if selected line is enable */
274   if ((EXTI->EMR & maskline) != 0x00u)
275   {
276     pExtiConfig->Mode |= EXTI_MODE_EVENT;
277   }
278 
279   /* 2] Get trigger for configurable lines : rising */
280   if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
281   {
282     /* Check if configuration of selected line is enable */
283     if ((EXTI->RTSR & maskline) != 0x00u)
284     {
285       pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
286     }
287     else
288     {
289       pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
290     }
291 
292     /* Get falling configuration */
293     /* Check if configuration of selected line is enable */
294     if ((EXTI->FTSR & maskline) != 0x00u)
295     {
296       pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
297     }
298 
299     /* Get Gpio port selection for gpio lines */
300     if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
301     {
302       assert_param(IS_EXTI_GPIO_PIN(linepos));
303 
304       regval = SYSCFG->EXTICR[linepos >> 2u];
305       pExtiConfig->GPIOSel = ((regval << (SYSCFG_EXTICR1_EXTI1_Pos * (3uL - (linepos & 0x03u)))) >> 24);
306     }
307     else
308     {
309       pExtiConfig->GPIOSel = 0x00u;
310     }
311   }
312   else
313   {
314     /* No Trigger selected */
315     pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
316     pExtiConfig->GPIOSel = 0x00u;
317   }
318 
319   return HAL_OK;
320 }
321 
322 /**
323   * @brief  Clear whole configuration of a dedicated Exti line.
324   * @param  hexti Exti handle.
325   * @retval HAL Status.
326   */
HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef * hexti)327 HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
328 {
329   uint32_t regval;
330   uint32_t linepos;
331   uint32_t maskline;
332 
333   /* Check null pointer */
334   if (hexti == NULL)
335   {
336     return HAL_ERROR;
337   }
338 
339   /* Check the parameter */
340   assert_param(IS_EXTI_LINE(hexti->Line));
341 
342   /* compute line mask */
343   linepos = (hexti->Line & EXTI_PIN_MASK);
344   maskline = (1uL << linepos);
345 
346   /* 1] Clear interrupt mode */
347   EXTI->IMR = (EXTI->IMR & ~maskline);
348 
349   /* 2] Clear event mode */
350   EXTI->EMR = (EXTI->EMR & ~maskline);
351 
352   /* 3] Clear triggers in case of configurable lines */
353   if ((hexti->Line & EXTI_CONFIG) != 0x00u)
354   {
355     EXTI->RTSR = (EXTI->RTSR & ~maskline);
356     EXTI->FTSR = (EXTI->FTSR & ~maskline);
357 
358     /* Get Gpio port selection for gpio lines */
359     if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
360     {
361       assert_param(IS_EXTI_GPIO_PIN(linepos));
362 
363       regval = SYSCFG->EXTICR[linepos >> 2u];
364       regval &= ~(SYSCFG_EXTICR1_EXTI0 << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
365       SYSCFG->EXTICR[linepos >> 2u] = regval;
366     }
367   }
368 
369   return HAL_OK;
370 }
371 
372 /**
373   * @brief  Register callback for a dedicated Exti line.
374   * @param  hexti Exti handle.
375   * @param  CallbackID User callback identifier.
376   *         This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
377   * @param  pPendingCbfn function pointer to be stored as callback.
378   * @retval HAL Status.
379   */
HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef * hexti,EXTI_CallbackIDTypeDef CallbackID,void (* pPendingCbfn)(void))380 HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
381 {
382   HAL_StatusTypeDef status = HAL_OK;
383 
384   switch (CallbackID)
385   {
386     case  HAL_EXTI_COMMON_CB_ID:
387       hexti->PendingCallback = pPendingCbfn;
388       break;
389 
390     default:
391       status = HAL_ERROR;
392       break;
393   }
394 
395   return status;
396 }
397 
398 /**
399   * @brief  Store line number as handle private field.
400   * @param  hexti Exti handle.
401   * @param  ExtiLine Exti line number.
402   *         This parameter can be from 0 to @ref EXTI_LINE_NB.
403   * @retval HAL Status.
404   */
HAL_EXTI_GetHandle(EXTI_HandleTypeDef * hexti,uint32_t ExtiLine)405 HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
406 {
407   /* Check the parameters */
408   assert_param(IS_EXTI_LINE(ExtiLine));
409 
410   /* Check null pointer */
411   if (hexti == NULL)
412   {
413     return HAL_ERROR;
414   }
415   else
416   {
417     /* Store line number as handle private field */
418     hexti->Line = ExtiLine;
419 
420     return HAL_OK;
421   }
422 }
423 
424 /**
425   * @}
426   */
427 
428 /** @addtogroup EXTI_Exported_Functions_Group2
429   *  @brief EXTI IO functions.
430   *
431 @verbatim
432  ===============================================================================
433                        ##### IO operation functions #####
434  ===============================================================================
435 
436 @endverbatim
437   * @{
438   */
439 
440 /**
441   * @brief  Handle EXTI interrupt request.
442   * @param  hexti Exti handle.
443   * @retval none.
444   */
HAL_EXTI_IRQHandler(EXTI_HandleTypeDef * hexti)445 void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
446 {
447   uint32_t regval;
448   uint32_t maskline;
449 
450   /* Compute line mask */
451   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
452 
453   /* Get pending bit  */
454   regval = (EXTI->PR & maskline);
455   if (regval != 0x00u)
456   {
457     /* Clear pending bit */
458     EXTI->PR = maskline;
459 
460     /* Call callback */
461     if (hexti->PendingCallback != NULL)
462     {
463       hexti->PendingCallback();
464     }
465   }
466 }
467 
468 /**
469   * @brief  Get interrupt pending bit of a dedicated line.
470   * @param  hexti Exti handle.
471   * @param  Edge Specify which pending edge as to be checked.
472   *         This parameter can be one of the following values:
473   *           @arg @ref EXTI_TRIGGER_RISING_FALLING
474   *         This parameter is kept for compatibility with other series.
475   * @retval 1 if interrupt is pending else 0.
476   */
HAL_EXTI_GetPending(EXTI_HandleTypeDef * hexti,uint32_t Edge)477 uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
478 {
479   uint32_t regval;
480   uint32_t linepos;
481   uint32_t maskline;
482 
483   /* Check parameters */
484   assert_param(IS_EXTI_LINE(hexti->Line));
485   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
486   assert_param(IS_EXTI_PENDING_EDGE(Edge));
487 
488   /* Compute line mask */
489   linepos = (hexti->Line & EXTI_PIN_MASK);
490   maskline = (1uL << linepos);
491 
492   /* return 1 if bit is set else 0 */
493   regval = ((EXTI->PR & maskline) >> linepos);
494   return regval;
495 }
496 
497 /**
498   * @brief  Clear interrupt pending bit of a dedicated line.
499   * @param  hexti Exti handle.
500   * @param  Edge Specify which pending edge as to be clear.
501   *         This parameter can be one of the following values:
502   *           @arg @ref EXTI_TRIGGER_RISING_FALLING
503   *         This parameter is kept for compatibility with other series.
504   * @retval None.
505   */
HAL_EXTI_ClearPending(EXTI_HandleTypeDef * hexti,uint32_t Edge)506 void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
507 {
508   uint32_t maskline;
509 
510   /* Check parameters */
511   assert_param(IS_EXTI_LINE(hexti->Line));
512   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
513   assert_param(IS_EXTI_PENDING_EDGE(Edge));
514 
515   /* Compute line mask */
516   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
517 
518   /* Clear Pending bit */
519   EXTI->PR =  maskline;
520 }
521 
522 /**
523   * @brief  Generate a software interrupt for a dedicated line.
524   * @param  hexti Exti handle.
525   * @retval None.
526   */
HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef * hexti)527 void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
528 {
529   uint32_t maskline;
530 
531   /* Check parameters */
532   assert_param(IS_EXTI_LINE(hexti->Line));
533   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
534 
535   /* Compute line mask */
536   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
537 
538   /* Generate Software interrupt */
539   EXTI->SWIER = maskline;
540 }
541 
542 /**
543   * @}
544   */
545 
546 /**
547   * @}
548   */
549 
550 #endif /* HAL_EXTI_MODULE_ENABLED */
551 /**
552   * @}
553   */
554 
555 /**
556   * @}
557   */
558 
559 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
560