stm32h5xx_hal_exti.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_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. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2023 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 diffenrents
  37. interrupt 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_GetConfigLine().
  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_GetPending().
  68. (#) Generate software interrupt using HAL_EXTI_GenerateSWI().
  69. @endverbatim
  70. */
  71. /* Includes ------------------------------------------------------------------*/
  72. #include "stm32h5xx_hal.h"
  73. /** @addtogroup STM32H5xx_HAL_Driver
  74. * @{
  75. */
  76. /** @addtogroup EXTI
  77. * @{
  78. */
  79. #ifdef HAL_EXTI_MODULE_ENABLED
  80. /* Private typedef -----------------------------------------------------------*/
  81. /* Private defines ------------------------------------------------------------*/
  82. /** @defgroup EXTI_Private_Constants EXTI Private Constants
  83. * @{
  84. */
  85. #define EXTI_MODE_OFFSET 0x04U /* 0x10: byte offset between: IMR1/EMR1 and IMR2/EMR2 registers */
  86. #define EXTI_CONFIG_OFFSET 0x08U /* 0x20: byte offset between Rising1/Falling1 and Rising2/Falling2
  87. configuration registers */
  88. #define EXTI_PRIVCFGR_OFFSET 0x08U /* 0x20: byte offset between PRIVCFGR1 and PRIVCFGR2 registers */
  89. #define EXTI_SECCFGR_OFFSET 0x08U /* 0x20: byte offset between SECCFGR1 and SECCFGR2 registers */
  90. /**
  91. * @}
  92. */
  93. /* Private macros ------------------------------------------------------------*/
  94. /* Private variables ---------------------------------------------------------*/
  95. /* Private function prototypes -----------------------------------------------*/
  96. /* Exported functions --------------------------------------------------------*/
  97. /** @addtogroup EXTI_Exported_Functions
  98. * @{
  99. */
  100. /** @addtogroup EXTI_Exported_Functions_Group1
  101. * @brief Configuration functions
  102. *
  103. @verbatim
  104. ===============================================================================
  105. ##### Configuration functions #####
  106. ===============================================================================
  107. @endverbatim
  108. * @{
  109. */
  110. /**
  111. * @brief Set configuration of a dedicated Exti line.
  112. * @param hexti Exti handle.
  113. * @param pExtiConfig Pointer on EXTI configuration to be set.
  114. * @retval HAL Status.
  115. */
  116. HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  117. {
  118. __IO uint32_t *regaddr;
  119. uint32_t regval;
  120. uint32_t linepos;
  121. uint32_t maskline;
  122. uint32_t offset;
  123. /* Check null pointer */
  124. if ((hexti == NULL) || (pExtiConfig == NULL))
  125. {
  126. return HAL_ERROR;
  127. }
  128. /* Check the 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 register offset and line mask */
  134. offset = ((pExtiConfig->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  135. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  136. maskline = (1UL << linepos);
  137. /* Configure triggers for configurable lines */
  138. if ((pExtiConfig->Line & EXTI_CONFIG) != 0U)
  139. {
  140. assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
  141. /* Configure rising trigger */
  142. regaddr = (__IO uint32_t *)(&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  143. regval = *regaddr;
  144. /* Mask or set line */
  145. if ((pExtiConfig->Trigger & EXTI_TRIGGER_RISING) != 0U)
  146. {
  147. regval |= maskline;
  148. }
  149. else
  150. {
  151. regval &= ~maskline;
  152. }
  153. /* Store rising trigger mode */
  154. *regaddr = regval;
  155. /* Configure falling trigger */
  156. regaddr = (__IO uint32_t *)(&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  157. regval = *regaddr;
  158. /* Mask or set line */
  159. if ((pExtiConfig->Trigger & EXTI_TRIGGER_FALLING) != 0U)
  160. {
  161. regval |= maskline;
  162. }
  163. else
  164. {
  165. regval &= ~maskline;
  166. }
  167. /* Store falling trigger mode */
  168. *regaddr = regval;
  169. /* Configure gpio port selection in case of gpio exti line */
  170. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  171. {
  172. assert_param(IS_EXTI_GPIO_PORT(pExtiConfig->GPIOSel));
  173. assert_param(IS_EXTI_GPIO_PIN(linepos));
  174. regval = EXTI->EXTICR[(linepos >> 2U) & 0x03UL];
  175. regval &= ~(EXTI_EXTICR1_EXTI0 << (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03U)));
  176. regval |= (pExtiConfig->GPIOSel << (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03U)));
  177. EXTI->EXTICR[(linepos >> 2U) & 0x03UL] = regval;
  178. }
  179. }
  180. /* Configure interrupt mode : read current mode */
  181. regaddr = (__IO uint32_t *)(&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  182. regval = *regaddr;
  183. /* Mask or set line */
  184. if ((pExtiConfig->Mode & EXTI_MODE_INTERRUPT) != 0U)
  185. {
  186. regval |= maskline;
  187. }
  188. else
  189. {
  190. regval &= ~maskline;
  191. }
  192. /* Store interrupt mode */
  193. *regaddr = regval;
  194. /* Configure event mode : read current mode */
  195. regaddr = (__IO uint32_t *)(&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  196. regval = *regaddr;
  197. /* Mask or set line */
  198. if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0U)
  199. {
  200. regval |= maskline;
  201. }
  202. else
  203. {
  204. regval &= ~maskline;
  205. }
  206. /* Store event mode */
  207. *regaddr = regval;
  208. return HAL_OK;
  209. }
  210. /**
  211. * @brief Get configuration of a dedicated Exti line.
  212. * @param hexti Exti handle.
  213. * @param pExtiConfig Pointer on structure to store Exti configuration.
  214. * @retval HAL Status.
  215. */
  216. HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  217. {
  218. const __IO uint32_t *regaddr;
  219. uint32_t regval;
  220. uint32_t linepos;
  221. uint32_t maskline;
  222. uint32_t offset;
  223. /* Check null pointer */
  224. if ((hexti == NULL) || (pExtiConfig == NULL))
  225. {
  226. return HAL_ERROR;
  227. }
  228. /* Check the parameter */
  229. assert_param(IS_EXTI_LINE(hexti->Line));
  230. /* Store handle line number to configiguration structure */
  231. pExtiConfig->Line = hexti->Line;
  232. /* compute line register offset and line mask */
  233. offset = ((pExtiConfig->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  234. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  235. maskline = (1UL << linepos);
  236. /* 1] Get core mode : interrupt */
  237. regaddr = (__IO uint32_t *)(&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  238. regval = *regaddr;
  239. /* Check if selected line is enable */
  240. if ((regval & maskline) != 0U)
  241. {
  242. pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
  243. }
  244. else
  245. {
  246. pExtiConfig->Mode = EXTI_MODE_NONE;
  247. }
  248. /* Get event mode */
  249. regaddr = (__IO uint32_t *)(&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  250. regval = *regaddr;
  251. /* Check if selected line is enable */
  252. if ((regval & maskline) != 0U)
  253. {
  254. pExtiConfig->Mode |= EXTI_MODE_EVENT;
  255. }
  256. /* 2] Get trigger for configurable lines : rising */
  257. if ((pExtiConfig->Line & EXTI_CONFIG) != 0U)
  258. {
  259. regaddr = (__IO uint32_t *)(&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  260. regval = *regaddr;
  261. /* Get default Trigger and GPIOSel configuration */
  262. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  263. pExtiConfig->GPIOSel = 0x00u;
  264. /* Check if configuration of selected line is enable */
  265. if ((regval & maskline) != 0U)
  266. {
  267. pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
  268. }
  269. /* Get falling configuration */
  270. regaddr = (__IO uint32_t *)(&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  271. regval = *regaddr;
  272. /* Check if configuration of selected line is enable */
  273. if ((regval & maskline) != 0U)
  274. {
  275. pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
  276. }
  277. /* Get Gpio port selection for gpio lines */
  278. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  279. {
  280. assert_param(IS_EXTI_GPIO_PIN(linepos));
  281. regval = EXTI->EXTICR[(linepos >> 2U) & 0x03UL];
  282. pExtiConfig->GPIOSel = (regval >> (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03u))) & EXTI_EXTICR1_EXTI0;
  283. }
  284. }
  285. return HAL_OK;
  286. }
  287. /**
  288. * @brief Clear whole configuration of a dedicated Exti line.
  289. * @param hexti Exti handle.
  290. * @retval HAL Status.
  291. */
  292. HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(const EXTI_HandleTypeDef *hexti)
  293. {
  294. __IO uint32_t *regaddr;
  295. uint32_t regval;
  296. uint32_t linepos;
  297. uint32_t maskline;
  298. uint32_t offset;
  299. /* Check null pointer */
  300. if (hexti == NULL)
  301. {
  302. return HAL_ERROR;
  303. }
  304. /* Check the parameter */
  305. assert_param(IS_EXTI_LINE(hexti->Line));
  306. /* compute line register offset and line mask */
  307. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  308. linepos = (hexti->Line & EXTI_PIN_MASK);
  309. maskline = (1UL << linepos);
  310. /* 1] Clear interrupt mode */
  311. regaddr = (__IO uint32_t *)(&EXTI->IMR1 + (EXTI_MODE_OFFSET * offset));
  312. regval = (*regaddr & ~maskline);
  313. *regaddr = regval;
  314. /* 2] Clear event mode */
  315. regaddr = (__IO uint32_t *)(&EXTI->EMR1 + (EXTI_MODE_OFFSET * offset));
  316. regval = (*regaddr & ~maskline);
  317. *regaddr = regval;
  318. /* 3] Clear triggers in case of configurable lines */
  319. if ((hexti->Line & EXTI_CONFIG) != 0U)
  320. {
  321. regaddr = (__IO uint32_t *)(&EXTI->RTSR1 + (EXTI_CONFIG_OFFSET * offset));
  322. regval = (*regaddr & ~maskline);
  323. *regaddr = regval;
  324. regaddr = (__IO uint32_t *)(&EXTI->FTSR1 + (EXTI_CONFIG_OFFSET * offset));
  325. regval = (*regaddr & ~maskline);
  326. *regaddr = regval;
  327. /* Get Gpio port selection for gpio lines */
  328. if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
  329. {
  330. assert_param(IS_EXTI_GPIO_PIN(linepos));
  331. regval = EXTI->EXTICR[(linepos >> 2U) & 0x03UL];
  332. regval &= ~(EXTI_EXTICR1_EXTI0 << (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03U)));
  333. EXTI->EXTICR[(linepos >> 2U) & 0x03UL] = regval;
  334. }
  335. }
  336. return HAL_OK;
  337. }
  338. /**
  339. * @brief Register callback for a dedicaated Exti line.
  340. * @param hexti Exti handle.
  341. * @param CallbackID User callback identifier.
  342. * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
  343. * @param pPendingCbfn function pointer to be stored as callback.
  344. * @retval HAL Status.
  345. */
  346. HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID,
  347. void (*pPendingCbfn)(void))
  348. {
  349. HAL_StatusTypeDef status = HAL_OK;
  350. switch (CallbackID)
  351. {
  352. case HAL_EXTI_COMMON_CB_ID:
  353. hexti->RisingCallback = pPendingCbfn;
  354. hexti->FallingCallback = pPendingCbfn;
  355. break;
  356. case HAL_EXTI_RISING_CB_ID:
  357. hexti->RisingCallback = pPendingCbfn;
  358. break;
  359. case HAL_EXTI_FALLING_CB_ID:
  360. hexti->FallingCallback = pPendingCbfn;
  361. break;
  362. default:
  363. status = HAL_ERROR;
  364. break;
  365. }
  366. return status;
  367. }
  368. /**
  369. * @brief Store line number as handle private field.
  370. * @param hexti Exti handle.
  371. * @param ExtiLine Exti line number.
  372. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  373. * @retval HAL Status.
  374. */
  375. HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_EXTI_LINE(ExtiLine));
  379. /* Check null pointer */
  380. if (hexti == NULL)
  381. {
  382. return HAL_ERROR;
  383. }
  384. else
  385. {
  386. /* Store line number as handle private field */
  387. hexti->Line = ExtiLine;
  388. return HAL_OK;
  389. }
  390. }
  391. /**
  392. * @}
  393. */
  394. /** @addtogroup EXTI_Exported_Functions_Group2
  395. * @brief EXTI IO functions.
  396. *
  397. @verbatim
  398. ===============================================================================
  399. ##### IO operation functions #####
  400. ===============================================================================
  401. @endverbatim
  402. * @{
  403. */
  404. /**
  405. * @brief Handle EXTI interrupt request.
  406. * @param hexti Exti handle.
  407. * @retval none.
  408. */
  409. void HAL_EXTI_IRQHandler(const EXTI_HandleTypeDef *hexti)
  410. {
  411. __IO uint32_t *regaddr;
  412. uint32_t regval;
  413. uint32_t maskline;
  414. uint32_t offset;
  415. /* Compute line register offset and line mask */
  416. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  417. maskline = (1UL << (hexti->Line & EXTI_PIN_MASK));
  418. /* Get rising edge pending bit */
  419. regaddr = (__IO uint32_t *)(&EXTI->RPR1 + (EXTI_CONFIG_OFFSET * offset));
  420. regval = (*regaddr & maskline);
  421. if (regval != 0U)
  422. {
  423. /* Clear pending bit */
  424. *regaddr = maskline;
  425. /* Call rising callback */
  426. if (hexti->RisingCallback != NULL)
  427. {
  428. hexti->RisingCallback();
  429. }
  430. }
  431. /* Get falling edge pending bit */
  432. regaddr = (__IO uint32_t *)(&EXTI->FPR1 + (EXTI_CONFIG_OFFSET * offset));
  433. regval = (*regaddr & maskline);
  434. if (regval != 0U)
  435. {
  436. /* Clear pending bit */
  437. *regaddr = maskline;
  438. /* Call rising callback */
  439. if (hexti->FallingCallback != NULL)
  440. {
  441. hexti->FallingCallback();
  442. }
  443. }
  444. }
  445. /**
  446. * @brief Get interrupt pending bit of a dedicated line.
  447. * @param hexti Exti handle.
  448. * @param Edge Specify which pending edge as to be checked.
  449. * This parameter can be one of the following values:
  450. * @arg @ref EXTI_TRIGGER_RISING
  451. * @arg @ref EXTI_TRIGGER_FALLING
  452. * @retval 1 if interrupt is pending else 0.
  453. */
  454. uint32_t HAL_EXTI_GetPending(const EXTI_HandleTypeDef *hexti, uint32_t Edge)
  455. {
  456. const __IO uint32_t *regaddr;
  457. uint32_t regval;
  458. uint32_t linepos;
  459. uint32_t maskline;
  460. uint32_t offset;
  461. /* Check the parameters */
  462. assert_param(IS_EXTI_LINE(hexti->Line));
  463. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  464. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  465. /* compute line register offset and line mask */
  466. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  467. linepos = (hexti->Line & EXTI_PIN_MASK);
  468. maskline = (1UL << linepos);
  469. if (Edge != EXTI_TRIGGER_RISING)
  470. {
  471. /* Get falling edge pending bit */
  472. regaddr = (__IO uint32_t *)(&EXTI->FPR1 + (EXTI_CONFIG_OFFSET * offset));
  473. }
  474. else
  475. {
  476. /* Get rising edge pending bit */
  477. regaddr = (__IO uint32_t *)(&EXTI->RPR1 + (EXTI_CONFIG_OFFSET * offset));
  478. }
  479. /* return 1 if bit is set else 0 */
  480. regval = ((*regaddr & maskline) >> linepos);
  481. return regval;
  482. }
  483. /**
  484. * @brief Clear interrupt pending bit of a dedicated line.
  485. * @param hexti Exti handle.
  486. * @param Edge Specify which pending edge as to be clear.
  487. * This parameter can be one of the following values:
  488. * @arg @ref EXTI_TRIGGER_RISING
  489. * @arg @ref EXTI_TRIGGER_FALLING
  490. * @retval None.
  491. */
  492. void HAL_EXTI_ClearPending(const EXTI_HandleTypeDef *hexti, uint32_t Edge)
  493. {
  494. __IO uint32_t *regaddr;
  495. uint32_t maskline;
  496. uint32_t offset;
  497. /* Check the parameters */
  498. assert_param(IS_EXTI_LINE(hexti->Line));
  499. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  500. assert_param(IS_EXTI_PENDING_EDGE(Edge));
  501. /* compute line register offset and line mask */
  502. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  503. maskline = (1UL << (hexti->Line & EXTI_PIN_MASK));
  504. if (Edge != EXTI_TRIGGER_RISING)
  505. {
  506. /* Get falling edge pending register address */
  507. regaddr = (__IO uint32_t *)(&EXTI->FPR1 + (EXTI_CONFIG_OFFSET * offset));
  508. }
  509. else
  510. {
  511. /* Get falling edge pending register address */
  512. regaddr = (__IO uint32_t *)(&EXTI->RPR1 + (EXTI_CONFIG_OFFSET * offset));
  513. }
  514. /* Clear Pending bit */
  515. *regaddr = maskline;
  516. }
  517. /**
  518. * @brief Generate a software interrupt for a dedicated line.
  519. * @param hexti Exti handle.
  520. * @retval None.
  521. */
  522. void HAL_EXTI_GenerateSWI(const EXTI_HandleTypeDef *hexti)
  523. {
  524. __IO uint32_t *regaddr;
  525. uint32_t maskline;
  526. uint32_t offset;
  527. /* Check the parameters */
  528. assert_param(IS_EXTI_LINE(hexti->Line));
  529. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  530. /* compute line register offset and line mask */
  531. offset = ((hexti->Line & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  532. maskline = (1UL << (hexti->Line & EXTI_PIN_MASK));
  533. regaddr = (__IO uint32_t *)(&EXTI->SWIER1 + (EXTI_CONFIG_OFFSET * offset));
  534. *regaddr = maskline;
  535. }
  536. /**
  537. * @}
  538. */
  539. /** @defgroup EXTI_Exported_Functions_Group3 EXTI line attributes management functions
  540. * @brief EXTI attributes management functions.
  541. *
  542. @verbatim
  543. ===============================================================================
  544. ##### EXTI attributes functions #####
  545. ===============================================================================
  546. @endverbatim
  547. * @{
  548. */
  549. /**
  550. * @brief Configure the EXTI line attribute(s).
  551. * @note Available attributes are to secure EXTI line and set EXT line as privileged.
  552. * Default state is not secure and unprivileged access allowed.
  553. * @note Secure and non-secure attributes can only be set from the secure
  554. * state when the system implements the security (TZEN=1).
  555. * @note Security and privilege attributes can be set independently.
  556. * @param ExtiLine Exti line number.
  557. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  558. * @param LineAttributes can be one or a combination of the following values:
  559. * @arg @ref EXTI_LINE_PRIV Privileged-only access
  560. * @arg @ref EXTI_LINE_NPRIV Privileged/Non-privileged access
  561. * @arg @ref EXTI_LINE_SEC Secure-only access
  562. * @arg @ref EXTI_LINE_NSEC Secure/Non-secure access
  563. * @retval None
  564. */
  565. void HAL_EXTI_ConfigLineAttributes(uint32_t ExtiLine, uint32_t LineAttributes)
  566. {
  567. __IO uint32_t *regaddr;
  568. uint32_t regval;
  569. uint32_t linepos;
  570. uint32_t maskline;
  571. uint32_t offset;
  572. /* Check the parameters */
  573. assert_param(IS_EXTI_LINE(ExtiLine));
  574. assert_param(IS_EXTI_LINE_ATTRIBUTES(LineAttributes));
  575. /* compute line register offset and line mask */
  576. offset = ((ExtiLine & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  577. linepos = (ExtiLine & EXTI_PIN_MASK);
  578. maskline = (1UL << linepos);
  579. /* Configure privilege or non-privilege attributes */
  580. regaddr = (__IO uint32_t *)(&EXTI->PRIVCFGR1 + (EXTI_PRIVCFGR_OFFSET * offset));
  581. regval = *regaddr;
  582. /* Mask or set line */
  583. if ((LineAttributes & EXTI_LINE_PRIV) == EXTI_LINE_PRIV)
  584. {
  585. regval |= maskline;
  586. }
  587. else if ((LineAttributes & EXTI_LINE_NPRIV) == EXTI_LINE_NPRIV)
  588. {
  589. regval &= ~maskline;
  590. }
  591. else
  592. {
  593. /* do nothing */
  594. }
  595. /* Store privilege or non-privilege attribute */
  596. *regaddr = regval;
  597. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  598. /* Configure secure or non-secure attributes */
  599. regaddr = (__IO uint32_t *)(&EXTI->SECCFGR1 + (EXTI_SECCFGR_OFFSET * offset));
  600. regval = *regaddr;
  601. /* Mask or set line */
  602. if ((LineAttributes & EXTI_LINE_SEC) == EXTI_LINE_SEC)
  603. {
  604. regval |= maskline;
  605. }
  606. else if ((LineAttributes & EXTI_LINE_NSEC) == EXTI_LINE_NSEC)
  607. {
  608. regval &= ~maskline;
  609. }
  610. else
  611. {
  612. /* do nothing */
  613. }
  614. /* Store secure or non-secure attribute */
  615. *regaddr = regval;
  616. #endif /* __ARM_FEATURE_CMSE */
  617. }
  618. /**
  619. * @brief Get the EXTI line attribute(s).
  620. * @note Secure and non-secure attributes are only available from secure state
  621. * when the system implements the security (TZEN=1)
  622. * @param ExtiLine Exti line number.
  623. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  624. * @param pLineAttributes: pointer to return line attributes.
  625. * @retval HAL Status.
  626. */
  627. HAL_StatusTypeDef HAL_EXTI_GetConfigLineAttributes(uint32_t ExtiLine, uint32_t *pLineAttributes)
  628. {
  629. const __IO uint32_t *regaddr;
  630. uint32_t linepos;
  631. uint32_t maskline;
  632. uint32_t offset;
  633. uint32_t attributes;
  634. /* Check null pointer */
  635. if (pLineAttributes == NULL)
  636. {
  637. return HAL_ERROR;
  638. }
  639. /* Check the parameters */
  640. assert_param(IS_EXTI_LINE(ExtiLine));
  641. /* Compute line register offset and line mask */
  642. offset = ((ExtiLine & EXTI_REG_MASK) >> EXTI_REG_SHIFT);
  643. linepos = (ExtiLine & EXTI_PIN_MASK);
  644. maskline = (1UL << linepos);
  645. /* Get privilege or non-privilege attribute */
  646. regaddr = (__IO uint32_t *)(&EXTI->PRIVCFGR1 + (EXTI_PRIVCFGR_OFFSET * offset));
  647. if ((*regaddr & maskline) != 0U)
  648. {
  649. attributes = EXTI_LINE_PRIV;
  650. }
  651. else
  652. {
  653. attributes = EXTI_LINE_NPRIV;
  654. }
  655. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  656. /* Get secure or non-secure attribute */
  657. regaddr = (__IO uint32_t *)(&EXTI->SECCFGR1 + (EXTI_SECCFGR_OFFSET * offset));
  658. if ((*regaddr & maskline) != 0U)
  659. {
  660. attributes |= EXTI_LINE_SEC;
  661. }
  662. else
  663. {
  664. attributes |= EXTI_LINE_NSEC;
  665. }
  666. #endif /* __ARM_FEATURE_CMSE */
  667. /* return value */
  668. *pLineAttributes = attributes;
  669. return HAL_OK;
  670. }
  671. #if defined (EXTI_LOCKR_LOCK)
  672. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  673. /**
  674. * @brief Lock the global EXTI security and privilege configuration.
  675. * @retval HAL Status.
  676. */
  677. HAL_StatusTypeDef HAL_EXTI_LockConfigAttributes(void)
  678. {
  679. EXTI->LOCKR = EXTI_ATTRIBUTES_LOCKED;
  680. return HAL_OK;
  681. }
  682. /**
  683. * @brief Get the global EXTI security and privilege lock configuration.
  684. * @param pLockState : Pointer to returned security and privilege configuration
  685. * @retval HAL Status.
  686. */
  687. HAL_StatusTypeDef HAL_EXTI_GetLockConfigAttributes(uint32_t *const pLockState)
  688. {
  689. uint32_t attributes;
  690. const __IO uint32_t *regaddr;
  691. /* Check null pointer */
  692. if (pLockState == NULL)
  693. {
  694. return HAL_ERROR;
  695. }
  696. /* Get security and privilege configuration */
  697. regaddr = (__IO uint32_t *)(&EXTI->LOCKR);
  698. if ((*regaddr & EXTI_LOCKR_LOCK) != 0U)
  699. {
  700. attributes = EXTI_ATTRIBUTES_LOCKED;
  701. }
  702. else
  703. {
  704. attributes = EXTI_ATTRIBUTES_UNLOCKED;
  705. }
  706. /* return value */
  707. *pLockState = attributes;
  708. return HAL_OK;
  709. }
  710. #endif /* defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
  711. #endif /* defined (EXTI_LOCKR_LOCK) */
  712. /**
  713. * @}
  714. */
  715. /**
  716. * @}
  717. */
  718. #endif /* HAL_EXTI_MODULE_ENABLED */
  719. /**
  720. * @}
  721. */
  722. /**
  723. * @}
  724. */