introduction
Improving the quality of software code is an issue that every software designer must consider, which involves the effectiveness and economic value of the software. Most software designs based on embedded systems are based on real-time operating systems, which is very different from traditional programming based on Windows operating systems. The former requires a deeper understanding of the operating system, requires users to have a considerable understanding of the working principles of their own processors and compilers, and to be able to write a certain amount of porting code to achieve the connection between the operating system and the underlying hardware. μC/OS-II is a preemptive real-time operating system kernel with open source code. This article mainly combines the application of μC/OS-II system functions to explain how to use the parameters and return values of μC/OS-II system functions to improve programming efficiency and code quality.
1 Parameter and return value classification
Through the study and research of μC/OS-II, it can be found that most of the system functions it provides are written in standard C language; restricted by the syntax rules of C language, these functions have only one return value. In order to obtain more status information when using the system functions of μC/OSII, the status information is saved in the function parameters. In this way, the parameters of μC/OS-II system functions can be divided into two categories: the first category is ordinary formal parameters, which conform to the traditional usage method and mainly pass the value of the actual parameter, playing the role of numerical transmission; the second category of formal parameters does not pass valid values when used, but is only a variable. The status information generated during the execution of the system function is saved in the second category of parameters. At the end of the system function call, the value of this type of parameter can be used to view the status information generated during the execution of the system function.
This article takes the function 0SSemPend() as an example. This function has no return value. The specific meaning of each of its formal parameters can be found in the references, and no specific description is given here. Its parameters can be classified into the above two categories: OS_EVENT*pevent and INTl6U timeout are the first category, and the actual parameters in the application program must give them specific values; INT8U*err is the second category, and the actual parameters in the application program do not need to give specific values. When the function code is executed, INT8U*err will be assigned a value according to different situations. This value reflects the execution status of the function. As shown in the application of the OSSemPend() function.
2 Status information in function parameters and return values
According to the actual situation, the system functions of μC/OS-II can be divided into functions without parameters and return values, functions with parameters but no return values, and functions with both parameters and return values. The first case is not discussed here. This paper mainly studies the second and third cases. As mentioned above, in order to increase the status information and return value generated by the execution of system functions, μC/OS-II puts the status information into the function parameters. Through the study of the system functions of μC/OS-II, the author found that these functions do not all put the status information into the function parameters. Some also put it into the return value, such as the OSsemQtJery() function, which uses the status information passed by the return value and the valid information passed by the function parameters. These status information reflects the problems that occur when using the system functions of μC/OS-II. By reading these status information, the execution status of the system function can be known. Therefore, from a security perspective, all status information should be read when using these system functions, and corresponding processing instructions should be given according to different status. According to this idea, the application of the OSSemPend() function is improved as follows:
It can be seen that when calling the system service function OSSemPend(), the temporary variable err is passed to OSSemPend() as the actual parameter. After executing this function, the temporary variable err contains the status information generated when the function is executed. These status information use constants instead of a constant to increase the readability and versatility of the software. The specific definitions and meanings are listed in Table 1, where the first two return values are normal: the first is when a signal is available, and normal processing is performed; the second is when no signal arrives within the specified time, and timeout processing is performed. The last three situations are caused by human errors. After calling the OSSemPend() system function, the variable containing the status information must be analyzed and processed. The process is shown in the above program. Due to space constraints, only a simple sentence is used here to represent the processing process.
3 Use of status information
When calling each system function of μC/OS-II, as long as the called function provides status information, the status information should be analyzed and processed. Professional software designers believe in such a truth: "The best way to write error-free code is to put error prevention in the first place". Take calling the system function OSSemPend() of μC/OS-II as an example. Users do not need to change the code of the OSSemPend() function, assuming that there is no problem with this part of the content. Now we need to detect the status of this function when it is executed, that is, the error information it generates. This function returns three results to indicate the errors used by the user, as listed in Table 1: OS_ERR_EVENT_TYPE means that the pointer data provided by the user when calling the OSSemPend() function does not point to the semaphore, and a type error occurs; OS_ERR_PEVENT_NULL means that the pointer provided by the user as the actual parameter is a null pointer; OS_ERR_PEND_ISR means that the user called the OSSemPend() function in the interrupt service program; these three status errors are caused by the user's carelessness or lack of understanding of the μC/OS-II system function during the software design stage. As long as the user is careful during the design process, such errors can be avoided. However, from the perspective of preventing errors, it is necessary to detect and handle the status of these errors, so that when an error occurs, the error handler will give a simple prompt or even modify the error. The error handler prevents repeated reading of program code during program debugging, avoids spending a lot of energy to find errors, and improves software design efficiency.
The embedded system software designed according to the above scheme can be considered an ideal compiler. Now consider, if the compiler can correctly point out all the problems in the code, what will the error situation of the corresponding program be? This does not only refer to syntax errors, but also any problems in the program, no matter how hidden it is. Obviously, all current compilers cannot achieve this function, so the functions of the compiler need to be extended. This design idea can be considered as: the software designer needs to design the extended functions of the compiler so that the compiler can check errors by itself when designing the software. If this can be done, the workload of software design will be greatly reduced.
4 Debug and delivery versions of software
The previous improved program handles all possible states generated by the OSSemPend() function call, and most of this part of the code is redundant code, in order to facilitate software design and debugging. When using the real-time operating system μC/0S-II for embedded software design, there are certainly more than one system function used, OSSemPend(). If each function call is handled as in the program, the code space will increase rapidly and the program efficiency will be greatly reduced.
To solve this problem, first of all, if the program design is done very carefully, most of the state detection and processing processes can be omitted. The reason for detecting and processing each state information is to improve the efficiency of software design and debugging. After the software is debugged, it is unnecessary to detect some state information. This is like buying insurance before taking a flight. After the flight arrives at the destination, the insurance is useless. The software is ultimately provided to customers as a product. This product is the final version (of course it will be continuously upgraded). There will be a debugging version when designing the product, and this debugging version must run through the entire software life cycle. The debugging version is for software design, debugging and upgrading. It will not be provided to users, let alone appear in the product.
When it comes to embedded system software design, we still use the code that calls the OS_SemPend() function as an example to illustrate the problem.
By observing the above program and the previous improvements, it is found that several conditional compilation instructions are added to this program. If the TEST flag is not defined, a part of the code will not be compiled, which is the delivery version of the software. On the contrary, if the TEST flag is defined, it means it is a debug version, and all instruction codes will be compiled. By comparing the two versions, we can see that the code of the delivery version is much less than that of the debug version. And through analysis, we know that after the software is debugged, there will be no errors of OS_ERR_EVENT_TYPE, 0S_ERR_PEND_ISR and OS_ERR_PEVENT_NULL. The functions implemented by these two versions are exactly the same, and there is really no need to compile this part of the code.
Conclusion
In the process of embedded system software design, embedded real-time operating systems are used in most cases. On the premise of ensuring the quality of the designed code, users must also fully consider the state information generated when calling system functions and perform appropriate processing. Only in this way can the design efficiency of the software be improved and the design cycle be shortened.
Previous article:Design of Isolated Word Speech Recognition System Based on TMS320VC5402
Next article:FPGA Verification in Embedded Video Processing Systems
Recommended ReadingLatest update time:2024-11-17 02:42
- Popular Resources
- Popular amplifiers
- Beyond the Threaded Programming Model on Real-Time Operating Systems
- Write your own CPU (Lei Silei)
- STC8 series MCU development guide: analysis and application of processors, programming and operating systems
- Programming Technology Based on Embedded Real-Time Operating System (2nd Edition) (Wu Guangwen)
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Problems with long pressing of infrared remote control
- Looking for a simple 24V to 12V100mA circuit
- 【Chicken hatching and breeding box integrated controller】ESP32-S2-Kaluga-1/rp2040 controller
- CircuitPython in Space
- Experience sharing on porting ardupilot to ti platform
- CS8683 Mono 130W High Power Class D Amplifier IC Solution
- "Playing with the Board" + Replaying MicroPython on the STM32F7DISC (2)
- FPGA implementation of sliding average filtering algorithm and LZW compression algorithm
- Qorvo has acquired Decawave and Custom MMIC, so powerful!
- EEWORLD University Hall----Statistical Machine Learning