Reprinted please indicate the source
Author: Pony
I have been talking about this topic for countless times. I only talk about embedded software from three aspects: reliability, efficiency, and portability.
1. Reliability
The embedded field involves a wide range of fields, which can be divided into four aspects: defense, Medical Electronics, consumer electronics, and automotive electronics. obviously, the first two fields have the highest software reliability requirements. if your MP3 device crashes, you can restart it. A medical device failure may be related to human life.
For serial communication, we often use serial communication. The hardware connection is only three lines (txd, rxd, Gnd), and The Checksum and handshaking protocols are all completed by software. what is used for verification, how to define the handshake signal, the format of the communication frame, how the retransmission mechanism is defined, and how much the baud rate is set will affect the reliability of serial communication.
2. Efficiency
Embedded software requires very high code execution efficiency. The principle is very simple. The host of embedded code running is not a Pentium processor with super processing speed, but only a few hundred MB, even a few M embedded processors, limited environments require more efficient code. Many years ago, it was said that embedded systems also have strict requirements on code size. However, with the continuous expansion of the memory capacity and the reduction of cost, the Size Factor of the Code is gradually becoming secondary.
So how can we improve the efficiency of embedded code execution. Summarize the following points (which may be missing ):
1 Embedded Assembly
2 inline functions
3. avoid multiplication and division (use other methods instead)
4. It is best to use do... while in a loop.
5. The address must be aligned.
6. Use macros for simple functions (because macros can generate embedded code ).
Take the last one as an example.
# Define min (A, B) (a) <= (B )? (A) :( B )).
Three portability
I always think that the concept of code portability is born for Embedded Software (of course not). desktop software is either Windows or Linux. The CPU is also basically occupied by Intel. the powerful portability functions of code are hardly reflected. the embedded field is quite different. there are hundreds of embedded CPUs. from 8-bit to 64-bit. each embedded CPU plays an immeasurable role in its appropriate field. there is no such phenomenon in the field of embedded systems.
So, how to improve the portability of embedded software is too detailed. Based on my own experiences in actual development, I can only talk about two points.
1. All platform-related data types must be redefined using typedef. it is best to be in an independent. h file. there are many platform-related data types, such as integer type and floating point type. C language does not specify the length of each data type. The length of the data type is determined by the CPU and the compiler.
2. Perform hierarchical management of complex operations as much as possible. the underlying layer is hardware-related operations. it has nothing to do with the platform. take serial communication as an example. assume that the PC has a host computer software that operates on a machine through a serial port. you can organize your code by using a layer-based principle similar to TCP/IP protocol. as shown in: