stm32g4xx_hal_exti.c 17 KB

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