1.0 The STM32 input and output pins have the following 8 possible configurations: (4 inputs + 2 outputs + 2 multiplexed outputs)
1) Floating Input _IN_FLOATING
2) Pull-Up Input _IPU
3) Pull-Down Input _IPD
4) Analog Input _AIN
5) Open-Drain Output _OUT_OD
6) Push-Pull Output _OUT_PP
7) Multiplexed Push-Pull Output _AF_PP
8) Multiplexed Open-Drain Output _AF_OD
1.1 In the output mode of the I/O ports, there are 3 output speeds to choose from (2M, 10M, 50MHz). This speed refers to the response speed of the I/O port driving circuit, not the output signal speed. By selecting the speed, different output driving modules can be chosen to achieve optimal noise control and reduce power consumption.
1.1.1 For serial ports, if the maximum baud rate is only 115.2K, then using a 2M GPIO pin speed is sufficient, which saves power and reduces noise.
1.1.2 For I2C interfaces, if using a 400K baud rate, to leave a greater margin, the 2M GPIO pin speed may not be enough, at which point a 10M GPIO pin speed can be selected.
1.1.3 For SPI interfaces, if using an 18M or 9M baud rate, a 10M GPIO pin speed is clearly insufficient, requiring a 50M GPIO pin speed.
1.2 When the GPIO port is set to input, the output driving circuit is disconnected from the port, so the output speed configuration is meaningless.
1.3 During the reset period and just after reset, the multiplexed function is not enabled, and the I/O ports are set to floating input mode.
1.4 All ports have external interrupt functionality; to use the external interrupt line, the port must be configured to input mode.
1.5 The GPIO port configuration has a locking feature; once the GPIO port is configured, the configuration can be locked by the program until the next chip reset to unlock it.
2 How to configure the IO ports used by the internal peripherals in STM32
A peripheral is activated (turned on) after a. configuring the input clock and b. initializing; c. If using the input and output pins of this peripheral, the corresponding GPIO ports need to be configured d. then the peripheral is configured in detail.
There are three cases corresponding to the input and output functions of the peripherals:
2.1: If the corresponding pin of the peripheral is output: it needs to select the corresponding pin as a multiplexed push-pull output or multiplexed open-drain output based on the configuration of the peripheral circuit.
2.2: If the corresponding pin of the peripheral is input: then based on the configuration of the peripheral circuit, floating input, pull-up input, or pull-down input can be selected.
2.3: For the ADC corresponding pin: configure the pin as an analog input.
If the port is configured as a multiplexed output function, the pin is disconnected from the output register and connected to the output signal of the on-chip peripheral. Once the pin is configured as a multiplexed output function, if the peripheral is not activated, its output will be uncertain.
3 General IO Port (GPIO) Initialization:
3.1 GPIO Initialization
3.1.1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|B|C, ENABLE): Enable the APB2 bus peripheral clock
3.1.2 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|B|C, DISABLE): Release GPIO reset
3.2 Configure each PIN port
3.3 GPIO initialization completed 