Embedded study notes - understanding the GPIO port of STM32

foreword

I have written a lot of long-winded words before. Starting from this article, I officially entered the actual development process of STM32. This series uses registers for development, operates from the bottom layer, and realizes the corresponding functions by viewing the configuration registers in the manual. The whole process will be recorded. It's a bit bloated, and the structure of the full text is probably that the first part introduces theoretical knowledge, and the second half introduces practical operations and codes. The author tries to be as concise as possible. If there are any deficiencies in the article, I hope you can correct me.

Know GPIO

What is GPIO

First of all, we need to know what GPIO is. For this kind of problem, we still use the old routine, first ask "C knows".
insert image description here
I feel that it is not clear enough, let's look at Baidu Encyclopedia's answer.
GPIO (English: General-purpose input/output), the abbreviation of general-purpose input and output, the function is similar to P0-P3 of 8051, its pins can be freely used by users through program control, and PIN pins can be used as general-purpose inputs according to practical considerations ( GPI) or general output (GPO) or general input and output (GPIO), such as clk generator, chip select, etc.
Since a pin can be used for input, output, or other special functions, there must be registers to select these functions. For input, it must be possible to determine the level of the pin potential by reading a certain register; for output, it must be possible to make the pin output high or low potential by writing to a certain register; for other special functions, there are other registers to control them.

Well, after reading Baidu's explanation, students who have studied 51-series MCUs or other MCUs must have a rough answer. The functions are similar to 51's P0-P3, and they can do input, output, or both. Then he must be the same as The 51 single-chip microcomputer is almost pin-out.
For students who have never studied other MCUs, they may still be confused. I know you are confused but don’t be confused. Let me give you an answer.
Generally speaking, GPIO is most of the pins on STM32, as shown in the figure below. Show the majority in the red circle.
insert image description here
Pay attention to extracting keywords. I said most, not all, which means that the two cannot be equated.

不全是
GPIO
引脚

So what is the difference between the two? Let's look at another picture, which specifically marks the names corresponding to each pin of the above chip.
insert image description here
Observing carefully, the feet here seem to be quite regular.
1. Most of them are PA0-15, PB0-15...PE0-15. This part is the GPIO mentioned above, that is to say, the label PXX in STM32 is GPIO ports, they can be used for input, output, or both;
2. The remaining part is VDD, VSS, VREF, VDDA, VSSA, etc. This part was mentioned in yesterday's hardware introduction, which is STM32 Various power packs, they are the interfaces that provide energy input for the entire microcontroller;
3. There are also NRST, BOOT0, these are the pins written in the smallest system yesterday.
Well, here, we have an idea of ​​what GPIO is, and then solve the second problem

What is the use of GPIO

Let’s take a look at the picture. We know that STM32 has various internal and external devices. GPIO is also a kind of internal and external devices, but it has a big difference from other internal and external devices. As can be seen from the figure below, GPIO The position is on the periphery of the whole chip, only it can contact with the outside world, all the on-chip peripherals of the whole chip and the core must pass through the GPIO to exchange information with the outside, it is the channel connecting the inside and outside of the microcontroller, it can be The information of the core and other on-chip peripherals is transmitted (output), and the information of off-chip peripherals can also be transmitted to the core or on-chip peripherals (input), sometimes taking into account the functions of input and output.
insert image description here
Well, we also have a basic understanding of what is the use of GPIO. Next, we need to get the key content of today, how to use it.

How to use GPIO

Before exploring how to use it, it is necessary to add some relevant content about GPIO on STM32:

The naming and quantity of GPIO on STM32

Regarding the issue of GPIO naming and quantity on STM32, this was mentioned in the section of the chip manual before.
The port number of GPIO is from PA, PB——PI; there are 9 port numbers in total, and each port number has 16 pin numbers from 0 to 15. That is to say, theoretically, a STM32 microcontroller has 16*9=144 GPIOs.
Seeing this, some students will have problems. You have 144 GPIOs in theory, but the STM32F407VE above you only has 100 pins, and there are so many dedicated pins for power supply and the smallest system. You are wrong. ah.
Yes, our F407VE does not have 144 GPIOs. Do you remember what we introduced in the chip naming section before? According to different naming, the number of chip pins is different, and the number of GPIOs is omitted in the middle. 144 GPIOs is too much for most projects and is a waste of resources. This is why there are so many chip models. We must pay attention to applicability when choosing the main control in the project.
Here we take the chip manual diagram of STM32F407VE to make a detailed introduction, as shown in the figure below:
insert image description here
STM32F407VE has a total of 82 GPIO ports, and
the GPIO port numbers and pins used are:
insert image description here
insert image description here

