SDK Development

Discuss and develop the mbed SDK.

mbed sdk coding style

Mbed OS 2 and Mbed OS 5

This is a guide for Mbed OS 2. If you’re working with Mbed OS 5, please see the Coding Style guide in the new handbook.

Coding style

This page describes mbed coding style. Our goal is the files written in mbed SDK conform this standard (exceptions are there, for instance 3rd libraries supplied by partners).

Be consistent.

The mbed SDK code follows K&R style (Reference: with at least 2 exceptions which can be found in the list below the code snippet.


static const PinMap PinMap_ADC[] = {
    {PTC2, ADC0_SE4b, 0},
    {NC  , NC       , 0}

uint32_t adc_function(analogin_t *obj, uint32_t options) 
    uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
    switch (options) {
        case 1:
            timeout = 6;
            timeout = 10;

    while (!adc_hal_is_conversion_completed(instance, 0)) {
        if (timeout == 0) {
        } else {

    if (obj->adc == ADC_CHANNEL0) {
    } else {
        error("channel not available");

    for (uint32_t i = 0; i < 10; i++) {
        printf("Loop : %d", i);
    return adc_hal_get_conversion_value(instance, 0);


  • Indentation - 4 spaces. Please do not use tabs.
  • Braces - K&R (see the exception 1 TBS below)
  • 1 TBS -use braces for statements if, else, while, for (exception from K&R) Reference:
  • One line per statement
  • Preprocessor macro starts at the beginning of a new line, the code inside is indented accordingly the code above it
  • Cases within switch are indented (exception from K&R)
  • Space after statements if, while, for, switch, same applies to binary and ternary operators
  • Each line has preferably at most 120 characters
  • For pointers, '*' is adjacent to a data name (analogin_t *obj) or a function name (analog_t *get_analogin_object())
  • Don't leave trailing spaces at the end of lines
  • Empty lines should have no trailing spaces
  • Unix line endings are default option for files
  • Use capital letters for macros
  • A file should have an empty line at the end

Naming convention


  • begins with a capital letter, and each word in it begins also with a capital letter (AnalogIn, BusInOut).
  • methods contain small letters, distinct words separated by underscore
  • private members starts with an underscore.

User defined types (typedef)

  • structures - suffix _t - to denote it is user defined type
  • enumeration - the type name and values name - same naming convention as classes (e.g MyNewEnum)


  • contain lower case letters (as methods within classes)
  • distinct words separated by underscore (wait_ms(), read_u16())


  • contain lower case letters
  • distinct words separated by underscore (result_adc)



class AnalogIn {
    /** Create an AnalogIn, connected to the specified pin
     * @param pin AnalogIn pin to connect to
     * @param name (optional) A string to identify the object
    AnalogIn(PinName pin) 
        analogin_init(&_adc, pin);

    /** Read the input voltage, represented as a float in the range [0.0, 1.0]
     * @returns 
     *    A floating-point value representing the current input voltage, measured as a percentage
    uint32_t read() 
        return analogin_read(&_adc, operation);

    analogin_t _adc;

typedef enum {
    ADC0_SE0 = (0 << ADC_INSTANCE_SHIFT) | 0,
} ADCName;

struct analogin_s {
    ADCName adc;

typedef struct analogin_s analogin_t;

Doxygen documentation

All functions/methods should contain a documentation using doxygen javadoc in a header file. More information regarding writing API Documentation, follow the link


#ifndef ADC_H
#define ADC_H

#ifdef __cplusplus
extern "C" {

/** ADC Measurement method
 *  @param obj Pointer to the analogin object.
 *  @param options Options to be enabled by ADC peripheral.
 *  @returns 
 *    Measurement value on defined ADC channel.
uint32_t adc_function(analogin_t *obj, uint32_t options) 

#ifdef __cplusplus


Source code indenter

In Mbed project you can use AStyle (Artistic Style) source code indenter to help you auto format your source code. It will for sure not correct all your coding styles but for sure will eliminate most of them. You can download AStyle from this location.

Official Mbed SDK styles include below AStyle styles (defined by command line switched):

--style=kr --indent=spaces=4 --indent-switches

To format your file you can execute below command. Just replace $(FULL_CURRENT_PATH) with path to your source file.

astyle.exe --style=kr --indent=spaces=4 --indent-switches $(FULL_CURRENT_PATH)

All wikipages