1*10465441SEvalZero RT-Thread Coding Style 2*10465441SEvalZero 3*10465441SEvalZeroThis is an developing instruction for RT-Thread developers. As an open source 4*10465441SEvalZerosoftware, RT-Thread is done by the cooperation of different people. This 5*10465441SEvalZerodocument is a guide for RT-Thread developers and please obey it if you are. 6*10465441SEvalZeroRT-Thread users could also get to know some conventions in the code through it 7*10465441SEvalZeroand thus easier to understand the implementations of RT-Thread. 8*10465441SEvalZero 9*10465441SEvalZero 10*10465441SEvalZero1. Directory Naming 11*10465441SEvalZero 12*10465441SEvalZeroIn normal conditions, please name directories in lower-case. Directories should 13*10465441SEvalZerohave descriptive names. For example, the port of a chip should be composed of 14*10465441SEvalZerothe name of the chip and the category of the chip. Directories under components/ 15*10465441SEvalZeroshould stand for what the component does. 16*10465441SEvalZero 17*10465441SEvalZero 18*10465441SEvalZero2. File Naming 19*10465441SEvalZero 20*10465441SEvalZeroIn normal conditions, please name files in lower-case. If the file is 21*10465441SEvalZeroreferencing other places, it can have the original name. To avoid naming 22*10465441SEvalZerocollision, do not use general names or the names that are frequently used. 23*10465441SEvalZero 24*10465441SEvalZero 25*10465441SEvalZero3. Header Files 26*10465441SEvalZero 27*10465441SEvalZeroTo avoid include the same header file for multiple times, you need to define a 28*10465441SEvalZerosymbol like this: 29*10465441SEvalZero 30*10465441SEvalZero #ifndef __FILE_H__ 31*10465441SEvalZero #define __FILE_H__ 32*10465441SEvalZero /* header file content */ 33*10465441SEvalZero #endif 34*10465441SEvalZero 35*10465441SEvalZeroThe symbol should begin and end with "__" to avoid naming collision. The words 36*10465441SEvalZeroof the file name should be connected by "_". 37*10465441SEvalZero 38*10465441SEvalZero 39*10465441SEvalZero4. Header File Comments 40*10465441SEvalZero 41*10465441SEvalZeroIn every header file, there should be copyright information and Change Log 42*10465441SEvalZerorecord like this: 43*10465441SEvalZero 44*10465441SEvalZero /* 45*10465441SEvalZero * File : rtthread.h 46*10465441SEvalZero * This file is part of RT-Thread RTOS 47*10465441SEvalZero * COPYRIGHT (C) 2006, RT-Thread Development Team 48*10465441SEvalZero * 49*10465441SEvalZero * The license and distribution terms for this file may be 50*10465441SEvalZero * found in the file LICENSE in this distribution or at 51*10465441SEvalZero * http://www.rt-thread.org/license/LICENSE. 52*10465441SEvalZero * 53*10465441SEvalZero * Change Logs: 54*10465441SEvalZero * Date Author Notes 55*10465441SEvalZero * 2006-03-18 Bernard the first version 56*10465441SEvalZero * 2006-04-26 Bernard add semaphore APIs 57*10465441SEvalZero * ... 58*10465441SEvalZero */ 59*10465441SEvalZero 60*10465441SEvalZero 61*10465441SEvalZero5. Structure Defines 62*10465441SEvalZero 63*10465441SEvalZeroPlease name structures in lower-case and connect words with "_". For example: 64*10465441SEvalZero 65*10465441SEvalZero struct rt_list_node 66*10465441SEvalZero { 67*10465441SEvalZero struct rt_list_node *next; 68*10465441SEvalZero struct rt_list_node *prev; 69*10465441SEvalZero }; 70*10465441SEvalZero 71*10465441SEvalZeroBraces should have their own lines and the members should be defines with 72*10465441SEvalZeroindent. 73*10465441SEvalZero 74*10465441SEvalZeroThe names of type defines such like structure types should be the structure name 75*10465441SEvalZeroplus "_t". For example: 76*10465441SEvalZero 77*10465441SEvalZero typedef struct rt_list_node rt_list_t; 78*10465441SEvalZero 79*10465441SEvalZero 80*10465441SEvalZeroIn order to be easily referenced, the types of objects in kernel is pointer. For 81*10465441SEvalZeroexample: 82*10465441SEvalZero 83*10465441SEvalZero typedef struct rt_timer* rt_timer_t; 84*10465441SEvalZero 85*10465441SEvalZero 86*10465441SEvalZero6. Macros 87*10465441SEvalZero 88*10465441SEvalZeroIn RT-Thread, please use upper-case names for macro definitions. Words are 89*10465441SEvalZeroconnected by "_". Like: 90*10465441SEvalZero 91*10465441SEvalZero #define RT_TRUE 1 92*10465441SEvalZero 93*10465441SEvalZero 94*10465441SEvalZero7. Function Naming and Declaration 95*10465441SEvalZero 96*10465441SEvalZeroPlease name functions in lower-case and separate words with "_". API provided to 97*10465441SEvalZeroupper application should be declared in header files. If the function don't have 98*10465441SEvalZeroparameters, it should be declared as void: 99*10465441SEvalZero 100*10465441SEvalZero rt_thread_t rt_thread_self(void); 101*10465441SEvalZero 102*10465441SEvalZero 103*10465441SEvalZero8. Commenting 104*10465441SEvalZero 105*10465441SEvalZeroPlease use English to comment. There shouldn't be too much comments and the 106*10465441SEvalZerocomments should describe what does the code do. And it should describe how the 107*10465441SEvalZerocomplicated algorithm works. Comments to statements should be placed before them 108*10465441SEvalZeroor right of them. Anther places are illegal. 109*10465441SEvalZero 110*10465441SEvalZero 111*10465441SEvalZero9. Indent 112*10465441SEvalZero 113*10465441SEvalZeroPlease use TAB or 4 spaces to indent. It's preferred to use 4 spaces. If no 114*10465441SEvalZeroother special meanings, the indent should begin right after "{": 115*10465441SEvalZero 116*10465441SEvalZero if (condition) 117*10465441SEvalZero { 118*10465441SEvalZero /* others */ 119*10465441SEvalZero } 120*10465441SEvalZero 121*10465441SEvalZeroThe only one exception is switch. In switch-case statements, "case" should be 122*10465441SEvalZeroaligned with "switch": 123*10465441SEvalZero 124*10465441SEvalZero switch (value) 125*10465441SEvalZero { 126*10465441SEvalZero case value1: 127*10465441SEvalZero break; 128*10465441SEvalZero } 129*10465441SEvalZero 130*10465441SEvalZero"case" is aligned with "switch", the following code block should be indented. 131*10465441SEvalZero 132*10465441SEvalZero 133*10465441SEvalZero10. Braces and Spaces 134*10465441SEvalZero 135*10465441SEvalZeroFor ease of reading, it is advised that braces should occupy the whole line 136*10465441SEvalZeroinstead of following other statements. Like: 137*10465441SEvalZero 138*10465441SEvalZero if (condition) 139*10465441SEvalZero { 140*10465441SEvalZero /* others */ 141*10465441SEvalZero } 142*10465441SEvalZero 143*10465441SEvalZeroWhen matching braces have their own lines, the reader would identify the code 144*10465441SEvalZeroblocks easily. 145*10465441SEvalZero 146*10465441SEvalZeroThere should be a space before parentheses when it's not a function call. For 147*10465441SEvalZeroexample: 148*10465441SEvalZero 149*10465441SEvalZero if (x <= y) 150*10465441SEvalZero { 151*10465441SEvalZero /* others */ 152*10465441SEvalZero } 153*10465441SEvalZero 154*10465441SEvalZero for (index = 0; index < MAX_NUMBER; index ++) 155*10465441SEvalZero { 156*10465441SEvalZero /* others */ 157*10465441SEvalZero } 158*10465441SEvalZero 159*10465441SEvalZeroIn expressions, there should be a space between most binary and ternary 160*10465441SEvalZerooperators and the strings. No spaces around(inside) parentheses, like: 161*10465441SEvalZero 162*10465441SEvalZero if ( x <= y ) 163*10465441SEvalZero { 164*10465441SEvalZero /* other */ 165*10465441SEvalZero } 166*10465441SEvalZero 167*10465441SEvalZeroThis is a bad practice. 168*10465441SEvalZero 169*10465441SEvalZero 170*10465441SEvalZero11. trace, log Informations 171*10465441SEvalZero 172*10465441SEvalZeroIn RT-Thread, rt_kprintf is a commonly used logging routine. In RT-Thread 173*10465441SEvalZerort_kprintf is implemented as a polling, non-interrupting string output. It is 174*10465441SEvalZerosuitable in "instant" situations such as interrupt context. The polling method 175*10465441SEvalZerowould have influence to the timing sequence of the log output. 176*10465441SEvalZero 177*10465441SEvalZeroIt is not recommended to use rt_kprintf frequently. Unless you are aware of that 178*10465441SEvalZeroit's not a big deal to run slower. 179*10465441SEvalZero 180*10465441SEvalZeroLogging should be off by default and can be turned on by a switch(e.g. a 181*10465441SEvalZerovariable or a macro). When logging, it should be easy to understand and easy to 182*10465441SEvalZerodetermine where the problem is. 183*10465441SEvalZero 184*10465441SEvalZero 185*10465441SEvalZero12. Functions 186*10465441SEvalZero 187*10465441SEvalZeroFunctions in kernel should be K.I.S.S. If the function is too long, you should 188*10465441SEvalZerosplit it into smaller ones and make each of them simplified and easy to 189*10465441SEvalZerounderstand. 190*10465441SEvalZero 191*10465441SEvalZero 192*10465441SEvalZero13. Objects 193*10465441SEvalZero 194*10465441SEvalZeroThe kernel of RT-Thread uses object-oriented tech in C. The naming convention 195*10465441SEvalZerois: structure names are the object names, object names + verb phrases are the 196*10465441SEvalZeromethod names of objects: 197*10465441SEvalZero 198*10465441SEvalZero struct rt_timer 199*10465441SEvalZero { 200*10465441SEvalZero struct rt_object parent; 201*10465441SEvalZero /* other fields */ 202*10465441SEvalZero }; 203*10465441SEvalZero typedef struct rt_timer* rt_timer_t; 204*10465441SEvalZero 205*10465441SEvalZeroThe definition of structure rt_timer stands for the object definition of timer 206*10465441SEvalZeroobject. 207*10465441SEvalZero 208*10465441SEvalZero rt_timer_t rt_timer_create(const char* name, 209*10465441SEvalZero void (*timeout)(void* parameter), void* parameter, 210*10465441SEvalZero rt_tick_t time, rt_uint8_t flag); 211*10465441SEvalZero rt_err_t rt_timer_delete(rt_timer_t timer); 212*10465441SEvalZero rt_err_t rt_timer_start(rt_timer_t timer); 213*10465441SEvalZero rt_err_t rt_timer_stop(rt_timer_t timer); 214*10465441SEvalZero 215*10465441SEvalZerort_timer + verb phrase stands for the method that could be used on timer object. 216*10465441SEvalZero 217*10465441SEvalZeroWhen creating a new object, think twice on memory allocations: whether a static 218*10465441SEvalZeroobject could be created or it could only created dynamically on heap. 219*10465441SEvalZero 220*10465441SEvalZero14. Use astyle to format the code automatically 221*10465441SEvalZeroparameters: --style=allman 222*10465441SEvalZero --indent=spaces=4 223*10465441SEvalZero --indent-preproc-block 224*10465441SEvalZero --pad-oper 225*10465441SEvalZero --pad-header 226*10465441SEvalZero --unpad-paren 227*10465441SEvalZero --suffix=none 228*10465441SEvalZero --align-pointer=name 229*10465441SEvalZero --lineend=linux 230*10465441SEvalZero --convert-tabs 231*10465441SEvalZero --verbose 232*10465441SEvalZero 233