
-
How can I write beautiful code? -
Where can I find excellent code to reference? -
How can I improve my coding skills?
To write beautiful and excellent code, first, your code must have a set of standards.
Coding Standards
uC/OS and FreeRTOS both follow the MISRA C coding standard, both support PC-Lint static checking, and both have explanations in their official documents.
Tip:
The MISRA C standard refers to the automotive C language development standard, you can refer to the previously shared article: What is the MISRA C: 2012 Standard?
1.uC/OS
2.FreeRTOS
FreeRTOS also clearly states that it adopts the MISRA C coding standard, but does not support standards after C99, and also supports PC-Lint static checking.
FreeRTOS has a lot of explanations (comments) in the source code for PC-Lint static checking that may cause exceptions, which is not present in the uC/OS source code.
For example:

Configuration Files
The contents of the configuration files for the two RTOS (also known as “trimming” files) are somewhat similar and are categorized.
However, uC/OS’s categorization and comments are more user-friendly and more suitable for beginners to understand.
1.uC/OS
The configuration file for uC/OS is usually: os_cfg.h
2.FreeRTOS
The configuration file for FreeRTOS is usually: FreeRTOSConfig.h

Headers
The contents of the headers for the two RTOS are somewhat similar, just one is centered and the other is left-aligned.
1.uC/OS
Includes RTOS version, copyright statement, open-source protocol statement, etc.:
2.FreeRTOS
Includes RTOS version, copyright statement, open-source protocol statement, website, etc., similar to uC/OS.

Naming
The naming rules for the two RTOS differ somewhat, but both comply with conventional code naming rules.
1.File Names
FreeRTOS is more straightforward, which may be due to a lack of long-term planning during naming, and has been used continuously for compatibility. (This is more likely to cause file name conflicts)

2.Macros
Both have similarities: underscores separate uppercase letters, but there are certain differences in prefixes.
For example, with the “configuration file”: uC/OS starts with 【OS_CFG_】, while FreeRTOS starts with 【config】.
3.Data Types
The data types defined by uC/OS are relatively more common and also more suitable for beginners.
The data types defined by FreeRTOS are more “systematic”, suitable for experienced users, and relatively less friendly for beginners.
4.Function Names
The two are somewhat similar, differing in prefixes, and both distinguish with 【uppercase letters at the start】.
For example: the function name for creating tasks:
FreeRTOS has the following description for its prefixes:
-
Static (static) functions are prefixed with prv. For example: the prvIdleTask function.
-
API functions are prefixed with their return type, with void types suffixed with v at the end. For example: the vTaskDelete function.
Comments
// /* */ are the two most common methods of commenting, but these two RTOS mainly use 【/* */】 comments.
The position of comments is also important, usually one line above the line of code, or at the end of the line of code.
It should be noted that FreeRTOS does not provide detailed comments on the functionality, parameters, return values, etc. in the code.
Taking the “create task” function as an example:
Of course, the function comment information of FreeRTOS is described in detail in the manual.
Indentation
Both RTOS use the same indentation method, which is 4 spaces:
It is important to note not to mix Tabs and spaces, otherwise the code will be a mess. (This often occurs in many beginners or junior engineers, making the code hard to read.)
Abbreviations
Different fields have different ways of abbreviating. There are also some common abbreviations in RTOS, such as:
Abbreviation | Meaning |
Addr | Address |
Blk | Block |
Chk | Check |
Clr | Clear |
Cnt | Count |
Ctr | Counter |
Ctx | Context |
Cur | Current |
Del | Delete |
Dly | Delay |
Err | Error |
OS | Operating System |
CPU | Center Processing Unit |
Alright, that’s all for this article. The above only lists some typical coding styles, for more details, everyone can take time to study on their own.
———— END ————
● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from the Embedded Column
Follow the public account reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
