stm32f1xx_hal_exti.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. @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) 2019 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 "stm32f1xx_hal.h"
  74. /** @addtogroup STM32F1xx_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 = AFIO->EXTICR[linepos >> 2u];
  167. regval &= ~(AFIO_EXTICR1_EXTI0 << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  168. regval |= (pExtiConfig->GPIOSel << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  169. AFIO->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. /* Get default Trigger and GPIOSel configuration */
  234. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  235. pExtiConfig->GPIOSel = 0x00u;
  236. /* 2] Get trigger for configurable lines : rising */
  237. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  238. {
  239. /* Check if configuration of selected line is enable */
  240. if ((EXTI->RTSR & maskline) != 0x00u)
  241. {
  242. pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
  243. }
  244. /* Get falling configuration */
  245. /* Check if configuration of selected line is enable */
  246. if ((EXTI->FTSR & maskline) != 0x00u)
  247. {
  248. pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
  249. }
  250. /* Get Gpio port selection for gpio lines */
  251. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  252. {
  253. assert_param(IS_EXTI_GPIO_PIN(linepos));
  254. regval = AFIO->EXTICR[linepos >> 2u];
  255. pExtiConfig->GPIOSel = ((regval << (AFIO_EXTICR1_EXTI1_Pos * (3uL - (linepos & 0x03u)))) >> 24);
  256. }
  257. }
  258. return HAL_OK;
  259. }
  260. /**
  261. * @brief Clear whole configuration of a dedicated Exti line.
  262. * @param hexti Exti handle.
  263. * @retval HAL Status.
  264. */
  265. HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
  266. {
  267. uint32_t regval;
  268. uint32_t linepos;
  269. uint32_t maskline;
  270. /* Check null pointer */
  271. if (hexti == NULL)
  272. {
  273. return HAL_ERROR;
  274. }
  275. /* Check the parameter */
  276. assert_param(IS_EXTI_LINE(hexti->Line));
  277. /* compute line mask */
  278. linepos = (hexti->Line & EXTI_PIN_MASK);
  279. maskline = (1uL << linepos);
  280. /* 1] Clear interrupt mode */
  281. EXTI->IMR = (EXTI->IMR & ~maskline);
  282. /* 2] Clear event mode */
  283. EXTI->EMR = (EXTI->EMR & ~maskline);
  284. /* 3] Clear triggers in case of configurable lines */
  285. if ((hexti->Line & EXTI_CONFIG) != 0x00u)
  286. {
  287. EXTI->RTSR = (EXTI->RTSR & ~maskline);
  288. EXTI->FTSR = (EXTI->FTSR & ~maskline);
  289. /* Get Gpio port selection for gpio lines */
  290. if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
  291. {
  292. assert_param(IS_EXTI_GPIO_PIN(linepos));
  293. regval = AFIO->EXTICR[linepos >> 2u];
  294. regval &= ~(AFIO_EXTICR1_EXTI0 << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  295. AFIO->EXTICR[linepos >> 2u] = regval;
  296. }
  297. }
  298. return HAL_OK;
  299. }
  300. /**
  301. * @brief Register callback for a dedicated Exti line.
  302. * @param hexti Exti handle.
  303. * @param CallbackID User callback identifier.
  304. * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
  305. * @param pPendingCbfn function pointer to be stored as callback.
  306. * @retval HAL Status.
  307. */
  308. HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
  309. {
  310. HAL_StatusTypeDef status = HAL_OK;
  311. switch (CallbackID)
  312. {
  313. case HAL_EXTI_COMMON_CB_ID:
  314. hexti->PendingCallback = pPendingCbfn;
  315. break;
  316. default:
  317. status = HAL_ERROR;
  318. break;
  319. }
  320. return status;
  321. }
  322. /**
  323. * @brief Store line number as handle private field.
  324. * @param hexti Exti handle.
  325. * @param ExtiLine Exti line number.
  326. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  327. * @retval HAL Status.
  328. */
  329. HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
  330. {
  331. /* Check the parameters */
  332. assert_param(IS_EXTI_LINE(ExtiLine));
  333. /* Check null pointer */
  334. if (hexti == NULL)
  335. {
  336. return HAL_ERROR;
  337. }
  338. else
  339. {
  340. /* Store line number as handle private field */
  341. hexti->Line = ExtiLine;
  342. return HAL_OK;
  343. }
  344. }
  345. /**
  346. * @}
  347. */
  348. /** @addtogroup EXTI_Exported_Functions_Group2
  349. * @brief EXTI IO functions.
  350. *
  351. @verbatim
  352. ===============================================================================
  353. ##### IO operation functions #####
  354. ===============================================================================
  355. @endverbatim
  356. * @{
  357. */
  358. /**
  359. * @brief Handle EXTI interrupt request.
  360. * @param hexti Exti handle.
  361. * @retval none.
  362. */
  363. void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
  364. {
  365. uint32_t regval;
  366. uint32_t maskline;
  367. /* Compute line mask */
  368. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  369. /* Get pending bit */
  370. regval = (EXTI->PR & maskline);
  371. if (regval != 0x00u)
  372. {
  373. /* Clear pending bit */
  374. EXTI->PR = maskline;
  375. /* Call callback */
  376. if (hexti->PendingCallback != NULL)
  377. {
  378. hexti->PendingCallback();
  379. }
  380. }
  381. }
  382. /**
  383. * @brief Get interrupt pending bit of a dedicated line.
  384. * @param hexti Exti handle.
  385. * @param Edge Specify which pending edge as to be checked.
  386. * This parameter can be one of the following values:
  387. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  388. * This parameter is kept for compatibility with other series.
  389. * @retval 1 if interrupt is pending else 0.
  390. */
  391. uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  392. {
  393. uint32_t regval;
  394. uint32_t maskline;
  395. uint32_t linepos;
  396. /* Check parameters */
  397. assert_param(IS_EXTI_LINE(hexti->Line));
  398. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  399. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  400. /* Prevent unused argument compilation warning */
  401. UNUSED(Edge);
  402. /* Compute line mask */
  403. linepos = (hexti->Line & EXTI_PIN_MASK);
  404. maskline = (1uL << linepos);
  405. /* return 1 if bit is set else 0 */
  406. regval = ((EXTI->PR & maskline) >> linepos);
  407. return regval;
  408. }
  409. /**
  410. * @brief Clear interrupt pending bit of a dedicated line.
  411. * @param hexti Exti handle.
  412. * @param Edge Specify which pending edge as to be clear.
  413. * This parameter can be one of the following values:
  414. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  415. * This parameter is kept for compatibility with other series.
  416. * @retval None.
  417. */
  418. void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  419. {
  420. uint32_t maskline;
  421. /* Check parameters */
  422. assert_param(IS_EXTI_LINE(hexti->Line));
  423. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  424. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  425. /* Prevent unused argument compilation warning */
  426. UNUSED(Edge);
  427. /* Compute line mask */
  428. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  429. /* Clear Pending bit */
  430. EXTI->PR = maskline;
  431. }
  432. /**
  433. * @brief Generate a software interrupt for a dedicated line.
  434. * @param hexti Exti handle.
  435. * @retval None.
  436. */
  437. void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
  438. {
  439. uint32_t maskline;
  440. /* Check parameters */
  441. assert_param(IS_EXTI_LINE(hexti->Line));
  442. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  443. /* Compute line mask */
  444. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  445. /* Generate Software interrupt */
  446. EXTI->SWIER = maskline;
  447. }
  448. /**
  449. * @}
  450. */
  451. /**
  452. * @}
  453. */
  454. #endif /* HAL_EXTI_MODULE_ENABLED */
  455. /**
  456. * @}
  457. */
  458. /**
  459. * @}
  460. */
  461. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/