FreeRTOS Coding Standards and Data Types

Scan to FollowLearn Embedded Together, let’s learn and grow together

FreeRTOS Coding Standards and Data Types

FreeRTOS introductory tutorial has been delayed for too long.

Recently, I plan to complete this series, while organizing my knowledge, I hope to help beginners quickly get started and master the basic principles and usage methods of FreeRTOS.

Previously published two articles about FreeRTOS:
FreeRTOS Quick Start – An Introduction to the System
FreeRTOS Official Chinese Website is Live!

After having a general understanding of FreeRTOS, we will gradually learn how to use it.

For every software product or software project, there will be coding standards and the data types used.

The core source code files of FreeRTOS follow the MISRA coding rules.

As a beginner, it is necessary to understand its coding standards and data type definitions to better use FreeRTOS for development.

Naming Rules

Beginners may be confused about the naming of variables and functions in FreeRTOS, here is an introduction:

Variable Names

In FreeRTOS, when defining variables, the type of the variable is often used as a prefix, which helps users immediately know the type of the variable. The definitions of various type names are as follows:

Variables defined with uint32_t are prefixed with ul. ‘u’ stands for unsigned, and ‘l’ stands for long.

Variables defined with uint16_t are prefixed with us. ‘u’ stands for unsigned, and ‘s’ stands for short.

Variables defined with uint8_t are prefixed with uc. ‘u’ stands for unsigned, and ‘c’ stands for char.

For variable types not defined in stdint.h, the prefix ‘x’ should be added when defining variables, such as variables defined with BaseType_t and TickType_t.

For unsigned variable types not defined in stdint.h, the prefix ‘u’ should be added when defining variables, such as variables defined with UBaseType_t should be prefixed with ux.

Variables defined with size_t should also be prefixed with ux.

Enumeration variables will be prefixed with e.

Pointer variables will be prefixed with p, for example, pointer variables defined with uint16_t will be prefixed with pus.

According to MISRA coding rules, variables defined with char can only be used for ASCII characters, prefixed with c.

According to MISRA coding rules, pointer variables defined with char * can only be used for ASCII strings, prefixed with pc.

Functions

Function names include the return type of the function, the file name of the function, and the function’s functionality; if it is a private function, a ‘prv’ (private) prefix will be added. Functions declared with static should also have the prefix prv.

Notably, including the file name in the function name greatly helps users improve the efficiency of finding function definitions and understanding the purpose of the function.

For functions with return values, based on the return type, the corresponding prefix should be added; if there is no return value, i.e., void type, the prefix ‘v’ should be added to the function.

According to the file name, the corresponding function definitions in the file will also include the file name in the function naming, for example, in the tasks.c file, the function vTaskDelete(), where ‘task’ is from the file name.

Macro Definitions

Macros are represented by uppercase letters, with a lowercase prefix indicating where the macro is defined.

For example, the macro definition configUSE_PREEMPTION is defined in the file FreeRTOSConfig.h. The prefix ‘config’ comes from the file name. Note that the prefix should be lowercase.

Except for the prefix, the rest is all uppercase, separated by underscores.

Data Types

In FreeRTOS, the data types used are all standard C data types, but they have been redefined for different processors, giving them new names.

For example, char is redefined as portCHAR, where ‘port’ indicates the interface, meaning FreeRTOS needs these interface files to connect when porting to these processors.

The data types used in FreeRTOS are mainly divided into two types: those defined in stdint.h and those defined by FreeRTOS itself. Special attention should be paid to variables defined with char and char *.

The data type redefinitions are implemented in the portmacro.h header file.

FreeRTOS mainly defines the following four data types:

  • TickType_t

    If the user enables the macro definition configUSE_16_BIT_TICKS, then TickType_t is defined as a 16-bit unsigned number; if not enabled, it is defined as a 32-bit unsigned number.

  • BaseType_t

    This data type is determined by the bitness of the system architecture; for 32-bit architectures, BaseType_t is defined as a 32-bit signed number; for 16-bit architectures, it is defined as a 16-bit signed number.

  • UBaseType_t

    This data type is the unsigned version of BaseType_t.

  • StackType_t

    This data type defines the stack variable type, determined by the system architecture; for 16-bit architectures, StackType_t is defined as a 16-bit variable; for 32-bit systems, it is defined as a 32-bit variable.

Screenshot of data type redefinitions in portmacro.h

FreeRTOS Coding Standards and Data Types

OK, the content introduction is complete. Thank you for reading, keep it up~

FreeRTOS Coding Standards and Data Types

Scan to join the high-quality embedded group

FreeRTOS Coding Standards and Data Types

Follow me 【Learn Embedded Together】, let’s learn and grow together.

If you think the article is good, click “Share“, “Like“, or “View“!

Leave a Comment

×