2 ******************************************************************************
3 * @file stm32f30x_gpio.c
4 * @author MCD Application Team
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 2014 STMicroelectronics</center></h2>
61 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
62 * You may not use this file except in compliance with the License.
63 * You may obtain a copy of the License at:
65 * http://www.st.com/software_license_agreement_liberty_v2
67 * Unless required by applicable law or agreed to in writing, software
68 * distributed under the License is distributed on an "AS IS" BASIS,
69 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
70 * See the License for the specific language governing permissions and
71 * limitations under the License.
73 ******************************************************************************
76 /* Includes ------------------------------------------------------------------*/
77 #include "stm32f30x_gpio.h"
78 #include "stm32f30x_rcc.h"
80 /** @addtogroup STM32F30x_StdPeriph_Driver
85 * @brief GPIO driver modules
90 /* Private typedef -----------------------------------------------------------*/
91 /* Private define ------------------------------------------------------------*/
94 /* Private macro -------------------------------------------------------------*/
95 /* Private variables ---------------------------------------------------------*/
96 /* Private function prototypes -----------------------------------------------*/
97 /* Private functions ---------------------------------------------------------*/
99 /** @defgroup GPIO_Private_Functions
103 /** @defgroup GPIO_Group1 Initialization and Configuration
104 * @brief Initialization and Configuration
107 ===============================================================================
108 ##### Initialization and Configuration #####
109 ===============================================================================
116 * @brief Deinitializes the GPIOx peripheral registers to their default reset
118 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
121 void GPIO_DeInit(GPIO_TypeDef
* GPIOx
)
123 /* Check the parameters */
124 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
128 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA
, ENABLE
);
129 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA
, DISABLE
);
131 else if(GPIOx
== GPIOB
)
133 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB
, ENABLE
);
134 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB
, DISABLE
);
136 else if(GPIOx
== GPIOC
)
138 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC
, ENABLE
);
139 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC
, DISABLE
);
141 else if(GPIOx
== GPIOD
)
143 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD
, ENABLE
);
144 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD
, DISABLE
);
146 else if(GPIOx
== GPIOE
)
148 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE
, ENABLE
);
149 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE
, DISABLE
);
155 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF
, ENABLE
);
156 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF
, DISABLE
);
162 * @brief Initializes the GPIOx peripheral according to the specified
163 * parameters in the GPIO_InitStruct.
164 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
165 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
166 * contains the configuration information for the specified GPIO
168 * @note GPIO_Pin: selects the pin to be configured:
169 * GPIO_Pin_0->GPIO_Pin_15 for GPIOA, GPIOB, GPIOC, GPIOD and GPIOE;
170 * GPIO_Pin_0->GPIO_Pin_2, GPIO_Pin_4, GPIO_Pin_6, GPIO_Pin_9
171 * and GPIO_Pin_10 for GPIOF.
174 void GPIO_Init(GPIO_TypeDef
* GPIOx
, GPIO_InitTypeDef
* GPIO_InitStruct
)
176 uint32_t pinpos
= 0x00, pos
= 0x00 , currentpin
= 0x00;
177 uint32_t tmpreg
= 0x00;
179 /* Check the parameters */
180 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
181 assert_param(IS_GPIO_PIN(GPIO_InitStruct
->GPIO_Pin
));
182 assert_param(IS_GPIO_MODE(GPIO_InitStruct
->GPIO_Mode
));
183 assert_param(IS_GPIO_PUPD(GPIO_InitStruct
->GPIO_PuPd
));
185 /*-------------------------- Configure the port pins -----------------------*/
186 /*-- GPIO Mode Configuration --*/
187 for (pinpos
= 0x00; pinpos
< 0x10; pinpos
++)
189 pos
= ((uint32_t)0x01) << pinpos
;
191 /* Get the port pins position */
192 currentpin
= (GPIO_InitStruct
->GPIO_Pin
) & pos
;
194 if (currentpin
== pos
)
196 if ((GPIO_InitStruct
->GPIO_Mode
== GPIO_Mode_OUT
) || (GPIO_InitStruct
->GPIO_Mode
== GPIO_Mode_AF
))
198 /* Check Speed mode parameters */
199 assert_param(IS_GPIO_SPEED(GPIO_InitStruct
->GPIO_Speed
));
201 /* Speed mode configuration */
202 GPIOx
->OSPEEDR
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (pinpos
* 2));
203 GPIOx
->OSPEEDR
|= ((uint32_t)(GPIO_InitStruct
->GPIO_Speed
) << (pinpos
* 2));
205 /* Check Output mode parameters */
206 assert_param(IS_GPIO_OTYPE(GPIO_InitStruct
->GPIO_OType
));
208 /* Output mode configuration */
209 GPIOx
->OTYPER
&= ~((GPIO_OTYPER_OT_0
) << ((uint16_t)pinpos
));
210 GPIOx
->OTYPER
|= (uint16_t)(((uint16_t)GPIO_InitStruct
->GPIO_OType
) << ((uint16_t)pinpos
));
213 GPIOx
->MODER
&= ~(GPIO_MODER_MODER0
<< (pinpos
* 2));
215 GPIOx
->MODER
|= (((uint32_t)GPIO_InitStruct
->GPIO_Mode
) << (pinpos
* 2));
217 /* Use temporary variable to update PUPDR register configuration, to avoid
218 unexpected transition in the GPIO pin configuration. */
219 tmpreg
= GPIOx
->PUPDR
;
220 tmpreg
&= ~(GPIO_PUPDR_PUPDR0
<< ((uint16_t)pinpos
* 2));
221 tmpreg
|= (((uint32_t)GPIO_InitStruct
->GPIO_PuPd
) << (pinpos
* 2));
222 GPIOx
->PUPDR
= tmpreg
;
228 * @brief Fills each GPIO_InitStruct member with its default value.
229 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
233 void GPIO_StructInit(GPIO_InitTypeDef
* GPIO_InitStruct
)
235 /* Reset GPIO init structure parameters values */
236 GPIO_InitStruct
->GPIO_Pin
= GPIO_Pin_All
;
237 GPIO_InitStruct
->GPIO_Mode
= GPIO_Mode_IN
;
238 GPIO_InitStruct
->GPIO_Speed
= GPIO_Speed_2MHz
;
239 GPIO_InitStruct
->GPIO_OType
= GPIO_OType_PP
;
240 GPIO_InitStruct
->GPIO_PuPd
= GPIO_PuPd_NOPULL
;
244 * @brief Locks GPIO Pins configuration registers.
245 * The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
246 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
247 * @note The configuration of the locked GPIO pins can no longer be modified
248 * until the next reset.
249 * @param GPIOx: where x can be (A or B or D) to select the GPIO peripheral.
250 * @param GPIO_Pin: specifies the port bit to be written.
251 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
254 void GPIO_PinLockConfig(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
256 uint32_t tmp
= 0x00010000;
258 /* Check the parameters */
259 assert_param(IS_GPIO_LIST_PERIPH(GPIOx
));
260 assert_param(IS_GPIO_PIN(GPIO_Pin
));
266 GPIOx
->LCKR
= GPIO_Pin
;
279 /** @defgroup GPIO_Group2 GPIO Read and Write
280 * @brief GPIO Read and Write
283 ===============================================================================
284 ##### GPIO Read and Write #####
285 ===============================================================================
292 * @brief Reads the specified input port pin.
293 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
294 * @param GPIO_Pin: specifies the port bit to read.
295 * @note This parameter can be GPIO_Pin_x where x can be :
296 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
297 * (0..2, 4, 6, 9..10) for GPIOF.
298 * @retval The input port pin value.
300 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
302 uint8_t bitstatus
= 0x00;
304 /* Check the parameters */
305 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
306 assert_param(IS_GET_GPIO_PIN(GPIO_Pin
));
308 if ((GPIOx
->IDR
& GPIO_Pin
) != (uint32_t)Bit_RESET
)
310 bitstatus
= (uint8_t)Bit_SET
;
314 bitstatus
= (uint8_t)Bit_RESET
;
320 * @brief Reads the specified input port pin.
321 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
322 * @retval The input port pin value.
324 uint16_t GPIO_ReadInputData(GPIO_TypeDef
* GPIOx
)
326 /* Check the parameters */
327 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
329 return ((uint16_t)GPIOx
->IDR
);
333 * @brief Reads the specified output data port bit.
334 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
335 * @param GPIO_Pin: Specifies the port bit to read.
336 * @note This parameter can be GPIO_Pin_x where x can be :
337 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
338 * (0..2, 4, 6, 9..10) for GPIOF.
339 * @retval The output port pin value.
341 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
343 uint8_t bitstatus
= 0x00;
345 /* Check the parameters */
346 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
347 assert_param(IS_GET_GPIO_PIN(GPIO_Pin
));
349 if ((GPIOx
->ODR
& GPIO_Pin
) != (uint32_t)Bit_RESET
)
351 bitstatus
= (uint8_t)Bit_SET
;
355 bitstatus
= (uint8_t)Bit_RESET
;
361 * @brief Reads the specified GPIO output data port.
362 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
363 * @retval GPIO output data port value.
365 uint16_t GPIO_ReadOutputData(GPIO_TypeDef
* GPIOx
)
367 /* Check the parameters */
368 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
370 return ((uint16_t)GPIOx
->ODR
);
374 * @brief Sets the selected data port bits.
375 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
376 * @param GPIO_Pin: specifies the port bits to be written.
377 * @note This parameter can be GPIO_Pin_x where x can be :
378 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
379 * (0..2, 4, 6, 9..10) for GPIOF.
382 void GPIO_SetBits(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
384 /* Check the parameters */
385 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
386 assert_param(IS_GPIO_PIN(GPIO_Pin
));
388 GPIOx
->BSRR
= GPIO_Pin
;
392 * @brief Clears the selected data port bits.
393 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
394 * @param GPIO_Pin: specifies the port bits to be written.
395 * @note This parameter can be GPIO_Pin_x where x can be :
396 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
397 * (0..2, 4, 6, 9..10) for GPIOF.
400 void GPIO_ResetBits(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
402 /* Check the parameters */
403 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
404 assert_param(IS_GPIO_PIN(GPIO_Pin
));
406 GPIOx
->BRR
= GPIO_Pin
;
410 * @brief Sets or clears the selected data port bit.
411 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
412 * @param GPIO_Pin: specifies the port bit to be written.
413 * @note This parameter can be GPIO_Pin_x where x can be :
414 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
415 * (0..2, 4, 6, 9..10) for GPIOF.
416 * @param BitVal: specifies the value to be written to the selected bit.
417 * This parameter can be one of the BitAction enumeration values:
418 * @arg Bit_RESET: to clear the port pin
419 * @arg Bit_SET: to set the port pin
422 void GPIO_WriteBit(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
, BitAction BitVal
)
424 /* Check the parameters */
425 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
426 assert_param(IS_GET_GPIO_PIN(GPIO_Pin
));
427 assert_param(IS_GPIO_BIT_ACTION(BitVal
));
429 if (BitVal
!= Bit_RESET
)
431 GPIOx
->BSRR
= GPIO_Pin
;
435 GPIOx
->BRR
= GPIO_Pin
;
440 * @brief Writes data to the specified GPIO data port.
441 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
442 * @param PortVal: specifies the value to be written to the port output data
446 void GPIO_Write(GPIO_TypeDef
* GPIOx
, uint16_t PortVal
)
448 /* Check the parameters */
449 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
451 GPIOx
->ODR
= PortVal
;
458 /** @defgroup GPIO_Group3 GPIO Alternate functions configuration functions
459 * @brief GPIO Alternate functions configuration functions
462 ===============================================================================
463 ##### GPIO Alternate functions configuration functions #####
464 ===============================================================================
471 * @brief Writes data to the specified GPIO data port.
472 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
473 * @param GPIO_PinSource: specifies the pin for the Alternate function.
474 * This parameter can be GPIO_PinSourcex where x can be (0..15).
475 * @param GPIO_AF: selects the pin to be used as Alternate function.
476 * This parameter can be one of the following value:
477 * @arg GPIO_AF_0: JTCK-SWCLK, JTDI, JTDO/TRACESW0, JTMS-SWDAT, MCO, NJTRST,
479 * @arg GPIO_AF_1: OUT, TIM2, TIM15, TIM16, TIM17.
480 * @arg GPIO_AF_2: COMP1_OUT, TIM1, TIM2, TIM3, TIM4, TIM8, TIM15, TIM16.
481 * @arg GPIO_AF_3: COMP7_OUT, TIM8, TIM15, Touch, HRTIM.
482 * @arg GPIO_AF_4: I2C1, I2C2, TIM1, TIM8, TIM16, TIM17.
483 * @arg GPIO_AF_5: IR_OUT, I2S2, I2S3, SPI1, SPI2, TIM8, USART4, USART5
484 * @arg GPIO_AF_6: IR_OUT, I2S2, I2S3, SPI2, SPI3, TIM1, TIM8
485 * @arg GPIO_AF_7: AOP2_OUT, CAN, COMP3_OUT, COMP5_OUT, COMP6_OUT, USART1,
487 * @arg GPIO_AF_8: COMP1_OUT, COMP2_OUT, COMP3_OUT, COMP4_OUT, COMP5_OUT,
489 * @arg GPIO_AF_9: AOP4_OUT, CAN, TIM1, TIM8, TIM15.
490 * @arg GPIO_AF_10: AOP1_OUT, AOP3_OUT, TIM2, TIM3, TIM4, TIM8, TIM17.
491 * @arg GPIO_AF_11: TIM1, TIM8.
492 * @arg GPIO_AF_12: TIM1, HRTIM.
493 * @arg GPIO_AF_13: HRTIM, AOP2_OUT.
494 * @arg GPIO_AF_14: USBDM, USBDP.
495 * @arg GPIO_AF_15: OUT.
496 * @note The pin should already been configured in Alternate Function mode(AF)
497 * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
498 * @note Refer to the Alternate function mapping table in the device datasheet
499 * for the detailed mapping of the system and peripherals alternate
503 void GPIO_PinAFConfig(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_PinSource
, uint8_t GPIO_AF
)
505 uint32_t temp
= 0x00;
506 uint32_t temp_2
= 0x00;
508 /* Check the parameters */
509 assert_param(IS_GPIO_ALL_PERIPH(GPIOx
));
510 assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource
));
511 assert_param(IS_GPIO_AF(GPIO_AF
));
513 temp
= ((uint32_t)(GPIO_AF
) << ((uint32_t)((uint32_t)GPIO_PinSource
& (uint32_t)0x07) * 4));
514 GPIOx
->AFR
[GPIO_PinSource
>> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource
& (uint32_t)0x07) * 4));
515 temp_2
= GPIOx
->AFR
[GPIO_PinSource
>> 0x03] | temp
;
516 GPIOx
->AFR
[GPIO_PinSource
>> 0x03] = temp_2
;
535 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/