1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_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   ******************************************************************************
12   * @attention
13   *
14   * Copyright (c) 2019 STMicroelectronics.
15   * All rights reserved.
16   *
17   * This software is licensed under terms that can be found in the LICENSE file
18   * in the root directory of this software component.
19   * If no LICENSE file comes with this software, it is provided AS-IS.
20   *
21   ******************************************************************************
22   @verbatim
23   ==============================================================================
24                     ##### EXTI Peripheral features #####
25   ==============================================================================
26   [..]
27     (+) Each Exti line can be configured within this driver.
28 
29     (+) Exti line can be configured in 3 different modes
30         (++) Interrupt
31         (++) Event
32         (++) Both of them
33 
34     (+) Configurable Exti lines can be configured with 3 different triggers
35         (++) Rising
36         (++) Falling
37         (++) Both of them
38 
39     (+) When set in interrupt mode, configurable Exti lines have two different
40         interrupts pending registers which allow to distinguish which transition
41         occurs:
42         (++) Rising edge pending interrupt
43         (++) Falling
44 
45     (+) Exti lines 0 to 15 are linked to gpio pin number 0 to 15. Gpio port can
46         be selected through multiplexer.
47 
48                      ##### How to use this driver #####
49   ==============================================================================
50   [..]
51 
52     (#) Configure the EXTI line using HAL_EXTI_SetConfigLine().
53         (++) Choose the interrupt line number by setting "Line" member from
54              EXTI_ConfigTypeDef structure.
55         (++) Configure the interrupt and/or event mode using "Mode" member from
56              EXTI_ConfigTypeDef structure.
57         (++) For configurable lines, configure rising and/or falling trigger
58              "Trigger" member from EXTI_ConfigTypeDef structure.
59         (++) For Exti lines linked to gpio, choose gpio port using "GPIOSel"
60              member from GPIO_InitTypeDef structure.
61 
62     (#) Get current Exti configuration of a dedicated line using
63         HAL_EXTI_GetConfigLine().
64         (++) Provide exiting handle as parameter.
65         (++) Provide pointer on EXTI_ConfigTypeDef structure as second parameter.
66 
67     (#) Clear Exti configuration of a dedicated line using HAL_EXTI_ClearConfigLine().
68         (++) Provide exiting handle as parameter.
69 
70     (#) Register callback to treat Exti interrupts using HAL_EXTI_RegisterCallback().
71         (++) Provide exiting handle as first parameter.
72         (++) Provide which callback will be registered using one value from
73              EXTI_CallbackIDTypeDef.
74         (++) Provide callback function pointer.
75 
76     (#) Get interrupt pending bit using HAL_EXTI_GetPending().
77 
78     (#) Clear interrupt pending bit using HAL_EXTI_ClearPending().
79 
80     (#) Generate software interrupt using HAL_EXTI_GenerateSWI().
81 
82   @endverbatim
83   ******************************************************************************
84   */
85 
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32f2xx_hal.h"
88 
89 /** @addtogroup STM32F2xx_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   /* Get default Trigger and GPIOSel configuration */
280   pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
281   pExtiConfig->GPIOSel = 0x00u;
282 
283   /* 2] Get trigger for configurable lines : rising */
284   if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
285   {
286     /* Check if configuration of selected line is enable */
287     if ((EXTI->RTSR & maskline) != 0x00u)
288     {
289       pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
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 * (linepos & 0x03u))) & SYSCFG_EXTICR1_EXTI0;
306     }
307   }
308 
309   return HAL_OK;
310 }
311 
312 /**
313   * @brief  Clear whole configuration of a dedicated Exti line.
314   * @param  hexti Exti handle.
315   * @retval HAL Status.
316   */
HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef * hexti)317 HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
318 {
319   uint32_t regval;
320   uint32_t linepos;
321   uint32_t maskline;
322 
323   /* Check null pointer */
324   if (hexti == NULL)
325   {
326     return HAL_ERROR;
327   }
328 
329   /* Check the parameter */
330   assert_param(IS_EXTI_LINE(hexti->Line));
331 
332   /* compute line mask */
333   linepos = (hexti->Line & EXTI_PIN_MASK);
334   maskline = (1uL << linepos);
335 
336   /* 1] Clear interrupt mode */
337   EXTI->IMR = (EXTI->IMR & ~maskline);
338 
339   /* 2] Clear event mode */
340   EXTI->EMR = (EXTI->EMR & ~maskline);
341 
342   /* 3] Clear triggers in case of configurable lines */
343   if ((hexti->Line & EXTI_CONFIG) != 0x00u)
344   {
345     EXTI->RTSR = (EXTI->RTSR & ~maskline);
346     EXTI->FTSR = (EXTI->FTSR & ~maskline);
347 
348     /* Get Gpio port selection for gpio lines */
349     if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
350     {
351       assert_param(IS_EXTI_GPIO_PIN(linepos));
352 
353       regval = SYSCFG->EXTICR[linepos >> 2u];
354       regval &= ~(SYSCFG_EXTICR1_EXTI0 << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
355       SYSCFG->EXTICR[linepos >> 2u] = regval;
356     }
357   }
358 
359   return HAL_OK;
360 }
361 
362 /**
363   * @brief  Register callback for a dedicated Exti line.
364   * @param  hexti Exti handle.
365   * @param  CallbackID User callback identifier.
366   *         This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
367   * @param  pPendingCbfn function pointer to be stored as callback.
368   * @retval HAL Status.
369   */
HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef * hexti,EXTI_CallbackIDTypeDef CallbackID,void (* pPendingCbfn)(void))370 HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
371 {
372   HAL_StatusTypeDef status = HAL_OK;
373 
374   switch (CallbackID)
375   {
376     case  HAL_EXTI_COMMON_CB_ID:
377       hexti->PendingCallback = pPendingCbfn;
378       break;
379 
380     default:
381       status = HAL_ERROR;
382       break;
383   }
384 
385   return status;
386 }
387 
388 /**
389   * @brief  Store line number as handle private field.
390   * @param  hexti Exti handle.
391   * @param  ExtiLine Exti line number.
392   *         This parameter can be from 0 to @ref EXTI_LINE_NB.
393   * @retval HAL Status.
394   */
HAL_EXTI_GetHandle(EXTI_HandleTypeDef * hexti,uint32_t ExtiLine)395 HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
396 {
397   /* Check the parameters */
398   assert_param(IS_EXTI_LINE(ExtiLine));
399 
400   /* Check null pointer */
401   if (hexti == NULL)
402   {
403     return HAL_ERROR;
404   }
405   else
406   {
407     /* Store line number as handle private field */
408     hexti->Line = ExtiLine;
409 
410     return HAL_OK;
411   }
412 }
413 
414 /**
415   * @}
416   */
417 
418 /** @addtogroup EXTI_Exported_Functions_Group2
419   *  @brief EXTI IO functions.
420   *
421 @verbatim
422  ===============================================================================
423                        ##### IO operation functions #####
424  ===============================================================================
425 
426 @endverbatim
427   * @{
428   */
429 
430 /**
431   * @brief  Handle EXTI interrupt request.
432   * @param  hexti Exti handle.
433   * @retval none.
434   */
HAL_EXTI_IRQHandler(EXTI_HandleTypeDef * hexti)435 void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
436 {
437   uint32_t regval;
438   uint32_t maskline;
439 
440   /* Compute line mask */
441   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
442 
443   /* Get pending bit  */
444   regval = (EXTI->PR & maskline);
445   if (regval != 0x00u)
446   {
447     /* Clear pending bit */
448     EXTI->PR = maskline;
449 
450     /* Call callback */
451     if (hexti->PendingCallback != NULL)
452     {
453       hexti->PendingCallback();
454     }
455   }
456 }
457 
458 /**
459   * @brief  Get interrupt pending bit of a dedicated line.
460   * @param  hexti Exti handle.
461   * @param  Edge Specify which pending edge as to be checked.
462   *         This parameter can be one of the following values:
463   *           @arg @ref EXTI_TRIGGER_RISING_FALLING
464   *         This parameter is kept for compatibility with other series.
465   * @retval 1 if interrupt is pending else 0.
466   */
HAL_EXTI_GetPending(EXTI_HandleTypeDef * hexti,uint32_t Edge)467 uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
468 {
469   uint32_t regval;
470   uint32_t linepos;
471   uint32_t maskline;
472 
473   /* Check parameters */
474   assert_param(IS_EXTI_LINE(hexti->Line));
475   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
476   assert_param(IS_EXTI_PENDING_EDGE(Edge));
477 
478   /* Compute line mask */
479   linepos = (hexti->Line & EXTI_PIN_MASK);
480   maskline = (1uL << linepos);
481 
482   /* return 1 if bit is set else 0 */
483   regval = ((EXTI->PR & maskline) >> linepos);
484   return regval;
485 }
486 
487 /**
488   * @brief  Clear interrupt pending bit of a dedicated line.
489   * @param  hexti Exti handle.
490   * @param  Edge Specify which pending edge as to be clear.
491   *         This parameter can be one of the following values:
492   *           @arg @ref EXTI_TRIGGER_RISING_FALLING
493   *         This parameter is kept for compatibility with other series.
494   * @retval None.
495   */
HAL_EXTI_ClearPending(EXTI_HandleTypeDef * hexti,uint32_t Edge)496 void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
497 {
498   uint32_t maskline;
499 
500   /* Check parameters */
501   assert_param(IS_EXTI_LINE(hexti->Line));
502   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
503   assert_param(IS_EXTI_PENDING_EDGE(Edge));
504 
505   /* Compute line mask */
506   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
507 
508   /* Clear Pending bit */
509   EXTI->PR =  maskline;
510 }
511 
512 /**
513   * @brief  Generate a software interrupt for a dedicated line.
514   * @param  hexti Exti handle.
515   * @retval None.
516   */
HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef * hexti)517 void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
518 {
519   uint32_t maskline;
520 
521   /* Check parameters */
522   assert_param(IS_EXTI_LINE(hexti->Line));
523   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
524 
525   /* Compute line mask */
526   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
527 
528   /* Generate Software interrupt */
529   EXTI->SWIER = maskline;
530 }
531 
532 /**
533   * @}
534   */
535 
536 /**
537   * @}
538   */
539 
540 #endif /* HAL_EXTI_MODULE_ENABLED */
541 /**
542   * @}
543   */
544 
545 /**
546   * @}
547   */
548 
549