Table of Contents

ChibiOS Code Style Guide

ChibiOS/RT code follows well define style conventions that must be followed in order to have code accepted in the distribution.

Why so much focus on Style

Simply because we believe that a good product must be consistent, it is not acceptable to have different modules following different styles.

C dialect

The whole portable code base must be C99 compliant. Compiler-specific constructs must be restricted in non-portable modules or abstraction macros, modules tagged as portable must be strict C99.

In addition, portable modules must be MISRA-94 compliant and checked, deviation to the rules must be marked either with code tagging or a global waiver.

C Code style conventions

ChibiOS follows the K&R indentation style with few modifications:

Empty Lines

The use of empty lines varies:

Example:

  /* Comment about the block1.*/
  Block1 statements...
 
  /* Comment about the block2.*/
  Block2 statements...
 
  /* Comment about the block3.*/
  Block3 statements...
  statement {
    /* Comment about the sub-block1.*/
    Sub-block1 statements...
  }
  Block3 more statements...

Comments

Of course there are rules about comments.

Global objects

Doxygen usage

Please use the following general Doxygen template:

/**
 * @brief   A brief description, one sentence, one line.
 * @details A detailed description of the functionality, it can span over
 *          multiple lines, can be omitted.
 * @pre     Prerequisites about the use of the functionality, there can be
 *          more than one "pre" tags, can be omitted.
 * @post    Postrequisites about the use of the functionality, there can be
 *          more than one "post" tags, can be omitted.
 * @note    There can be one or more notes, can be omitted.
 *
 * @param[in] p1        description of parameter one
 * @param[out] p2       description of parameter two
 * @param[in,out] p3    description of parameter three
 * @return              Description of the returned value, must be omitted if
 *                      a function returns void.
 * @retval VALUE1       description of the special returned value one, can be
 *                      omitted.
 * @retval VALUE2       description of the special returned value two, can be
 *                      omitted.
 *
 * @api|@notapi|@special|@init|@sclass|@iclass|@xclass|@isr
 */

Notes:

Comments inside functions

The preferred style of comment are the following, note:

  /* This is a single line comment.*/
 
  /* This is a very long comment that necessarily has to span over
     multiple lines.*/