stm32h5xx_hal_gpio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_hal_gpio.c
  4. * @author MCD 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. * @attention
  12. *
  13. * Copyright (c) 2023 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### GPIO Peripheral features #####
  24. ==============================================================================
  25. [..]
  26. (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
  27. configured by software in several modes:
  28. (++) Input mode
  29. (++) Analog mode
  30. (++) Output mode
  31. (++) Alternate function mode
  32. (++) External interrupt/event lines
  33. (+) During and just after reset, the alternate functions and external interrupt
  34. lines are not active and the I/O ports are configured in input floating mode.
  35. (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
  36. activated or not.
  37. (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
  38. type and the IO speed can be selected depending on the VDD value.
  39. (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
  40. multiplexer that allows only one peripheral alternate function (AF) connected
  41. to an IO pin at a time. In this way, there can be no conflict between peripherals
  42. sharing the same IO pin.
  43. (+) All ports have external interrupt/event capability. To use external interrupt
  44. lines, the port must be configured in input mode. All available GPIO pins are
  45. connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
  46. (+) The external interrupt/event controller consists of up to 39 edge detectors
  47. (16 lines are connected to GPIO) for generating event/interrupt requests (each
  48. input line can be independently configured to select the type (interrupt or event)
  49. and the corresponding trigger event (rising or falling or both). Each line can
  50. also be masked independently.
  51. ##### How to use this driver #####
  52. ==============================================================================
  53. [..]
  54. (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
  55. (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
  56. (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
  57. (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
  58. structure.
  59. (++) In case of Output or alternate function mode selection: the speed is
  60. configured through "Speed" member from GPIO_InitTypeDef structure.
  61. (++) In alternate mode is selection, the alternate function connected to the IO
  62. is configured through "Alternate" member from GPIO_InitTypeDef structure.
  63. (++) Analog mode is required when a pin is to be used as ADC channel
  64. or DAC output.
  65. (++) In case of external interrupt/event selection the "Mode" member from
  66. GPIO_InitTypeDef structure select the type (interrupt or event) and
  67. the corresponding trigger event (rising or falling or both).
  68. (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
  69. mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
  70. HAL_NVIC_EnableIRQ().
  71. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  72. (#) To set/reset the level of a pin configured in output mode use
  73. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  74. (#) To set the level of several pins and reset level of several other pins in
  75. same cycle, use HAL_GPIO_WriteMultipleStatePin().
  76. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  77. (#) During and just after reset, the alternate functions are not
  78. active and the GPIO pins are configured in input floating mode (except JTAG
  79. pins).
  80. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  81. (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
  82. priority over the GPIO function.
  83. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  84. general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
  85. The HSE has priority over the GPIO function.
  86. @endverbatim
  87. ******************************************************************************
  88. */
  89. /* Includes ------------------------------------------------------------------*/
  90. #include "stm32h5xx_hal.h"
  91. /** @addtogroup STM32H5xx_HAL_Driver
  92. * @{
  93. */
  94. /** @defgroup GPIO GPIO
  95. * @brief GPIO HAL module driver
  96. * @{
  97. */
  98. #ifdef HAL_GPIO_MODULE_ENABLED
  99. /* Private typedef -----------------------------------------------------------*/
  100. /* Private defines -----------------------------------------------------------*/
  101. /** @defgroup GPIO_Private_Defines GPIO Private Defines
  102. * @{
  103. */
  104. #define GPIO_MODE (0x00000003U)
  105. #define EXTI_MODE (0x10000000U)
  106. #define GPIO_MODE_IT (0x00010000U)
  107. #define GPIO_MODE_EVT (0x00020000U)
  108. #define RISING_EDGE (0x00100000U)
  109. #define FALLING_EDGE (0x00200000U)
  110. #define GPIO_OUTPUT_TYPE (0x00000010U)
  111. #define GPIO_NUMBER (16U)
  112. /**
  113. * @}
  114. */
  115. /* Private macros ------------------------------------------------------------*/
  116. /* Private variables ---------------------------------------------------------*/
  117. /** @defgroup GPIO_Private_Macros GPIO Private Macros
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /* Private function prototypes -----------------------------------------------*/
  124. /* Exported functions --------------------------------------------------------*/
  125. /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
  126. * @{
  127. */
  128. /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
  129. * @brief Initialization and Configuration functions
  130. *
  131. @verbatim
  132. ===============================================================================
  133. ##### Initialization and de-initialization functions #####
  134. ===============================================================================
  135. @endverbatim
  136. * @{
  137. */
  138. /**
  139. * @brief Initialize the GPIOx peripheral according to the specified parameters in the pGPIO_Init.
  140. * @note If GPIOx peripheral pin is used in EXTI_MODE and the pin is secure in case
  141. * the system implements the security (TZEN=1), it is up to the secure application to
  142. * insure that the corresponding EXTI line is set secure.
  143. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  144. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  145. * @param pGPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  146. * the configuration information for the specified GPIO peripheral.
  147. * @retval None
  148. */
  149. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, const GPIO_InitTypeDef *pGPIO_Init)
  150. {
  151. uint32_t tmp;
  152. uint32_t iocurrent;
  153. uint32_t position = 0U;
  154. /* Check the parameters */
  155. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  156. assert_param(IS_GPIO_PIN(pGPIO_Init->Pin));
  157. assert_param(IS_GPIO_MODE(pGPIO_Init->Mode));
  158. assert_param(IS_GPIO_PULL(pGPIO_Init->Pull));
  159. /* Configure the port pins */
  160. while (((pGPIO_Init->Pin) >> position) != 0U)
  161. {
  162. /* Get current io position */
  163. iocurrent = (pGPIO_Init->Pin) & (1UL << position);
  164. if (iocurrent != 0U)
  165. {
  166. /*--------------------- GPIO Mode Configuration ------------------------*/
  167. /* In case of Alternate function mode selection */
  168. if ((pGPIO_Init->Mode == GPIO_MODE_AF_PP) || (pGPIO_Init->Mode == GPIO_MODE_AF_OD))
  169. {
  170. /* Check the Alternate function parameters */
  171. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  172. assert_param(IS_GPIO_AF(pGPIO_Init->Alternate));
  173. /* Configure Alternate function mapped with the current IO */
  174. tmp = GPIOx->AFR[position >> 3U];
  175. tmp &= ~(0x0FUL << ((position & 0x07U) * GPIO_AFRL_AFSEL1_Pos));
  176. tmp |= ((pGPIO_Init->Alternate & 0x0FUL) << ((position & 0x07U) * GPIO_AFRL_AFSEL1_Pos));
  177. GPIOx->AFR[position >> 3U] = tmp;
  178. }
  179. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  180. tmp = GPIOx->MODER;
  181. tmp &= ~(GPIO_MODER_MODE0 << (position * GPIO_MODER_MODE1_Pos));
  182. tmp |= ((pGPIO_Init->Mode & GPIO_MODE) << (position * GPIO_MODER_MODE1_Pos));
  183. GPIOx->MODER = tmp;
  184. /* In case of Output or Alternate function mode selection */
  185. if ((pGPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (pGPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  186. (pGPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (pGPIO_Init->Mode == GPIO_MODE_AF_OD))
  187. {
  188. /* Check the Speed parameter */
  189. assert_param(IS_GPIO_SPEED(pGPIO_Init->Speed));
  190. /* Configure the IO Speed */
  191. tmp = GPIOx->OSPEEDR;
  192. tmp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * GPIO_OSPEEDR_OSPEED1_Pos));
  193. tmp |= (pGPIO_Init->Speed << (position * GPIO_OSPEEDR_OSPEED1_Pos));
  194. GPIOx->OSPEEDR = tmp;
  195. /* Configure the IO Output Type */
  196. tmp = GPIOx->OTYPER;
  197. tmp &= ~(GPIO_OTYPER_OT0 << position) ;
  198. tmp |= (((pGPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
  199. GPIOx->OTYPER = tmp;
  200. }
  201. if (((pGPIO_Init->Mode & GPIO_MODE) != GPIO_MODE_ANALOG) ||
  202. (((pGPIO_Init->Mode & GPIO_MODE) == GPIO_MODE_ANALOG) && (pGPIO_Init->Pull != GPIO_PULLUP)))
  203. {
  204. /* Check the Pull parameters */
  205. assert_param(IS_GPIO_PULL(pGPIO_Init->Pull));
  206. /* Activate the Pull-up or Pull down resistor for the current IO */
  207. tmp = GPIOx->PUPDR;
  208. tmp &= ~(GPIO_PUPDR_PUPD0 << (position * GPIO_PUPDR_PUPD1_Pos));
  209. tmp |= ((pGPIO_Init->Pull) << (position * GPIO_PUPDR_PUPD1_Pos));
  210. GPIOx->PUPDR = tmp;
  211. }
  212. /*--------------------- EXTI Mode Configuration ------------------------*/
  213. /* Configure the External Interrupt or event for the current IO */
  214. if ((pGPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  215. {
  216. tmp = EXTI->EXTICR[position >> 2U];
  217. tmp &= ~((0x0FUL) << ((position & 0x03U) * EXTI_EXTICR1_EXTI1_Pos));
  218. tmp |= (GPIO_GET_INDEX(GPIOx) << ((position & 0x03U) * EXTI_EXTICR1_EXTI1_Pos));
  219. EXTI->EXTICR[position >> 2U] = tmp;
  220. /* Clear Rising Falling edge configuration */
  221. tmp = EXTI->RTSR1;
  222. tmp &= ~((uint32_t)iocurrent);
  223. if ((pGPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  224. {
  225. tmp |= iocurrent;
  226. }
  227. EXTI->RTSR1 = tmp;
  228. tmp = EXTI->FTSR1;
  229. tmp &= ~((uint32_t)iocurrent);
  230. if ((pGPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  231. {
  232. tmp |= iocurrent;
  233. }
  234. EXTI->FTSR1 = tmp;
  235. /* Clear EXTI line configuration */
  236. tmp = EXTI->EMR1;
  237. tmp &= ~((uint32_t)iocurrent);
  238. if ((pGPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  239. {
  240. tmp |= iocurrent;
  241. }
  242. EXTI->EMR1 = tmp;
  243. tmp = EXTI->IMR1;
  244. tmp &= ~((uint32_t)iocurrent);
  245. if ((pGPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  246. {
  247. tmp |= iocurrent;
  248. }
  249. EXTI->IMR1 = tmp;
  250. }
  251. }
  252. position++;
  253. }
  254. }
  255. /**
  256. * @brief De-initialize the GPIOx peripheral registers to their default reset values.
  257. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  258. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  259. * @param GPIO_Pin: specifies the port bit to be written.
  260. * This parameter can be one 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 tmp;
  266. uint32_t iocurrent;
  267. uint32_t position = 0U;
  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) != 0U)
  273. {
  274. /* Get current io position */
  275. iocurrent = (GPIO_Pin) & (1UL << position);
  276. if (iocurrent != 0U)
  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) << ((position & 0x03U) * EXTI_EXTICR1_EXTI1_Pos));
  282. if (tmp == (GPIO_GET_INDEX(GPIOx) << ((position & 0x03U) * EXTI_EXTICR1_EXTI1_Pos)))
  283. {
  284. /* Clear EXTI line configuration */
  285. EXTI->IMR1 &= ~(iocurrent);
  286. EXTI->EMR1 &= ~(iocurrent);
  287. /* Clear Rising Falling edge configuration */
  288. EXTI->RTSR1 &= ~(iocurrent);
  289. EXTI->FTSR1 &= ~(iocurrent);
  290. tmp = (0x0FUL) << ((position & 0x03U) * EXTI_EXTICR1_EXTI1_Pos);
  291. EXTI->EXTICR[position >> 2U] &= ~tmp;
  292. }
  293. /*------------------------- GPIO Mode Configuration --------------------*/
  294. /* Configure IO in Analog Mode */
  295. GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * GPIO_MODER_MODE1_Pos));
  296. /* Configure the default Alternate Function in current IO */
  297. GPIOx->AFR[position >> 3U] &= ~(0x0FUL << ((position & 0x07U) * GPIO_AFRL_AFSEL1_Pos));
  298. /* Configure the default value for IO Speed */
  299. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * GPIO_OSPEEDR_OSPEED1_Pos));
  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 * GPIO_PUPDR_PUPD1_Pos));
  304. }
  305. position++;
  306. }
  307. }
  308. /**
  309. * @}
  310. */
  311. /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
  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..I) for stm32h56xxx and stm32h57xxx family lines and
  324. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  325. * @param GPIO_Pin: specifies the port bit to read.
  326. * This parameter can be GPIO_PIN_x where x can be (0..15).
  327. * @retval The input port pin value.
  328. */
  329. GPIO_PinState HAL_GPIO_ReadPin(const GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  330. {
  331. GPIO_PinState bitstatus;
  332. /* Check the parameters */
  333. assert_param(IS_GPIO_PIN(GPIO_Pin));
  334. if ((GPIOx->IDR & GPIO_Pin) != 0U)
  335. {
  336. bitstatus = GPIO_PIN_SET;
  337. }
  338. else
  339. {
  340. bitstatus = GPIO_PIN_RESET;
  341. }
  342. return bitstatus;
  343. }
  344. /**
  345. * @brief Set or clear the selected data port bit.
  346. *
  347. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  348. * accesses. In this way, there is no risk of an IRQ occurring between
  349. * the read and the modify access.
  350. *
  351. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  352. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  353. * @param GPIO_Pin: specifies the port bit to be written.
  354. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  355. * @param PinState: specifies the value to be written to the selected bit.
  356. * This parameter can be one of the GPIO_PinState enum values:
  357. * @arg GPIO_PIN_RESET: to clear the port pin
  358. * @arg GPIO_PIN_SET: to set the port pin
  359. * @retval None
  360. */
  361. void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  362. {
  363. /* Check the parameters */
  364. assert_param(IS_GPIO_PIN(GPIO_Pin));
  365. assert_param(IS_GPIO_PIN_ACTION(PinState));
  366. if (PinState != GPIO_PIN_RESET)
  367. {
  368. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  369. }
  370. else
  371. {
  372. GPIOx->BRR = (uint32_t)GPIO_Pin;
  373. }
  374. }
  375. /**
  376. * @brief Set and clear several pins of a dedicated port in same cycle.
  377. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  378. * accesses.
  379. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  380. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  381. * @param PinReset specifies the port bits to be reset
  382. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
  383. * @param PinSet specifies the port bits to be set
  384. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
  385. * @note Both PinReset and PinSet combinations shall not get any common bit, else
  386. * assert would be triggered.
  387. * @note At least one of the two parameters used to set or reset shall be different from zero.
  388. * @retval None
  389. */
  390. void HAL_GPIO_WriteMultipleStatePin(GPIO_TypeDef *GPIOx, uint16_t PinReset, uint16_t PinSet)
  391. {
  392. uint32_t tmp;
  393. /* Check the parameters */
  394. /* Make sure at least one parameter is different from zero and that there is no common pin */
  395. assert_param(IS_GPIO_PIN((uint32_t)PinReset | (uint32_t)PinSet));
  396. assert_param(IS_GPIO_COMMON_PIN(PinReset, PinSet));
  397. tmp = (((uint32_t)PinReset << 16) | PinSet);
  398. GPIOx->BSRR = tmp;
  399. }
  400. /**
  401. * @brief Toggle the specified GPIO pin.
  402. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral for STM32H5 family
  403. * @param GPIO_Pin: specifies the pin to be toggled.
  404. * @retval None
  405. */
  406. void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  407. {
  408. uint32_t odr;
  409. /* Check the parameters */
  410. assert_param(IS_GPIO_PIN(GPIO_Pin));
  411. /* get current Output Data Register value */
  412. odr = GPIOx->ODR;
  413. /* Set selected pins that were at low level, and reset ones that were high */
  414. GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
  415. }
  416. /**
  417. * @brief Lock GPIO Pins configuration registers.
  418. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  419. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  420. * @note The configuration of the locked GPIO pins can no longer be modified
  421. * until the next reset.
  422. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  423. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  424. * @param GPIO_Pin: specifies the port bits to be locked.
  425. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  426. * @retval None
  427. */
  428. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  429. {
  430. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  431. /* Check the parameters */
  432. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  433. assert_param(IS_GPIO_PIN(GPIO_Pin));
  434. /* Apply lock key write sequence */
  435. tmp |= GPIO_Pin;
  436. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  437. GPIOx->LCKR = tmp;
  438. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  439. GPIOx->LCKR = GPIO_Pin;
  440. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  441. GPIOx->LCKR = tmp;
  442. /* Read LCKK bit*/
  443. tmp = GPIOx->LCKR;
  444. /* read again in order to confirm lock is active */
  445. if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != GPIO_LCKR_LCKK)
  446. {
  447. return HAL_ERROR;
  448. }
  449. return HAL_OK;
  450. }
  451. /**
  452. * @brief Enable speed optimization for several pin of dedicated port.
  453. * @note Not all I/Os support the HSLV mode. Refer to the I/O structure in the corresponding
  454. * datasheet for the list of I/Os supporting this feature. Other I/Os HSLV configuration must
  455. * be kept at reset value.
  456. * @note It must be used only if the I/O supply voltage is below 2.7 V.
  457. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  458. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  459. * @param GPIO_Pin: specifies the port bit to be written.
  460. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  461. * @retval None
  462. */
  463. void HAL_GPIO_EnableHighSPeedLowVoltage(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  464. {
  465. /* Check the parameters */
  466. assert_param(IS_GPIO_PIN(GPIO_Pin));
  467. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  468. /* Set HSLVR gpio pin */
  469. SET_BIT(GPIOx->HSLVR, GPIO_Pin);
  470. }
  471. /**
  472. * @brief Disable speed optimization for several pin of dedicated port.
  473. * @note Not all I/Os support the HSLV mode. Refer to the I/O structure in the corresponding
  474. * datasheet for the list of I/Os supporting this feature. Other I/Os HSLV configuration must
  475. * be kept at reset value.
  476. * @note It must be used only if the I/O supply voltage is below 2.7 V.
  477. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  478. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  479. * @param GPIO_Pin: specifies the port bit to be written.
  480. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  481. * @retval None
  482. */
  483. void HAL_GPIO_DisableHighSPeedLowVoltage(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  484. {
  485. /* Check the parameters */
  486. assert_param(IS_GPIO_PIN(GPIO_Pin));
  487. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  488. /* Clear HSLVR gpio pin */
  489. CLEAR_BIT(GPIOx->HSLVR, GPIO_Pin);
  490. }
  491. /**
  492. * @brief Handle EXTI interrupt request.
  493. * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
  494. * @retval None
  495. */
  496. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  497. {
  498. /* EXTI line interrupt detected */
  499. if (__HAL_GPIO_EXTI_GET_RISING_IT(GPIO_Pin) != 0U)
  500. {
  501. __HAL_GPIO_EXTI_CLEAR_RISING_IT(GPIO_Pin);
  502. HAL_GPIO_EXTI_Rising_Callback(GPIO_Pin);
  503. }
  504. if (__HAL_GPIO_EXTI_GET_FALLING_IT(GPIO_Pin) != 0U)
  505. {
  506. __HAL_GPIO_EXTI_CLEAR_FALLING_IT(GPIO_Pin);
  507. HAL_GPIO_EXTI_Falling_Callback(GPIO_Pin);
  508. }
  509. }
  510. /**
  511. * @brief EXTI line rising detection callback.
  512. * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
  513. * @retval None
  514. */
  515. __weak void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
  516. {
  517. /* Prevent unused argument(s) compilation warning */
  518. UNUSED(GPIO_Pin);
  519. /* NOTE: This function should not be modified, when the callback is needed,
  520. the HAL_GPIO_EXTI_Rising_Callback could be implemented in the user file
  521. */
  522. }
  523. /**
  524. * @brief EXTI line falling detection callback.
  525. * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
  526. * @retval None
  527. */
  528. __weak void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
  529. {
  530. /* Prevent unused argument(s) compilation warning */
  531. UNUSED(GPIO_Pin);
  532. /* NOTE: This function should not be modified, when the callback is needed,
  533. the HAL_GPIO_EXTI_Falling_Callback could be implemented in the user file
  534. */
  535. }
  536. /**
  537. * @}
  538. */
  539. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  540. /** @defgroup GPIO_Exported_Functions_Group3 IO attributes management functions
  541. * @brief GPIO attributes management functions.
  542. *
  543. @verbatim
  544. ===============================================================================
  545. ##### IO attributes functions #####
  546. ===============================================================================
  547. @endverbatim
  548. * @{
  549. */
  550. /**
  551. * @brief Configure the GPIO pins attributes.
  552. * @note Available attributes are to secure GPIO pin(s), so this function is
  553. * only available in secure
  554. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  555. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  556. * @param GPIO_Pin: specifies the pin(s) to configure the secure attribute
  557. * @param PinAttributes: specifies the pin(s) to be set in secure mode, other being set non secured.
  558. * @retval None
  559. */
  560. void HAL_GPIO_ConfigPinAttributes(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, uint32_t PinAttributes)
  561. {
  562. uint32_t tmp;
  563. uint32_t iocurrent;
  564. uint32_t position = 0U;
  565. /* Check the parameters */
  566. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  567. assert_param(IS_GPIO_PIN(GPIO_Pin));
  568. assert_param(IS_GPIO_PIN_ATTRIBUTES(PinAttributes));
  569. tmp = GPIOx->SECCFGR;
  570. /* Configure the port pins */
  571. while ((GPIO_Pin >> position) != 0U)
  572. {
  573. /* Get current io position */
  574. iocurrent = GPIO_Pin & (1UL << position);
  575. if (iocurrent != 0U)
  576. {
  577. /* Configure the IO secure attribute */
  578. tmp &= ~(GPIO_SECCFGR_SEC0 << position);
  579. tmp |= (PinAttributes << position);
  580. }
  581. position++;
  582. }
  583. /* Set secure attributes */
  584. GPIOx->SECCFGR = tmp;
  585. }
  586. /**
  587. * @brief Get the GPIO pins attributes.
  588. * @note Available attributes are to secure GPIO pin(s), so this function is
  589. * only available in secure
  590. * @param GPIOx: where x can be (A..I) for stm32h56xxx and stm32h57xxx family lines and
  591. * (A..D or H) for stm32h503xx family line to select the GPIO peripheral for STM32H5 family
  592. * @param GPIO_Pin: specifies the single pin to get the secure attribute from
  593. * @param pPinAttributes: pointer to return the pin attributes.
  594. * @retval HAL Status.
  595. */
  596. HAL_StatusTypeDef HAL_GPIO_GetConfigPinAttributes(const GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin,
  597. uint32_t *pPinAttributes)
  598. {
  599. uint32_t iocurrent;
  600. uint32_t position = 0U;
  601. /* Check null pointer */
  602. if (pPinAttributes == NULL)
  603. {
  604. return HAL_ERROR;
  605. }
  606. /* Check the parameters */
  607. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  608. assert_param(IS_GPIO_SINGLE_PIN(GPIO_Pin));
  609. /* Get secure attribute of the port pin */
  610. while ((GPIO_Pin >> position) != 0U)
  611. {
  612. /* Get current io position */
  613. iocurrent = GPIO_Pin & (1UL << position);
  614. if (iocurrent != 0U)
  615. {
  616. /* Get the IO secure attribute */
  617. if ((GPIOx->SECCFGR & (GPIO_SECCFGR_SEC0 << position)) != 0U)
  618. {
  619. *pPinAttributes = GPIO_PIN_SEC;
  620. }
  621. else
  622. {
  623. *pPinAttributes = GPIO_PIN_NSEC;
  624. }
  625. break;
  626. }
  627. position++;
  628. }
  629. return HAL_OK;
  630. }
  631. /**
  632. * @}
  633. */
  634. #endif /* __ARM_FEATURE_CMSE */
  635. /**
  636. * @}
  637. */
  638. #endif /* HAL_GPIO_MODULE_ENABLED */
  639. /**
  640. * @}
  641. */
  642. /**
  643. * @}
  644. */