You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_gpio.c 19KB

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