stm32f1xx_hal_exti.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 "stm32f1xx_hal.h"
  73. /** @addtogroup STM32F1xx_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. /**
  93. * @}
  94. */
  95. /* Private macros ------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private function prototypes -----------------------------------------------*/
  98. /* Exported functions --------------------------------------------------------*/
  99. /** @addtogroup EXTI_Exported_Functions
  100. * @{
  101. */
  102. /** @addtogroup EXTI_Exported_Functions_Group1
  103. * @brief Configuration functions
  104. *
  105. @verbatim
  106. ===============================================================================
  107. ##### Configuration functions #####
  108. ===============================================================================
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Set configuration of a dedicated Exti line.
  114. * @param hexti Exti handle.
  115. * @param pExtiConfig Pointer on EXTI configuration to be set.
  116. * @retval HAL Status.
  117. */
  118. HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  119. {
  120. uint32_t regval;
  121. uint32_t linepos;
  122. uint32_t maskline;
  123. /* Check null pointer */
  124. if ((hexti == NULL) || (pExtiConfig == NULL))
  125. {
  126. return HAL_ERROR;
  127. }
  128. /* Check parameters */
  129. assert_param(IS_EXTI_LINE(pExtiConfig->Line));
  130. assert_param(IS_EXTI_MODE(pExtiConfig->Mode));
  131. /* Assign line number to handle */
  132. hexti->Line = pExtiConfig->Line;
  133. /* Compute line mask */
  134. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  135. maskline = (1uL << linepos);
  136. /* Configure triggers for configurable lines */
  137. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  138. {
  139. assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
  140. /* Configure rising trigger */
  141. /* Mask or set line */
  142. if ((pExtiConfig->Trigger & EXTI_TRIGGER_RISING) != 0x00u)
  143. {
  144. EXTI->RTSR |= maskline;
  145. }
  146. else
  147. {
  148. EXTI->RTSR &= ~maskline;
  149. }
  150. /* Configure falling trigger */
  151. /* Mask or set line */
  152. if ((pExtiConfig->Trigger & EXTI_TRIGGER_FALLING) != 0x00u)
  153. {
  154. EXTI->FTSR |= maskline;
  155. }
  156. else
  157. {
  158. EXTI->FTSR &= ~maskline;
  159. }
  160. /* Configure gpio port selection in case of gpio exti line */
  161. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  162. {
  163. assert_param(IS_EXTI_GPIO_PORT(pExtiConfig->GPIOSel));
  164. assert_param(IS_EXTI_GPIO_PIN(linepos));
  165. regval = AFIO->EXTICR[linepos >> 2u];
  166. regval &= ~(AFIO_EXTICR1_EXTI0 << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  167. regval |= (pExtiConfig->GPIOSel << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  168. AFIO->EXTICR[linepos >> 2u] = regval;
  169. }
  170. }
  171. /* Configure interrupt mode : read current mode */
  172. /* Mask or set line */
  173. if ((pExtiConfig->Mode & EXTI_MODE_INTERRUPT) != 0x00u)
  174. {
  175. EXTI->IMR |= maskline;
  176. }
  177. else
  178. {
  179. EXTI->IMR &= ~maskline;
  180. }
  181. /* Configure event mode : read current mode */
  182. /* Mask or set line */
  183. if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0x00u)
  184. {
  185. EXTI->EMR |= maskline;
  186. }
  187. else
  188. {
  189. EXTI->EMR &= ~maskline;
  190. }
  191. return HAL_OK;
  192. }
  193. /**
  194. * @brief Get configuration of a dedicated Exti line.
  195. * @param hexti Exti handle.
  196. * @param pExtiConfig Pointer on structure to store Exti configuration.
  197. * @retval HAL Status.
  198. */
  199. HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  200. {
  201. uint32_t regval;
  202. uint32_t linepos;
  203. uint32_t maskline;
  204. /* Check null pointer */
  205. if ((hexti == NULL) || (pExtiConfig == NULL))
  206. {
  207. return HAL_ERROR;
  208. }
  209. /* Check the parameter */
  210. assert_param(IS_EXTI_LINE(hexti->Line));
  211. /* Store handle line number to configuration structure */
  212. pExtiConfig->Line = hexti->Line;
  213. /* Compute line mask */
  214. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  215. maskline = (1uL << linepos);
  216. /* 1] Get core mode : interrupt */
  217. /* Check if selected line is enable */
  218. if ((EXTI->IMR & maskline) != 0x00u)
  219. {
  220. pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
  221. }
  222. else
  223. {
  224. pExtiConfig->Mode = EXTI_MODE_NONE;
  225. }
  226. /* Get event mode */
  227. /* Check if selected line is enable */
  228. if ((EXTI->EMR & maskline) != 0x00u)
  229. {
  230. pExtiConfig->Mode |= EXTI_MODE_EVENT;
  231. }
  232. /* Get default Trigger and GPIOSel configuration */
  233. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  234. pExtiConfig->GPIOSel = 0x00u;
  235. /* 2] Get trigger for configurable lines : rising */
  236. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  237. {
  238. /* Check if configuration of selected line is enable */
  239. if ((EXTI->RTSR & maskline) != 0x00u)
  240. {
  241. pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
  242. }
  243. /* Get falling configuration */
  244. /* Check if configuration of selected line is enable */
  245. if ((EXTI->FTSR & maskline) != 0x00u)
  246. {
  247. pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
  248. }
  249. /* Get Gpio port selection for gpio lines */
  250. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  251. {
  252. assert_param(IS_EXTI_GPIO_PIN(linepos));
  253. regval = AFIO->EXTICR[linepos >> 2u];
  254. pExtiConfig->GPIOSel = (regval >> (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u))) & AFIO_EXTICR1_EXTI0;
  255. }
  256. }
  257. return HAL_OK;
  258. }
  259. /**
  260. * @brief Clear whole configuration of a dedicated Exti line.
  261. * @param hexti Exti handle.
  262. * @retval HAL Status.
  263. */
  264. HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
  265. {
  266. uint32_t regval;
  267. uint32_t linepos;
  268. uint32_t maskline;
  269. /* Check null pointer */
  270. if (hexti == NULL)
  271. {
  272. return HAL_ERROR;
  273. }
  274. /* Check the parameter */
  275. assert_param(IS_EXTI_LINE(hexti->Line));
  276. /* compute line mask */
  277. linepos = (hexti->Line & EXTI_PIN_MASK);
  278. maskline = (1uL << linepos);
  279. /* 1] Clear interrupt mode */
  280. EXTI->IMR = (EXTI->IMR & ~maskline);
  281. /* 2] Clear event mode */
  282. EXTI->EMR = (EXTI->EMR & ~maskline);
  283. /* 3] Clear triggers in case of configurable lines */
  284. if ((hexti->Line & EXTI_CONFIG) != 0x00u)
  285. {
  286. EXTI->RTSR = (EXTI->RTSR & ~maskline);
  287. EXTI->FTSR = (EXTI->FTSR & ~maskline);
  288. /* Get Gpio port selection for gpio lines */
  289. if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
  290. {
  291. assert_param(IS_EXTI_GPIO_PIN(linepos));
  292. regval = AFIO->EXTICR[linepos >> 2u];
  293. regval &= ~(AFIO_EXTICR1_EXTI0 << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  294. AFIO->EXTICR[linepos >> 2u] = regval;
  295. }
  296. }
  297. return HAL_OK;
  298. }
  299. /**
  300. * @brief Register callback for a dedicated Exti line.
  301. * @param hexti Exti handle.
  302. * @param CallbackID User callback identifier.
  303. * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
  304. * @param pPendingCbfn function pointer to be stored as callback.
  305. * @retval HAL Status.
  306. */
  307. HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
  308. {
  309. HAL_StatusTypeDef status = HAL_OK;
  310. switch (CallbackID)
  311. {
  312. case HAL_EXTI_COMMON_CB_ID:
  313. hexti->PendingCallback = pPendingCbfn;
  314. break;
  315. default:
  316. status = HAL_ERROR;
  317. break;
  318. }
  319. return status;
  320. }
  321. /**
  322. * @brief Store line number as handle private field.
  323. * @param hexti Exti handle.
  324. * @param ExtiLine Exti line number.
  325. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  326. * @retval HAL Status.
  327. */
  328. HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
  329. {
  330. /* Check the parameters */
  331. assert_param(IS_EXTI_LINE(ExtiLine));
  332. /* Check null pointer */
  333. if (hexti == NULL)
  334. {
  335. return HAL_ERROR;
  336. }
  337. else
  338. {
  339. /* Store line number as handle private field */
  340. hexti->Line = ExtiLine;
  341. return HAL_OK;
  342. }
  343. }
  344. /**
  345. * @}
  346. */
  347. /** @addtogroup EXTI_Exported_Functions_Group2
  348. * @brief EXTI IO functions.
  349. *
  350. @verbatim
  351. ===============================================================================
  352. ##### IO operation functions #####
  353. ===============================================================================
  354. @endverbatim
  355. * @{
  356. */
  357. /**
  358. * @brief Handle EXTI interrupt request.
  359. * @param hexti Exti handle.
  360. * @retval none.
  361. */
  362. void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
  363. {
  364. uint32_t regval;
  365. uint32_t maskline;
  366. /* Compute line mask */
  367. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  368. /* Get pending bit */
  369. regval = (EXTI->PR & maskline);
  370. if (regval != 0x00u)
  371. {
  372. /* Clear pending bit */
  373. EXTI->PR = maskline;
  374. /* Call callback */
  375. if (hexti->PendingCallback != NULL)
  376. {
  377. hexti->PendingCallback();
  378. }
  379. }
  380. }
  381. /**
  382. * @brief Get interrupt pending bit of a dedicated line.
  383. * @param hexti Exti handle.
  384. * @param Edge Specify which pending edge as to be checked.
  385. * This parameter can be one of the following values:
  386. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  387. * This parameter is kept for compatibility with other series.
  388. * @retval 1 if interrupt is pending else 0.
  389. */
  390. uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  391. {
  392. uint32_t regval;
  393. uint32_t maskline;
  394. uint32_t linepos;
  395. /* Check parameters */
  396. assert_param(IS_EXTI_LINE(hexti->Line));
  397. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  398. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  399. /* Prevent unused argument compilation warning */
  400. UNUSED(Edge);
  401. /* Compute line mask */
  402. linepos = (hexti->Line & EXTI_PIN_MASK);
  403. maskline = (1uL << linepos);
  404. /* return 1 if bit is set else 0 */
  405. regval = ((EXTI->PR & maskline) >> linepos);
  406. return regval;
  407. }
  408. /**
  409. * @brief Clear interrupt pending bit of a dedicated line.
  410. * @param hexti Exti handle.
  411. * @param Edge Specify which pending edge as to be clear.
  412. * This parameter can be one of the following values:
  413. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  414. * This parameter is kept for compatibility with other series.
  415. * @retval None.
  416. */
  417. void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  418. {
  419. uint32_t maskline;
  420. /* Check parameters */
  421. assert_param(IS_EXTI_LINE(hexti->Line));
  422. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  423. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  424. /* Prevent unused argument compilation warning */
  425. UNUSED(Edge);
  426. /* Compute line mask */
  427. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  428. /* Clear Pending bit */
  429. EXTI->PR = maskline;
  430. }
  431. /**
  432. * @brief Generate a software interrupt for a dedicated line.
  433. * @param hexti Exti handle.
  434. * @retval None.
  435. */
  436. void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
  437. {
  438. uint32_t maskline;
  439. /* Check parameters */
  440. assert_param(IS_EXTI_LINE(hexti->Line));
  441. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  442. /* Compute line mask */
  443. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  444. /* Generate Software interrupt */
  445. EXTI->SWIER = maskline;
  446. }
  447. /**
  448. * @}
  449. */
  450. /**
  451. * @}
  452. */
  453. #endif /* HAL_EXTI_MODULE_ENABLED */
  454. /**
  455. * @}
  456. */
  457. /**
  458. * @}
  459. */