How to improve your code proficiency in embedded development

When you start learning embedded systems or microcontroller systems, most people’s learning methods are to find video tutorials or books to learn by themselves, and some people may pay for a training class or something to learn from the teacher.

When you can slowly write code independently, you will feel that you have learned. In particular, after being able to complete some simple projects independently, I feel that I can be a teacher. Slowly, with the exercise of various projects, the ability to write code will slowly improve. At the end of the writing, you will find that in fact, many projects are recombined with different codes, and after a function is written, it can be used in each project. For example, after writing a serial port function, the project uses the serial port, directly copies the previously written code and finishes, and finally the development project becomes a process of building blocks.

At this time, the basic writing of code seems to have lost the passion for learning at the beginning, writing code has become a manual work, basically do not have to use their brains, are copied and pasted. The ability to write code seems to be difficult to break through, and the bottleneck is felt. At this time, it is difficult to find videos or books or teachers to teach themselves how to break through this bottleneck. At this point, if you want to go further with your code level, how to do it, I will share my experience below.

After writing a feature yourself, if you want to know how you wrote it, the best way is to find a code that someone else wrote with the same function to compare. See what the difference is between what you write and what others write. When looking for code, you can’t just find one online, because you don’t know if the code you find is written by a beginner or a master. Easy to be misled, then you have to find everyone to recognize, the code style is better to compare. I usually find code that is an atom and a fire. Let’s analyze and compare it with a simple LED code.

First, look at the code of an atom

This is the code for led.h
How to improve your code proficiency in embedded development

This is the code for led.c

How to improve your code proficiency in embedded development

The bit operations used in led.h are defined in the sys.h header file.

How to improve your code proficiency in embedded development

This initialization code looks very concise, first enabling the used IO port clock, and then initializing the corresponding IO port. In the initialization function, you can see at a glance that the two LEDs are PA2 and PD8 ports. The code is very easy to understand.

Next look at the code
header file for a fire bsp_led.h
How to improve your code proficiency in embedded development

C file bsp_led.c

How to improve your code proficiency in embedded development

First of all, the difference can be found from the file name, the upper is led.h, the lower is bsp_led.h. With a bsp added in front, what does this bsp mean? BSP is the board-level support package. That is, this file is related to the current hardware system. Then look at the contents of the header file, and find that in the header file, the IO port and specific pin used in the LED have redefined a name. Then when the LED port is initialized in the C file, the new name is used. The initialization process is also to first enable the clock of the IO port that can be used, and the relevant IO port used in initialization. However, it is difficult to see from the initialization code which port the LED port is currently used. The code looks a bit strenuous.

Why is a simple LED function, but the style is so different? Which of these two styles is closer to your own code? From my own point of view, I liked the above style at the beginning, it was simple and clear, and the code was easy to write. But as the time spent writing code increased, I gradually began to like the following style. The code has also gradually changed from the above style to the following style, and the differences between the two styles will be discussed separately below.

When you first start writing code, the first style is more suitable, it is relatively simple to write, easy to understand, and it is more convenient and fast to find when there is a problem with the code. However, with the increase of the project and the various changes in the requirements, it will be found that the first code is more troublesome to modify, such as the LED port has changed, then the header file and the initialization function need to be modified, the change must be modified several places at a time, and it is easy to miss some places when modifying, resulting in various problems in debugging continuously, increasing the difficulty of debugging. At this time, I thought, can I modify a place, I can modify the place where the LED port is used. This is where the second type of code is needed. In the header file, the ports and clocks used by leDs are abstracted to redefine a name. When initializing the code, only the LED port is initialized, as for which port the LED corresponds to, it is directly defined in the header file. At this time, the initialization code does not need to be modified.

The simple understanding is that the first method is simple to write, for specific IO mouths, but once the project has been changed, it is more troublesome to modify and port. The second method is complex to write, but is an abstract method whose initialization function has nothing to do with the concrete IO port. It is more convenient when the project changes or migrates. hmi panel

These two methods have their own advantages and disadvantages, you can choose different methods according to your actual situation, you can also combine the two methods. For example, I think that the bitband operation of the first method is relatively simple, and it is more convenient to define all bit operations in one file. The bitband operations of the second method are unloaded in the header file, which is more cumbersome and complicated to write. However, the second method abstracts the IO port and pin used by the LED port. Then you can use the bitband operation of the first type and the port abstraction of the second type on your own project at the same time.

In this way, through the analysis of other people’s code, and then combined with their own code, compare the advantages and disadvantages of different codes, and then modify their own code. Constantly imitating the excellence of others, their own code will be written better and better. If you feel that your technology is better, then you can look at the source code of the library function, or the source code of the operating system, and see how the official source code abstracts different types and models of microcontrollers into a unified function. If one day you see the advantages and disadvantages of the code at a glance after seeing other people’s code, then it means that your level is already very high and you have broken through your own bottleneck.