Mastering STM32: A Comprehensive Guide for Beginners

Mastering STM32: A Comprehensive Guide for Beginners

Mastering STM32: A Comprehensive Guide for Beginners

Learning microcontrollers just for the sake of it is not the correct approach.
You may ask: How to systematically start learning STM32?
This is fundamentally a wrong question. If you know how to use the 8051 and can write C, then there is no need to deliberately learn STM32.
What you should consider is what I can achieve with STM32?
Why use STM32 instead of 8051? Is it because the 51’s frequency is too low to meet computational needs? Is it because the 51 has too few pins to accommodate many peripherals’ IO? Is it because the 51’s power consumption is too high, and the battery can’t hold out? Is it because the 51’s functionality is too weak, while you need to use SPI, I2C, ADC, DMA? Is it because the 51’s memory is too small while you need to store too much?
Mastering STM32: A Comprehensive Guide for Beginners
When you need to use certain functions of STM32 that the 51 cannot implement, then there is no need to study STM32 deliberately; you will directly look for the usage methods of specific aspects of STM32. For example, using SPI protocol network cards, serial communication, RTOS, etc…

01

Steps to Learn from Novice to Expert

Mastering STM32: A Comprehensive Guide for Beginners
We assume that everyone has a certain understanding of STM32 books or documents. If not, please read the STM32 documentation immediately to obtain the most basic knowledge points.
Students with good English should not think they are great and can only read English documents. After all, you are Chinese, and what you are most familiar with and understand best is still Chinese. Reading English is still slower than reading Chinese; what we want is the shortest time, not pursuing memorizing all details in a short time. Of course, if it’s a thesis, it is still beneficial to read the original English version.
When STM32 processors entered the domestic market, ST official (or third-party) promotion work was done very well. A large number of English documents were translated to cater to the thinking of many domestic engineers.
When learning, focus on two important documents: “STM32F103xxx Reference Manual” and “STM32 Firmware Library User Manual”. This is for students with ample time and energy; it is recommended to download the documents you need to refer to for more information.
When reading the “STM32F103xxx Reference Manual”, it is important to note that you do not need to read it all — there is no time for that. It is recommended to select readings, but the first few chapters are essential. Topics such as memory and bus architecture, power control, backup registers, reset and clock control, general and multiplexing functions I/O, interrupts, and timers must be read carefully.
The later chapters describe the design of specific functional modules. If we use a certain module, we can read that module. For example, when using ADC, we need to read Chapter 10 on ADC. No other examples are needed. I believe every beginner has their own research direction and judgment.
Reading the “STM32 Firmware Library User Manual” is mainly to simplify programming. STM32 provides us with a very good firmware function library, and we just need to call it. Of course, we can also choose not to touch these firmware libraries — it is said that using them will reduce code efficiency, which is reasonable. Many netizens have also written their own code without using the firmware library functions. How to choose depends on your choice.
Here I mainly emphasize that when reading the “STM32 Firmware Library User Manual”, the first few chapters are also essential reading. For example, the first chapter on naming conventions and coding rules in the document and library specifications need attention. The second chapter is the most critical; I hope everyone reads it thoroughly. The second chapter describes the architecture of the firmware library, how we use the firmware library, and so on. With the foundation of the second chapter, we can write our own code using the firmware library. From the fourth chapter onwards, you can read according to your needs. In fact, the subsequent chapters describe what functions each module has and how to use each function.
Regarding the subsequent chapters, it is recommended to be familiar with GPIO library functions, interrupt part library functions, reset and clock setting library functions, as they are commonly used.
The two documents mentioned above are already enough for you to read. Haha. I hope you can gain a lot of basic knowledge about STM32 from them.

02

Set a Good Two-Week Introduction Plan

