Seeing the code specifications written by others is very envious, so I also sorted out a code style of my own, used to standardize my own code, enhance readability, non-standard specifications. Forcing yourself to form a good coding style is conducive to the development of large-scale programs without clutter. Refer to the STM32 firmware library encoding style and FreeRTOS encoding style.
First, the organization structure of the project documents
The new project file should contain all or part of the following folders:
· usrSrc: User source file for storing .c files and other source files. Main.c should be placed here.
· usrInc: User header file used to store .h files.
· usrDoc: User documentation for storing documents written by the user during development, usually in .txt format. For example, Readme.txt, instructions, etc.
· Src: The source file of the reference library.
· Inc: The header file of the application library.
· Lib: The referenced library file. A project must contain a main.c file, which is only used to store the main function. The rest of the function definition should be in the corresponding .c file, declared in the corresponding .h file.
Source File
· File header, an introduction to the file
/************************************************* ************************
* 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 short.
* Revision : 1.01
* Author : Jimbo Zhang
* Date : 2017.03.10
* Update : Introduce the difference from previous version.**************************************** *********************************/
· Necessary comments and notes The source file should contain only its own header files, and other header files are 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 are declared with extern in the header file. The type definition is defined in the header file.
/* Includes ----------------------------------------------- ------------*/
/*only include it's own header file, the others header file included by USB_Ctrl.h*/#include "USB_Ctrl.h"
/* Declaration ----------------------------------------------- ---------*/
/*here are the local function declare, global fuction declare in header file.*/
Void delay( uint32_t n);
/* Global variable ---------------------------------------------- ------*/
· Function head
/************************************************* ****** Brief : Delay n ms* Parameter : * n: the number of delay microsecond.* Return : None.********************** *********************************/
Void delay( uint32_t n)
{ for( i=0; i<110; i++) ;
}
head File
· File header, an introduction to the file
/************************************************* ************************
- 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 comments and statements
/* Includes ----------------------------------------------- ------------*/
/* Define ----------------------------------------------- --------------*/
/* Typedef ----------------------------------------------- -------------*/typedef unsigned int apiStatus; //api return code
/* Enume ----------------------------------------------- ---------------*/
/* Extern ----------------------------------------------- --------------*/
/* Declaration ----------------------------------------------- ---------*/
Naming rules
Refer to the FreeRTOS naming convention, the MISRA C specification. - Try to use uint8_t , uint16_t , uint32_t , etc. when defining variables. The header file is stdint.h.
Typedef signed char int8_t;typedef short int16_t;typedef int int32_t;typedef long long int64_t;typedef unsigned char uint8_t;typedef unsigned short uint16_t;typedef unsigned int uint32_t;typedef unsigned long long uint64_t;
· Variables of type uint32_t use the prefix ul, where 'u' means 'unsigned' and 'l' means 'long'
· Variables of type uint16_t use the prefix us, where 'u' means 'unsigned' and 's' means 'short'
· Variables of type uint8_t use the prefix uc, where 'u' means 'unsigned' and 'c' means 'char'
· Enum type variables use the prefix e
· Pointer type variables are prefixed with p on the type. For example, the pointer variable to uint16_t is prefixed with pus.
· Consistent with the MISRA guidelines, char type variables are only allowed to hold ASCII characters with a prefix of c
· Consistent with the MISRA guidelines, char * type variables are only allowed to point to ASCII strings, prefixed with pc
· Macro definitions are all capitalized, and the two words are separated by an underscore.
· Objects with file scopes are as static as possible.
· Global variables are prefixed with 'g_'. Variables that can be used throughout the project are not limited to file scope.
Code style
· Indent: Indents use tabs, one tab is equal to 4 spaces.
· Note: The comment line does 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 snippet, the first part shows the file layout, and the second part shows the C code design format.
/* #defines, Add parentheses at reasonable locations. */ #define A_DEFINITION ( 1 )
/*
* followed by a static (file internal) function prototype,
* If the comment has multiple lines, refer to the comment style of this article - each line starts with '*'.
*/ static void prvAFunction( uint32_t ulParameter );
/* File scope variable (used internally in this file), before the function body definition. */ static BaseType_t xMyVariable.
/* The end of each function has a line of dashes, leaving a blank line between the dash and the first function below. */
/*------------------------------------------------ -----------*/
Void vAFunction( void )
{
The /* function body is defined here, be careful to enclose it in braces */
} /*----------------------------------------------- ------------*/
Static UBaseType_t prvNextFunction( void )
{
The /* function body is defined here. */
} /*----------------------------------------------- ------------*/
/*
* The function name always takes one line, including the return type. There is no space before the left parenthesis. There is a space after the left parenthesis.
* There is a space after each parameter, the name of the parameter should be descriptive.
*/ void vAnExampleFunction( long lParameter1, unsigned short usParameter2 )
{ / * Variable declarations are not indented. */
Uint8_t ucByte;
/* Code to align. Braces occupy a single line. */
For( ucByte = 0U; ucByte < fileBUFFER_LENGTH; ucByte++ )
{
/* Here again indented. */
}
}
/*
* The for, while, do, and if structures have similar patterns. There are no spaces between these keywords and the left parenthesis.
* There is a space after the left parenthesis, and a space before the right parenthesis, followed by a space after each semicolon.
* One space before and after each operator. Use parentheses to prioritize operators. 0 is not allowed
* A number other than the number (the number of devils) appears, if necessary, replace these numbers with a constant that can represent the meaning of the number or
* Macro definition.
*/ for( ucByte = 0U; ucByte < fileBUFFER_LENGTH; ucByte++ )
{
}
While( ucByte < fileBUFFER_LENGTH )
{
}
/*
* Due to the complexity of operator precedence, we can't believe that we are wary of operator precedence
* and can be used correctly, so use parentheses to prioritize priorities when working with multiple expressions
*/ 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 )
{
/* Add a counter for tracking to the TCB. */
pxNewTCB->uxTCBNumber = uxTaskNumber;
} #endif
/* Leave a space before and after the square brackets*/
ucBuffer[ 0 ] = 0U;
ucBuffer[ fileBUFFER_LENGTH - 1U ] = 0U;
Programming thinking
· Encapsulate code for a specific function into a function.
C language programming rules
Reference MISRA
· Rule1: Do not use the ternary operator (? : ).
· Rule2: There must be no discarded code that has been commented out.
· Rule3: All identifiers do not exceed 31 characters.
·
Rule4: Variable names in different namespaces must not be the same. For example: typedef struct MyStruct {... } MyStruct; (violation)
·
Struct Person {
Char* name;
...
};
Char name[32]; (violation)
· Rule5: Do not use char, int, float, double, long and other basic types, you should use the type defined in stdint.h to display the size of the representation type, such as uint16_t, int32_t.
· Rule6: The use of octal numbers is prohibited. (Because constants like 086U can easily cause misunderstandings).
• Rule7: Objects with the same name as an identifier in the external scope must not be defined to avoid obscuring identifiers in external scopes.
Rule8: Objects with file scope are as static as possible.
· Rule9: Automatic objects (stack objects) must be assigned initial values ​​before use.
·
Rule10: The right expression of the operators && and || must not have a side-effect. That is, expressions like if (x == 20 && ++y == 19) are disabled.
·
·
Rule11: Bit operations must not be applied to signed numbers. For example, 1 << 4 will be disabled and 1UL << 4 must be written.
·
· Rule12: The sizeof operator must not be applied to expressions that have side effects.
· Rule13: Do not use comma expressions except for loop control statements.
· Rule14: The equality and inequality of floating point numbers must not be explicitly judged.
· Rule15: Do not leave the code "never used".
· Rule16: Labels must not be used except for switch statements.
· Rule17: Do not use goto.
· Rule18: Do not use continue.
· Rule19: except for the switch statement, you must not use break
Rule20: if, else if, else, while, do..while, the for statement block must be enclosed in {}.
· Rule21: The value of the loop counter must not be modified in the loop body.
· Rule22: Disable any direct and indirect recursive function calls.
· Rule23: #undef should not be used.
· Rule24: Macros must not be passed as arguments to macro functions.
· Rule25: The # or ## symbol can only appear once in a macro definition.
· Rule26: Disable pointer arithmetic (instead of array subscript operation).
· Rule27: Disable more than two levels of pointers.
· Rule28: Prohibit the use of non-constant pointers to functions.
· Rule29: It is forbidden to use setjmp, longjmp.
· Rule30: It is forbidden to use atoi, atof, atol. (I agree with this, it is recommended to use strtol, strtod and other functions)
· Rule31: Abort, exit, getenv are prohibited.
Elliptical gear flowmeter (also known as displacement flowmeter and gear flowmeter) belongs to a kind of positive displacement flowmeter, which has high precision in flow instruments. It uses mechanical measuring elements to continuously divide the fluid into a single known volume part, and measures the total flow volume according to the number of times that the metering chamber fills and discharges the fluid in the volume part step by step and repeatedly. Oval gear flowmeter can be made of different materials (cast iron, cast steel, 304 stainless steel and 316 stainless steel). It is suitable for flow measurement in chemical industry, petroleum, medicine, electric power, metallurgy, food and other industrial departments.
Ellipse Gear Flowmeter,Oval Gear Flow Meter,Gear Oval Flowmeter,Plastic Oval Gear Flowmeter
Taizhou Jiabo Instrument Technology Co., Ltd. , https://www.taizhoujbcbyq.com