stm32f7xx_hal_gpio.c 18 KB

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