stm32h7xx_hal_gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 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. ##### GPIO Peripheral features #####
  25. ==============================================================================
  26. [..]
  27. (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
  28. configured by software in several modes:
  29. (++) Input mode
  30. (++) Analog mode
  31. (++) Output mode
  32. (++) Alternate function mode
  33. (++) External interrupt/event lines
  34. (+) During and just after reset, the alternate functions and external interrupt
  35. lines are not active and the I/O ports are configured in input floating mode.
  36. (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
  37. activated or not.
  38. (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
  39. type and the IO speed can be selected depending on the VDD value.
  40. (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
  41. multiplexer that allows only one peripheral alternate function (AF) connected
  42. to an IO pin at a time. In this way, there can be no conflict between peripherals
  43. sharing the same IO pin.
  44. (+) All ports have external interrupt/event capability. To use external interrupt
  45. lines, the port must be configured in input mode. All available GPIO pins are
  46. connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
  47. The external interrupt/event controller consists of up to 23 edge detectors
  48. (16 lines are connected to GPIO) for generating event/interrupt requests (each
  49. input line can be independently configured to select the type (interrupt or event)
  50. and the corresponding trigger event (rising or falling or both). Each line can
  51. also be masked independently.
  52. ##### How to use this driver #####
  53. ==============================================================================
  54. [..]
  55. (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
  56. (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
  57. (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
  58. (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
  59. structure.
  60. (++) In case of Output or alternate function mode selection: the speed is
  61. configured through "Speed" member from GPIO_InitTypeDef structure.
  62. (++) In alternate mode is selection, the alternate function connected to the IO
  63. is configured through "Alternate" member from GPIO_InitTypeDef structure.
  64. (++) Analog mode is required when a pin is to be used as ADC channel
  65. or DAC output.
  66. (++) In case of external interrupt/event selection the "Mode" member from
  67. GPIO_InitTypeDef structure select the type (interrupt or event) and
  68. the corresponding trigger event (rising or falling or both).
  69. (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
  70. mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
  71. HAL_NVIC_EnableIRQ().
  72. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  73. (#) To set/reset the level of a pin configured in output mode use
  74. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  75. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  76. (#) During and just after reset, the alternate functions are not
  77. active and the GPIO pins are configured in input floating mode (except JTAG
  78. pins).
  79. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  80. (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
  81. priority over the GPIO function.
  82. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  83. general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
  84. The HSE has priority over the GPIO function.
  85. @endverbatim
  86. ******************************************************************************
  87. */
  88. /* Includes ------------------------------------------------------------------*/
  89. #include "stm32h7xx_hal.h"
  90. /** @addtogroup STM32H7xx_HAL_Driver
  91. * @{
  92. */
  93. /** @defgroup GPIO GPIO
  94. * @brief GPIO HAL module driver
  95. * @{
  96. */
  97. #ifdef HAL_GPIO_MODULE_ENABLED
  98. /* Private typedef -----------------------------------------------------------*/
  99. /* Private defines ------------------------------------------------------------*/
  100. /** @addtogroup GPIO_Private_Constants GPIO Private Constants
  101. * @{
  102. */
  103. #if defined(DUAL_CORE)
  104. #define EXTI_CPU1 (0x01000000U)
  105. #define EXTI_CPU2 (0x02000000U)
  106. #endif /*DUAL_CORE*/
  107. #define GPIO_NUMBER (16U)
  108. /**
  109. * @}
  110. */
  111. /* Private macro -------------------------------------------------------------*/
  112. /* Private variables ---------------------------------------------------------*/
  113. /* Private function prototypes -----------------------------------------------*/
  114. /* Private functions ---------------------------------------------------------*/
  115. /* Exported functions --------------------------------------------------------*/
  116. /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
  117. * @{
  118. */
  119. /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
  120. * @brief Initialization and Configuration functions
  121. *
  122. @verbatim
  123. ===============================================================================
  124. ##### Initialization and de-initialization functions #####
  125. ===============================================================================
  126. [..]
  127. This section provides functions allowing to initialize and de-initialize the GPIOs
  128. to be ready for use.
  129. @endverbatim
  130. * @{
  131. */
  132. /**
  133. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  134. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
  135. * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  136. * the configuration information for the specified GPIO peripheral.
  137. * @retval None
  138. */
  139. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  140. {
  141. uint32_t position = 0x00U;
  142. uint32_t iocurrent;
  143. uint32_t temp;
  144. EXTI_Core_TypeDef *EXTI_CurrentCPU;
  145. #if defined(DUAL_CORE) && defined(CORE_CM4)
  146. EXTI_CurrentCPU = EXTI_D2; /* EXTI for CM4 CPU */
  147. #else
  148. EXTI_CurrentCPU = EXTI_D1; /* EXTI for CM7 CPU */
  149. #endif
  150. /* Check the parameters */
  151. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  152. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  153. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  154. /* Configure the port pins */
  155. while (((GPIO_Init->Pin) >> position) != 0x00U)
  156. {
  157. /* Get current io position */
  158. iocurrent = (GPIO_Init->Pin) & (1UL << position);
  159. if (iocurrent != 0x00U)
  160. {
  161. /*--------------------- GPIO Mode Configuration ------------------------*/
  162. /* In case of Output or Alternate function mode selection */
  163. if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
  164. {
  165. /* Check the Speed parameter */
  166. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  167. /* Configure the IO Speed */
  168. temp = GPIOx->OSPEEDR;
  169. temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
  170. temp |= (GPIO_Init->Speed << (position * 2U));
  171. GPIOx->OSPEEDR = temp;
  172. /* Configure the IO Output Type */
  173. temp = GPIOx->OTYPER;
  174. temp &= ~(GPIO_OTYPER_OT0 << position) ;
  175. temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
  176. GPIOx->OTYPER = temp;
  177. }
  178. if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
  179. {
  180. /* Check the Pull parameter */
  181. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  182. /* Activate the Pull-up or Pull down resistor for the current IO */
  183. temp = GPIOx->PUPDR;
  184. temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
  185. temp |= ((GPIO_Init->Pull) << (position * 2U));
  186. GPIOx->PUPDR = temp;
  187. }
  188. /* In case of Alternate function mode selection */
  189. if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
  190. {
  191. /* Check the Alternate function parameters */
  192. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  193. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  194. /* Configure Alternate function mapped with the current IO */
  195. temp = GPIOx->AFR[position >> 3U];
  196. temp &= ~(0xFU << ((position & 0x07U) * 4U));
  197. temp |= ((GPIO_Init->Alternate) << ((position & 0x07U) * 4U));
  198. GPIOx->AFR[position >> 3U] = temp;
  199. }
  200. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  201. temp = GPIOx->MODER;
  202. temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
  203. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
  204. GPIOx->MODER = temp;
  205. /*--------------------- EXTI Mode Configuration ------------------------*/
  206. /* Configure the External Interrupt or event for the current IO */
  207. if ((GPIO_Init->Mode & EXTI_MODE) != 0x00U)
  208. {
  209. /* Enable SYSCFG Clock */
  210. __HAL_RCC_SYSCFG_CLK_ENABLE();
  211. temp = SYSCFG->EXTICR[position >> 2U];
  212. temp &= ~(0x0FUL << (4U * (position & 0x03U)));
  213. temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
  214. SYSCFG->EXTICR[position >> 2U] = temp;
  215. /* Clear Rising Falling edge configuration */
  216. temp = EXTI->RTSR1;
  217. temp &= ~(iocurrent);
  218. if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00U)
  219. {
  220. temp |= iocurrent;
  221. }
  222. EXTI->RTSR1 = temp;
  223. temp = EXTI->FTSR1;
  224. temp &= ~(iocurrent);
  225. if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00U)
  226. {
  227. temp |= iocurrent;
  228. }
  229. EXTI->FTSR1 = temp;
  230. temp = EXTI_CurrentCPU->EMR1;
  231. temp &= ~(iocurrent);
  232. if ((GPIO_Init->Mode & EXTI_EVT) != 0x00U)
  233. {
  234. temp |= iocurrent;
  235. }
  236. EXTI_CurrentCPU->EMR1 = temp;
  237. /* Clear EXTI line configuration */
  238. temp = EXTI_CurrentCPU->IMR1;
  239. temp &= ~(iocurrent);
  240. if ((GPIO_Init->Mode & EXTI_IT) != 0x00U)
  241. {
  242. temp |= iocurrent;
  243. }
  244. EXTI_CurrentCPU->IMR1 = temp;
  245. }
  246. }
  247. position++;
  248. }
  249. }
  250. /**
  251. * @brief De-initializes the GPIOx peripheral registers to their default reset values.
  252. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
  253. * @param GPIO_Pin: specifies the port bit to be written.
  254. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  255. * @retval None
  256. */
  257. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  258. {
  259. uint32_t position = 0x00U;
  260. uint32_t iocurrent;
  261. uint32_t tmp;
  262. EXTI_Core_TypeDef *EXTI_CurrentCPU;
  263. #if defined(DUAL_CORE) && defined(CORE_CM4)
  264. EXTI_CurrentCPU = EXTI_D2; /* EXTI for CM4 CPU */
  265. #else
  266. EXTI_CurrentCPU = EXTI_D1; /* EXTI for CM7 CPU */
  267. #endif
  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 = SYSCFG->EXTICR[position >> 2U];
  281. tmp &= (0x0FUL << (4U * (position & 0x03U)));
  282. if (tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
  283. {
  284. /* Clear EXTI line configuration for Current CPU */
  285. EXTI_CurrentCPU->IMR1 &= ~(iocurrent);
  286. EXTI_CurrentCPU->EMR1 &= ~(iocurrent);
  287. /* Clear Rising Falling edge configuration */
  288. EXTI->FTSR1 &= ~(iocurrent);
  289. EXTI->RTSR1 &= ~(iocurrent);
  290. tmp = 0x0FUL << (4U * (position & 0x03U));
  291. SYSCFG->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. /* Deactivate the Pull-up and Pull-down resistor for the current IO */
  299. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
  300. /* Configure the default value IO Output Type */
  301. GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
  302. /* Configure the default value for IO Speed */
  303. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
  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 Reads the specified input port pin.
  323. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
  324. * @param GPIO_Pin: specifies the port bit to read.
  325. * This parameter can be 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 Sets or clears the selected data port bit.
  345. *
  346. * @note This function uses GPIOx_BSRR register to allow atomic read/modify
  347. * accesses. In this way, there is no risk of an IRQ occurring between
  348. * the read and the modify access.
  349. *
  350. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
  351. * @param GPIO_Pin: specifies the port bit to be written.
  352. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  353. * @param PinState: specifies the value to be written to the selected bit.
  354. * This parameter can be one of the GPIO_PinState enum values:
  355. * @arg GPIO_PIN_RESET: to clear the port pin
  356. * @arg GPIO_PIN_SET: to set the port pin
  357. * @retval None
  358. */
  359. void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  360. {
  361. /* Check the parameters */
  362. assert_param(IS_GPIO_PIN(GPIO_Pin));
  363. assert_param(IS_GPIO_PIN_ACTION(PinState));
  364. if (PinState != GPIO_PIN_RESET)
  365. {
  366. GPIOx->BSRR = GPIO_Pin;
  367. }
  368. else
  369. {
  370. GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
  371. }
  372. }
  373. /**
  374. * @brief Toggles the specified GPIO pins.
  375. * @param GPIOx: Where x can be (A..K) to select the GPIO peripheral.
  376. * @param GPIO_Pin: Specifies the pins to be toggled.
  377. * @retval None
  378. */
  379. void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  380. {
  381. uint32_t odr;
  382. /* Check the parameters */
  383. assert_param(IS_GPIO_PIN(GPIO_Pin));
  384. /* get current Output Data Register value */
  385. odr = GPIOx->ODR;
  386. /* Set selected pins that were at low level, and reset ones that were high */
  387. GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
  388. }
  389. /**
  390. * @brief Locks GPIO Pins configuration registers.
  391. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  392. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  393. * @note The configuration of the locked GPIO pins can no longer be modified
  394. * until the next reset.
  395. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32H7 family
  396. * @param GPIO_Pin: specifies the port bit to be locked.
  397. * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
  398. * @retval None
  399. */
  400. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  401. {
  402. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  403. /* Check the parameters */
  404. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  405. assert_param(IS_GPIO_PIN(GPIO_Pin));
  406. /* Apply lock key write sequence */
  407. tmp |= GPIO_Pin;
  408. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  409. GPIOx->LCKR = tmp;
  410. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  411. GPIOx->LCKR = GPIO_Pin;
  412. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  413. GPIOx->LCKR = tmp;
  414. /* Read LCKK register. This read is mandatory to complete key lock sequence*/
  415. tmp = GPIOx->LCKR;
  416. /* read again in order to confirm lock is active */
  417. if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00U)
  418. {
  419. return HAL_OK;
  420. }
  421. else
  422. {
  423. return HAL_ERROR;
  424. }
  425. }
  426. /**
  427. * @brief Handle EXTI interrupt request.
  428. * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
  429. * @retval None
  430. */
  431. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  432. {
  433. #if defined(DUAL_CORE) && defined(CORE_CM4)
  434. if (__HAL_GPIO_EXTID2_GET_IT(GPIO_Pin) != 0x00U)
  435. {
  436. __HAL_GPIO_EXTID2_CLEAR_IT(GPIO_Pin);
  437. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  438. }
  439. #else
  440. /* EXTI line interrupt detected */
  441. if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00U)
  442. {
  443. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  444. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  445. }
  446. #endif
  447. }
  448. /**
  449. * @brief EXTI line detection callback.
  450. * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
  451. * @retval None
  452. */
  453. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  454. {
  455. /* Prevent unused argument(s) compilation warning */
  456. UNUSED(GPIO_Pin);
  457. /* NOTE: This function Should not be modified, when the callback is needed,
  458. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  459. */
  460. }
  461. /**
  462. * @}
  463. */
  464. /**
  465. * @}
  466. */
  467. #endif /* HAL_GPIO_MODULE_ENABLED */
  468. /**
  469. * @}
  470. */
  471. /**
  472. * @}
  473. */