You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

642 lines
17 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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 General Purpose Input/Output (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 one
  26. interrupt pending register:
  27. (++) Trigger request occurred
  28. (+) Exti lines 0 to 15 are linked to gpio pin number 0 to 15. Gpio port can
  29. be selected throught multiplexer.
  30. ##### How to use this driver #####
  31. ==============================================================================
  32. [..]
  33. (#) Configure the EXTI line using HAL_EXTI_SetConfigLine().
  34. (++) Choose the interrupt line number by setting "Line" member from
  35. EXTI_ConfigTypeDef structure.
  36. (++) Configure the interrupt and/or event mode using "Mode" member from
  37. EXTI_ConfigTypeDef structure.
  38. (++) For configurable lines, configure rising and/or falling trigger
  39. "Trigger" member from EXTI_ConfigTypeDef structure.
  40. (++) For Exti lines linked to gpio, choose gpio port using "GPIOSel"
  41. member from GPIO_InitTypeDef structure.
  42. (#) Get current Exti configuration of a dedicated line using
  43. HAL_EXTI_GetConfigLine().
  44. (++) Provide exiting handle as parameter.
  45. (++) Provide pointer on EXTI_ConfigTypeDef structure as second parameter.
  46. (#) Clear Exti configuration of a dedicated line using HAL_EXTI_GetConfigLine().
  47. (++) Provide exiting handle as parameter.
  48. (#) Register callback to treat Exti interrupts using HAL_EXTI_RegisterCallback().
  49. (++) Provide exiting handle as first parameter.
  50. (++) Provide which callback will be registered using one value from
  51. EXTI_CallbackIDTypeDef.
  52. (++) Provide callback function pointer.
  53. (#) Get interrupt pending bit using HAL_EXTI_GetPending().
  54. (#) Clear interrupt pending bit using HAL_EXTI_GetPending().
  55. (#) Generate software interrupt using HAL_EXTI_GenerateSWI().
  56. @endverbatim
  57. ******************************************************************************
  58. * @attention
  59. *
  60. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  61. * All rights reserved.</center></h2>
  62. *
  63. * This software component is licensed by ST under BSD 3-Clause license,
  64. * the "License"; You may not use this file except in compliance with the
  65. * License. You may obtain a copy of the License at:
  66. * opensource.org/licenses/BSD-3-Clause
  67. *
  68. ******************************************************************************
  69. */
  70. /* Includes ------------------------------------------------------------------*/
  71. #include "stm32wbxx_hal.h"
  72. /** @addtogroup STM32WBxx_HAL_Driver
  73. * @{
  74. */
  75. /** @addtogroup EXTI
  76. * @{
  77. */
  78. /** MISRA C:2012 deviation rule has been granted for following rule:
  79. * Rule-18.1_b - Medium: Array `EXTICR' 1st subscript interval [0,7] may be out
  80. * of bounds [0,3] in following API :
  81. * HAL_EXTI_SetConfigLine
  82. * HAL_EXTI_GetConfigLine
  83. * HAL_EXTI_ClearConfigLine
  84. */
  85. #ifdef HAL_EXTI_MODULE_ENABLED
  86. /* Private typedef -----------------------------------------------------------*/
  87. /* Private defines ------------------------------------------------------------*/
  88. /** @defgroup EXTI_Private_Constants EXTI Private Constants
  89. * @{
  90. */
  91. #define EXTI_MODE_OFFSET 0x04u /* 0x10: offset between CPU IMR/EMR registers */
  92. #define EXTI_CONFIG_OFFSET 0x08u /* 0x20: offset between CPU Rising/Falling configuration registers */
  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. __IO uint32_t *regaddr;
  122. uint32_t regval;
  123. uint32_t linepos;
  124. uint32_t maskline;
  125. uint32_t offset;
  126. /* Check null pointer */
  127. if ((hexti == NULL) || (pExtiConfig == NULL))
  128. {
  129. return HAL_ERROR;
  130. }
  131. /* Check parameters */
  132. assert_param(IS_EXTI_LINE(pExtiConfig->Line));
  133. assert_param(IS_EXTI_MODE(pExtiConfig->Mode));
  134. /* Assign line number to handle */
  135. hexti->Line = pExtiConfig->Line;
  136. /* compute line register offset and line mask */
  137. offset = ((pExtiConfig->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  138. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  139. maskline = (1uL << linepos);
  140. /* Configure triggers for configurable lines */
  141. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  142. {
  143. assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
  144. /* Configure rising trigger */
  145. regaddr = (&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  146. regval = *regaddr;
  147. /* Mask or set line */
  148. if ((pExtiConfig->Trigger & EXTI_TRIGGER_RISING) != 0x00u)
  149. {
  150. regval |= maskline;
  151. }
  152. else
  153. {
  154. regval &= ~maskline;
  155. }
  156. /* Store rising trigger mode */
  157. *regaddr = regval;
  158. /* Configure falling trigger */
  159. regaddr = (&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  160. regval = *regaddr;
  161. /* Mask or set line */
  162. if ((pExtiConfig->Trigger & EXTI_TRIGGER_FALLING) != 0x00u)
  163. {
  164. regval |= maskline;
  165. }
  166. else
  167. {
  168. regval &= ~maskline;
  169. }
  170. /* Store falling trigger mode */
  171. *regaddr = regval;
  172. /* Configure gpio port selection in case of gpio exti line */
  173. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  174. {
  175. assert_param(IS_EXTI_GPIO_PORT(pExtiConfig->GPIOSel));
  176. assert_param(IS_EXTI_GPIO_PIN(linepos));
  177. regval = SYSCFG->EXTICR[linepos >> 2u];
  178. regval &= ~(SYSCFG_EXTICR1_EXTI0 << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  179. regval |= (pExtiConfig->GPIOSel << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  180. SYSCFG->EXTICR[linepos >> 2u] = regval;
  181. }
  182. }
  183. /* Configure interrupt mode : read current mode */
  184. regaddr = (&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  185. regval = *regaddr;
  186. /* Mask or set line */
  187. if ((pExtiConfig->Mode & EXTI_MODE_INTERRUPT) != 0x00u)
  188. {
  189. regval |= maskline;
  190. }
  191. else
  192. {
  193. regval &= ~maskline;
  194. }
  195. /* Store interrupt mode */
  196. *regaddr = regval;
  197. /* The event mode cannot be configured if the line does not support it */
  198. assert_param(((pExtiConfig->Line & EXTI_EVENT) == EXTI_EVENT) || ((pExtiConfig->Mode & EXTI_MODE_EVENT) != EXTI_MODE_EVENT));
  199. /* Configure event mode : read current mode */
  200. regaddr = (&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  201. regval = *regaddr;
  202. /* Mask or set line */
  203. if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0x00u)
  204. {
  205. regval |= maskline;
  206. }
  207. else
  208. {
  209. regval &= ~maskline;
  210. }
  211. /* Store event mode */
  212. *regaddr = regval;
  213. return HAL_OK;
  214. }
  215. /**
  216. * @brief Get configuration of a dedicated Exti line.
  217. * @param hexti Exti handle.
  218. * @param pExtiConfig Pointer on structure to store Exti configuration.
  219. * @retval HAL Status.
  220. */
  221. HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  222. {
  223. __IO uint32_t *regaddr;
  224. uint32_t regval;
  225. uint32_t linepos;
  226. uint32_t maskline;
  227. uint32_t offset;
  228. /* Check null pointer */
  229. if ((hexti == NULL) || (pExtiConfig == NULL))
  230. {
  231. return HAL_ERROR;
  232. }
  233. /* Check the parameter */
  234. assert_param(IS_EXTI_LINE(hexti->Line));
  235. /* Store handle line number to configiguration structure */
  236. pExtiConfig->Line = hexti->Line;
  237. /* compute line register offset and line mask */
  238. offset = ((pExtiConfig->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  239. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  240. maskline = (1uL << linepos);
  241. /* 1] Get core mode : interrupt */
  242. regaddr = (&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  243. regval = *regaddr;
  244. /* Check if selected line is enable */
  245. if ((regval & maskline) != 0x00u)
  246. {
  247. pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
  248. }
  249. else
  250. {
  251. pExtiConfig->Mode = EXTI_MODE_NONE;
  252. }
  253. /* Get event mode */
  254. regaddr = (&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  255. regval = *regaddr;
  256. /* Check if selected line is enable */
  257. if ((regval & maskline) != 0x00u)
  258. {
  259. pExtiConfig->Mode |= EXTI_MODE_EVENT;
  260. }
  261. /* 2] Get trigger for configurable lines : rising */
  262. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  263. {
  264. regaddr = (&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  265. regval = *regaddr;
  266. /* Check if configuration of selected line is enable */
  267. if ((regval & maskline) != 0x00u)
  268. {
  269. pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
  270. }
  271. else
  272. {
  273. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  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 * (3uL - (linepos & 0x03u)))) >> 24);
  289. }
  290. else
  291. {
  292. pExtiConfig->GPIOSel = 0x00u;
  293. }
  294. }
  295. else
  296. {
  297. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  298. pExtiConfig->GPIOSel = 0x00u;
  299. }
  300. return HAL_OK;
  301. }
  302. /**
  303. * @brief Clear whole configuration of a dedicated Exti line.
  304. * @param hexti Exti handle.
  305. * @retval HAL Status.
  306. */
  307. HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
  308. {
  309. __IO uint32_t *regaddr;
  310. uint32_t regval;
  311. uint32_t linepos;
  312. uint32_t maskline;
  313. uint32_t offset;
  314. /* Check null pointer */
  315. if (hexti == NULL)
  316. {
  317. return HAL_ERROR;
  318. }
  319. /* Check the parameter */
  320. assert_param(IS_EXTI_LINE(hexti->Line));
  321. /* compute line register offset and line mask */
  322. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  323. linepos = (hexti->Line & EXTI_PIN_MASK);
  324. maskline = (1uL << linepos);
  325. /* 1] Clear interrupt mode */
  326. regaddr = (&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  327. regval = (*regaddr & ~maskline);
  328. *regaddr = regval;
  329. /* 2] Clear event mode */
  330. regaddr = (&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  331. regval = (*regaddr & ~maskline);
  332. *regaddr = regval;
  333. /* 3] Clear triggers in case of configurable lines */
  334. if ((hexti->Line & EXTI_CONFIG) != 0x00u)
  335. {
  336. regaddr = (&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  337. regval = (*regaddr & ~maskline);
  338. *regaddr = regval;
  339. regaddr = (&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  340. regval = (*regaddr & ~maskline);
  341. *regaddr = regval;
  342. /* Get Gpio port selection for gpio lines */
  343. if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
  344. {
  345. assert_param(IS_EXTI_GPIO_PIN(linepos));
  346. regval = SYSCFG->EXTICR[linepos >> 2u];
  347. regval &= ~(SYSCFG_EXTICR1_EXTI0 << (SYSCFG_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  348. SYSCFG->EXTICR[linepos >> 2u] = regval;
  349. }
  350. }
  351. return HAL_OK;
  352. }
  353. /**
  354. * @brief Register callback for a dedicaated Exti line.
  355. * @param hexti Exti handle.
  356. * @param CallbackID User callback identifier.
  357. * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
  358. * @param pPendingCbfn function pointer to be stored as callback.
  359. * @retval HAL Status.
  360. */
  361. HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
  362. {
  363. HAL_StatusTypeDef status = HAL_OK;
  364. switch (CallbackID)
  365. {
  366. case HAL_EXTI_COMMON_CB_ID:
  367. hexti->PendingCallback = pPendingCbfn;
  368. break;
  369. default:
  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 and line mask */
  423. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  424. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  425. /* Get pending bit */
  426. regaddr = (&EXTI->PR1 + (EXTI_CONFIG_OFFSET * offset));
  427. regval = (*regaddr & maskline);
  428. if (regval != 0x00u)
  429. {
  430. /* Clear pending bit */
  431. *regaddr = maskline;
  432. /* Call callback */
  433. if (hexti->PendingCallback != NULL)
  434. {
  435. hexti->PendingCallback();
  436. }
  437. }
  438. }
  439. /**
  440. * @brief Get interrupt pending bit of a dedicated line.
  441. * @param hexti Exti handle.
  442. * @param Edge Specify which pending edge as to be checked.
  443. * This parameter can be one of the following values:
  444. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  445. * This parameter is kept for compatibility with other series.
  446. * @retval 1 if interrupt is pending else 0.
  447. */
  448. uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  449. {
  450. __IO uint32_t *regaddr;
  451. uint32_t regval;
  452. uint32_t linepos;
  453. uint32_t maskline;
  454. uint32_t offset;
  455. /* Check parameters */
  456. assert_param(IS_EXTI_LINE(hexti->Line));
  457. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  458. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  459. /* compute line register offset and line mask */
  460. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  461. linepos = (hexti->Line & EXTI_PIN_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 Specify which pending edge as to be clear.
  473. * This parameter can be one of the following values:
  474. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  475. * This parameter is kept for compatibility with other series.
  476. * @retval None.
  477. */
  478. void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  479. {
  480. __IO uint32_t *regaddr;
  481. uint32_t maskline;
  482. uint32_t offset;
  483. /* Check parameters */
  484. assert_param(IS_EXTI_LINE(hexti->Line));
  485. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  486. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  487. /* compute line register offset and line mask */
  488. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  489. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  490. /* Get pending register address */
  491. regaddr = (&EXTI->PR1 + (EXTI_CONFIG_OFFSET * offset));
  492. /* Clear Pending bit */
  493. *regaddr = maskline;
  494. }
  495. /**
  496. * @brief Generate a software interrupt for a dedicated line.
  497. * @param hexti Exti handle.
  498. * @retval None.
  499. */
  500. void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
  501. {
  502. __IO uint32_t *regaddr;
  503. uint32_t maskline;
  504. uint32_t offset;
  505. /* Check parameters */
  506. assert_param(IS_EXTI_LINE(hexti->Line));
  507. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  508. /* compute line register offset and line mask */
  509. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  510. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  511. regaddr = (&EXTI->SWIER1 + (EXTI_CONFIG_OFFSET * offset));
  512. *regaddr = maskline;
  513. }
  514. /**
  515. * @}
  516. */
  517. /**
  518. * @}
  519. */
  520. #endif /* HAL_EXTI_MODULE_ENABLED */
  521. /**
  522. * @}
  523. */
  524. /**
  525. * @}
  526. */
  527. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/