Piconomic Logo www.piconomic.co.za

The Piconomic C coding style guide

Sections:

Introduction

A clear, consistent coding style improves readibility, reduces errors and improves maintainability. This page provides a concise guide to the C coding style used.

Modules

An object-like programming approach is recommended. This means that the data and functionality of an object is encapsulated in an H and C file. A module name must be chosen that is the same or consistent with the filename. This module name must be prefixed to all function names, structures, defines, enumerations, etc.
For example, see tmr.h : Polled software timers
    // Create a timer object
    tmr_t tmr;
    
    // Start timer with a 1s timeout
    tmr_start(&tmr, TMR_MS_TO_TICKS(1000));

Templates

Template files have been created for consistent organisation of each C module. See template.h, template.c & template.S

Comments

// Single line comment

/*
 * Multi-line comment
 *  
 * Detailed paragraph. 
 *  
 */
Doxygen is used to document each module. See Using Doxygen source documentation system.

Indentation and Brace style

The ANSI C style is followed. 4 spaces must be used for each indentation level, not tabs. Braces must be used, even for single lines.
    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            buffer[i][j] = 0;
        }
    }

Naming convention

Functions
Lower case letters with underscores separating words. The name must have the module name prefixed.
    extern bool_t tmr_has_expired(tmr_t *tmr);
Preprocessor #defines and Macros
Capital letters with underscores separating words. The name must have the module name prefixed.
    #define CRC16_CCITT_POLYNOMIAL  0x8408
    
    #define ARRAY_LENGTH(array)     (sizeof(array)/sizeof((array)[0]))
Variables
Lower case letters with underscores separating words. If the variable is declared outside function or structure scope, then it must have the module name prefixed.
    int nr_of_items;
Typedef
Lower case letters with underscores separating words. The name must have the module name prefixed. An "_t" suffix must be added to indicate that it is a typedef.
    /// Size definition of the tick counter
    typedef u16_t pit_ticks_t;
Structures
Lower case letters with underscores separating words. The name must have the module name prefixed. An "_s" suffix must be added to indicate that it is a struct.
    struct list_item_s
    {
        struct list_item_s *next_item;
        struct list_item_s *previous_item;
    };

Standard Types

Standard types have been defined and should be used to keep code portable. See common.h : Common definitions
    #include "common.h"
    
    typedef struct
    {
        u8_t  msg_type;
        u8_t  data[13];
        u16_t crc;
    } msg_t;
Generated on Fri Aug 13 16:50:36 2010 for Piconomic Firmware Library by doxygen 1.6.3