This article introduces the <span>FreeRTOS</span> coding standards. Good coding practices are the foundation for writing quality code and collaborative software development.
Data Types
<span>FreeRTOS</span>‘ data types are defined in the <span>portmacrocommon.h</span> file. From the definitions, <span>FreeRTOS</span> has redefined the standard data types in <span>C</span>. For example, <span>char</span> is redefined as <span>portCHAR</span>.
/**
* @brief Type definitions.
*/
#define portCHAR char
#define portFLOAT float
#define portDOUBLE double
#define portLONG long
#define portSHORT short
#define portSTACK_TYPE uint32_t
#define portBASE_TYPE long
typedef portSTACK_TYPE StackType_t;
typedef long BaseType_t;
typedef unsigned long UBaseType_t;
#if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
typedef uint16_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffff
#elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
typedef uint32_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
/* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
* not need to be guarded with a critical section. */
#define portTICK_TYPE_IS_ATOMIC 1
#else
#error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
#endif
<span>IAR</span> defaults <span>char</span> as an unsigned type.

Several data types that need special mention may vary depending on the platform being ported.
-
TickType_t
For 32-bit architectures,
<span>configUSE_16_BIT_TICKS</span>should always be set to 0. - If
<span>configUSE_16_BIT_TICKS</span>is set to non-zero (<span>true</span>), then<span>TickType_t</span>is defined as an unsigned 16-bit type. - If
<span>configUSE_16_BIT_TICKS</span>is set to zero (<span>false</span>), then<span>TickType_t</span>is defined as an unsigned 32-bit type. -
<span>BaseType_t </span>is defined as a 32-bit type on 32-bit architectures. On 16-bit architectures,<span>BaseType_t</span>is defined as a 16-bit type. -
<span>UBaseType_t</span>is an unsigned<span>BaseType_t</span>. -
<span>StackType_t</span>refers to the type used by the architecture to store stack items. Typically, it is a 16-bit type on 16-bit architectures and a 32-bit type on 32-bit architectures, but there are exceptions. It is used internally by<span>FreeRTOS</span>.
Naming
Variables
-
<span>uint32_t</span>type variables are prefixed with<span>ul</span>, where “u” stands for “unsigned” and “l” stands for “long”. -
<span>uint16_t</span>type variables are prefixed with<span>us</span>, where “u” stands for “unsigned” and “s” stands for “short”. -
<span>uint8_t</span>type variables are prefixed with<span>uc</span>, where “u” stands for “unsigned” and “c” stands for “char”. -
<span>size_t</span>type variables are also prefixed with<span>ux</span>. -
Enumeration variables are prefixed with
<span>e</span>. -
Pointers are prefixed with
<span>p</span>, for example, a pointer to<span>uint16_t</span>will be prefixed with<span>pus</span>. -
Function names include the return type of the function, the filename where the function is located, and the function’s purpose. If it is a private function, a
<span>prv (private)</span>prefix will be added.<span>FreeRTOS</span>has a good coding standard where the function name includes the filename, which greatly helps users improve the efficiency of finding function definitions and understanding the purpose of the function. For example:
<span>void vListInitialise( List_t * const pxList )</span>, v – void return type; List – located in list.c file; Initialise – function’s purpose.
<span>QueueHandle_t xQueueCreateStatic(UBaseType_t uxQueueLength, UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxQueueBuffer) </span>
x – QueueHandle_t return type; Queue – located in queue.c file; CreateStatic – function’s purpose
Macros
- Macros are prefixed with the name of the file where the macro is defined. The prefix is in lowercase. For example,
<span>configUSE_PREEMPTION</span>is defined in<span>FreeRTOSConfig.h</span>. - All macros, except for the prefix, are written in uppercase letters and use underscores to separate words.
Style Guide
-
Indentation uses four space characters.
-
Comments. Use
<span>FreeRTOS</span>recommended “block comments” (/* … */) or “line comments” (// …), avoid mixing; block comments are used for long multi-line texts (like function headers), and line comments are used for single-line explanations (like key code lines). -
Coding style. I prefer using
<span>UTF-8</span>.
