stm32f1xx_hal_gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 20 edge detectors in connectivity
  39. line devices, or 19 edge detectors in other devices for generating event/interrupt requests.
  40. Each input line can be independently configured to select the type (event or interrupt) and
  41. the corresponding trigger event (rising or falling or both). Each line can also masked
  42. independently. A pending register maintains the status line of the interrupt requests
  43. ##### How to use this driver #####
  44. ==============================================================================
  45. [..]
  46. (#) Enable the GPIO APB2 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. (++) Analog mode is required when a pin is to be used as ADC channel
  54. or DAC output.
  55. (++) In case of external interrupt/event selection the "Mode" member from
  56. GPIO_InitTypeDef structure select the type (interrupt or event) and
  57. the corresponding trigger event (rising or falling or both).
  58. (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
  59. mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
  60. HAL_NVIC_EnableIRQ().
  61. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  62. (#) To set/reset the level of a pin configured in output mode use
  63. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  64. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  65. (#) During and just after reset, the alternate functions are not
  66. active and the GPIO pins are configured in input floating mode (except JTAG
  67. pins).
  68. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  69. (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
  70. priority over the GPIO function.
  71. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  72. general purpose PD0 and PD1, respectively, when the HSE oscillator is off.
  73. The HSE has priority over the GPIO function.
  74. @endverbatim
  75. ******************************************************************************
  76. * @attention
  77. *
  78. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  79. *
  80. * Redistribution and use in source and binary forms, with or without modification,
  81. * are permitted provided that the following conditions are met:
  82. * 1. Redistributions of source code must retain the above copyright notice,
  83. * this list of conditions and the following disclaimer.
  84. * 2. Redistributions in binary form must reproduce the above copyright notice,
  85. * this list of conditions and the following disclaimer in the documentation
  86. * and/or other materials provided with the distribution.
  87. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  88. * may be used to endorse or promote products derived from this software
  89. * without specific prior written permission.
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  92. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  94. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  95. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  96. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  97. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  98. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  99. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  100. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  101. *
  102. ******************************************************************************
  103. */
  104. /* Includes ------------------------------------------------------------------*/
  105. #include "stm32f1xx_hal.h"
  106. /** @addtogroup STM32F1xx_HAL_Driver
  107. * @{
  108. */
  109. /** @defgroup GPIO GPIO
  110. * @brief GPIO HAL module driver
  111. * @{
  112. */
  113. #ifdef HAL_GPIO_MODULE_ENABLED
  114. /* Private typedef -----------------------------------------------------------*/
  115. /* Private define ------------------------------------------------------------*/
  116. /** @addtogroup GPIO_Private_Constants GPIO Private Constants
  117. * @{
  118. */
  119. #define GPIO_MODE 0x00000003U
  120. #define EXTI_MODE 0x10000000U
  121. #define GPIO_MODE_IT 0x00010000U
  122. #define GPIO_MODE_EVT 0x00020000U
  123. #define RISING_EDGE 0x00100000U
  124. #define FALLING_EDGE 0x00200000U
  125. #define GPIO_OUTPUT_TYPE 0x00000010U
  126. #define GPIO_NUMBER 16U
  127. /* Definitions for bit manipulation of CRL and CRH register */
  128. #define GPIO_CR_MODE_INPUT 0x00000000U /*!< 00: Input mode (reset state) */
  129. #define GPIO_CR_CNF_ANALOG 0x00000000U /*!< 00: Analog mode */
  130. #define GPIO_CR_CNF_INPUT_FLOATING 0x00000004U /*!< 01: Floating input (reset state) */
  131. #define GPIO_CR_CNF_INPUT_PU_PD 0x00000008U /*!< 10: Input with pull-up / pull-down */
  132. #define GPIO_CR_CNF_GP_OUTPUT_PP 0x00000000U /*!< 00: General purpose output push-pull */
  133. #define GPIO_CR_CNF_GP_OUTPUT_OD 0x00000004U /*!< 01: General purpose output Open-drain */
  134. #define GPIO_CR_CNF_AF_OUTPUT_PP 0x00000008U /*!< 10: Alternate function output Push-pull */
  135. #define GPIO_CR_CNF_AF_OUTPUT_OD 0x0000000CU /*!< 11: Alternate function output Open-drain */
  136. /**
  137. * @}
  138. */
  139. /* Private macro -------------------------------------------------------------*/
  140. /* Private variables ---------------------------------------------------------*/
  141. /* Private function prototypes -----------------------------------------------*/
  142. /* Private functions ---------------------------------------------------------*/
  143. /* Exported functions --------------------------------------------------------*/
  144. /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
  145. * @{
  146. */
  147. /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
  148. * @brief Initialization and Configuration functions
  149. *
  150. @verbatim
  151. ===============================================================================
  152. ##### Initialization and de-initialization functions #####
  153. ===============================================================================
  154. [..]
  155. This section provides functions allowing to initialize and de-initialize the GPIOs
  156. to be ready for use.
  157. @endverbatim
  158. * @{
  159. */
  160. /**
  161. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  162. * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  163. * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  164. * the configuration information for the specified GPIO peripheral.
  165. * @retval None
  166. */
  167. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  168. {
  169. uint32_t position;
  170. uint32_t ioposition = 0x00U;
  171. uint32_t iocurrent = 0x00U;
  172. uint32_t temp = 0x00U;
  173. uint32_t config = 0x00U;
  174. __IO uint32_t *configregister; /* Store the address of CRL or CRH register based on pin number */
  175. uint32_t registeroffset = 0U; /* offset used during computation of CNF and MODE bits placement inside CRL or CRH register */
  176. /* Check the parameters */
  177. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  178. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  179. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  180. /* Configure the port pins */
  181. for (position = 0U; position < GPIO_NUMBER; position++)
  182. {
  183. /* Get the IO position */
  184. ioposition = (0x01U << position);
  185. /* Get the current IO position */
  186. iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
  187. if (iocurrent == ioposition)
  188. {
  189. /* Check the Alternate function parameters */
  190. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  191. /* Based on the required mode, filling config variable with MODEy[1:0] and CNFy[3:2] corresponding bits */
  192. switch (GPIO_Init->Mode)
  193. {
  194. /* If we are configuring the pin in OUTPUT push-pull mode */
  195. case GPIO_MODE_OUTPUT_PP:
  196. /* Check the GPIO speed parameter */
  197. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  198. config = GPIO_Init->Speed + GPIO_CR_CNF_GP_OUTPUT_PP;
  199. break;
  200. /* If we are configuring the pin in OUTPUT open-drain mode */
  201. case GPIO_MODE_OUTPUT_OD:
  202. /* Check the GPIO speed parameter */
  203. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  204. config = GPIO_Init->Speed + GPIO_CR_CNF_GP_OUTPUT_OD;
  205. break;
  206. /* If we are configuring the pin in ALTERNATE FUNCTION push-pull mode */
  207. case GPIO_MODE_AF_PP:
  208. /* Check the GPIO speed parameter */
  209. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  210. config = GPIO_Init->Speed + GPIO_CR_CNF_AF_OUTPUT_PP;
  211. break;
  212. /* If we are configuring the pin in ALTERNATE FUNCTION open-drain mode */
  213. case GPIO_MODE_AF_OD:
  214. /* Check the GPIO speed parameter */
  215. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  216. config = GPIO_Init->Speed + GPIO_CR_CNF_AF_OUTPUT_OD;
  217. break;
  218. /* If we are configuring the pin in INPUT (also applicable to EVENT and IT mode) */
  219. case GPIO_MODE_INPUT:
  220. case GPIO_MODE_IT_RISING:
  221. case GPIO_MODE_IT_FALLING:
  222. case GPIO_MODE_IT_RISING_FALLING:
  223. case GPIO_MODE_EVT_RISING:
  224. case GPIO_MODE_EVT_FALLING:
  225. case GPIO_MODE_EVT_RISING_FALLING:
  226. /* Check the GPIO pull parameter */
  227. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  228. if (GPIO_Init->Pull == GPIO_NOPULL)
  229. {
  230. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_FLOATING;
  231. }
  232. else if (GPIO_Init->Pull == GPIO_PULLUP)
  233. {
  234. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
  235. /* Set the corresponding ODR bit */
  236. GPIOx->BSRR = ioposition;
  237. }
  238. else /* GPIO_PULLDOWN */
  239. {
  240. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
  241. /* Reset the corresponding ODR bit */
  242. GPIOx->BRR = ioposition;
  243. }
  244. break;
  245. /* If we are configuring the pin in INPUT analog mode */
  246. case GPIO_MODE_ANALOG:
  247. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_ANALOG;
  248. break;
  249. /* Parameters are checked with assert_param */
  250. default:
  251. break;
  252. }
  253. /* Check if the current bit belongs to first half or last half of the pin count number
  254. in order to address CRH or CRL register*/
  255. configregister = (iocurrent < GPIO_PIN_8) ? &GPIOx->CRL : &GPIOx->CRH;
  256. registeroffset = (iocurrent < GPIO_PIN_8) ? (position << 2U) : ((position - 8U) << 2U);
  257. /* Apply the new configuration of the pin to the register */
  258. MODIFY_REG((*configregister), ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset), (config << registeroffset));
  259. /*--------------------- EXTI Mode Configuration ------------------------*/
  260. /* Configure the External Interrupt or event for the current IO */
  261. if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  262. {
  263. /* Enable AFIO Clock */
  264. __HAL_RCC_AFIO_CLK_ENABLE();
  265. temp = AFIO->EXTICR[position >> 2U];
  266. CLEAR_BIT(temp, (0x0FU) << (4U * (position & 0x03U)));
  267. SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
  268. AFIO->EXTICR[position >> 2U] = temp;
  269. /* Configure the interrupt mask */
  270. if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  271. {
  272. SET_BIT(EXTI->IMR, iocurrent);
  273. }
  274. else
  275. {
  276. CLEAR_BIT(EXTI->IMR, iocurrent);
  277. }
  278. /* Configure the event mask */
  279. if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  280. {
  281. SET_BIT(EXTI->EMR, iocurrent);
  282. }
  283. else
  284. {
  285. CLEAR_BIT(EXTI->EMR, iocurrent);
  286. }
  287. /* Enable or disable the rising trigger */
  288. if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  289. {
  290. SET_BIT(EXTI->RTSR, iocurrent);
  291. }
  292. else
  293. {
  294. CLEAR_BIT(EXTI->RTSR, iocurrent);
  295. }
  296. /* Enable or disable the falling trigger */
  297. if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  298. {
  299. SET_BIT(EXTI->FTSR, iocurrent);
  300. }
  301. else
  302. {
  303. CLEAR_BIT(EXTI->FTSR, iocurrent);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. /**
  310. * @brief De-initializes the GPIOx peripheral registers to their default reset values.
  311. * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  312. * @param GPIO_Pin: specifies the port bit to be written.
  313. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  314. * @retval None
  315. */
  316. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  317. {
  318. uint32_t position = 0x00U;
  319. uint32_t iocurrent = 0x00U;
  320. uint32_t tmp = 0x00U;
  321. __IO uint32_t *configregister; /* Store the address of CRL or CRH register based on pin number */
  322. uint32_t registeroffset = 0U;
  323. /* Check the parameters */
  324. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  325. assert_param(IS_GPIO_PIN(GPIO_Pin));
  326. /* Configure the port pins */
  327. while ((GPIO_Pin >> position) != 0U)
  328. {
  329. /* Get current io position */
  330. iocurrent = (GPIO_Pin) & (1U << position);
  331. if (iocurrent)
  332. {
  333. /*------------------------- GPIO Mode Configuration --------------------*/
  334. /* Check if the current bit belongs to first half or last half of the pin count number
  335. in order to address CRH or CRL register */
  336. configregister = (iocurrent < GPIO_PIN_8) ? &GPIOx->CRL : &GPIOx->CRH;
  337. registeroffset = (iocurrent < GPIO_PIN_8) ? (position << 2U) : ((position - 8U) << 2U);
  338. /* CRL/CRH default value is floating input(0x04) shifted to correct position */
  339. MODIFY_REG(*configregister, ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset), GPIO_CRL_CNF0_0 << registeroffset);
  340. /* ODR default value is 0 */
  341. CLEAR_BIT(GPIOx->ODR, iocurrent);
  342. /*------------------------- EXTI Mode Configuration --------------------*/
  343. /* Clear the External Interrupt or Event for the current IO */
  344. tmp = AFIO->EXTICR[position >> 2U];
  345. tmp &= 0x0FU << (4U * (position & 0x03U));
  346. if (tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
  347. {
  348. tmp = 0x0FU << (4U * (position & 0x03U));
  349. CLEAR_BIT(AFIO->EXTICR[position >> 2U], tmp);
  350. /* Clear EXTI line configuration */
  351. CLEAR_BIT(EXTI->IMR, (uint32_t)iocurrent);
  352. CLEAR_BIT(EXTI->EMR, (uint32_t)iocurrent);
  353. /* Clear Rising Falling edge configuration */
  354. CLEAR_BIT(EXTI->RTSR, (uint32_t)iocurrent);
  355. CLEAR_BIT(EXTI->FTSR, (uint32_t)iocurrent);
  356. }
  357. }
  358. position++;
  359. }
  360. }
  361. /**
  362. * @}
  363. */
  364. /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
  365. * @brief GPIO Read and Write
  366. *
  367. @verbatim
  368. ===============================================================================
  369. ##### IO operation functions #####
  370. ===============================================================================
  371. [..]
  372. This subsection provides a set of functions allowing to manage the GPIOs.
  373. @endverbatim
  374. * @{
  375. */
  376. /**
  377. * @brief Reads the specified input port pin.
  378. * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  379. * @param GPIO_Pin: specifies the port bit to read.
  380. * This parameter can be GPIO_PIN_x where x can be (0..15).
  381. * @retval The input port pin value.
  382. */
  383. GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  384. {
  385. GPIO_PinState bitstatus;
  386. /* Check the parameters */
  387. assert_param(IS_GPIO_PIN(GPIO_Pin));
  388. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
  389. {
  390. bitstatus = GPIO_PIN_SET;
  391. }
  392. else
  393. {
  394. bitstatus = GPIO_PIN_RESET;
  395. }
  396. return bitstatus;
  397. }
  398. /**
  399. * @brief Sets or clears the selected data port bit.
  400. *
  401. * @note This function uses GPIOx_BSRR register to allow atomic read/modify
  402. * accesses. In this way, there is no risk of an IRQ occurring between
  403. * the read and the modify access.
  404. *
  405. * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  406. * @param GPIO_Pin: specifies the port bit to be written.
  407. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  408. * @param PinState: specifies the value to be written to the selected bit.
  409. * This parameter can be one of the GPIO_PinState enum values:
  410. * @arg GPIO_PIN_RESET: to clear the port pin
  411. * @arg GPIO_PIN_SET: to set the port pin
  412. * @retval None
  413. */
  414. void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  415. {
  416. /* Check the parameters */
  417. assert_param(IS_GPIO_PIN(GPIO_Pin));
  418. assert_param(IS_GPIO_PIN_ACTION(PinState));
  419. if (PinState != GPIO_PIN_RESET)
  420. {
  421. GPIOx->BSRR = GPIO_Pin;
  422. }
  423. else
  424. {
  425. GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
  426. }
  427. }
  428. /**
  429. * @brief Toggles the specified GPIO pin
  430. * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  431. * @param GPIO_Pin: Specifies the pins to be toggled.
  432. * @retval None
  433. */
  434. void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  435. {
  436. /* Check the parameters */
  437. assert_param(IS_GPIO_PIN(GPIO_Pin));
  438. GPIOx->ODR ^= GPIO_Pin;
  439. }
  440. /**
  441. * @brief Locks GPIO Pins configuration registers.
  442. * @note The locking mechanism allows the IO configuration to be frozen. When the LOCK sequence
  443. * has been applied on a port bit, it is no longer possible to modify the value of the port bit until
  444. * the next reset.
  445. * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  446. * @param GPIO_Pin: specifies the port bit to be locked.
  447. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  448. * @retval None
  449. */
  450. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  451. {
  452. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  453. /* Check the parameters */
  454. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  455. assert_param(IS_GPIO_PIN(GPIO_Pin));
  456. /* Apply lock key write sequence */
  457. SET_BIT(tmp, GPIO_Pin);
  458. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  459. GPIOx->LCKR = tmp;
  460. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  461. GPIOx->LCKR = GPIO_Pin;
  462. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  463. GPIOx->LCKR = tmp;
  464. /* Read LCKK bit*/
  465. tmp = GPIOx->LCKR;
  466. if ((uint32_t)(GPIOx->LCKR & GPIO_LCKR_LCKK))
  467. {
  468. return HAL_OK;
  469. }
  470. else
  471. {
  472. return HAL_ERROR;
  473. }
  474. }
  475. /**
  476. * @brief This function handles EXTI interrupt request.
  477. * @param GPIO_Pin: Specifies the pins connected EXTI line
  478. * @retval None
  479. */
  480. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  481. {
  482. /* EXTI line interrupt detected */
  483. if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
  484. {
  485. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  486. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  487. }
  488. }
  489. /**
  490. * @brief EXTI line detection callbacks.
  491. * @param GPIO_Pin: Specifies the pins connected EXTI line
  492. * @retval None
  493. */
  494. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  495. {
  496. /* Prevent unused argument(s) compilation warning */
  497. UNUSED(GPIO_Pin);
  498. /* NOTE: This function Should not be modified, when the callback is needed,
  499. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  500. */
  501. }
  502. /**
  503. * @}
  504. */
  505. /**
  506. * @}
  507. */
  508. #endif /* HAL_GPIO_MODULE_ENABLED */
  509. /**
  510. * @}
  511. */
  512. /**
  513. * @}
  514. */
  515. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/