stm32l4xx_hal_gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 39 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 "stm32l4xx_hal.h"
  90. /** @addtogroup STM32L4xx_HAL_Driver
  91. * @{
  92. */
  93. /** @defgroup GPIO GPIO
  94. * @brief GPIO HAL module driver
  95. * @{
  96. */
  97. /** MISRA C:2012 deviation rule has been granted for following rules:
  98. * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
  99. * range of the shift operator in following API :
  100. * HAL_GPIO_Init
  101. * HAL_GPIO_DeInit
  102. */
  103. #ifdef HAL_GPIO_MODULE_ENABLED
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private defines -----------------------------------------------------------*/
  106. /** @addtogroup GPIO_Private_Defines GPIO Private Defines
  107. * @{
  108. */
  109. #define GPIO_NUMBER (16u)
  110. /**
  111. * @}
  112. */
  113. /* Private macros ------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /* Exported functions --------------------------------------------------------*/
  117. /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
  118. * @{
  119. */
  120. /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
  121. * @brief Initialization and Configuration functions
  122. *
  123. @verbatim
  124. ===============================================================================
  125. ##### Initialization and de-initialization functions #####
  126. ===============================================================================
  127. @endverbatim
  128. * @{
  129. */
  130. /**
  131. * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  132. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
  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 = 0x00u;
  140. uint32_t iocurrent;
  141. uint32_t temp;
  142. /* Check the parameters */
  143. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  144. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  145. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  146. /* Configure the port pins */
  147. while (((GPIO_Init->Pin) >> position) != 0x00u)
  148. {
  149. /* Get current io position */
  150. iocurrent = (GPIO_Init->Pin) & (1uL << position);
  151. if (iocurrent != 0x00u)
  152. {
  153. /*--------------------- GPIO Mode Configuration ------------------------*/
  154. /* In case of Output or Alternate function mode selection */
  155. if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
  156. {
  157. /* Check the Speed parameter */
  158. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  159. /* Configure the IO Speed */
  160. temp = GPIOx->OSPEEDR;
  161. temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
  162. temp |= (GPIO_Init->Speed << (position * 2u));
  163. GPIOx->OSPEEDR = temp;
  164. /* Configure the IO Output Type */
  165. temp = GPIOx->OTYPER;
  166. temp &= ~(GPIO_OTYPER_OT0 << position) ;
  167. temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
  168. GPIOx->OTYPER = temp;
  169. }
  170. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
  171. /* In case of Analog mode, check if ADC control mode is selected */
  172. if((GPIO_Init->Mode & GPIO_MODE_ANALOG) == GPIO_MODE_ANALOG)
  173. {
  174. /* Configure the IO Output Type */
  175. temp = GPIOx->ASCR;
  176. temp &= ~(GPIO_ASCR_ASC0 << position) ;
  177. temp |= (((GPIO_Init->Mode & GPIO_MODE_ANALOG_ADC_CONTROL) >> 3) << position);
  178. GPIOx->ASCR = temp;
  179. }
  180. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
  181. /* Activate the Pull-up or Pull down resistor for the current IO */
  182. if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
  183. {
  184. /* Check the Pull parameter */
  185. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  186. temp = GPIOx->PUPDR;
  187. temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
  188. temp |= ((GPIO_Init->Pull) << (position * 2U));
  189. GPIOx->PUPDR = temp;
  190. }
  191. /* In case of Alternate function mode selection */
  192. if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
  193. {
  194. /* Check the Alternate function parameters */
  195. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  196. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  197. /* Configure Alternate function mapped with the current IO */
  198. temp = GPIOx->AFR[position >> 3u];
  199. temp &= ~(0xFu << ((position & 0x07u) * 4u));
  200. temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
  201. GPIOx->AFR[position >> 3u] = temp;
  202. }
  203. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  204. temp = GPIOx->MODER;
  205. temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
  206. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
  207. GPIOx->MODER = temp;
  208. /*--------------------- EXTI Mode Configuration ------------------------*/
  209. /* Configure the External Interrupt or event for the current IO */
  210. if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
  211. {
  212. /* Enable SYSCFG Clock */
  213. __HAL_RCC_SYSCFG_CLK_ENABLE();
  214. temp = SYSCFG->EXTICR[position >> 2u];
  215. temp &= ~(0x0FuL << (4u * (position & 0x03u)));
  216. temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
  217. SYSCFG->EXTICR[position >> 2u] = temp;
  218. /* Clear Rising Falling edge configuration */
  219. temp = EXTI->RTSR1;
  220. temp &= ~(iocurrent);
  221. if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
  222. {
  223. temp |= iocurrent;
  224. }
  225. EXTI->RTSR1 = temp;
  226. temp = EXTI->FTSR1;
  227. temp &= ~(iocurrent);
  228. if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
  229. {
  230. temp |= iocurrent;
  231. }
  232. EXTI->FTSR1 = temp;
  233. /* Clear EXTI line configuration */
  234. temp = EXTI->EMR1;
  235. temp &= ~(iocurrent);
  236. if ((GPIO_Init->Mode & EXTI_EVT) != 0x00u)
  237. {
  238. temp |= iocurrent;
  239. }
  240. EXTI->EMR1 = temp;
  241. temp = EXTI->IMR1;
  242. temp &= ~(iocurrent);
  243. if ((GPIO_Init->Mode & EXTI_IT) != 0x00u)
  244. {
  245. temp |= iocurrent;
  246. }
  247. EXTI->IMR1 = temp;
  248. }
  249. }
  250. position++;
  251. }
  252. }
  253. /**
  254. * @brief De-initialize the GPIOx peripheral registers to their default reset values.
  255. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
  256. * @param GPIO_Pin specifies the port bit to be written.
  257. * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
  258. * @retval None
  259. */
  260. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  261. {
  262. uint32_t position = 0x00u;
  263. uint32_t iocurrent;
  264. uint32_t tmp;
  265. /* Check the parameters */
  266. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  267. assert_param(IS_GPIO_PIN(GPIO_Pin));
  268. /* Configure the port pins */
  269. while ((GPIO_Pin >> position) != 0x00u)
  270. {
  271. /* Get current io position */
  272. iocurrent = (GPIO_Pin) & (1uL << position);
  273. if (iocurrent != 0x00u)
  274. {
  275. /*------------------------- EXTI Mode Configuration --------------------*/
  276. /* Clear the External Interrupt or Event for the current IO */
  277. tmp = SYSCFG->EXTICR[position >> 2u];
  278. tmp &= (0x0FuL << (4u * (position & 0x03u)));
  279. if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
  280. {
  281. /* Clear EXTI line configuration */
  282. EXTI->IMR1 &= ~(iocurrent);
  283. EXTI->EMR1 &= ~(iocurrent);
  284. /* Clear Rising Falling edge configuration */
  285. EXTI->FTSR1 &= ~(iocurrent);
  286. EXTI->RTSR1 &= ~(iocurrent);
  287. tmp = 0x0FuL << (4u * (position & 0x03u));
  288. SYSCFG->EXTICR[position >> 2u] &= ~tmp;
  289. }
  290. /*------------------------- GPIO Mode Configuration --------------------*/
  291. /* Configure IO in Analog Mode */
  292. GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
  293. /* Configure the default Alternate Function in current IO */
  294. GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
  295. /* Configure the default value for IO Speed */
  296. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
  297. /* Configure the default value IO Output Type */
  298. GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
  299. /* Deactivate the Pull-up and Pull-down resistor for the current IO */
  300. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
  301. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
  302. /* Deactivate the Control bit of Analog mode for the current IO */
  303. GPIOx->ASCR &= ~(GPIO_ASCR_ASC0<< position);
  304. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
  305. }
  306. position++;
  307. }
  308. }
  309. /**
  310. * @}
  311. */
  312. /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
  313. * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
  314. *
  315. @verbatim
  316. ===============================================================================
  317. ##### IO operation functions #####
  318. ===============================================================================
  319. @endverbatim
  320. * @{
  321. */
  322. /**
  323. * @brief Read the specified input port pin.
  324. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
  325. * @param GPIO_Pin specifies the port bit to read.
  326. * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
  327. * @retval The input port pin value.
  328. */
  329. GPIO_PinState HAL_GPIO_ReadPin(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) != 0x00u)
  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..H) to select the GPIO peripheral for STM32L4 family
  352. * @param GPIO_Pin specifies the port bit to be written.
  353. * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
  354. * @param PinState specifies the value to be written to the selected bit.
  355. * This parameter can be one of the GPIO_PinState enum values:
  356. * @arg GPIO_PIN_RESET: to clear the port pin
  357. * @arg GPIO_PIN_SET: to set the port pin
  358. * @retval None
  359. */
  360. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  361. {
  362. /* Check the parameters */
  363. assert_param(IS_GPIO_PIN(GPIO_Pin));
  364. assert_param(IS_GPIO_PIN_ACTION(PinState));
  365. if(PinState != GPIO_PIN_RESET)
  366. {
  367. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  368. }
  369. else
  370. {
  371. GPIOx->BRR = (uint32_t)GPIO_Pin;
  372. }
  373. }
  374. /**
  375. * @brief Toggle the specified GPIO pin.
  376. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
  377. * @param GPIO_Pin specifies the pin to be toggled.
  378. * @retval None
  379. */
  380. void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  381. {
  382. uint32_t odr;
  383. /* Check the parameters */
  384. assert_param(IS_GPIO_PIN(GPIO_Pin));
  385. /* get current Output Data Register value */
  386. odr = GPIOx->ODR;
  387. /* Set selected pins that were at low level, and reset ones that were high */
  388. GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
  389. }
  390. /**
  391. * @brief Lock GPIO Pins configuration registers.
  392. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  393. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  394. * @note The configuration of the locked GPIO pins can no longer be modified
  395. * until the next reset.
  396. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
  397. * @param GPIO_Pin specifies the port bits to be locked.
  398. * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
  399. * @retval None
  400. */
  401. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  402. {
  403. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  404. /* Check the parameters */
  405. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  406. assert_param(IS_GPIO_PIN(GPIO_Pin));
  407. /* Apply lock key write sequence */
  408. tmp |= GPIO_Pin;
  409. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  410. GPIOx->LCKR = tmp;
  411. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  412. GPIOx->LCKR = GPIO_Pin;
  413. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  414. GPIOx->LCKR = tmp;
  415. /* Read LCKK register. This read is mandatory to complete key lock sequence */
  416. tmp = GPIOx->LCKR;
  417. /* Read again in order to confirm lock is active */
  418. if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
  419. {
  420. return HAL_OK;
  421. }
  422. else
  423. {
  424. return HAL_ERROR;
  425. }
  426. }
  427. /**
  428. * @brief Handle EXTI interrupt request.
  429. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  430. * @retval None
  431. */
  432. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  433. {
  434. /* EXTI line interrupt detected */
  435. if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
  436. {
  437. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  438. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  439. }
  440. }
  441. /**
  442. * @brief EXTI line detection callback.
  443. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  444. * @retval None
  445. */
  446. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  447. {
  448. /* Prevent unused argument(s) compilation warning */
  449. UNUSED(GPIO_Pin);
  450. /* NOTE: This function should not be modified, when the callback is needed,
  451. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  452. */
  453. }
  454. /**
  455. * @}
  456. */
  457. /**
  458. * @}
  459. */
  460. #endif /* HAL_GPIO_MODULE_ENABLED */
  461. /**
  462. * @}
  463. */
  464. /**
  465. * @}
  466. */