The port number pin number Remark
GPIOA 0-15 GPIO
GPIOB 0-15 GPIO
GPIOC 0-15 GPIO
GPIOD 0-15 GPIO
GPIOE 0-15 General-purpose input and output total 80 GPIO
GPIOH 0-1 External Clock Interface

It can be seen that this MCU has a total of 80 GPIO ports. If the internal clock is used, two external clock pins, GPIOH0 and GPIOH1, can also be expanded.

Block diagram of GPIO port (emphasis)

Next, it is the first big focus of this article, about the explanation of the GPIO block diagram, through this diagram, we need to basically understand, 1. The input and output modes of the GPIO port; 2. The corresponding role of each mode; 3. Which on-chip peripherals and registers need to be used when using different modes of GPIO.
The introduction to the structure diagram of GPIO in the programming manual is shown in the figure below. Officially, all modes and input and output are integrated on one diagram. For the convenience of understanding, let’s split it up.
insert image description here

Input block diagram analysis

The above figure is slightly split, as shown in the figure below, which is the input block diagram of GPIO.
First of all, start from the far left. From the figure, we can see that there are three ways to finally input the MCU core. The first is through the analog input of the ADC, and the second is through the multiplexing input of other on-chip peripherals. The third type is directly input to the core after passing through the input data register at the bottom. Next, let's take a look at the introduction of these three types of input.
insert image description here

Three input modes

1. Analog input: The analog input mode, as the name implies, means that the signal input from the off-chip peripherals is an analog quantity.
Here we need to distinguish between analog and digital quantities. We all know that whether it is a single-chip microcomputer or a computer, what they can recognize and process is always a binary number, that is, the data string of 0101000..., the data composed of 1010100 ... The string is a digital quantity. In layman's terms, it is 0 and 1. My digital quantity 0 and 1 correspond to the low level and high level of the analog voltage; so what is an analog quantity, such as our common voltage, temperature, and humidity? And so on, these are called analog quantities. In short, the quantities that need to be converted by a certain circuit before they can be recognized by the microcontroller and computer are analog quantities.
It is precisely because the core of the single-chip microcomputer cannot directly recognize the input of the analog quantity, so after the analog quantity signal of the peripheral device enters the chip, it needs to go through the on-chip peripheral device that converts the analog quantity to the digital quantity, the light green ADC in the above picture. That is to say, this input mode is specially used to process analog signals. When we determine that the input is analog, we must configure GPIO as analog input mode .
2. Multiplexed input: Regarding the multiplexed input mode, you can compare it after you have figured out the above analog input. Since an input path has been specially opened up for ADC, other on-chip peripherals naturally have to fight for it, so there is Multiplexing input mode, the biggest difference between this multiplexing input and analog input is that the signals it receives are all digital quantities, but these digital quantities are defined, packaged, packaged, and inputted according to different on-chip peripheral requirements. Before the kernel, it needs to be parsed by the on-chip peripherals of these corresponding protocols, unpacked, and then handed over to the kernel for processing. On-chip peripherals like SPI, I2C, and UART all need to use this mode. When we determine that the input quantity needs to be processed using these on-chip peripherals, we must use this mode .
3. General input:The last input mode is the universal input mode. To put it bluntly, there are only two states of input, 0 and 1. It is similar to pressing or not pressing a button, and the threshold value of a certain sensor exceeds or does not exceed the standard. It only involves on and off . This mode is used when monitoring is off .

Internal devices and their functions during GPIO input

So far, we have figured out the three input modes of GPIO, but there are still some devices that have not been mentioned in the figure. Here is a brief introduction: (from right to left)

1. Protection diode