Mastering STM32: A Comprehensive Guide for Beginners
(1) Here, the so-called “introduction” refers to the ability to understand and master some commonly used STM32 peripherals. If you really want to master a processor, two weeks do not explain much. It can only be said that you have some understanding. But this is enough for us beginners.
(2) Here, the so-called “two weeks” varies according to each person’s time arrangement.
If you have ample time to study every day, then you can set a goal to quickly be able to independently conduct simple STM32 development.
If you only have spare time to study STM32, it is recommended to arrange time according to your specific situation. After all, if the planned time is too tight, it will have adverse effects, and you will fall into a vicious cycle, which we want to avoid.
However, it is recommended that regardless of whether you have ample time or not, you must make a plan for yourself!!
Here is a thought process for reference.
Step One,Install the STM32 learning software, such as J-Link, Keil for ARM (MDK), ISP (if you need to download from the serial port). The detailed steps for installing this software can be found in the corresponding tutorials we provide.
Step Two,Select some example HEX files, such as the LED example HEX file, download it to the STM32 development board, and observe the blinking of the two LED lights. This part of the operation can refer to the corresponding tutorials we provide.
In fact, the above two steps are just to familiarize yourself with the software tools you will use. It’s just a phase of getting a feel for it. We have not yet started learning STM32!
Step Three,Prepare several commonly used documents, such as the “STM32 User Manual”, “STM32 Firmware Library User Manual”, etc., for daily reference. These documents can be found in the chip manual directory on the CD.
Step Four,Start looking at how the examples are written, see how the examples are written, and whether you can modify the examples to achieve the desired effect?
Step Five,Should you try porting Ucos-II?
Congratulations, at this point, you can freely conduct independent development. The last step is to set a goal (project) for yourself and implement it!
Again, the above is just a thought process for learning STM32, for everyone’s reference. Below are the key steps listed earlier; I hope everyone can quickly get started.
Step 1: Familiarize Yourself with Debugging Software
For beginners, we need to install at least two software: J-Link driver software and MDK (the original Keil) software.
You can refer to the Shenzhou development board user manual during the installation process of these two software; I will not repeat it here. You can refer to our tutorials on “How to Install J-Link Driver Software” and “How to Install MDK (Keil) Software”. How to verify that you are already familiar with the debugging software operation? It’s simple; the Shenzhou STM32 development board comes with many HEX format files; you can select some HEX files to observe the running results.
The goal of this step: familiarize yourself with the debugging software. If there are problems burning HEX, you can simply determine the problem and solve it independently.
Step 2: GPIO Programming
This is the first time you encounter programming with a firmware library, and you must grit your teeth and understand the firmware library. It is recommended to use the firmware library as much as possible rather than avoiding the firmware library and writing code yourself — this only happens during learning. In actual projects, there will be hundreds or thousands of lines of code; how can you write them all by yourself? Calling functions from the firmware library to complete the task is the way to go.

Programming GPIO itself is actually very simple

1. Set the GPIO pin to input or output mode. When we write the code for lighting, we generally set it to push-pull output mode.
2. Operate the register, setting it to 1 or clearing it — this step is already provided by the firmware library with dedicated GPIO_SetBits and GPIO_ResetBits functions; we just need to call them to achieve setting and clearing the IO port.
3. Implement various patterns of LED flashing to familiarize yourself with the GPIO programming process.
The goal of this step: familiarize yourself with the debugging software. If there are problems burning HEX, you can simply determine the problem and solve it independently.
Step 3: Start a New In-depth Study of STM32
After familiarizing yourself with the debugging software and GPIO programming, I believe you have a certain understanding of STM32.
You should at least know how to use the STM32 firmware library to write code. At this stage, you will encounter serial port programming, TFT LCD screen driver programming, timer programming, serial peripheral interface (SPI) programming, memory programming, SD card and file system porting, USB read/write, UCOS porting, etc. If you have the energy, you can also study other peripherals.

02

Detailed Explanation of the 8 Working Modes of GPIO

Mastering STM32: A Comprehensive Guide for Beginners
Push-Pull Output
Can output high and low levels, connecting digital devices; the push-pull structure generally refers to two transistors controlled by two complementary signals, where one transistor is always on while the other is off. The high and low levels are determined by the IC’s power supply.
The push-pull circuit consists of two identical transistors or MOSFETs, existing in the circuit in a push-pull manner, each responsible for amplifying the positive and negative half cycles of the waveform. During circuit operation, only one of the two symmetrical power switches is on at a time, so conduction loss is small, and efficiency is high. The output can both source current to the load and sink current from the load. The push-pull output stage enhances the circuit’s load capacity and improves switching speed.
Open-Drain OutputThe output terminal is equivalent to the collector of a transistor; to achieve a high-level state, a pull-up resistor is needed. It is suitable for current-type drives, with a relatively strong ability to absorb current (generally within 20mA). The open-drain form of the circuit has the following characteristics:
1. Utilize the driving capacity of external circuits to reduce the drive within the IC. When the internal MOSFET of the IC is on, the driving current flows from the external VCC through the pull-up resistor, the MOSFET to GND. The IC only needs a small gate drive current.
2. Generally, open-drain is used to connect devices at different levels, matching levels. Because the open-drain pin cannot output a high level without an external pull-up resistor, if a high-level output function is needed, a pull-up resistor must be connected. A great advantage is that by changing the voltage of the pull-up power supply, the transmission level can be changed. For example, adding a pull-up resistor can provide TTL/CMOS level output, etc. (The resistance value of the pull-up resistor determines the speed of logic level conversion. The larger the resistance, the slower the speed and the lower the power consumption, so the selection of load resistance must consider both power consumption and speed).
3. Open-drain output provides a flexible output method, but it also has its weaknesses, causing rise time delays. Because the rise time is charged through the external pull-up passive resistor to the load, if a small resistor is selected, the delay is small but power consumption is high; conversely, the delay is large but power consumption is low. Therefore, if there are timing requirements, it is recommended to use falling edge output.
4. Multiple open-drain outputs can be connected to one line. Through a pull-up resistor, without adding any components, a “AND logic” relationship can be formed, i.e., “line AND”. It can be simply understood as: when all pins are connected together, with an external pull-up resistor, if one pin outputs logic 0, it is equivalent to grounding, and the parallel circuit with it is “shorted by a wire”, so the external circuit’s logic level becomes 0; only when all are high levels, the result of the AND is logic 1.
Regarding push-pull output and open-drain output, let’s summarize with the simplest diagram: the left side of the diagram is the push-pull output mode, where the comparator outputs a high level, the lower PNP transistor is off, while the upper NPN transistor is on, outputting a high level. When the comparator outputs a low level, it is precisely the opposite; the PNP transistor is on, connecting to ground, outputting a low level. The right side can be understood as an open-drain output form, which requires a pull-up.
Mastering STM32: A Comprehensive Guide for Beginners
Floating Input
For floating input, I have never found a very authoritative explanation, so I can only understand it from the following diagram.
Mastering STM32: A Comprehensive Guide for Beginners
Since floating input is generally used for external button inputs, combined with the input circuit shown in the diagram, I understand that in the floating input state, the IO level state is uncertain and completely determined by external input. If the pin is floating, reading the port level is uncertain.

