You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_exti.c 15KB

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