1. When the peripheral signal comes in, it first passes through the position where there is a protective diode on the upper and lower sides. These two diodes can be used as clamping diodes, that is, to control the input voltage within the acceptable range of the IO port. When the external input When the voltage of the signal is higher than VDD_FT, the protection diode in the upper part will be turned on to take away the high voltage to protect the IO and the internal circuit of the microcontroller as shown in the figure below: Similarly, when the voltage of the input signal is lower than VSS
insert image description here
, The protection diode in the lower part will be turned on to form a reverse loop to protect the IO port from being burned.
insert image description here

2. Pull-up and pull-down resistors (configurable)

2. After the protection circuit, you will see two programmable pull-up and pull-down resistors as shown in the figure below. These two resistors are generally not used because their pull-up and pull-down capabilities are limited and not very stable. , in order to ensure the yield rate, it is generally implemented by using an external pull-down resistor on the IO port, which will be reflected in tomorrow's key input programming. Of course, some manufacturers will save costs, that is, use internal pull-up and pull-down to control the initial state of the IO port. We have encountered this phenomenon and will introduce it. Here we only need to know that this circuit can be controlled in our program.
Pull-up resistor: pull-up function, IO port keeps high level during idle time,
pull-down resistor: pull-down function, IO port keeps low level during idle time.

insert image description here

3. Schmitt trigger

3. After going through the pull-up and pull-down resistor circuit, it is a Schmitt trigger. This device does not require us to operate it in programming. It can be found that the first analog input mode does not go through it, but goes directly to the back , which also shows from the side that its role here is to convert the analog electrical signal into digital quantities of 0 and 1. When the high level passes through it, it will output 1, and when the low level passes through it, it will output 0.
insert image description here

4. Input data register (the core directly reads the high and low levels of the IO port through it)

4. The last is the input data register, its function is to receive the digital quantity converted by the Schmitt trigger, note that it is only useful in the general input mode, the digital signal of the multiplexing mode does not pass through it, we will pass it later This register is required to obtain the level status of the IO in the general input mode.
insert image description here
The introduction of the input part is here first, and I will elaborate on it later when I design related programming.

Output block diagram analysis

Regarding the analysis of the output block diagram, we still simplify the official drawings and take out the output part separately.
As shown in the figure below, it is not much different from the above input mode. Generally speaking, there are three modes: 1. General output mode including direct output and indirect output of the core; 2. After the output is processed by other on-chip peripherals Output multiplexing output mode; 3. Output analog output mode through DAC.
insert image description here

Three modes of output

1. General output mode: directly use the core to control the GPIO output high and low levels for control, without any other on-chip and external processing, the most typical one is the control of corresponding LED lights.
2. Multiplexing output mode: The multiplexing output mode is the same as the multiplexing input mode above, that is, after the core data is output, special processing needs to be performed by the on-chip peripherals, and the data is encapsulated, defined, and packaged, and then these packet data transmitted to off-chip peripherals. Generally, when there are protocol requirements such as SPI, I2C, and UART in peripherals, or when other on-chip peripherals need to be used, the multiplexed output mode needs to be used.
3. Analog output mode: The analog output mode is the same as the above analog input mode. When it is necessary to directly use the IO port to output the analog quantity, since the core is a digital quantity, it needs to be converted by DAC (digital to analog), and then through the GPIO port Output to the outside world, this mode is also dedicated to DAC, and it is not used at other times.

Internal devices and their functions during GPIO output

Regarding the entire block diagram of the output, we still look from right to left. First of all, the functions of the front protection diode and the pull-up and pull-down resistors are still the same as the input mode. We will not explain it anymore. After this part, let’s take a look at the following figure The function of the circuit in the red box.

1. Output control circuit (programmable control)