Pull-Up Input/Pull-Down Input/Analog Input

These concepts are easy to understand and can be easily read from the literal meaning.

Multiplexed Open-Drain Output/Multiplexed Push-Pull Output

It can be understood as the configuration situation when the GPIO is used as a second function (i.e., not used as a general IO port).

Summary of Choosing IO Modes in STM32

1. Floating Input GPIO_IN_FLOATING — Floating input, can be used for KEY recognition, RX1;

2. Pull-Up Input GPIO_IPU — IO internal pull-up resistor input;

3. Pull-Down Input GPIO_IPD — IO internal pull-down resistor input;

4. Analog Input GPIO_AIN — Used for ADC analog input, or low power consumption during sleep;

5. Open-Drain Output GPIO_OUT_OD — IO output 0 connects to GND, IO output 1 is floating, requiring an external pull-up resistor to achieve high-level output. When output is 1, the IO state is pulled high by the pull-up resistor, but due to the open-drain output mode, the IO can also be changed to low by external circuits. It can read IO input level changes, achieving dual-directional functionality of C51 IO;

6. Push-Pull Output GPIO_OUT_PP — IO output 0 connects to GND, IO output 1 connects to VCC, reading input value is unknown;

7. Multiplexed Push-Pull Output GPIO_AF_PP — Internal peripheral function (I2C SCL, SDA);

8. Multiplexed Open-Drain Output GPIO_AF_OD — Internal peripheral function (TX1, MOSI, MISO, SCK, SS);

02

STM32 Configuration Example

Mastering STM32: A Comprehensive Guide for Beginners

1. Use open-drain output _OUT_OD with a pull-up resistor for analog I2C, allowing correct output of 0 and 1; when reading values, first use GPIO_SetBits(GPIOB, GPIO_Pin_0); to pull high, then read the IO value using GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);

2. If there is no pull-up resistor, the IO defaults to high; to read the IO value, you can use pull-up input _IPU, floating input _IN_FLOATING, and open-drain output _OUT_OD;

There are usually 5 ways to use a pin function

Their configuration methods are as follows:

1. As a regular GPIO input: Configure the pin as floating input, weak pull-up input, or weak pull-down input as needed, and do not enable all multiplexing function modules corresponding to this pin.

2. As a regular GPIO output: Configure the pin as push-pull output or open-drain output as needed, and do not enable all multiplexing function modules corresponding to this pin.

3. As a regular analog input: Configure the pin as analog input mode and do not enable all multiplexing function modules corresponding to this pin.

4. As an input for built-in peripherals: Configure the pin as floating input, weak pull-up input, or weak pull-down input as needed, and enable a corresponding multiplexing function module for this pin.

5. As an output for built-in peripherals: Configure the pin as multiplexed push-pull output or multiplexed open-drain output as needed, and enable all multiplexing function modules corresponding to this pin.

Note: If multiple multiplexing function modules correspond to the same pin, only one can be enabled, and the others must remain disabled. For example, to use the USART3 function of pins 47 and 48 of STM32F103VBT6, pin 47 needs to be configured as multiplexed push-pull output or multiplexed open-drain output, and pin 48 needs to be configured as a certain input mode while keeping I2C2 disabled. If pin 47 of STM32F103VBT6 is to be used as TIM2_CH3, TIM2 must be remapped, and then the corresponding pin must be configured according to the multiplexing function.

———— / END / ————

Mastering STM32: A Comprehensive Guide for Beginners

Mastering STM32: A Comprehensive Guide for Beginners

Leave a Comment

×