Coding Style: µCOS vs FreeRTOS

Readers often ask:
1. How can I write beautiful code?
2. Where can I find excellent code to reference?

3. How can I improve my coding skills?

In fact, there is excellent code all around us to draw from, you just haven’t taken the time to discover it. Whether it’s bare metal, RTOS, Linux, or various outstanding open-source codes, you must have seen them, and you’ve certainly interacted with some excellent open-source libraries.

To write beautiful and excellent code, first, your code must adhere to a set of standards.

Today, I will share the coding standards of RTOS that we are familiar with, using the latest versions of uC/OS-III V3.08.02 and FreeRTOS V10.5.1 as examples:

1. Coding Standards

Both uC/OS and FreeRTOS follow the MISRA C coding standard and support PC-Lint static checks, as stated in their official documentation.
Note:The MISRA C standard refers to the automotive C language development standard. You can refer to the previously shared article:MISRA C: What is the 2012 Standard?
1. uC/OS
The uC/OS article clearly states adherence to the MISRA C:2012 standard and also supports PC-Lint static checks.
Coding Style: µCOS vs FreeRTOS
2. FreeRTOS
FreeRTOS also clearly states adoption of the MISRA C coding standard, but does not support standards after C99, and also supports PC-Lint static checks.
Coding Style: µCOS vs FreeRTOS
FreeRTOS contains a lot of explanations (comments) in the source code regarding code that may cause exceptions during PC-Lint static checks, which is not present in the uC/OS source code.
For example:
Coding Style: µCOS vs FreeRTOS

2. Configuration Files

The configuration files of both RTOS (the so-called “trimming” files) have somewhat similar contents, but they are categorized.
However, the categorization and comments in uC/OS are more user-friendly and suitable for beginners to understand.
1. uC/OS
The configuration file for uC/OS is usually: os_cfg.h
Coding Style: µCOS vs FreeRTOS
2. FreeRTOS
FreeRTOS configuration file is usually: FreeRTOSConfig.h
Coding Style: µCOS vs FreeRTOS

3. Headers

The contents of the headers for both RTOS are somewhat similar, with one centered and the other left-aligned.
1. uC/OS
Includes RTOS version, copyright statement, open-source license statement, etc.
Coding Style: µCOS vs FreeRTOS
2. FreeRTOS
Includes RTOS version, copyright statement, open-source license statement, website, etc., similar to uC/OS.
Coding Style: µCOS vs FreeRTOS

4. Naming

The naming rules for both RTOS differ quite a bit, but they both adhere to conventional code naming rules.
1. File Names
uC/OS uses the format 【os_ system file】, which appears more standardized (reflecting modularity).
FreeRTOS is more straightforward, which may be due to a lack of long-term planning during naming, and has been carried forward for compatibility. (This can lead to file name collisions.)
Coding Style: µCOS vs FreeRTOS
2. Macros
Both have similarities: underscores separate uppercase letters, but the prefixes differ.
For example, regarding the “configuration file”: uC/OS starts with 【OS_CFG_】, while FreeRTOS starts with 【config】.
Coding Style: µCOS vs FreeRTOS
3. Data Types
Data types defined by uC/OS are relatively more common and more suitable for beginners.
Data types defined by FreeRTOS are more “systematic,” suitable for experienced users, and less friendly to beginners.
Coding Style: µCOS vs FreeRTOS
4. Function Names
Both are somewhat similar, differing in prefixes, with both distinguishing using 【uppercase letters at the beginning】.
For example, the function name for creating a task:
Coding Style: µCOS vs FreeRTOS

FreeRTOS has the following description for its prefixes:

  • Static (static) functions are prefixed with prv. For example: the prvIdleTask function.

  • API function prefixes correspond to their return types, with void types prefixed with v, for example, the vTaskDelete function.

5. Comments

// /* */ are the two most common methods of commenting, but these two RTOS mainly use 【/* */】 comments.
The placement of comments is also important, usually one line above the line of code or at the end of the line of code.
Here it should be noted that FreeRTOS does not have reliable comments in the code regarding function functionalities, parameters, return values, etc.
Taking the “create task function as an example:
Coding Style: µCOS vs FreeRTOS
Of course, the function comment information for FreeRTOS is reliably described in the manual.

6. Indentation

Both RTOS use the same indentation method, which is 4 spaces:
Coding Style: µCOS vs FreeRTOS
Here, it is important to remember not to mix tabs and spaces, otherwise the code will be chaotic. (This often occurs with many beginners or junior engineers, leading to code that is simply hard to look at.)

7. Abbreviations

Different fields have different abbreviation methods. In RTOS, there are also some common abbreviated words, 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

That’s all for this article. The above only lists some typical coding styles; for more details, everyone can take the time to research on their own.

Coding Style: µCOS vs FreeRTOS

END

Source: strongerHuang
Copyright belongs to the original author. If there is infringement, please contact for deletion.
Recommended Reading
Domestic MCU manufacturers, all in one network!
Foreign chip manufacturers lay off again: no R&D left!
Experience with domestic FPGA development boards: under a hundred yuan, challenging the high-end

→Follow to stay updated←

Leave a Comment

×