stm32l0xx_hal_gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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) 2016 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 28 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 IOPORT 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. (#) HAL_GPIO_DeInit allows to set register values to their reset value. This function
  73. is also to be used when unconfiguring pin which was used as an external interrupt
  74. or in event mode. That is the only way to reset the corresponding bit in
  75. EXTI & SYSCFG registers.
  76. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  77. (#) To set/reset the level of a pin configured in output mode use
  78. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  79. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  80. (#) During and just after reset, the alternate functions are not
  81. active and the GPIO pins are configured in input floating mode (except JTAG
  82. pins).
  83. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  84. (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
  85. priority over the GPIO function.
  86. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  87. general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
  88. The HSE has priority over the GPIO function.
  89. @endverbatim
  90. ******************************************************************************
  91. */
  92. /* Includes ------------------------------------------------------------------*/
  93. #include "stm32l0xx_hal.h"
  94. /** @addtogroup STM32L0xx_HAL_Driver
  95. * @{
  96. */
  97. #ifdef HAL_GPIO_MODULE_ENABLED
  98. /** @addtogroup GPIO
  99. * @brief GPIO HAL module driver
  100. * @{
  101. */
  102. /* Private define ------------------------------------------------------------*/
  103. /** @addtogroup GPIO_Private
  104. * @{
  105. */
  106. #define GPIO_NUMBER (16U)
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup GPIO_Exported_Functions
  111. * @{
  112. */
  113. /** @addtogroup GPIO_Exported_Functions_Group1
  114. * @brief Initialization and de-initialization functions
  115. *
  116. @verbatim
  117. ===============================================================================
  118. ##### Initialization and de-initialization functions #####
  119. ===============================================================================
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  125. * @param GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
  126. * Note that GPIOE is not available on all devices.
  127. * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
  128. * the configuration information for the specified GPIO peripheral.
  129. * @retval None
  130. */
  131. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  132. {
  133. uint32_t position = 0x00U;
  134. uint32_t iocurrent = 0x00U;
  135. uint32_t temp = 0x00U;
  136. /* Check the parameters */
  137. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  138. assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, (GPIO_Init->Pin)));
  139. /* Configure the port pins */
  140. while (((GPIO_Init->Pin) >> position) != 0)
  141. {
  142. /* Get the IO position */
  143. iocurrent = (GPIO_Init->Pin) & (1U << position);
  144. if (iocurrent)
  145. {
  146. /*--------------------- GPIO Mode Configuration ------------------------*/
  147. /* In case of Output or Alternate function mode selection */
  148. if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) ||
  149. ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
  150. {
  151. /* Check the Speed parameter */
  152. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  153. /* Configure the IO Speed */
  154. temp = GPIOx->OSPEEDR;
  155. temp &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2U));
  156. temp |= (GPIO_Init->Speed << (position * 2U));
  157. GPIOx->OSPEEDR = temp;
  158. /* Configure the IO Output Type */
  159. temp = GPIOx->OTYPER;
  160. temp &= ~(GPIO_OTYPER_OT_0 << position) ;
  161. temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
  162. GPIOx->OTYPER = temp;
  163. }
  164. if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
  165. {
  166. /* Check the Pull parameter */
  167. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  168. /* Activate the Pull-up or Pull down resistor for the current IO */
  169. temp = GPIOx->PUPDR;
  170. temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
  171. temp |= ((GPIO_Init->Pull) << (position * 2U));
  172. GPIOx->PUPDR = temp;
  173. }
  174. /* In case of Alternate function mode selection */
  175. if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
  176. {
  177. /* Check the Alternate function parameters */
  178. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  179. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  180. /* Configure Alternate function mapped with the current IO */
  181. temp = GPIOx->AFR[position >> 3U];
  182. temp &= ~(0xFUL << ((uint32_t)(position & 0x07UL) * 4U));
  183. temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07U) * 4U));
  184. GPIOx->AFR[position >> 3U] = temp;
  185. }
  186. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  187. temp = GPIOx->MODER;
  188. temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
  189. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
  190. GPIOx->MODER = temp;
  191. /*--------------------- EXTI Mode Configuration ------------------------*/
  192. /* Configure the External Interrupt or event for the current IO */
  193. if ((GPIO_Init->Mode & EXTI_MODE) != 0x00U)
  194. {
  195. /* Enable SYSCFG Clock */
  196. __HAL_RCC_SYSCFG_CLK_ENABLE();
  197. temp = SYSCFG->EXTICR[position >> 2U];
  198. CLEAR_BIT(temp, (0x0FUL) << (4U * (position & 0x03U)));
  199. SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03U)));
  200. SYSCFG->EXTICR[position >> 2U] = temp;
  201. /* Clear Rising Falling edge configuration */
  202. temp = EXTI->RTSR;
  203. temp &= ~((uint32_t)iocurrent);
  204. if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00U)
  205. {
  206. temp |= iocurrent;
  207. }
  208. EXTI->RTSR = temp;
  209. temp = EXTI->FTSR;
  210. temp &= ~((uint32_t)iocurrent);
  211. if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00U)
  212. {
  213. temp |= iocurrent;
  214. }
  215. EXTI->FTSR = temp;
  216. temp = EXTI->EMR;
  217. temp &= ~((uint32_t)iocurrent);
  218. if ((GPIO_Init->Mode & EXTI_EVT) != 0x00U)
  219. {
  220. temp |= iocurrent;
  221. }
  222. EXTI->EMR = temp;
  223. /* Clear EXTI line configuration */
  224. temp = EXTI->IMR;
  225. temp &= ~((uint32_t)iocurrent);
  226. if ((GPIO_Init->Mode & EXTI_IT) != 0x00U)
  227. {
  228. temp |= iocurrent;
  229. }
  230. EXTI->IMR = temp;
  231. }
  232. }
  233. position++;
  234. }
  235. }
  236. /**
  237. * @brief De-initializes the GPIOx peripheral registers to their default reset values.
  238. * @param GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
  239. * Note that GPIOE is not available on all devices.
  240. * @param GPIO_Pin specifies the port bit to be written.
  241. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  242. * All port bits are not necessarily available on all GPIOs.
  243. * @retval None
  244. */
  245. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  246. {
  247. uint32_t position = 0x00U;
  248. uint32_t iocurrent = 0x00U;
  249. uint32_t tmp = 0x00U;
  250. /* Check the parameters */
  251. assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, GPIO_Pin));
  252. /* Configure the port pins */
  253. while ((GPIO_Pin >> position) != 0)
  254. {
  255. /* Get the IO position */
  256. iocurrent = (GPIO_Pin) & (1U << position);
  257. if (iocurrent)
  258. {
  259. /*------------------------- EXTI Mode Configuration --------------------*/
  260. /* Clear the External Interrupt or Event for the current IO */
  261. tmp = SYSCFG->EXTICR[position >> 2U];
  262. tmp &= ((0x0FUL) << (4U * (position & 0x03U)));
  263. if (tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
  264. {
  265. /* Clear EXTI line configuration */
  266. EXTI->IMR &= ~((uint32_t)iocurrent);
  267. EXTI->EMR &= ~((uint32_t)iocurrent);
  268. /* Clear Rising Falling edge configuration */
  269. EXTI->FTSR &= ~((uint32_t)iocurrent);
  270. EXTI->RTSR &= ~((uint32_t)iocurrent);
  271. tmp = (0x0FUL) << (4U * (position & 0x03U));
  272. SYSCFG->EXTICR[position >> 2U] &= ~tmp;
  273. }
  274. /*------------------------- GPIO Mode Configuration --------------------*/
  275. /* Configure IO Direction in Analog Mode (reset state) */
  276. GPIOx->MODER |= (GPIO_MODE_ANALOG << (position * 2U));
  277. /* Configure the default Alternate Function in current IO */
  278. GPIOx->AFR[position >> 3U] &= ~(0xFUL << ((uint32_t)(position & 0x07UL) * 4U));
  279. /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
  280. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
  281. /* Configure the default value IO Output Type */
  282. GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position);
  283. /* Configure the default value for IO Speed */
  284. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2U));
  285. }
  286. position++;
  287. }
  288. }
  289. /**
  290. * @}
  291. */
  292. /** @addtogroup GPIO_Exported_Functions_Group2
  293. * @brief GPIO Read and Write
  294. *
  295. @verbatim
  296. ===============================================================================
  297. ##### IO operation functions #####
  298. ===============================================================================
  299. @endverbatim
  300. * @{
  301. */
  302. /**
  303. * @brief Reads the specified input port pin.
  304. * @param GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
  305. * Note that GPIOE is not available on all devices.
  306. * @param GPIO_Pin specifies the port bit to read.
  307. * This parameter can be GPIO_PIN_x where x can be (0..15).
  308. * All port bits are not necessarily available on all GPIOs.
  309. * @retval The input port pin value.
  310. */
  311. GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  312. {
  313. GPIO_PinState bitstatus;
  314. /* Check the parameters */
  315. assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, GPIO_Pin));
  316. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
  317. {
  318. bitstatus = GPIO_PIN_SET;
  319. }
  320. else
  321. {
  322. bitstatus = GPIO_PIN_RESET;
  323. }
  324. return bitstatus;
  325. }
  326. /**
  327. * @brief Sets or clears the selected data port bit.
  328. *
  329. * @note This function uses GPIOx_BSRR register to allow atomic read/modify
  330. * accesses. In this way, there is no risk of an IRQ occurring between
  331. * the read and the modify access.
  332. *
  333. * @param GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
  334. * Note that GPIOE is not available on all devices.
  335. * @param GPIO_Pin specifies the port bit to be written.
  336. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  337. * All port bits are not necessarily available on all GPIOs.
  338. * @param PinState specifies the value to be written to the selected bit.
  339. * This parameter can be one of the GPIO_PinState enum values:
  340. * GPIO_PIN_RESET: to clear the port pin
  341. * GPIO_PIN_SET: to set the port pin
  342. * @retval None
  343. */
  344. void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  345. {
  346. /* Check the parameters */
  347. assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, GPIO_Pin));
  348. assert_param(IS_GPIO_PIN_ACTION(PinState));
  349. if (PinState != GPIO_PIN_RESET)
  350. {
  351. GPIOx->BSRR = GPIO_Pin;
  352. }
  353. else
  354. {
  355. GPIOx->BRR = GPIO_Pin ;
  356. }
  357. }
  358. /**
  359. * @brief Toggles the specified GPIO pins.
  360. * @param GPIOx Where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
  361. * Note that GPIOE is not available on all devices.
  362. * All port bits are not necessarily available on all GPIOs.
  363. * @param GPIO_Pin Specifies the pins to be toggled.
  364. * @retval None
  365. */
  366. void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  367. {
  368. uint32_t odr;
  369. /* Check the parameters */
  370. assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, GPIO_Pin));
  371. /* get current Output Data Register value */
  372. odr = GPIOx->ODR;
  373. /* Set selected pins that were at low level, and reset ones that were high */
  374. GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
  375. }
  376. /**
  377. * @brief Locks GPIO Pins configuration registers.
  378. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  379. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  380. * @note The configuration of the locked GPIO pins can no longer be modified
  381. * until the next reset.
  382. * @param GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family.
  383. * Note that GPIOE is not available on all devices.
  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. * All port bits are not necessarily available on all GPIOs.
  387. * @retval None
  388. */
  389. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  390. {
  391. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  392. /* Check the parameters */
  393. assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, GPIO_Pin));
  394. /* Apply lock key write sequence */
  395. tmp |= GPIO_Pin;
  396. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  397. GPIOx->LCKR = tmp;
  398. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  399. GPIOx->LCKR = GPIO_Pin;
  400. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  401. GPIOx->LCKR = tmp;
  402. /* Read LCKK register. This read is mandatory to complete key lock sequence */
  403. tmp = GPIOx->LCKR;
  404. /* read again in order to confirm lock is active */
  405. if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
  406. {
  407. return HAL_OK;
  408. }
  409. else
  410. {
  411. return HAL_ERROR;
  412. }
  413. }
  414. /**
  415. * @brief This function handles EXTI interrupt request.
  416. * @param GPIO_Pin Specifies the pins connected to the EXTI line.
  417. * @retval None
  418. */
  419. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  420. {
  421. /* EXTI line interrupt detected */
  422. if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
  423. {
  424. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  425. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  426. }
  427. }
  428. /**
  429. * @brief EXTI line detection callbacks.
  430. * @param GPIO_Pin Specifies the pins connected to the EXTI line.
  431. * @retval None
  432. */
  433. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  434. {
  435. /* Prevent unused argument(s) compilation warning */
  436. UNUSED(GPIO_Pin);
  437. /* NOTE: This function Should not be modified, when the callback is needed,
  438. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  439. */
  440. }
  441. /**
  442. * @}
  443. */
  444. /**
  445. * @}
  446. */
  447. /**
  448. * @}
  449. */
  450. #endif /* HAL_GPIO_MODULE_ENABLED */
  451. /**
  452. * @}
  453. */