Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_exti.c
blob1d87be4420a777ec17c58103ef6e4b4873d34056
1 /**
2 ******************************************************************************
3 * @file stm32F7xx_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
11 @verbatim
12 ==============================================================================
13 ##### EXTI Peripheral features #####
14 ==============================================================================
15 [..]
16 (+) Each Exti line can be configured within this driver.
18 (+) Exti line can be configured in 3 different modes
19 (++) Interrupt
20 (++) Event
21 (++) Both of them
23 (+) Configurable Exti lines can be configured with 3 different triggers
24 (++) Rising
25 (++) Falling
26 (++) Both of them
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
34 (+) Exti lines 0 to 15 are linked to gpio pin number 0 to 15. Gpio port can
35 be selected through multiplexer.
37 ##### How to use this driver #####
38 ==============================================================================
39 [..]
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.
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.
56 (#) Clear Exti configuration of a dedicated line using HAL_EXTI_GetConfigLine().
57 (++) Provide exiting handle as parameter.
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.
65 (#) Get interrupt pending bit using HAL_EXTI_GetPending().
67 (#) Clear interrupt pending bit using HAL_EXTI_GetPending().
69 (#) Generate software interrupt using HAL_EXTI_GenerateSWI().
71 @endverbatim
72 ******************************************************************************
73 * @attention
75 * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
76 * All rights reserved.</center></h2>
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
83 ******************************************************************************
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32f7xx_hal.h"
88 #include "stm32f7xx_hal_exti.h"
90 /** @addtogroup STM32F7xx_HAL_Driver
91 * @{
94 /** @addtogroup EXTI
95 * @{
97 /** MISRA C:2012 deviation rule has been granted for following rule:
98 * Rule-18.1_b - Medium: Array `EXTICR' 1st subscript interval [0,7] may be out
99 * of bounds [0,3] in following API :
100 * HAL_EXTI_SetConfigLine
101 * HAL_EXTI_GetConfigLine
102 * HAL_EXTI_ClearConfigLine
105 #ifdef HAL_EXTI_MODULE_ENABLED
107 /* Private typedef -----------------------------------------------------------*/
108 /* Private defines ------------------------------------------------------------*/
109 /** @defgroup EXTI_Private_Constants EXTI Private Constants
110 * @{
114 * @}
117 /* Private macros ------------------------------------------------------------*/
118 /* Private variables ---------------------------------------------------------*/
119 /* Private function prototypes -----------------------------------------------*/
120 /* Exported functions --------------------------------------------------------*/
122 /** @addtogroup EXTI_Exported_Functions
123 * @{
126 /** @addtogroup EXTI_Exported_Functions_Group1
127 * @brief Configuration functions
129 @verbatim
130 ===============================================================================
131 ##### Configuration functions #####
132 ===============================================================================
134 @endverbatim
135 * @{
139 * @brief Set configuration of a dedicated Exti line.
140 * @param hexti Exti handle.
141 * @param pExtiConfig Pointer on EXTI configuration to be set.
142 * @retval HAL Status.
144 HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
146 uint32_t regval;
148 /* Check null pointer */
149 if ((hexti == NULL) || (pExtiConfig == NULL))
151 return HAL_ERROR;
154 /* Check parameters */
155 assert_param(IS_EXTI_LINE(pExtiConfig->Line));
156 assert_param(IS_EXTI_MODE(pExtiConfig->Mode));
157 assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
159 /* Assign line number to handle */
160 hexti->Line = pExtiConfig->Line;
162 /* Clear EXTI line configuration */
163 EXTI->IMR &= ~pExtiConfig->Line;
164 EXTI->EMR &= ~pExtiConfig->Line;
166 /* Select the Mode for the selected external interrupts */
167 regval = (uint32_t)EXTI_BASE;
168 regval += pExtiConfig->Mode;
169 *(__IO uint32_t *) regval |= pExtiConfig->Line;
171 /* Clear Rising Falling edge configuration */
172 EXTI->RTSR &= ~pExtiConfig->Line;
173 EXTI->FTSR &= ~pExtiConfig->Line;
175 /* Select the trigger for the selected external interrupts */
176 if (pExtiConfig->Trigger == EXTI_TRIGGER_RISING_FALLING)
178 /* Rising Falling edge */
179 EXTI->RTSR |= pExtiConfig->Line;
180 EXTI->FTSR |= pExtiConfig->Line;
182 else
184 regval = (uint32_t)EXTI_BASE;
185 regval += pExtiConfig->Trigger;
186 *(__IO uint32_t *) regval |= pExtiConfig->Line;
188 return HAL_OK;
192 * @brief Get configuration of a dedicated Exti line.
193 * @param hexti Exti handle.
194 * @param pExtiConfig Pointer on structure to store Exti configuration.
195 * @retval HAL Status.
197 HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
199 /* Check null pointer */
200 if ((hexti == NULL) || (pExtiConfig == NULL))
202 return HAL_ERROR;
205 /* Check the parameter */
206 assert_param(IS_EXTI_LINE(hexti->Line));
208 /* Store handle line number to configuration structure */
209 pExtiConfig->Line = hexti->Line;
211 /* Get EXTI mode to configiguration structure */
212 if ((EXTI->IMR & hexti->Line) == hexti->Line)
214 pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
216 else if ((EXTI->EMR & hexti->Line) == hexti->Line)
218 pExtiConfig->Mode = EXTI_MODE_EVENT;
220 else
222 /* No MODE selected */
223 pExtiConfig->Mode = 0x0Bu;
226 /* Get EXTI Trigger to configiguration structure */
227 if ((EXTI->RTSR & hexti->Line) == hexti->Line)
229 if ((EXTI->FTSR & hexti->Line) == hexti->Line)
231 pExtiConfig->Trigger = EXTI_TRIGGER_RISING_FALLING;
233 else
235 pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
238 else if ((EXTI->FTSR & hexti->Line) == hexti->Line)
240 pExtiConfig->Trigger = EXTI_TRIGGER_FALLING;
242 else
244 /* No Trigger selected */
245 pExtiConfig->Trigger = 0x00u;
248 return HAL_OK;
252 * @brief Clear whole configuration of a dedicated Exti line.
253 * @param hexti Exti handle.
254 * @retval HAL Status.
256 HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
258 /* Check null pointer */
259 if (hexti == NULL)
261 return HAL_ERROR;
264 /* Check the parameter */
265 assert_param(IS_EXTI_LINE(hexti->Line));
267 /* 1] Clear interrupt mode */
268 EXTI->IMR = (EXTI->IMR & ~hexti->Line);
270 /* 2] Clear event mode */
271 EXTI->EMR = (EXTI->EMR & ~hexti->Line);
273 /* 3] Clear triggers */
274 EXTI->RTSR = (EXTI->RTSR & ~hexti->Line);
275 EXTI->FTSR = (EXTI->FTSR & ~hexti->Line);
277 return HAL_OK;
281 * @brief Register callback for a dedicated Exti line.
282 * @param hexti Exti handle.
283 * @param CallbackID User callback identifier.
284 * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
285 * @param pPendingCbfn function pointer to be stored as callback.
286 * @retval HAL Status.
288 HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
290 HAL_StatusTypeDef status = HAL_OK;
292 switch (CallbackID)
294 case HAL_EXTI_COMMON_CB_ID:
295 hexti->PendingCallback = pPendingCbfn;
296 break;
298 default:
299 status = HAL_ERROR;
300 break;
303 return status;
307 * @brief Store line number as handle private field.
308 * @param hexti Exti handle.
309 * @param ExtiLine Exti line number.
310 * This parameter can be from 0 to @ref EXTI_LINE_NB.
311 * @retval HAL Status.
313 HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
315 /* Check the parameters */
316 assert_param(IS_EXTI_LINE(ExtiLine));
318 /* Check null pointer */
319 if (hexti == NULL)
321 return HAL_ERROR;
323 else
325 /* Store line number as handle private field */
326 hexti->Line = ExtiLine;
328 return HAL_OK;
333 * @}
336 /** @addtogroup EXTI_Exported_Functions_Group2
337 * @brief EXTI IO functions.
339 @verbatim
340 ===============================================================================
341 ##### IO operation functions #####
342 ===============================================================================
344 @endverbatim
345 * @{
349 * @brief Handle EXTI interrupt request.
350 * @param hexti Exti handle.
351 * @retval none.
353 void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
355 __IO uint32_t *regaddr;
356 uint32_t regval;
358 /* Get pending bit */
359 regaddr = (&EXTI->PR);
360 regval = (*regaddr & hexti->Line);
362 if (regval != 0x00u)
364 /* Clear pending bit */
365 *regaddr = hexti->Line;
367 /* Call callback */
368 if (hexti->PendingCallback != NULL)
370 hexti->PendingCallback();
376 * @brief Get interrupt pending bit of a dedicated line.
377 * @param hexti Exti handle.
378 * @param Edge Specify which pending edge as to be checked.
379 * This parameter can be one of the following values:
380 * @arg @ref EXTI_TRIGGER_RISING_FALLING
381 * This parameter is kept for compatibility with other series.
382 * @retval 1 if interrupt is pending else 0.
384 uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
386 __IO uint32_t *regaddr;
387 uint32_t regval;
389 /* Check parameters */
390 assert_param(IS_EXTI_LINE(hexti->Line));
391 assert_param(IS_EXTI_PENDING_EDGE(Edge));
393 /* Get pending bit */
394 regaddr = &EXTI->PR;
396 /* return 1 if bit is set else 0 */
397 regval = ((*regaddr & hexti->Line) >> POSITION_VAL(hexti->Line));
399 return regval;
403 * @brief Clear interrupt pending bit of a dedicated line.
404 * @param hexti Exti handle.
405 * @param Edge Specify which pending edge as to be clear.
406 * This parameter can be one of the following values:
407 * @arg @ref EXTI_TRIGGER_RISING_FALLING
408 * This parameter is kept for compatibility with other series.
409 * @retval None.
411 void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
413 /* Check parameters */
414 assert_param(IS_EXTI_LINE(hexti->Line));
415 assert_param(IS_EXTI_PENDING_EDGE(Edge));
417 /* Clear Pending bit */
418 EXTI->PR = hexti->Line;
422 * @brief Generate a software interrupt for a dedicated line.
423 * @param hexti Exti handle.
424 * @retval None.
426 void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
428 /* Check parameters */
429 assert_param(IS_EXTI_LINE(hexti->Line));
431 EXTI->SWIER = hexti->Line;
435 * @}
439 * @}
442 #endif /* HAL_EXTI_MODULE_ENABLED */
444 * @}
448 * @}
451 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/