stm32f3xx_hal_exti.c 17 KB

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