2 ******************************************************************************
3 * @file stm32f30x_gpio.c
4 * @author MCD Application Team
6 * @date 27-February-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the GPIO peripheral:
9 * + Initialization and Configuration functions
10 * + GPIO Read and Write functions
11 * + GPIO Alternate functions configuration functions
16 ===============================================================================
17 ##### How to use this driver #####
18 ===============================================================================
20 (#) Enable the GPIO AHB clock using RCC_AHBPeriphClockCmd()
21 (#) Configure the GPIO pin(s) using GPIO_Init()
22 Four possible configuration are available for each pin:
23 (++) Input: Floating, Pull-up, Pull-down.
24 (++) Output: Push-Pull (Pull-up, Pull-down or no Pull),
25 Open Drain (Pull-up, Pull-down or no Pull).
26 In output mode, the speed is configurable: Low, Medium, Fast or High.
27 (++) Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull),
28 Open Drain (Pull-up, Pull-down or no Pull).
29 (++) Analog: required mode when a pin is to be used as ADC channel,
30 DAC output or comparator input.
31 (#) Peripherals alternate function:
32 (++) For ADC, DAC and comparators, configure the desired pin in
33 analog mode using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN
34 (++) For other peripherals (TIM, USART...):
35 (+++) Connect the pin to the desired peripherals' Alternate
36 Function (AF) using GPIO_PinAFConfig() function.
37 (+++) Configure the desired pin in alternate function mode using
38 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
39 (+++) Select the type, pull-up/pull-down and output speed via
40 GPIO_PuPd, GPIO_OType and GPIO_Speed members.
41 (+++) Call GPIO_Init() function.
42 (#) To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()
43 (#) To set/reset the level of a pin configured in output mode use
44 GPIO_SetBits()/GPIO_ResetBits()
45 (#) During and just after reset, the alternate functions are not active
46 and the GPIO pins are configured in input floating mode (except JTAG pins).
47 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as
48 general-purpose (PC14 and PC15, respectively) when the LSE
49 oscillator is off. The LSE has priority over the GPIO function.
50 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as general-purpose
51 (PF0 and PF1 respectively) when the HSE oscillator is off. The HSE has
52 the priority over the GPIO function.
56 ******************************************************************************
59 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
61 * Redistribution and use in source and binary forms, with or without modification,
62 * are permitted provided that the following conditions are met:
63 * 1. Redistributions of source code must retain the above copyright notice,
64 * this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright notice,
66 * this list of conditions and the following disclaimer in the documentation
67 * and/or other materials provided with the distribution.
68 * 3. Neither the name of STMicroelectronics nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
72 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
73 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
75 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
78 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
79 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
80 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 ******************************************************************************
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32f30x_gpio.h"
88 #include "stm32f30x_rcc.h"
90 /** @addtogroup STM32F30x_StdPeriph_Driver
95 * @brief GPIO driver modules
100 /* Private typedef -----------------------------------------------------------*/
101 /* Private define ------------------------------------------------------------*/
104 /* Private macro -------------------------------------------------------------*/
105 /* Private variables ---------------------------------------------------------*/
106 /* Private function prototypes -----------------------------------------------*/
107 /* Private functions ---------------------------------------------------------*/
109 /** @defgroup GPIO_Private_Functions
113 /** @defgroup GPIO_Group1 Initialization and Configuration
114 * @brief Initialization and Configuration
117 ===============================================================================
118 ##### Initialization and Configuration #####
119 ===============================================================================
126 * @brief Deinitializes the GPIOx peripheral registers to their default reset
128 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
131 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
133 /* Check the parameters */
134 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
138 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
139 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
141 else if(GPIOx == GPIOB)
143 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
144 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
146 else if(GPIOx == GPIOC)
148 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
149 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
151 else if(GPIOx == GPIOD)
153 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
154 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
156 else if(GPIOx == GPIOE)
158 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);
159 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);
165 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, ENABLE);
166 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, DISABLE);
172 * @brief Initializes the GPIOx peripheral according to the specified
173 * parameters in the GPIO_InitStruct.
174 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
175 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
176 * contains the configuration information for the specified GPIO
178 * @note GPIO_Pin: selects the pin to be configured:
179 * GPIO_Pin_0->GPIO_Pin_15 for GPIOA, GPIOB, GPIOC, GPIOD and GPIOE;
180 * GPIO_Pin_0->GPIO_Pin_2, GPIO_Pin_4, GPIO_Pin_6, GPIO_Pin_9
181 * and GPIO_Pin_10 for GPIOF.
184 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
186 uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
187 uint32_t tmpreg = 0x00;
189 /* Check the parameters */
190 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
191 assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
192 assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
193 assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
195 /*-------------------------- Configure the port pins -----------------------*/
196 /*-- GPIO Mode Configuration --*/
197 for (pinpos = 0x00; pinpos < 0x10; pinpos++)
199 pos = ((uint32_t)0x01) << pinpos;
201 /* Get the port pins position */
202 currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
204 if (currentpin == pos)
206 if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
208 /* Check Speed mode parameters */
209 assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
211 /* Speed mode configuration */
212 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
213 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
215 /* Check Output mode parameters */
216 assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
218 /* Output mode configuration */
219 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos));
220 GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
223 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
225 GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
227 /* Use temporary variable to update PUPDR register configuration, to avoid
228 unexpected transition in the GPIO pin configuration. */
229 tmpreg = GPIOx->PUPDR;
230 tmpreg &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
231 tmpreg |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
232 GPIOx->PUPDR = tmpreg;
238 * @brief Fills each GPIO_InitStruct member with its default value.
239 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
243 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
245 /* Reset GPIO init structure parameters values */
246 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
247 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
248 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
249 GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
250 GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
254 * @brief Locks GPIO Pins configuration registers.
255 * The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
256 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
257 * @note The configuration of the locked GPIO pins can no longer be modified
258 * until the next reset.
259 * @param GPIOx: where x can be (A or B or D) to select the GPIO peripheral.
260 * @param GPIO_Pin: specifies the port bit to be written.
261 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
264 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
266 uint32_t tmp = 0x00010000;
268 /* Check the parameters */
269 assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
270 assert_param(IS_GPIO_PIN(GPIO_Pin));
276 GPIOx->LCKR = GPIO_Pin;
289 /** @defgroup GPIO_Group2 GPIO Read and Write
290 * @brief GPIO Read and Write
293 ===============================================================================
294 ##### GPIO Read and Write #####
295 ===============================================================================
302 * @brief Reads the specified input port pin.
303 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
304 * @param GPIO_Pin: specifies the port bit to read.
305 * @note This parameter can be GPIO_Pin_x where x can be :
306 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
307 * (0..2, 4, 6, 9..10) for GPIOF.
308 * @retval The input port pin value.
310 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
312 uint8_t bitstatus = 0x00;
314 /* Check the parameters */
315 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
316 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
318 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
320 bitstatus = (uint8_t)Bit_SET;
324 bitstatus = (uint8_t)Bit_RESET;
330 * @brief Reads the specified input port pin.
331 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
332 * @retval The input port pin value.
334 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
336 /* Check the parameters */
337 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
339 return ((uint16_t)GPIOx->IDR);
343 * @brief Reads the specified output data port bit.
344 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
345 * @param GPIO_Pin: Specifies the port bit to read.
346 * @note This parameter can be GPIO_Pin_x where x can be :
347 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
348 * (0..2, 4, 6, 9..10) for GPIOF.
349 * @retval The output port pin value.
351 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
353 uint8_t bitstatus = 0x00;
355 /* Check the parameters */
356 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
357 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
359 if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
361 bitstatus = (uint8_t)Bit_SET;
365 bitstatus = (uint8_t)Bit_RESET;
371 * @brief Reads the specified GPIO output data port.
372 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
373 * @retval GPIO output data port value.
375 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
377 /* Check the parameters */
378 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
380 return ((uint16_t)GPIOx->ODR);
384 * @brief Sets the selected data port bits.
385 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
386 * @param GPIO_Pin: specifies the port bits to be written.
387 * @note This parameter can be GPIO_Pin_x where x can be :
388 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
389 * (0..2, 4, 6, 9..10) for GPIOF.
392 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
394 /* Check the parameters */
395 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
396 assert_param(IS_GPIO_PIN(GPIO_Pin));
398 GPIOx->BSRR = GPIO_Pin;
402 * @brief Clears the selected data port bits.
403 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
404 * @param GPIO_Pin: specifies the port bits to be written.
405 * @note This parameter can be GPIO_Pin_x where x can be :
406 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
407 * (0..2, 4, 6, 9..10) for GPIOF.
410 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
412 /* Check the parameters */
413 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
414 assert_param(IS_GPIO_PIN(GPIO_Pin));
416 GPIOx->BRR = GPIO_Pin;
420 * @brief Sets or clears the selected data port bit.
421 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
422 * @param GPIO_Pin: specifies the port bit to be written.
423 * @note This parameter can be GPIO_Pin_x where x can be :
424 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
425 * (0..2, 4, 6, 9..10) for GPIOF.
426 * @param BitVal: specifies the value to be written to the selected bit.
427 * This parameter can be one of the BitAction enumeration values:
428 * @arg Bit_RESET: to clear the port pin
429 * @arg Bit_SET: to set the port pin
432 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
434 /* Check the parameters */
435 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
436 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
437 assert_param(IS_GPIO_BIT_ACTION(BitVal));
439 if (BitVal != Bit_RESET)
441 GPIOx->BSRR = GPIO_Pin;
445 GPIOx->BRR = GPIO_Pin ;
450 * @brief Writes data to the specified GPIO data port.
451 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
452 * @param PortVal: specifies the value to be written to the port output data
456 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
458 /* Check the parameters */
459 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
461 GPIOx->ODR = PortVal;
468 /** @defgroup GPIO_Group3 GPIO Alternate functions configuration functions
469 * @brief GPIO Alternate functions configuration functions
472 ===============================================================================
473 ##### GPIO Alternate functions configuration functions #####
474 ===============================================================================
481 * @brief Writes data to the specified GPIO data port.
482 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
483 * @param GPIO_PinSource: specifies the pin for the Alternate function.
484 * This parameter can be GPIO_PinSourcex where x can be (0..15).
485 * @param GPIO_AF: selects the pin to be used as Alternate function.
486 * This parameter can be one of the following value:
487 * @arg GPIO_AF_0: JTCK-SWCLK, JTDI, JTDO/TRACESW0, JTMS-SWDAT, MCO, NJTRST,
489 * @arg GPIO_AF_1: OUT, TIM2, TIM15, TIM16, TIM17.
490 * @arg GPIO_AF_2: COMP1_OUT, TIM1, TIM2, TIM3, TIM4, TIM8, TIM15, TIM16.
491 * @arg GPIO_AF_3: COMP7_OUT, TIM8, TIM15, Touch, HRTIM.
492 * @arg GPIO_AF_4: I2C1, I2C2, TIM1, TIM8, TIM16, TIM17.
493 * @arg GPIO_AF_5: IR_OUT, I2S2, I2S3, SPI1, SPI2, TIM8, USART4, USART5
494 * @arg GPIO_AF_6: IR_OUT, I2S2, I2S3, SPI2, SPI3, TIM1, TIM8
495 * @arg GPIO_AF_7: AOP2_OUT, CAN, COMP3_OUT, COMP5_OUT, COMP6_OUT, USART1,
497 * @arg GPIO_AF_8: COMP1_OUT, COMP2_OUT, COMP3_OUT, COMP4_OUT, COMP5_OUT,
499 * @arg GPIO_AF_9: AOP4_OUT, CAN, TIM1, TIM8, TIM15.
500 * @arg GPIO_AF_10: AOP1_OUT, AOP3_OUT, TIM2, TIM3, TIM4, TIM8, TIM17.
501 * @arg GPIO_AF_11: TIM1, TIM8.
502 * @arg GPIO_AF_12: TIM1, HRTIM.
503 * @arg GPIO_AF_13: HRTIM, AOP2_OUT.
504 * @arg GPIO_AF_14: USBDM, USBDP.
505 * @arg GPIO_AF_15: OUT.
506 * @note The pin should already been configured in Alternate Function mode(AF)
507 * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
508 * @note Refer to the Alternate function mapping table in the device datasheet
509 * for the detailed mapping of the system and peripherals alternate
513 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
515 uint32_t temp = 0x00;
516 uint32_t temp_2 = 0x00;
518 /* Check the parameters */
519 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
520 assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
521 assert_param(IS_GPIO_AF(GPIO_AF));
523 temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
524 GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
525 temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
526 GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
545 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/