stm32l4xx_hal_exti.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2018 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 "stm32l4xx_hal.h"
  73. /** @addtogroup STM32L4xx_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 MCU IMR/EMR registers */
  93. #define EXTI_CONFIG_OFFSET 0x08u /* 0x20: offset between MCU 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->RTSR1 + (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->FTSR1 + (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->IMR1 + (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. /* The event mode cannot be configured if the line does not support it */
  199. assert_param(((pExtiConfig->Line & EXTI_EVENT) == EXTI_EVENT) || ((pExtiConfig->Mode & EXTI_MODE_EVENT) != EXTI_MODE_EVENT));
  200. /* Configure event mode : read current mode */
  201. regaddr = (&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  202. regval = *regaddr;
  203. /* Mask or set line */
  204. if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0x00u)
  205. {
  206. regval |= maskline;
  207. }
  208. else
  209. {
  210. regval &= ~maskline;
  211. }
  212. /* Store event mode */
  213. *regaddr = regval;
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief Get configuration of a dedicated Exti line.
  218. * @param hexti Exti handle.
  219. * @param pExtiConfig Pointer on structure to store Exti configuration.
  220. * @retval HAL Status.
  221. */
  222. HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  223. {
  224. __IO uint32_t *regaddr;
  225. uint32_t regval;
  226. uint32_t linepos;
  227. uint32_t maskline;
  228. uint32_t offset;
  229. /* Check null pointer */
  230. if ((hexti == NULL) || (pExtiConfig == NULL))
  231. {
  232. return HAL_ERROR;
  233. }
  234. /* Check the parameter */
  235. assert_param(IS_EXTI_LINE(hexti->Line));
  236. /* Store handle line number to configuration structure */
  237. pExtiConfig->Line = hexti->Line;
  238. /* Compute line register offset and line mask */
  239. offset = ((pExtiConfig->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  240. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  241. maskline = (1uL << linepos);
  242. /* 1] Get core mode : interrupt */
  243. regaddr = (&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  244. regval = *regaddr;
  245. /* Check if selected line is enable */
  246. if ((regval & maskline) != 0x00u)
  247. {
  248. pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
  249. }
  250. else
  251. {
  252. pExtiConfig->Mode = EXTI_MODE_NONE;
  253. }
  254. /* Get event mode */
  255. regaddr = (&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  256. regval = *regaddr;
  257. /* Check if selected line is enable */
  258. if ((regval & maskline) != 0x00u)
  259. {
  260. pExtiConfig->Mode |= EXTI_MODE_EVENT;
  261. }
  262. /* Get default Trigger and GPIOSel configuration */
  263. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  264. pExtiConfig->GPIOSel = 0x00u;
  265. /* 2] Get trigger for configurable lines : rising */
  266. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  267. {
  268. regaddr = (&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  269. regval = *regaddr;
  270. /* Check if configuration of selected line is enable */
  271. if ((regval & maskline) != 0x00u)
  272. {
  273. pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
  274. }
  275. /* Get falling configuration */
  276. regaddr = (&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  277. regval = *regaddr;
  278. /* Check if configuration of selected line is enable */
  279. if ((regval & maskline) != 0x00u)
  280. {
  281. pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
  282. }
  283. /* Get Gpio port selection for gpio lines */
  284. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  285. {
  286. assert_param(IS_EXTI_GPIO_PIN(linepos));
  287. regval = SYSCFG->EXTICR[linepos >> 2u];
  288. pExtiConfig->GPIOSel = (regval >> (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u))) & SYSCFG_EXTICR1_EXTI0;
  289. }
  290. }
  291. return HAL_OK;
  292. }
  293. /**
  294. * @brief Clear whole configuration of a dedicated Exti line.
  295. * @param hexti Exti handle.
  296. * @retval HAL Status.
  297. */
  298. HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
  299. {
  300. __IO uint32_t *regaddr;
  301. uint32_t regval;
  302. uint32_t linepos;
  303. uint32_t maskline;
  304. uint32_t offset;
  305. /* Check null pointer */
  306. if (hexti == NULL)
  307. {
  308. return HAL_ERROR;
  309. }
  310. /* Check the parameter */
  311. assert_param(IS_EXTI_LINE(hexti->Line));
  312. /* compute line register offset and line mask */
  313. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  314. linepos = (hexti->Line & EXTI_PIN_MASK);
  315. maskline = (1uL << linepos);
  316. /* 1] Clear interrupt mode */
  317. regaddr = (&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  318. regval = (*regaddr & ~maskline);
  319. *regaddr = regval;
  320. /* 2] Clear event mode */
  321. regaddr = (&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  322. regval = (*regaddr & ~maskline);
  323. *regaddr = regval;
  324. /* 3] Clear triggers in case of configurable lines */
  325. if ((hexti->Line & EXTI_CONFIG) != 0x00u)
  326. {
  327. regaddr = (&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  328. regval = (*regaddr & ~maskline);
  329. *regaddr = regval;
  330. regaddr = (&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  331. regval = (*regaddr & ~maskline);
  332. *regaddr = regval;
  333. /* Get Gpio port selection for gpio lines */
  334. if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
  335. {
  336. assert_param(IS_EXTI_GPIO_PIN(linepos));
  337. regval = SYSCFG->EXTICR[linepos >> 2u];
  338. regval &= ~(SYSCFG_EXTICR1_EXTI0 << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  339. SYSCFG->EXTICR[linepos >> 2u] = regval;
  340. }
  341. }
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief Register callback for a dedicated Exti line.
  346. * @param hexti Exti handle.
  347. * @param CallbackID User callback identifier.
  348. * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
  349. * @param pPendingCbfn function pointer to be stored as callback.
  350. * @retval HAL Status.
  351. */
  352. HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
  353. {
  354. HAL_StatusTypeDef status = HAL_OK;
  355. switch (CallbackID)
  356. {
  357. case HAL_EXTI_COMMON_CB_ID:
  358. hexti->PendingCallback = pPendingCbfn;
  359. break;
  360. default:
  361. status = HAL_ERROR;
  362. break;
  363. }
  364. return status;
  365. }
  366. /**
  367. * @brief Store line number as handle private field.
  368. * @param hexti Exti handle.
  369. * @param ExtiLine Exti line number.
  370. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  371. * @retval HAL Status.
  372. */
  373. HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
  374. {
  375. /* Check the parameters */
  376. assert_param(IS_EXTI_LINE(ExtiLine));
  377. /* Check null pointer */
  378. if (hexti == NULL)
  379. {
  380. return HAL_ERROR;
  381. }
  382. else
  383. {
  384. /* Store line number as handle private field */
  385. hexti->Line = ExtiLine;
  386. return HAL_OK;
  387. }
  388. }
  389. /**
  390. * @}
  391. */
  392. /** @addtogroup EXTI_Exported_Functions_Group2
  393. * @brief EXTI IO functions.
  394. *
  395. @verbatim
  396. ===============================================================================
  397. ##### IO operation functions #####
  398. ===============================================================================
  399. @endverbatim
  400. * @{
  401. */
  402. /**
  403. * @brief Handle EXTI interrupt request.
  404. * @param hexti Exti handle.
  405. * @retval none.
  406. */
  407. void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
  408. {
  409. __IO uint32_t *regaddr;
  410. uint32_t regval;
  411. uint32_t maskline;
  412. uint32_t offset;
  413. /* Compute line register offset and line mask */
  414. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  415. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  416. /* Get pending bit */
  417. regaddr = (&EXTI->PR1 + (EXTI_CONFIG_OFFSET * offset));
  418. regval = (*regaddr & maskline);
  419. if (regval != 0x00u)
  420. {
  421. /* Clear pending bit */
  422. *regaddr = maskline;
  423. /* Call callback */
  424. if (hexti->PendingCallback != NULL)
  425. {
  426. hexti->PendingCallback();
  427. }
  428. }
  429. }
  430. /**
  431. * @brief Get interrupt pending bit of a dedicated line.
  432. * @param hexti Exti handle.
  433. * @param Edge Specify which pending edge as to be checked.
  434. * This parameter can be one of the following values:
  435. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  436. * This parameter is kept for compatibility with other series.
  437. * @retval 1 if interrupt is pending else 0.
  438. */
  439. uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  440. {
  441. __IO uint32_t *regaddr;
  442. uint32_t regval;
  443. uint32_t linepos;
  444. uint32_t maskline;
  445. uint32_t offset;
  446. /* Prevent unused argument(s) compilation warning */
  447. UNUSED(Edge);
  448. /* Check parameters */
  449. assert_param(IS_EXTI_LINE(hexti->Line));
  450. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  451. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  452. /* Compute line register offset and line mask */
  453. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  454. linepos = (hexti->Line & EXTI_PIN_MASK);
  455. maskline = (1uL << linepos);
  456. /* Get pending bit */
  457. regaddr = (&EXTI->PR1 + (EXTI_CONFIG_OFFSET * offset));
  458. /* return 1 if bit is set else 0 */
  459. regval = ((*regaddr & maskline) >> linepos);
  460. return regval;
  461. }
  462. /**
  463. * @brief Clear interrupt pending bit of a dedicated line.
  464. * @param hexti Exti handle.
  465. * @param Edge Specify which pending edge as to be clear.
  466. * This parameter can be one of the following values:
  467. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  468. * This parameter is kept for compatibility with other series.
  469. * @retval None.
  470. */
  471. void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  472. {
  473. __IO uint32_t *regaddr;
  474. uint32_t maskline;
  475. uint32_t offset;
  476. /* Prevent unused argument(s) compilation warning */
  477. UNUSED(Edge);
  478. /* Check parameters */
  479. assert_param(IS_EXTI_LINE(hexti->Line));
  480. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  481. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  482. /* compute line register offset and line mask */
  483. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  484. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  485. /* Get pending register address */
  486. regaddr = (&EXTI->PR1 + (EXTI_CONFIG_OFFSET * offset));
  487. /* Clear Pending bit */
  488. *regaddr = maskline;
  489. }
  490. /**
  491. * @brief Generate a software interrupt for a dedicated line.
  492. * @param hexti Exti handle.
  493. * @retval None.
  494. */
  495. void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
  496. {
  497. __IO uint32_t *regaddr;
  498. uint32_t maskline;
  499. uint32_t offset;
  500. /* Check parameters */
  501. assert_param(IS_EXTI_LINE(hexti->Line));
  502. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  503. /* compute line register offset and line mask */
  504. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  505. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  506. regaddr = (&EXTI->SWIER1 + (EXTI_CONFIG_OFFSET * offset));
  507. *regaddr = maskline;
  508. }
  509. /**
  510. * @}
  511. */
  512. /**
  513. * @}
  514. */
  515. #endif /* HAL_EXTI_MODULE_ENABLED */
  516. /**
  517. * @}
  518. */
  519. /**
  520. * @}
  521. */