Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_gpio.c
blobfc080c07927385d00bc244b82e6eceb026b78f36
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_gpio.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-2017
7 * @brief GPIO HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
13 @verbatim
14 ==============================================================================
15 ##### GPIO Peripheral features #####
16 ==============================================================================
17 [..]
18 Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
19 port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
20 in several modes:
21 (+) Input mode
22 (+) Analog mode
23 (+) Output mode
24 (+) Alternate function mode
25 (+) External interrupt/event lines
27 [..]
28 During and just after reset, the alternate functions and external interrupt
29 lines are not active and the I/O ports are configured in input floating mode.
31 [..]
32 All GPIO pins have weak internal pull-up and pull-down resistors, which can be
33 activated or not.
35 [..]
36 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
37 type and the IO speed can be selected depending on the VDD value.
39 [..]
40 All ports have external interrupt/event capability. To use external interrupt
41 lines, the port must be configured in input mode. All available GPIO pins are
42 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
44 [..]
45 The external interrupt/event controller consists of up to 20 edge detectors in connectivity
46 line devices, or 19 edge detectors in other devices for generating event/interrupt requests.
47 Each input line can be independently configured to select the type (event or interrupt) and
48 the corresponding trigger event (rising or falling or both). Each line can also masked
49 independently. A pending register maintains the status line of the interrupt requests
51 ##### How to use this driver #####
52 ==============================================================================
53 [..]
54 (#) Enable the GPIO APB2 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 (++) Analog mode is required when a pin is to be used as ADC channel
63 or DAC output.
64 (++) In case of external interrupt/event selection the "Mode" member from
65 GPIO_InitTypeDef structure select the type (interrupt or event) and
66 the corresponding trigger event (rising or falling or both).
68 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
69 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
70 HAL_NVIC_EnableIRQ().
72 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
74 (#) To set/reset the level of a pin configured in output mode use
75 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
77 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
79 (#) During and just after reset, the alternate functions are not
80 active and the GPIO pins are configured in input floating mode (except JTAG
81 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.
87 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
88 general purpose PD0 and PD1, respectively, when the HSE oscillator is off.
89 The HSE has priority over the GPIO function.
91 @endverbatim
92 ******************************************************************************
93 * @attention
95 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
97 * Redistribution and use in source and binary forms, with or without modification,
98 * are permitted provided that the following conditions are met:
99 * 1. Redistributions of source code must retain the above copyright notice,
100 * this list of conditions and the following disclaimer.
101 * 2. Redistributions in binary form must reproduce the above copyright notice,
102 * this list of conditions and the following disclaimer in the documentation
103 * and/or other materials provided with the distribution.
104 * 3. Neither the name of STMicroelectronics nor the names of its contributors
105 * may be used to endorse or promote products derived from this software
106 * without specific prior written permission.
108 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
109 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
110 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
111 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
112 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
113 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
114 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
115 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
116 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
117 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
119 ******************************************************************************
122 /* Includes ------------------------------------------------------------------*/
123 #include "stm32f1xx_hal.h"
125 /** @addtogroup STM32F1xx_HAL_Driver
126 * @{
129 /** @defgroup GPIO GPIO
130 * @brief GPIO HAL module driver
131 * @{
134 #ifdef HAL_GPIO_MODULE_ENABLED
136 /* Private typedef -----------------------------------------------------------*/
137 /* Private define ------------------------------------------------------------*/
138 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
139 * @{
141 #define GPIO_MODE 0x00000003U
142 #define EXTI_MODE 0x10000000U
143 #define GPIO_MODE_IT 0x00010000U
144 #define GPIO_MODE_EVT 0x00020000U
145 #define RISING_EDGE 0x00100000U
146 #define FALLING_EDGE 0x00200000U
147 #define GPIO_OUTPUT_TYPE 0x00000010U
149 #define GPIO_NUMBER 16U
151 /* Definitions for bit manipulation of CRL and CRH register */
152 #define GPIO_CR_MODE_INPUT 0x00000000U /*!< 00: Input mode (reset state) */
153 #define GPIO_CR_CNF_ANALOG 0x00000000U /*!< 00: Analog mode */
154 #define GPIO_CR_CNF_INPUT_FLOATING 0x00000004U /*!< 01: Floating input (reset state) */
155 #define GPIO_CR_CNF_INPUT_PU_PD 0x00000008U /*!< 10: Input with pull-up / pull-down */
156 #define GPIO_CR_CNF_GP_OUTPUT_PP 0x00000000U /*!< 00: General purpose output push-pull */
157 #define GPIO_CR_CNF_GP_OUTPUT_OD 0x00000004U /*!< 01: General purpose output Open-drain */
158 #define GPIO_CR_CNF_AF_OUTPUT_PP 0x00000008U /*!< 10: Alternate function output Push-pull */
159 #define GPIO_CR_CNF_AF_OUTPUT_OD 0x0000000CU /*!< 11: Alternate function output Open-drain */
162 * @}
164 /* Private macro -------------------------------------------------------------*/
165 /* Private variables ---------------------------------------------------------*/
166 /* Private function prototypes -----------------------------------------------*/
167 /* Private functions ---------------------------------------------------------*/
168 /* Exported functions --------------------------------------------------------*/
169 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
170 * @{
173 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
174 * @brief Initialization and Configuration functions
176 @verbatim
177 ===============================================================================
178 ##### Initialization and de-initialization functions #####
179 ===============================================================================
180 [..]
181 This section provides functions allowing to initialize and de-initialize the GPIOs
182 to be ready for use.
184 @endverbatim
185 * @{
190 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
191 * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
192 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
193 * the configuration information for the specified GPIO peripheral.
194 * @retval None
196 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
198 uint32_t position;
199 uint32_t ioposition = 0x00U;
200 uint32_t iocurrent = 0x00U;
201 uint32_t temp = 0x00U;
202 uint32_t config = 0x00U;
203 __IO uint32_t *configregister; /* Store the address of CRL or CRH register based on pin number */
204 uint32_t registeroffset = 0U; /* offset used during computation of CNF and MODE bits placement inside CRL or CRH register */
206 /* Check the parameters */
207 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
208 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
209 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
211 /* Configure the port pins */
212 for (position = 0U; position < GPIO_NUMBER; position++)
214 /* Get the IO position */
215 ioposition = (0x01U << position);
217 /* Get the current IO position */
218 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
220 if (iocurrent == ioposition)
222 /* Check the Alternate function parameters */
223 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
225 /* Based on the required mode, filling config variable with MODEy[1:0] and CNFy[3:2] corresponding bits */
226 switch (GPIO_Init->Mode)
228 /* If we are configuring the pin in OUTPUT push-pull mode */
229 case GPIO_MODE_OUTPUT_PP:
230 /* Check the GPIO speed parameter */
231 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
232 config = GPIO_Init->Speed + GPIO_CR_CNF_GP_OUTPUT_PP;
233 break;
235 /* If we are configuring the pin in OUTPUT open-drain mode */
236 case GPIO_MODE_OUTPUT_OD:
237 /* Check the GPIO speed parameter */
238 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
239 config = GPIO_Init->Speed + GPIO_CR_CNF_GP_OUTPUT_OD;
240 break;
242 /* If we are configuring the pin in ALTERNATE FUNCTION push-pull mode */
243 case GPIO_MODE_AF_PP:
244 /* Check the GPIO speed parameter */
245 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
246 config = GPIO_Init->Speed + GPIO_CR_CNF_AF_OUTPUT_PP;
247 break;
249 /* If we are configuring the pin in ALTERNATE FUNCTION open-drain mode */
250 case GPIO_MODE_AF_OD:
251 /* Check the GPIO speed parameter */
252 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
253 config = GPIO_Init->Speed + GPIO_CR_CNF_AF_OUTPUT_OD;
254 break;
256 /* If we are configuring the pin in INPUT (also applicable to EVENT and IT mode) */
257 case GPIO_MODE_INPUT:
258 case GPIO_MODE_IT_RISING:
259 case GPIO_MODE_IT_FALLING:
260 case GPIO_MODE_IT_RISING_FALLING:
261 case GPIO_MODE_EVT_RISING:
262 case GPIO_MODE_EVT_FALLING:
263 case GPIO_MODE_EVT_RISING_FALLING:
264 /* Check the GPIO pull parameter */
265 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
266 if(GPIO_Init->Pull == GPIO_NOPULL)
268 config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_FLOATING;
270 else if(GPIO_Init->Pull == GPIO_PULLUP)
272 config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
274 /* Set the corresponding ODR bit */
275 GPIOx->BSRR = ioposition;
277 else /* GPIO_PULLDOWN */
279 config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
281 /* Reset the corresponding ODR bit */
282 GPIOx->BRR = ioposition;
284 break;
286 /* If we are configuring the pin in INPUT analog mode */
287 case GPIO_MODE_ANALOG:
288 config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_ANALOG;
289 break;
291 /* Parameters are checked with assert_param */
292 default:
293 break;
296 /* Check if the current bit belongs to first half or last half of the pin count number
297 in order to address CRH or CRL register*/
298 configregister = (iocurrent < GPIO_PIN_8) ? &GPIOx->CRL : &GPIOx->CRH;
299 registeroffset = (iocurrent < GPIO_PIN_8) ? (position << 2U) : ((position - 8U) << 2U);
301 /* Apply the new configuration of the pin to the register */
302 MODIFY_REG((*configregister), ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset ), (config << registeroffset));
304 /*--------------------- EXTI Mode Configuration ------------------------*/
305 /* Configure the External Interrupt or event for the current IO */
306 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
308 /* Enable AFIO Clock */
309 __HAL_RCC_AFIO_CLK_ENABLE();
310 temp = AFIO->EXTICR[position >> 2U];
311 CLEAR_BIT(temp, (0x0FU) << (4U * (position & 0x03U)));
312 SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
313 AFIO->EXTICR[position >> 2U] = temp;
316 /* Configure the interrupt mask */
317 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
319 SET_BIT(EXTI->IMR, iocurrent);
321 else
323 CLEAR_BIT(EXTI->IMR, iocurrent);
326 /* Configure the event mask */
327 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
329 SET_BIT(EXTI->EMR, iocurrent);
331 else
333 CLEAR_BIT(EXTI->EMR, iocurrent);
336 /* Enable or disable the rising trigger */
337 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
339 SET_BIT(EXTI->RTSR, iocurrent);
341 else
343 CLEAR_BIT(EXTI->RTSR, iocurrent);
346 /* Enable or disable the falling trigger */
347 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
349 SET_BIT(EXTI->FTSR, iocurrent);
351 else
353 CLEAR_BIT(EXTI->FTSR, iocurrent);
361 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
362 * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
363 * @param GPIO_Pin: specifies the port bit to be written.
364 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
365 * @retval None
367 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
369 uint32_t position = 0x00U;
370 uint32_t iocurrent = 0x00U;
371 uint32_t tmp = 0x00U;
372 __IO uint32_t *configregister; /* Store the address of CRL or CRH register based on pin number */
373 uint32_t registeroffset = 0U;
375 /* Check the parameters */
376 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
377 assert_param(IS_GPIO_PIN(GPIO_Pin));
379 /* Configure the port pins */
380 while ((GPIO_Pin >> position) != 0U)
382 /* Get current io position */
383 iocurrent = (GPIO_Pin) & (1U << position);
385 if (iocurrent)
387 /*------------------------- GPIO Mode Configuration --------------------*/
388 /* Check if the current bit belongs to first half or last half of the pin count number
389 in order to address CRH or CRL register */
390 configregister = (iocurrent < GPIO_PIN_8) ? &GPIOx->CRL : &GPIOx->CRH;
391 registeroffset = (iocurrent < GPIO_PIN_8) ? (position << 2U) : ((position - 8U) << 2U);
393 /* CRL/CRH default value is floating input(0x04) shifted to correct position */
394 MODIFY_REG(*configregister, ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset ), GPIO_CRL_CNF0_0 << registeroffset);
396 /* ODR default value is 0 */
397 CLEAR_BIT(GPIOx->ODR, iocurrent);
399 /*------------------------- EXTI Mode Configuration --------------------*/
400 /* Clear the External Interrupt or Event for the current IO */
402 tmp = AFIO->EXTICR[position >> 2U];
403 tmp &= 0x0FU << (4U * (position & 0x03U));
404 if(tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
406 tmp = 0x0FU << (4U * (position & 0x03U));
407 CLEAR_BIT(AFIO->EXTICR[position >> 2U], tmp);
409 /* Clear EXTI line configuration */
410 CLEAR_BIT(EXTI->IMR, (uint32_t)iocurrent);
411 CLEAR_BIT(EXTI->EMR, (uint32_t)iocurrent);
413 /* Clear Rising Falling edge configuration */
414 CLEAR_BIT(EXTI->RTSR, (uint32_t)iocurrent);
415 CLEAR_BIT(EXTI->FTSR, (uint32_t)iocurrent);
419 position++;
424 * @}
427 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
428 * @brief GPIO Read and Write
430 @verbatim
431 ===============================================================================
432 ##### IO operation functions #####
433 ===============================================================================
434 [..]
435 This subsection provides a set of functions allowing to manage the GPIOs.
437 @endverbatim
438 * @{
442 * @brief Reads the specified input port pin.
443 * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
444 * @param GPIO_Pin: specifies the port bit to read.
445 * This parameter can be GPIO_PIN_x where x can be (0..15).
446 * @retval The input port pin value.
448 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
450 GPIO_PinState bitstatus;
452 /* Check the parameters */
453 assert_param(IS_GPIO_PIN(GPIO_Pin));
455 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
457 bitstatus = GPIO_PIN_SET;
459 else
461 bitstatus = GPIO_PIN_RESET;
463 return bitstatus;
467 * @brief Sets or clears the selected data port bit.
469 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
470 * accesses. In this way, there is no risk of an IRQ occurring between
471 * the read and the modify access.
473 * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
474 * @param GPIO_Pin: specifies the port bit to be written.
475 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
476 * @param PinState: specifies the value to be written to the selected bit.
477 * This parameter can be one of the GPIO_PinState enum values:
478 * @arg GPIO_BIT_RESET: to clear the port pin
479 * @arg GPIO_BIT_SET: to set the port pin
480 * @retval None
482 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
484 /* Check the parameters */
485 assert_param(IS_GPIO_PIN(GPIO_Pin));
486 assert_param(IS_GPIO_PIN_ACTION(PinState));
488 if(PinState != GPIO_PIN_RESET)
490 GPIOx->BSRR = GPIO_Pin;
492 else
494 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
499 * @brief Toggles the specified GPIO pin
500 * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
501 * @param GPIO_Pin: Specifies the pins to be toggled.
502 * @retval None
504 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
506 /* Check the parameters */
507 assert_param(IS_GPIO_PIN(GPIO_Pin));
509 GPIOx->ODR ^= GPIO_Pin;
513 * @brief Locks GPIO Pins configuration registers.
514 * @note The locking mechanism allows the IO configuration to be frozen. When the LOCK sequence
515 * has been applied on a port bit, it is no longer possible to modify the value of the port bit until
516 * the next reset.
517 * @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
518 * @param GPIO_Pin: specifies the port bit to be locked.
519 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
520 * @retval None
522 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
524 __IO uint32_t tmp = GPIO_LCKR_LCKK;
526 /* Check the parameters */
527 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
528 assert_param(IS_GPIO_PIN(GPIO_Pin));
530 /* Apply lock key write sequence */
531 SET_BIT(tmp, GPIO_Pin);
532 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
533 GPIOx->LCKR = tmp;
534 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
535 GPIOx->LCKR = GPIO_Pin;
536 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
537 GPIOx->LCKR = tmp;
538 /* Read LCKK bit*/
539 tmp = GPIOx->LCKR;
541 if((uint32_t)(GPIOx->LCKR & GPIO_LCKR_LCKK))
543 return HAL_OK;
545 else
547 return HAL_ERROR;
552 * @brief This function handles EXTI interrupt request.
553 * @param GPIO_Pin: Specifies the pins connected EXTI line
554 * @retval None
556 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
558 /* EXTI line interrupt detected */
559 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
561 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
562 HAL_GPIO_EXTI_Callback(GPIO_Pin);
567 * @brief EXTI line detection callbacks.
568 * @param GPIO_Pin: Specifies the pins connected EXTI line
569 * @retval None
571 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
573 /* Prevent unused argument(s) compilation warning */
574 UNUSED(GPIO_Pin);
575 /* NOTE: This function Should not be modified, when the callback is needed,
576 the HAL_GPIO_EXTI_Callback could be implemented in the user file
581 * @}
585 * @}
588 #endif /* HAL_GPIO_MODULE_ENABLED */
590 * @}
594 * @}
597 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/