1.
void GPIO_DeInit(GPIO_TypeDef* GPIOx);
First, let’s take a look at this function. We can see that the parameter of this function is a GPIO group parameter (GPIOB, GPIOC, GPIOA, etc.),Now, let’s enter the definition of this function in the standard peripheral library.
void GPIO_DeInit(GPIO_TypeDef* GPIOx){ /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); if (GPIOx == GPIOA) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE); } else if (GPIOx == GPIOB) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, DISABLE); } else if (GPIOx == GPIOC) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, DISABLE); } 。。。。 。。。。 。。。。 }
Function Description:
The purpose of this function is:
To reset the specified GPIO port (such as GPIOA, GPIOB…) to its power-on default state.
In other words:
-
Clear all previous configurations of the port (input/output mode, pull-up/pull-down, multiplexing function, output value, etc.);
-
Reset its register values back tothe reset default values (Reset Value)..
This is equivalent to “clearing this GPIO before reinitializing it”.
Key Point: Use RCC peripheral reset function to clear configurations
In STM32, all peripherals (including GPIO) are connected to RCC (Clock Controller). RCC has a very important function:
It can “soft reset” any peripheral by setting the reset bit.
For example:
-
<span><span>RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE)</span></span>means:Put GPIOA into reset state; -
Then execute
<span><span>RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE)</span></span>means:Exit reset state.
This “reset-release” operation will clear the GPIOA registers, restoring them to factory default values.
Therefore, this function is rarely used.
2.
void GPIO_StructInit(GPIO_InitTypeDef*GPIO_InitStruct);
The parameter is a pointer to a structure. Next, we go to the standard peripheral library to see the definition of this function.
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct){ /* Reset GPIO init structure parameters values */ GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All; GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN; GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStruct->GPIO_OType = GPIO_OType_PP; GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;}
<span><span>GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;</span></span>
This means:
By default, selectall pins (Pin0~Pin15)..
In other words, the default structure assumes “I want to configure all pins of the entire port”, but in practice, you can modify it before use, for example:
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; // Only configure PA5 pin
<span><span>GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;</span></span>
The default mode is:
Input mode (Input Mode).
The GPIO modes in STM32 mainly include the following:
| Mode | Meaning |
|---|---|
<span>GPIO_Mode_IN</span> |
Input mode |
<span>GPIO_Mode_OUT</span> |
Push-pull output |
<span>GPIO_Mode_AF</span> |
Multiplexing function (e.g., USART, SPI) |
<span>GPIO_Mode_AN</span> |
Analog input (for ADC) |
The default is input mode forsafety reasons (to prevent accidental output causing short circuits or interference).
<span><span>GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;</span></span>
Set the default pin output speed to:
2MHz (low-speed mode)
This corresponds to the switching speed of the GPIO output signal. Different speeds correspond to different power consumption and interference levels:
| Parameter | Speed | Characteristics |
|---|---|---|
<span>GPIO_Speed_2MHz</span> |
Low speed | Low power, low interference (default) |
<span>GPIO_Speed_25MHz</span> |
Medium speed | Balanced for general peripheral use |
<span>GPIO_Speed_50MHz</span> |
High speed | Suitable for high-speed communication ports (SPI, etc.) |
<span>GPIO_Speed_100MHz</span> |
Ultra-high speed | Used for clock or high-speed peripheral lines |
<span><span>GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;</span></span>
Set the output type to:
Push-pull output (Push-Pull)
The characteristics of push-pull output are:
-
Can actively output high or low levels;
-
Strong driving capability.
The other type isopen-drain output (Open-Drain), used in situations like I²C bus where multiple devices share the line.
<span><span>GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;</span></span>
Set pull-up/pull-down to:
No pull-up or pull-down (No Pull-up or Pull-down)
The input pins of STM32 are in a “high-impedance state”. If no pull-up or pull-down is set, the level mayfloat unpredictably (floating)..
The options are as follows:
| Parameter | Function |
|---|---|
<span>GPIO_PuPd_NOPULL</span> |
No internal pull-up/pull-down |
<span>GPIO_PuPd_UP</span> |
Pull-up |
<span>GPIO_PuPd_DOWN</span> |
Pull-down |
Example Usage Scenarios
In practical applications, it is usually used like this:
GPIO_InitTypeDef GPIO_InitStructure;/* First call the structure initialization function to fill in default values */GPIO_StructInit(&GPIO_InitStructure);/* Then modify some fields as needed */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;/* Finally apply to the specific port */GPIO_Init(GPIOA, &GPIO_InitStructure);
This can avoid forgetting to initialize certain fields in the structure, reducing program errors.
3.
void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin){ __IO uint32_t tmp = 0x00010000; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GPIO_PIN(GPIO_Pin)); tmp |= GPIO_Pin; /* Set LCKK bit */ GPIOx->LCKR = tmp; /* Reset LCKK bit */ GPIOx->LCKR = GPIO_Pin; /* Set LCKK bit */ GPIOx->LCKR = tmp; /* Read LCKK bit*/ tmp = GPIOx->LCKR; /* Read LCKK bit*/ tmp = GPIOx->LCKR;
Background Principle — GPIO Lock Mechanism
Each GPIO port in STM32 has a dedicated register: GPIOx_LCKR (Lock Register)
The function is: Once locked, the configuration of this pin’s mode, pull-up/pull-down, output type, speed, etc., will be frozen. Even if you later call <span>GPIO_Init()</span> or directly write to the register, youcannot modify! You must reset the MCU to unlock it again.
So this function is used to lock a specified GPIO pin, and it is rarely used.
4.
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF);
1. Function Overview
<span>GPIO_PinAFConfig()</span>is used to selectthe peripheral multiplexing function for a GPIO pin.
In simple terms, STM32 pins are very “versatile”; a pin can serve as a regular IO or be connected to various peripherals. For example:
| Pin | Optional Functions |
|---|---|
| PA9 | Regular output / USART1_TX / TIM1_CH2 / … |
| PB6 | GPIO / I2C1_SCL / TIM4_CH1 / USART1_TX / … |
<span>GPIO_PinAFConfig()</span> tells the chip:
“Hey, PA9, you are no longer a GPIO; you are now USART1_TX!”
2. Function Prototype Analysis
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF);
| Parameter | Description |
|---|---|
<span>GPIOx</span> |
The port to be configured (e.g., GPIOA, GPIOB…) |
<span>GPIO_PinSource</span> |
Pin number (0~15, corresponding to Pin0~Pin15) |
<span>GPIO_AF</span> |
The selected multiplexing function number (AF0 ~ AF15) |
3. STM32 Multiplexing Function Structure Diagram
The function of each pin is controlled by registers:
GPIOx_AFRL register → Controls Pin0~Pin7
GPIOx_AFRH register → Controls Pin8~Pin15
Each pin occupies 4 bits to select the AF function number:
| AF Value | Peripheral Function (varies slightly by model) |
|---|---|
| AF0 | System functions (e.g., MCO, RTC, SWD) |
| AF1 | TIM1 / TIM2 |
| AF2 | TIM3 / TIM4 / TIM5 |
| AF3 | TIM8 / TIM9 / TIM10 / TIM11 |
| AF4 | I2C1 / I2C2 / I2C3 |
| AF5 | SPI1 / SPI2 |
| AF6 | SPI3 / I2S |
| AF7 | USART1 / USART2 |
| AF8 | USART6 / UART4 / UART5 |
| AF9 | CAN1 / CAN2 / TIM12 / TIM13 / TIM14 |
| AF10 | OTG_FS / OTG_HS |
| AF11 | Ethernet |
| AF12 | FSMC / SDIO / OTG_HS |
| AF13 | DCMI |
| AF14 | Undefined |
| AF15 | EVENTOUT |
Complete Multiplexing Configuration Process
Assuming we want to use PA9 as USART1_TX:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Enable GPIO clock
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Change mode to multiplexing function
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF7_USART1); // Configure AF mapping
Both steps are essential:
-
<span>GPIO_Mode_AF</span>— Switch the pin to multiplexing mode; -
<span>GPIO_PinAFConfig()</span>— Tell it who to multiplex to.