We can see that both the general output and the multiplexing output need to pass through the circuit in the red box, but the analog output does not need to pass through. After looking at the block diagram of the input part above, have you found a commonality? The input part is an analog input that does not pass through. Schmitt trigger, general-purpose input and multiplexing input need to pass through Schmitt trigger; the circuit in the red box here is just the inverse circuit of the Schmitt trigger, and its function is to combine the digital signal 0 of the core and the peripheral on the chip. 1. Switching to high and low levels. The difference is that the red box circuit here requires us to control the program. At the bottom right of the red box, there are three states: push-pull, open-drain and static, but there are only two states in the actual programming operation. Two states can be selected, one is the push-pull mode, and the other is the open-drain mode. Regarding the specific analysis of these two modes, here are two links for everyone. If you are interested, you can go to find out. If you are not interested, you only need to have the following The impression is enough,
**push-pull mode: **PMOS and NMOS in the red box can be used normally, that is to say, high and low level output can be achieved without any external circuit;
open-drain mode: all leaked , it can be understood one-sidedly that if this mode does not rely on an external pull-up circuit, it cannot output a high level, but can only output a low level. In fact, the PMOS is shielded and cannot output a high level. In actual use In the process, this mode is used only when the same IO port is used for both input and output, and there is an external pull-up resistor, otherwise, most of them use the push-pull mode.
1. Detailed explanation of the difference between GPIO push-pull output and open-drain output mode http://t.csdn.cn/M0zIQ
2. Deep understanding of GPIO (pull-up input, pull-down input, analog input, floating input, open-drain output, push-pull Output difference, take STM32 as an example) http://t.csdn.cn/HJ9Y7
3. Open-drain output and push-pull output http://t.csdn.cn/k9zGX
insert image description here

2. General output register (the core directly outputs high and low levels to it)

The last is the output data register and the set/reset register in the figure below. Regarding these two registers, the core is generally used to directly output data to the output data register to achieve high and low levels. After the core passes through the set/reset register Indirect outputs to output data registers are rarely used.
insert image description here

summary

So far, the introduction of GPIO structure and input and output modes is over. Let’s summarize, about GPIO input and output, GPIO input modes (
three types): general purpose, multiplexing, and analog;
GPIO output modes (also three ): general purpose, multiplexing, analog;
when the input and output are analog, the analog input mode and the analog output mode are used
when the input and output need to use the on-chip peripherals, the multiplexed input mode and the multiplexed output mode are used
. When GPIO directly controls GPIO for input and output, it uses general-purpose input and general-purpose output modes.
Then there is pull-up and pull-down, which are generally not used, and are implemented by external circuits. Generally, floating is used in the configuration process;
and then there are push-pull and open-drain modes in the output. Push-pull can output high and low levels without the help of external circuits. , Open-drain can only be realized with the help of an external pull-up circuit. Open-drain mode is generally used when the IO port is used as both input and output, such as I2C data lines, single-bus DS18B20, DHT11, etc.

example exercise

So much has been introduced above, let's take a look at a few practical examples:
first, the common LED circuit, obviously, in order to drive the small LED light, we need to select the output mode, there is no pull-up circuit in this circuit, in order to control the small To turn on and off the light, we need to use the push-pull output mode. Since the whole process can only be realized by controlling the GPIO through the core, the final mode selection of PA6 and PA7 is the general push-pull output mode, and the output low level light is on, and the output High level small light off.
insert image description here

Then there is the key input circuit, as shown in the figure below: First, it must be configured as a general input mode. Since there is an external pull-up circuit, the idle state is high, so there is no need to configure pull-up and pull-down.
insert image description here
Let’s look at another special case: the figure below shows the button input without external pull-up. First, it is in general input mode. Since there is no external pull-up, we need to ensure that the idle state is controllable and can effectively detect the pressed low level. At this time, An additional one-step pull-up mode is required.
insert image description here
The other is to use the multiplexing mode when using on-chip peripherals such as IIC or SPI.
insert image description here
When the GPIO uses the ADC and DAC functions in the block diagram below, we need to configure it in analog mode.
insert image description here

Summarize

This article mainly introduces what the GPIO of STM32 is, what it is used for, and how to use it. After reading the above, let's take a look back at the official GPIO function description. It has eight modes in total, which are actually what we mentioned above. The combination of general purpose, multiplexing, analog and input and output, as well as push-pull open drain, pull-up and pull-down.
insert image description here

The focus of this article

1. Understand the GPIO naming rules of STM32, and be able to query the number of corresponding GPIO ports through the corresponding chip manual;
2. Familiar with the input and output block diagram of STM32, master the characteristics of various modes, and know what kind of peripherals should use what kind of mode to to operate.

Guess you like

Origin blog.csdn.net/qq_41954556/article/details/129327196