"I envy the code specifications written by others, so I also sort out and summarize my own coding style to standardize my own code, enhance readability and non-standard specifications. Forcing yourself to form a good coding style is conducive to developing large-scale programs without being messy. Refer to STM32 firmware library coding style and FreeRTOS coding style.
1、 Organization structure of engineering documents
The new project file shall contain all or part of the following folders:
·Usrsrc: user source file, used to store. C files and other source files. Main. C should be here.
·Usrinc: user header file used to store. H files.
·Usrdoc: user description document, which is used to store documents written by users during development, generally in. TXT format. For example, readme.txt, instruction description, etc.
·SRC: refers to the source file of the library.
·Inc: header file of the application library.
·Lib: referenced library file. A project must contain a main. C file, which is only used to store the main function. The definitions of other functions should be in the corresponding. C file and declared in the corresponding. H file.
source file
·File header, file introduction
/*************************************************************************
* Copyright (c) 2017, Jimbo Zhang
* All rights reserved.
*
* File name : USB_ Ctrl.c
* Brief : USB API source code.
* Introduce the main function or content of this document briefly.
* Revision : 1.01
* Author : Jimbo Zhang
* Date : 2017.03.10
* Update : Introduce the difference from previous version.*************************************************************************/
·The source file should contain only its own header file, and other header files should be included in its own header file. Only local functions are declared in the source file, and global functions are declared in the header file. Global variables are defined in the corresponding source file and declared with extern in the header file. The type definition is defined in the header file.
/* Includes -----------------------------------------------------------*/
/*only include it's own header file, theothers header fileincluded byUSB_ Ctrl.h*/#include ""USB_ Ctrl.h""
/* Declaration --------------------------------------------------------*/
/*here are thelocalfunction declare, globalfuction declare inheader file.*/
void delay( uint32_ t n);
/* Global variable ----------------------------------------------------*/
·Function header
/******************************************************* Brief : Delay n ms* Parameter : * n: the number of delay microsecond.* Return : None.*******************************************************/
void delay( uint32_ t n)
{for( i=0; i<110; i++) ;
}
Header file
·File header, file introduction
/*************************************************************************
- Copyright (c) 2017, Jimbo Zhang
- All rights reserved.
-
- File name : USB_ Ctrl.h
- Brief : The header file of USB_ Ctrl.c.
- Revision : 1.01
- Author : Jimbo Zhang
- Date : 2017.03.10
- Update : Introduce the difference from previous version.*************************************************************************/
·Necessary notes and statements
/* Includes -----------------------------------------------------------*/
/* Define -------------------------------------------------------------*/
/* Typedef ------------------------------------------------------------*/typedefunsignedintapiStatus; // api return code
/* Enume --------------------------------------------------------------*/
/* Extern -------------------------------------------------------------*/
/* Declaration --------------------------------------------------------*/
Naming rules
Refer to FreeRTOS naming rules, Misra C specification- Use uint8 when defining variables_ t 、uint16_ t 、uint32_ T et al. The header file is stdint. H.
typedefsignedcharint8_ t; typedefshortint16_ t; typedefintint32_ t; typedeflonglongint64_ t; typedefunsignedcharuint8_ t; typedefunsignedshortuint16_ t; typedefunsignedintuint32_ t; typedefunsignedlonglonguint64_ t;
·uint32_ Variables of type t use the prefix UL, where 'U' represents' unsigned 'and' l 'represents' long'
·uint16_ Variables of type t use the prefix us, where 'U' means' unsigned 'and's' means' short'
·uint8_ Variables of type t use the prefix UC, where 'U' represents' unsigned 'and' C 'represents' char'
·Enumeration type variables use the prefix E
·Pointer type variables are prefixed with P based on the type, such as uint16_ The pointer variable prefix of T is pus
·Consistent with Misra guidelines, char type variables are only allowed to save ASCII characters, prefixed with C
·Consistent with Misra guidelines, char * type variables are only allowed to point to ASCII strings prefixed with PC
·Macro definitions are all capitalized and two words are separated by underscores.
·Objects with file scope should be named static as much as possible.
·Global variables are prefixed with 'g_'. Variables that can be used in the whole project are not limited to the file scope.
Code style
·Indent: indent uses tabs. One tab equals 4 spaces.
·Note: Note lines shall not exceed 80 columns, except in special cases.
·Layout: the source code should be designed to be as easy to view and read as possible.
In the following code slice, the first part shows the file layout, and the second part shows the C code design format.
/*#definitions, add parentheses where appropriate. * / #define a_ DEFINITION ( 1 )
/*
*Then there is the static (internal file) function prototype,
*If the comment has multiple lines, refer to the comment style of this article - each line starts with '*'
*/staticvoidprvAFunction( uint32_ t ulParameter );
/*The file scope variable (used internally in this file) should be defined before the function body. * / staticbasetype_ t xMyVariable.
/*Each function ends with a line of dashes, leaving a blank line between the dashes and the first function below*/
/*-----------------------------------------------------------*/
voidvAFunction( void)
{
/*The function body is defined here, and be sure to enclose it in curly braces*/
} /*-----------------------------------------------------------*/
staticUBaseType_ t prvNextFunction( void)
{
/*The function body is defined here*/
} /*-----------------------------------------------------------*/
/*
*The function name always occupies one line, including the return type. There is no space before the left parenthesis, and there is a space after the left parenthesis,
*There is a space after each parameter, and the name of the parameter should be descriptive
*/voidvAnExampleFunction( longlParameter1, unsignedshortusParameter2 )
{/ * variable declarations are not indented*/
uint8_ t ucByte;
/*The code should be aligned. Curly braces occupy a single line*/
for( ucByte = 0U; ucByte < fileBUFFER_ LENGTH; ucByte++ )
{
/*Here we indent again*/
}
}
/*
*For, while, do and if structures have similar patterns. There is no space between these keywords and the left parenthesis.
*There is a space after the left bracket, a space before the right bracket, and a space after each semicolon.
*One space before and after each operator. Use parentheses to specify the priority of the operator. 0 is not allowed
*When other numbers (devil numbers) appear, replace these numbers with constants or numbers that can express the meaning of numbers if necessary
*Macro definition.
*/for( ucByte = 0U; ucByte < fileBUFFER_ LENGTH; ucByte++ )
{
}
while( ucByte < fileBUFFER_ LENGTH )
{
}
/*
*Due to the complexity of operator priority, we can't believe that we are always vigilant about operator priority
*And can be used correctly. Therefore, when calculating multiple expressions, use parentheses to clarify the priority order
*/if( ( ucByte < fileBUFFER_ LENGTH ) && ( ucByte != 0U ) )
{
ulResult = ( ( ulValue1 + ulValue2 ) - ulValue3 ) * ulValue4;
}
/*Conditional expressions should also be aligned like other code*/# if( configUSE_ TRACE_ FACILITY == 1 )
Our other product: