py32f002b_hal_exti.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_exti.c
  4. * @author MCU 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 two diffenrents
  26. interrupt 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 throught 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) 2023 Puya Semiconductor Co.
  63. * All rights reserved.</center></h2>
  64. *
  65. * This software component is licensed by Puya 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. * @attention
  72. *
  73. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  74. * All rights reserved.</center></h2>
  75. *
  76. * This software component is licensed by ST under BSD 3-Clause license,
  77. * the "License"; You may not use this file except in compliance with the
  78. * License. You may obtain a copy of the License at:
  79. * opensource.org/licenses/BSD-3-Clause
  80. *
  81. ******************************************************************************
  82. */
  83. /* Includes ------------------------------------------------------------------*/
  84. #include "py32f0xx_hal.h"
  85. /** @addtogroup PY32F002B_HAL_Driver
  86. * @{
  87. */
  88. /** @addtogroup EXTI
  89. * @{
  90. */
  91. /** MISRA C:2012 deviation rule has been granted for following rule:
  92. * Rule-18.1_b - Medium: Array `EXTICR' 1st subscript interval [0,7] may be out
  93. * of bounds [0,3] in following API :
  94. * HAL_EXTI_SetConfigLine
  95. * HAL_EXTI_GetConfigLine
  96. * HAL_EXTI_ClearConfigLine
  97. */
  98. #ifdef HAL_EXTI_MODULE_ENABLED
  99. /* Private typedef -----------------------------------------------------------*/
  100. /* Private defines ------------------------------------------------------------*/
  101. /** @addtogroup EXTI_Private_Constants EXTI Private Constants
  102. * @{
  103. */
  104. #define EXTI_MODE_OFFSET 0x04u /* 0x10: offset between CPU IMR/EMR registers */
  105. #define EXTI_CONFIG_OFFSET 0x08u /* 0x20: offset between CPU Rising/Falling configuration registers */
  106. /**
  107. * @}
  108. */
  109. /* Private macros ------------------------------------------------------------*/
  110. /* Private variables ---------------------------------------------------------*/
  111. /* Private function prototypes -----------------------------------------------*/
  112. /* Exported functions --------------------------------------------------------*/
  113. /** @addtogroup EXTI_Exported_Functions
  114. * @{
  115. */
  116. /** @addtogroup EXTI_Exported_Functions_Group1
  117. * @brief Configuration functions
  118. *
  119. @verbatim
  120. ===============================================================================
  121. ##### Configuration functions #####
  122. ===============================================================================
  123. @endverbatim
  124. * @{
  125. */
  126. /**
  127. * @brief Set configuration of a dedicated Exti line.
  128. * @param hexti Exti handle.
  129. * @param pExtiConfig Pointer on EXTI configuration to be set.
  130. * @retval HAL Status.
  131. */
  132. HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  133. {
  134. uint32_t regval;
  135. uint32_t linepos;
  136. uint32_t maskline;
  137. /* Check null pointer */
  138. if ((hexti == NULL) || (pExtiConfig == NULL))
  139. {
  140. return HAL_ERROR;
  141. }
  142. /* Check parameters */
  143. assert_param(IS_EXTI_LINE(pExtiConfig->Line));
  144. assert_param(IS_EXTI_MODE(pExtiConfig->Mode));
  145. /* Assign line number to handle */
  146. hexti->Line = pExtiConfig->Line;
  147. /* Compute line mask */
  148. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  149. maskline = (1uL << linepos);
  150. /* Configure triggers for configurable lines */
  151. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  152. {
  153. assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
  154. /* Configure rising trigger */
  155. /* Mask or set line */
  156. if ((pExtiConfig->Trigger & EXTI_TRIGGER_RISING) != 0x00u)
  157. {
  158. EXTI->RTSR |= maskline;
  159. }
  160. else
  161. {
  162. EXTI->RTSR &= ~maskline;
  163. }
  164. /* Configure falling trigger */
  165. /* Mask or set line */
  166. if ((pExtiConfig->Trigger & EXTI_TRIGGER_FALLING) != 0x00u)
  167. {
  168. EXTI->FTSR |= maskline;
  169. }
  170. else
  171. {
  172. EXTI->FTSR &= ~maskline;
  173. }
  174. /* Configure gpio port selection in case of gpio exti line */
  175. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  176. {
  177. assert_param(IS_EXTI_GPIO_PORT(pExtiConfig->GPIOSel));
  178. assert_param(IS_EXTI_GPIO_PIN(linepos));
  179. regval = EXTI->EXTICR[linepos >> 2u];
  180. regval &= ~(EXTI_EXTICR1_EXTI0 << (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  181. regval |= (pExtiConfig->GPIOSel << (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  182. EXTI->EXTICR[linepos >> 2u] = regval;
  183. }
  184. }
  185. /* Configure interrupt mode : read current mode */
  186. /* Mask or set line */
  187. if ((pExtiConfig->Mode & EXTI_MODE_INTERRUPT) != 0x00u)
  188. {
  189. EXTI->IMR |= maskline;
  190. }
  191. else
  192. {
  193. EXTI->IMR &= ~maskline;
  194. }
  195. /* Configure event mode : read current mode */
  196. /* Mask or set line */
  197. if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0x00u)
  198. {
  199. EXTI->EMR |= maskline;
  200. }
  201. else
  202. {
  203. EXTI->EMR &= ~maskline;
  204. }
  205. return HAL_OK;
  206. }
  207. /**
  208. * @brief Get configuration of a dedicated Exti line.
  209. * @param hexti Exti handle.
  210. * @param pExtiConfig Pointer on structure to store Exti configuration.
  211. * @retval HAL Status.
  212. */
  213. HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
  214. {
  215. uint32_t regval;
  216. uint32_t linepos;
  217. uint32_t maskline;
  218. /* Check null pointer */
  219. if ((hexti == NULL) || (pExtiConfig == NULL))
  220. {
  221. return HAL_ERROR;
  222. }
  223. /* Check the parameter */
  224. assert_param(IS_EXTI_LINE(hexti->Line));
  225. /* Store handle line number to configuration structure */
  226. pExtiConfig->Line = hexti->Line;
  227. /* Compute line mask */
  228. linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
  229. maskline = (1uL << linepos);
  230. /* 1] Get core mode : interrupt */
  231. /* Check if selected line is enable */
  232. if ((EXTI->IMR & maskline) != 0x00u)
  233. {
  234. pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
  235. }
  236. else
  237. {
  238. pExtiConfig->Mode = EXTI_MODE_NONE;
  239. }
  240. /* Get event mode */
  241. /* Check if selected line is enable */
  242. if ((EXTI->EMR & maskline) != 0x00u)
  243. {
  244. pExtiConfig->Mode |= EXTI_MODE_EVENT;
  245. }
  246. /* 2] Get trigger for configurable lines : rising */
  247. if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
  248. {
  249. /* Check if configuration of selected line is enable */
  250. if ((EXTI->RTSR & maskline) != 0x00u)
  251. {
  252. pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
  253. }
  254. else
  255. {
  256. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  257. }
  258. /* Get falling configuration */
  259. /* Check if configuration of selected line is enable */
  260. if ((EXTI->FTSR & maskline) != 0x00u)
  261. {
  262. pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
  263. }
  264. /* Get Gpio port selection for gpio lines */
  265. if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
  266. {
  267. assert_param(IS_EXTI_GPIO_PIN(linepos));
  268. regval = EXTI->EXTICR[linepos >> 2u];
  269. pExtiConfig->GPIOSel = ((regval << (EXTI_EXTICR1_EXTI1_Pos * (3uL - (linepos & 0x03u)))) >> 24);
  270. }
  271. else
  272. {
  273. pExtiConfig->GPIOSel = 0x00u;
  274. }
  275. }
  276. else
  277. {
  278. /* No Trigger selected */
  279. pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
  280. pExtiConfig->GPIOSel = 0x00u;
  281. }
  282. return HAL_OK;
  283. }
  284. /**
  285. * @brief Clear whole configuration of a dedicated Exti line.
  286. * @param hexti Exti handle.
  287. * @retval HAL Status.
  288. */
  289. HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
  290. {
  291. uint32_t regval;
  292. uint32_t linepos;
  293. uint32_t maskline;
  294. /* Check null pointer */
  295. if (hexti == NULL)
  296. {
  297. return HAL_ERROR;
  298. }
  299. /* Check the parameter */
  300. assert_param(IS_EXTI_LINE(hexti->Line));
  301. /* compute line mask */
  302. linepos = (hexti->Line & EXTI_PIN_MASK);
  303. maskline = (1uL << linepos);
  304. /* 1] Clear interrupt mode */
  305. EXTI->IMR = (EXTI->IMR & ~maskline);
  306. /* 2] Clear event mode */
  307. EXTI->EMR = (EXTI->EMR & ~maskline);
  308. /* 3] Clear triggers in case of configurable lines */
  309. if ((hexti->Line & EXTI_CONFIG) != 0x00u)
  310. {
  311. EXTI->RTSR = (EXTI->RTSR & ~maskline);
  312. EXTI->FTSR = (EXTI->FTSR & ~maskline);
  313. /* Get Gpio port selection for gpio lines */
  314. if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
  315. {
  316. assert_param(IS_EXTI_GPIO_PIN(linepos));
  317. regval = EXTI->EXTICR[linepos >> 2u];
  318. regval &= ~(EXTI_EXTICR1_EXTI0 << (EXTI_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
  319. EXTI->EXTICR[linepos >> 2u] = regval;
  320. }
  321. }
  322. return HAL_OK;
  323. }
  324. /**
  325. * @brief Register callback for a dedicated Exti line.
  326. * @param hexti Exti handle.
  327. * @param CallbackID User callback identifier.
  328. * This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
  329. * @param pPendingCbfn function pointer to be stored as callback.
  330. * @retval HAL Status.
  331. */
  332. HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
  333. {
  334. HAL_StatusTypeDef status = HAL_OK;
  335. switch (CallbackID)
  336. {
  337. case HAL_EXTI_COMMON_CB_ID:
  338. hexti->PendingCallback = pPendingCbfn;
  339. break;
  340. default:
  341. status = HAL_ERROR;
  342. break;
  343. }
  344. return status;
  345. }
  346. /**
  347. * @brief Store line number as handle private field.
  348. * @param hexti Exti handle.
  349. * @param ExtiLine Exti line number.
  350. * This parameter can be from 0 to @ref EXTI_LINE_NB.
  351. * @retval HAL Status.
  352. */
  353. HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_EXTI_LINE(ExtiLine));
  357. /* Check null pointer */
  358. if (hexti == NULL)
  359. {
  360. return HAL_ERROR;
  361. }
  362. else
  363. {
  364. /* Store line number as handle private field */
  365. hexti->Line = ExtiLine;
  366. return HAL_OK;
  367. }
  368. }
  369. /**
  370. * @}
  371. */
  372. /** @addtogroup EXTI_Exported_Functions_Group2
  373. * @brief EXTI IO functions.
  374. *
  375. @verbatim
  376. ===============================================================================
  377. ##### IO operation functions #####
  378. ===============================================================================
  379. @endverbatim
  380. * @{
  381. */
  382. /**
  383. * @brief Handle EXTI interrupt request.
  384. * @param hexti Exti handle.
  385. * @retval none.
  386. */
  387. void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
  388. {
  389. uint32_t regval;
  390. uint32_t maskline;
  391. /* Compute line mask */
  392. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  393. /* Get pending bit */
  394. regval = (EXTI->PR & maskline);
  395. if (regval != 0x00u)
  396. {
  397. /* Clear pending bit */
  398. EXTI->PR = maskline;
  399. /* Call callback */
  400. if (hexti->PendingCallback != NULL)
  401. {
  402. hexti->PendingCallback();
  403. }
  404. }
  405. }
  406. /**
  407. * @brief Get interrupt pending bit of a dedicated line.
  408. * @param hexti Exti handle.
  409. * @param Edge Specify which pending edge as to be checked.
  410. * This parameter can be one of the following values:
  411. * @arg @ref EXTI_TRIGGER_RISING_FALLING
  412. * This parameter is kept for compatibility with other series.
  413. * @retval 1 if interrupt is pending else 0.
  414. */
  415. uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
  416. {
  417. uint32_t regval;
  418. uint32_t linepos;
  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. /* Compute line mask */
  425. linepos = (hexti->Line & EXTI_PIN_MASK);
  426. maskline = (1uL << linepos);
  427. /* return 1 if bit is set else 0 */
  428. regval = ((EXTI->PR & maskline) >> linepos);
  429. return regval;
  430. }
  431. /**
  432. * @brief Clear interrupt pending bit of a dedicated line.
  433. * @param hexti Exti handle.
  434. * @retval None.
  435. */
  436. void HAL_EXTI_ClearPending(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. /* Clear Pending bit */
  445. EXTI->PR = maskline;
  446. }
  447. /**
  448. * @brief Generate a software interrupt for a dedicated line.
  449. * @param hexti Exti handle.
  450. * @retval None.
  451. */
  452. void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
  453. {
  454. uint32_t maskline;
  455. /* Check parameters */
  456. assert_param(IS_EXTI_LINE(hexti->Line));
  457. assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
  458. /* Compute line mask */
  459. maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
  460. /* Generate Software interrupt */
  461. EXTI->SWIER = maskline;
  462. }
  463. /**
  464. * @}
  465. */
  466. /**
  467. * @}
  468. */
  469. #endif /* HAL_EXTI_MODULE_ENABLED */
  470. /**
  471. * @}
  472. */
  473. /**
  474. * @}
  475. */
  476. /************************ (C) COPYRIGHT Puya *****END OF FILE****/