Thursday, January 28, 2016

FreeRTOS - Coding Standard and Styling

This post is heavily based on the official webpage [1] and I guess it couldn't be very different. This time we'll understand a few things on how the code is written, for instance, what's up with the 'x' in front of xTaskCreate() or the 'ul' in ulValueToSend. First of all, we should have in mind that the FreeRTOS is coded in C language according to MISRA guidelines [2], which I don't know at all, but I'm sure we can get what they mean.

Naming Convention

Variables:

  • uint32_t: prefix 'ul', as in 'unsigned long'
  • uint16_t: prefix 'us', as in 'unsigned short'
  • uint8_t: prefix 'uc', as in 'unsigned char'
  • Non standard types: prefix 'x' or 'ux', when unsigned. Examples:
    • BaseType_t (used on xTaskCreate)
    • QueueHandle_t (used on xQueue)
    • TickType_t (used on xNextWakeTime)
  • Enumerated: prefix 'e'
  • Pointers have an additional prefix 'p'
Functions:
  • Private functions (file scope static only): prefix 'prv'. Examples:
    • prvQueueReceiveTask: the Receiver
    • prvQueueSendTask: the Sender
  • FreeRTOS API functions: prefixed according to its return type (see Variables) or 'v', as in 'void'; after this prefix, the name of the .c file where it is defined. Examples:
    • xTaskCreate: non standard type, defined on task.c
    • xQueueCreate: non standard type, defined on queue.c
    • vTaskStartScheduler: void type, defined on task.c
Macros:
  • Prefixed with the file where it is defined, in lowercase
  • Its name must be written in uppercase
  • Examples:
    • configMINIMAL_STACK_SIZE, configASSERT: defined in FreeRTOSConfig.h
    • mainQUEUE_RECEIVE_TASK_PRIORITY: defined in main.h (or .c, or in the case of Blinky, main_blinky.c)
    • pdMS_TO_TICKS: define in projdefs.h
    • portMAX_DELAY: defined in portmacro.h

Data Types

There are a few (4) special data types largely used:
  • TickType_t: if configUSE_16_BIT_TICKS != 0, then it's an unsigned short (16 bits), otherwise, it's an unsigned long (32 bits)
  • BaseType_t: the most efficient data type for the architecture (for 32-bit architectures, it's a signed 32 bit type; for a 16-bit, it's a signed 16 bits, etc)
  • UBaseType_t: just an unsigned BaseType_t
  • StackType_t: used internally by the FreeRTOS, has the width of the memory where the stack is located (usually 16 bits for 16-bit architecture and 32 bits for 32-bit)

Style Guide

This is something in which I believe a lot. Having a style, preferably a simple one makes the code much easier to read, which facilitates maintenance. FreeRTOS has its own rules:
  • Indentation with TABs (4 spaces)
  • Comments with '//' and don't pass column 80, unless after a parameter describing it
  • The layout must be easy to read (check FreeRTOS webpage for details [3])


No comments:

Post a Comment