py32f002b_hal_gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_gpio.c
  4. * @author MCU Application Team
  5. * @brief GPIO HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the General Purpose Input/Output (GPIO) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### GPIO Peripheral features #####
  14. ==============================================================================
  15. [..]
  16. (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
  17. configured by software in several modes:
  18. (++) Input mode
  19. (++) Analog mode
  20. (++) Output mode
  21. (++) Alternate function mode
  22. (++) External interrupt/event lines
  23. (+) During and just after reset, the alternate functions and external interrupt
  24. lines are not active and the I/O ports are configured in input floating mode.
  25. (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
  26. activated or not.
  27. (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
  28. type and the IO speed can be selected depending on the VDD value.
  29. (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
  30. multiplexer that allows only one peripheral alternate function (AF) connected
  31. to an IO pin at a time. In this way, there can be no conflict between peripherals
  32. sharing the same IO pin.
  33. (+) All ports have external interrupt/event capability. To use external interrupt
  34. lines, the port must be configured in input mode. All available GPIO pins are
  35. connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
  36. (+) The external interrupt/event controller consists of up to 28 edge detectors
  37. (16 lines are connected to GPIO) for generating event/interrupt requests (each
  38. input line can be independently configured to select the type (interrupt or event)
  39. and the corresponding trigger event (rising or falling or both). Each line can
  40. also be masked independently.
  41. ##### How to use this driver #####
  42. ==============================================================================
  43. [..]
  44. (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
  45. (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
  46. (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
  47. (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
  48. structure.
  49. (++) In case of Output or alternate function mode selection: the speed is
  50. configured through "Speed" member from GPIO_InitTypeDef structure.
  51. (++) In alternate mode is selection, the alternate function connected to the IO
  52. is configured through "Alternate" member from GPIO_InitTypeDef structure.
  53. (++) Analog mode is required when a pin is to be used as ADC channel
  54. or DAC output.
  55. (++) In case of external interrupt/event selection the "Mode" member from
  56. GPIO_InitTypeDef structure select the type (interrupt or event) and
  57. the corresponding trigger event (rising or falling or both).
  58. (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
  59. mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
  60. HAL_NVIC_EnableIRQ().
  61. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  62. (#) To set/reset the level of a pin configured in output mode use
  63. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  64. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  65. (#) During and just after reset, the alternate functions are not
  66. active and the GPIO pins are configured in input floating mode (except JTAG
  67. pins).
  68. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  69. when the LSE oscillator is off. The LSE has priority over the GPIO function.
  70. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  71. general purpose PF0 and PF1, respectively, when the HSE oscillator is off.
  72. The HSE has priority over the GPIO function.
  73. @endverbatim
  74. ******************************************************************************
  75. *
  76. ******************************************************************************
  77. * @attention
  78. *
  79. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  80. * All rights reserved.</center></h2>
  81. *
  82. * This software component is licensed by Puya under BSD 3-Clause license,
  83. * the "License"; You may not use this file except in compliance with the
  84. * License. You may obtain a copy of the License at:
  85. * opensource.org/licenses/BSD-3-Clause
  86. *
  87. ******************************************************************************
  88. * @attention
  89. *
  90. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  91. * All rights reserved.</center></h2>
  92. *
  93. * This software component is licensed by ST under BSD 3-Clause license,
  94. * the "License"; You may not use this file except in compliance with the
  95. * License. You may obtain a copy of the License at:
  96. * opensource.org/licenses/BSD-3-Clause
  97. *
  98. ******************************************************************************
  99. */
  100. /* Includes ------------------------------------------------------------------*/
  101. #include "py32f0xx_hal.h"
  102. /** @addtogroup PY32F002B_HAL_Driver
  103. * @{
  104. */
  105. /** @addtogroup GPIO
  106. * @{
  107. */
  108. /** MISRA C:2012 deviation rule has been granted for following rules:
  109. * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
  110. * range of the shift operator in following API :
  111. * HAL_GPIO_Init
  112. * HAL_GPIO_DeInit
  113. */
  114. #ifdef HAL_GPIO_MODULE_ENABLED
  115. /* Private typedef -----------------------------------------------------------*/
  116. /* Private defines ------------------------------------------------------------*/
  117. /** @defgroup GPIO_Private_Constants GPIO Private Constants
  118. * @{
  119. */
  120. #define GPIO_MODE (0x00000003u)
  121. #define EXTI_MODE (0x10000000u)
  122. #define GPIO_MODE_IT (0x00010000u)
  123. #define GPIO_MODE_EVT (0x00020000u)
  124. #define RISING_EDGE (0x00100000u)
  125. #define FALLING_EDGE (0x00200000u)
  126. #define GPIO_OUTPUT_TYPE (0x00000010u)
  127. #define GPIO_NUMBER (16u)
  128. /**
  129. * @}
  130. */
  131. #define __GPIO_LOCK_KEY_Msk (0x00010000U)
  132. /* Private macros ------------------------------------------------------------*/
  133. /* Private variables ---------------------------------------------------------*/
  134. /* Private function prototypes -----------------------------------------------*/
  135. /* Exported functions --------------------------------------------------------*/
  136. /** @addtogroup GPIO_Exported_Functions
  137. * @{
  138. */
  139. /** @addtogroup GPIO_Exported_Functions_Group1
  140. * @brief Initialization and Configuration functions
  141. *
  142. @verbatim
  143. ===============================================================================
  144. ##### Initialization and de-initialization functions #####
  145. ===============================================================================
  146. @endverbatim
  147. * @{
  148. */
  149. /**
  150. * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  151. * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
  152. * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
  153. * the configuration information for the specified GPIO peripheral.
  154. * @retval None
  155. */
  156. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  157. {
  158. uint32_t position = 0x00u;
  159. uint32_t iocurrent;
  160. uint32_t temp;
  161. /* Check the parameters */
  162. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  163. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  164. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  165. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  166. /* Configure the port pins */
  167. while (((GPIO_Init->Pin) >> position) != 0x00u)
  168. {
  169. /* Get current io position */
  170. iocurrent = (GPIO_Init->Pin) & (1uL << position);
  171. if (iocurrent != 0x00u)
  172. {
  173. /*--------------------- GPIO Mode Configuration ------------------------*/
  174. /* In case of Alternate function mode selection */
  175. if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  176. {
  177. /* Check the Alternate function parameters */
  178. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  179. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  180. /* Configure Alternate function mapped with the current IO */
  181. temp = GPIOx->AFR[position >> 3u];
  182. temp &= ~(0xFu << ((position & 0x07u) * 4u));
  183. temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
  184. GPIOx->AFR[position >> 3u] = temp;
  185. }
  186. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  187. temp = GPIOx->MODER;
  188. temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
  189. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
  190. GPIOx->MODER = temp;
  191. /* In case of Output or Alternate function mode selection */
  192. if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  193. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  194. {
  195. /* Check the Speed parameter */
  196. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  197. /* Configure the IO Speed */
  198. temp = GPIOx->OSPEEDR;
  199. temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
  200. temp |= (GPIO_Init->Speed << (position * 2u));
  201. GPIOx->OSPEEDR = temp;
  202. /* Configure the IO Output Type */
  203. temp = GPIOx->OTYPER;
  204. temp &= ~(GPIO_OTYPER_OT0 << position) ;
  205. temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4u) << position);
  206. GPIOx->OTYPER = temp;
  207. }
  208. /* Activate the Pull-up or Pull down resistor for the current IO */
  209. temp = GPIOx->PUPDR;
  210. temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
  211. temp |= ((GPIO_Init->Pull) << (position * 2u));
  212. GPIOx->PUPDR = temp;
  213. /*--------------------- EXTI Mode Configuration ------------------------*/
  214. /* Configure the External Interrupt or event for the current IO */
  215. if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  216. {
  217. temp = EXTI->EXTICR[position >> 2u];
  218. temp &= ~(0x0FuL << (8u * (position & 0x03u)));
  219. temp |= (GPIO_GET_INDEX(GPIOx) << (8u * (position & 0x03u)));
  220. EXTI->EXTICR[position >> 2u] = temp;
  221. /* Clear EXTI line configuration */
  222. temp = EXTI->IMR;
  223. temp &= ~(iocurrent);
  224. if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  225. {
  226. temp |= iocurrent;
  227. }
  228. EXTI->IMR = temp;
  229. temp = EXTI->EMR;
  230. temp &= ~(iocurrent);
  231. if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  232. {
  233. temp |= iocurrent;
  234. }
  235. EXTI->EMR = temp;
  236. /* Clear Rising Falling edge configuration */
  237. temp = EXTI->RTSR;
  238. temp &= ~(iocurrent);
  239. if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  240. {
  241. temp |= iocurrent;
  242. }
  243. EXTI->RTSR = temp;
  244. temp = EXTI->FTSR;
  245. temp &= ~(iocurrent);
  246. if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  247. {
  248. temp |= iocurrent;
  249. }
  250. EXTI->FTSR = temp;
  251. }
  252. }
  253. position++;
  254. }
  255. }
  256. /**
  257. * @brief De-initialize the GPIOx peripheral registers to their default reset values.
  258. * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
  259. * @param GPIO_Pin specifies the port bit to be written.
  260. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  261. * @retval None
  262. */
  263. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  264. {
  265. uint32_t position = 0x00u;
  266. uint32_t iocurrent;
  267. uint32_t tmp;
  268. /* Check the parameters */
  269. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  270. assert_param(IS_GPIO_PIN(GPIO_Pin));
  271. /* Configure the port pins */
  272. while ((GPIO_Pin >> position) != 0x00u)
  273. {
  274. /* Get current io position */
  275. iocurrent = (GPIO_Pin) & (1uL << position);
  276. if (iocurrent != 0x00u)
  277. {
  278. /*------------------------- EXTI Mode Configuration --------------------*/
  279. /* Clear the External Interrupt or Event for the current IO */
  280. tmp = EXTI->EXTICR[position >> 2u];
  281. tmp &= (0x0FuL << (8u * (position & 0x03u)));
  282. if (tmp == (GPIO_GET_INDEX(GPIOx) << (8u * (position & 0x03u))))
  283. {
  284. /* Clear EXTI line configuration */
  285. EXTI->IMR &= ~(iocurrent);
  286. EXTI->EMR &= ~(iocurrent);
  287. /* Clear Rising Falling edge configuration */
  288. EXTI->RTSR &= ~(iocurrent);
  289. EXTI->FTSR &= ~(iocurrent);
  290. tmp = 0x0FuL << (8u * (position & 0x03u));
  291. EXTI->EXTICR[position >> 2u] &= ~tmp;
  292. }
  293. /*------------------------- GPIO Mode Configuration --------------------*/
  294. /* Configure IO in Analog Mode */
  295. GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
  296. /* Configure the default Alternate Function in current IO */
  297. GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
  298. /* Configure the default value for IO Speed */
  299. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
  300. /* Configure the default value IO Output Type */
  301. GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
  302. /* Deactivate the Pull-up and Pull-down resistor for the current IO */
  303. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
  304. }
  305. position++;
  306. }
  307. }
  308. /**
  309. * @}
  310. */
  311. /** @addtogroup GPIO_Exported_Functions_Group2
  312. * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
  313. *
  314. @verbatim
  315. ===============================================================================
  316. ##### IO operation functions #####
  317. ===============================================================================
  318. @endverbatim
  319. * @{
  320. */
  321. /**
  322. * @brief Read the specified input port pin.
  323. * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
  324. * @param GPIO_Pin specifies the port bit to read.
  325. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  326. * @retval The input port pin value.
  327. */
  328. GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  329. {
  330. GPIO_PinState bitstatus;
  331. /* Check the parameters */
  332. assert_param(IS_GPIO_PIN(GPIO_Pin));
  333. if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
  334. {
  335. bitstatus = GPIO_PIN_SET;
  336. }
  337. else
  338. {
  339. bitstatus = GPIO_PIN_RESET;
  340. }
  341. return bitstatus;
  342. }
  343. /**
  344. * @brief Set or clear the selected data port bit.
  345. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  346. * accesses. In this way, there is no risk of an IRQ occurring between
  347. * the read and the modify access.
  348. * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
  349. * @param GPIO_Pin specifies the port bit to be written.
  350. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  351. * @param PinState specifies the value to be written to the selected bit.
  352. * This parameter can be one of the GPIO_PinState enum values:
  353. * @arg GPIO_PIN_RESET: to clear the port pin
  354. * @arg GPIO_PIN_SET: to set the port pin
  355. * @retval None
  356. */
  357. void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  358. {
  359. /* Check the parameters */
  360. assert_param(IS_GPIO_PIN(GPIO_Pin));
  361. assert_param(IS_GPIO_PIN_ACTION(PinState));
  362. if (PinState != GPIO_PIN_RESET)
  363. {
  364. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  365. }
  366. else
  367. {
  368. GPIOx->BRR = (uint32_t)GPIO_Pin;
  369. }
  370. }
  371. /**
  372. * @brief Toggle the specified GPIO pin.
  373. * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
  374. * @param GPIO_Pin specifies the pin to be toggled.
  375. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  376. * @retval None
  377. */
  378. void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  379. {
  380. /* Check the parameters */
  381. assert_param(IS_GPIO_PIN(GPIO_Pin));
  382. if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
  383. {
  384. GPIOx->BRR = (uint32_t)GPIO_Pin;
  385. }
  386. else
  387. {
  388. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  389. }
  390. }
  391. /**
  392. * @brief Lock GPIO Pins configuration registers.
  393. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  394. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  395. * @note The configuration of the locked GPIO pins can no longer be modified
  396. * until the next reset.
  397. * @param GPIOx where x can be (A..C) to select the GPIO peripheral for PY32F002B family
  398. * @param GPIO_Pin specifies the port bits to be locked.
  399. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  400. * @retval None
  401. */
  402. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  403. {
  404. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  405. /* Check the parameters */
  406. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  407. assert_param(IS_GPIO_PIN(GPIO_Pin));
  408. /* Apply lock key write sequence */
  409. tmp |= GPIO_Pin;
  410. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  411. GPIOx->LCKR = tmp;
  412. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  413. GPIOx->LCKR = GPIO_Pin;
  414. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  415. GPIOx->LCKR = tmp;
  416. /* Read LCKK register. This read is mandatory to complete key lock sequence */
  417. tmp = GPIOx->LCKR;
  418. /* read again in order to confirm lock is active */
  419. if ((GPIOx->LCKR & __GPIO_LOCK_KEY_Msk) != 0x00u)
  420. {
  421. return HAL_OK;
  422. }
  423. else
  424. {
  425. return HAL_ERROR;
  426. }
  427. }
  428. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  429. {
  430. /* Prevent unused argument(s) compilation warning */
  431. UNUSED(GPIO_Pin);
  432. /* NOTE: This function should not be modified, when the callback is needed,
  433. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  434. */
  435. }
  436. /**
  437. * @brief Handle EXTI interrupt request.
  438. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  439. * @retval None
  440. */
  441. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  442. {
  443. /* EXTI line interrupt detected */
  444. if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
  445. {
  446. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  447. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  448. }
  449. }
  450. /**
  451. * @}
  452. */
  453. /**
  454. * @}
  455. */
  456. #endif /* HAL_GPIO_MODULE_ENABLED */
  457. /**
  458. * @}
  459. */
  460. /**
  461. * @}
  462. */
  463. /************************ (C) COPYRIGHT Puya *****END OF FILE****/