Coding Style: µCOS vs FreeRTOS

Follow+Star Public Number, don’t miss wonderful content
Coding Style: µCOS vs FreeRTOS
Author | strongerHuang
WeChat Public Account | strongerHuang
Readers often ask:
  • How can I write beautiful code?
  • Where can I find excellent code to reference?
  • How can I improve my coding skills?
In fact, there are excellent codes all around us that we can learn from, it’s just that you haven’t seriously discovered them. You must have seen various excellent open-source codes like bare metal, RTOS, Linux, and you have certainly come across some excellent open-source libraries.

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

Today, I will share with you the coding standards of the well-known RTOS around us, taking the latest versions of uC/OS-III V3.08.02 and FreeRTOS V10.5.1 as examples:

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

The uC/OS article clearly states that it follows the MISRA C:2012 standard and supports PC-Lint static checking.

Coding Style: µCOS vs FreeRTOS

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.

Coding Style: µCOS vs FreeRTOS

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:

Coding Style: µCOS vs FreeRTOS

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

Coding Style: µCOS vs FreeRTOS

2.FreeRTOS

The configuration file for FreeRTOS is usually: FreeRTOSConfig.h

Coding Style: µCOS vs FreeRTOS

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.:

Coding Style: µCOS vs FreeRTOS

2.FreeRTOS

Includes RTOS version, copyright statement, open-source protocol statement, website, etc., similar to uC/OS.

Coding Style: µCOS vs FreeRTOS

Naming

The naming rules for the two RTOS differ somewhat, but both comply with 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 used continuously for compatibility. (This is more likely to cause file name conflicts)

Coding Style: µCOS vs FreeRTOS

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】.

Coding Style: µCOS vs FreeRTOS

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.

Coding Style: µCOS vs FreeRTOS

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:

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 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:

Coding Style: µCOS vs FreeRTOS

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:

Coding Style: µCOS vs FreeRTOS

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 ————

Coding Style: µCOS vs FreeRTOS

● 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.

Coding Style: µCOS vs FreeRTOS

Coding Style: µCOS vs FreeRTOS

Click “Read Original” to see more shares.

Leave a Comment

×