Coding Standard and Style Guide
[Getting Started]

On this page:


Coding Standard

The core FreeRTOS source files (those that are common to all ports) conform to the MISRA coding standard guidelines. As the standard is many pages long, and is available for purchase from MISRA for a very small fee, we have not replicated all the rules here.

Deviations from the MISRA standard are listed below:

FreeRTOS builds with many different compilers, some of which are more advanced than others. For that reason FreeRTOS does not use any of the features or syntax that have been introduced to the C language by or since the C99 standard. The one exception to this is the use of the stdint.h header file. The FreeRTOS/Source/include directory contains a file called stdint.readme that can be renamed stdint.h to provide the minimum stdint type definitions necessary to build FreeRTOS - should your compiler not provide its own.



Naming Conventions

The RTOS kernel and demo application source code use the following conventions:



Data Types

Only stdint.h types and the RTOS's own typedefs are used, with the following exceptions:


There are four types that are defined for each port. These are:



Style Guide

/* Library includes come first... */ #include <stdlib.h> /* ...followed by FreeRTOS includes... */ #include "FreeRTOS.h" /* ...followed by other includes. */ #include "HardwareSpecifics.h" /* #defines comes next, bracketed where possible. */ #define A_DEFINITION ( 1 ) /* * Static (file private) function prototypes appear next, with comments * in this style - with each line starting with a '*'. */ static void prvAFunction( uint32_t ulParameter ); /* File scope variables are the last thing before the function definitions. Comments for variables are in this style (without each line starting with a '*'). */ static BaseType_t xMyVariable. /* The following separate is used after the closing bracket of each function, with a blank line following before the start of the next function definition. */ /*-----------------------------------------------------------*/ void vAFunction( void ) { /* Function definition goes here - note the separator after the closing curly bracket. */ } /*-----------------------------------------------------------*/ static UBaseType_t prvNextFunction( void ) { /* Function definition goes here. */ } /*-----------------------------------------------------------*/ File Layout


/* Function names are always written on a single line, including the return type. As always, there is no space before the opening parenthesis. There is a space after an opening parenthesis. There is a space before a closing parenthesis. There is a space after each comma. Parameters are given verbose, descriptive names (unlike this example!). The opening and closing curly brackets appear on their own lines, lined up underneath each other. */ void vAnExampleFunction( long lParameter1, unsigned short usParameter2 ) { /* Variable declarations are not indented. */ uint8_t ucByte; /* Code is indented. Curly brackets are always on their own lines and lined up underneath each other. */ for( ucByte = 0U; ucByte < fileBUFFER_LENGTH; ucByte++ ) { /* Indent again. */ } } /* For, while, do and if constructs follow a similar pattern. There is no space before the opening parenthesis. There is a space after an opening parenthesis. There is a space before a closing parenthesis. There is a space after each semicolon (if there are any). There are spaces before and after each operator. No reliance is placed on operator precedence - parenthesis are always used to make precedence explicit. Magic numbers, other than zero, are always replaced with a constant or #defined constant. The opening and closing curly brackets appear on their own lines. */ for( ucByte = 0U; ucByte < fileBUFFER_LENGTH; ucByte++ ) { } while( ucByte < fileBUFFER_LENGTH ) { } /* There must be no reliance on operator precedence - every condition in a multi-condition decision must uniquely be bracketed, as must all sub-expressions. */ if( ( ucByte < fileBUFFER_LENGTH ) && ( ucByte != 0U ) ) { /* Example of no reliance on operator precedence! */ ulResult = ( ( ulValue1 + ulValue2 ) - ulValue3 ) * ulValue4; } /* Conditional compilations are laid out and indented as per any other code. */ #if( configUSE_TRACE_FACILITY == 1 ) { /* Add a counter into the TCB for tracing only. */ pxNewTCB->uxTCBNumber = uxTaskNumber; } #endif A space is placed after an opening square bracket, and before a closing square bracket. ucBuffer[ 0 ] = 0U; ucBuffer[ fileBUFFER_LENGTH - 1U ] = 0U; Formatting of C Constructs