Beruflich Dokumente
Kultur Dokumente
TABLE OF CONTENTS
1. Introduction ....................................................................................................................................................................................... 3
1.1. Purpose ....................................................................................................................................................................................... 3
1.2. Definitions and Abbreviations............................................................................................................................................ 3
1.3. References.................................................................................................................................................................................. 3
2. Program structure ........................................................................................................................................................................... 3
2.1. Project organization ............................................................................................................................................................... 3
2.2. Header file structure .............................................................................................................................................................. 4
2.3. Source file structure ............................................................................................................................................................... 5
3. Naming convention ........................................................................................................................................................................ 7
3.1. General recommendations .................................................................................................................................................. 7
3.2. Files ............................................................................................................................................................................................... 7
3.3. Variables ..................................................................................................................................................................................... 7
3.4. Types ............................................................................................................................................................................................ 8
3.5. Marcos ......................................................................................................................................................................................... 9
3.6. Functions .................................................................................................................................................................................... 9
4. Source formatting ........................................................................................................................................................................... 9
4.1. Indentation ................................................................................................................................................................................ 9
4.2. Spaces ....................................................................................................................................................................................... 10
4.3. Comments ............................................................................................................................................................................... 10
4.4. Long lines ................................................................................................................................................................................ 11
4.5. Control flow statements .................................................................................................................................................... 11
4.6. Function prototypes and function implementations ............................................................................................. 11
4.7. Example .................................................................................................................................................................................... 12
5. Custom type definitions ............................................................................................................................................................ 15
5.1. Standard data types ............................................................................................................................................................ 15
5.2. Structure data type .............................................................................................................................................................. 15
6. Appendix.......................................................................................................................................................................................... 15
Page 2 of 15
1. Introduction
1.1. Purpose
This document provides guidance on selecting appropriate identifiers for elements as well as a
general structure for source files writing in the C programming language.
For consistency throughout your code and improved maintainability, you should consider using
these conventions consistently throughout your code.
Description
Note
1.3. References
List all reference documents such as related processes or check lists
Number
2. Program structure
2.1. Project organization
A standard firmware project should be organized as below:
Mt d n firmware chun c th c t chc nh di.
<PROJECT NAME>
CONFIG
Page 3 of 15
UTILS
SOFTWARE_FRAMEWORK
DRIVERS
<MODULE 1>
<MODULE N>
SERVICES
<SERVICE 1>
<SERVICE M>
main.c
Folder CONFIG cha cc header file c s dng cu hnh cho h thng
Folder UTILS cha cc file header v source chung.
Folder driver cha cc module c bit ging nh USB, ADC, SPI
Folder SERCICES cha cc hm API
Note: You should place related source files and header files in the same folder.
Page 4 of 15
Note: <Note>
********************************************************************************/
#ifndef <FILE_DESCRIPTOR>
#define <FILE_DESCRIPTOR>
/*-----------------------------------------------------------------------------*/
/* Header inclusions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Data type definitions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Constant definitions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Macro definitions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Global variables
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Function prototypes
*/
/*-----------------------------------------------------------------------------*/
#endif // <FILE_DESCRIPTOR>
Recommendations
Do NOT include any other header files in the header file except general purpose header files.
You can include them in the source file that uses the header instead.
Parameters in function prototypes should have full name, so that developers can easily learn
and use these functions.
The outer file description macro must be defined in order to prevent multiple definitions. It is
named based on the module name with some underscores. The form of this macro name is
__<MODULE_NAME>_H__ where <MODULE_NAME> is the module name in upper case.
Be sure to use the correct date format (DD-MMM-YY) and the developers full name in file
headers.
Page 5 of 15
Recommendations
Reduce using global variables as few as possible. It is recommended that you define global
variables in a separated source file.
Parameters in function implementation headers should have the same name as parameters
in function prototypes.
Page 6 of 15
3. Naming convention
3.1. General recommendations
Tt c tn, comment, m t phi c thc hin bng ting anh.
Chn cch d hiu nht t tn
3.2. Files
Tn source files v header file phi t bng ch in thng.
Tn file khng c cha nhiu hn 1 du ..
Tn file khng c qu 8 k t, v 3 k t m rng.
3.3. Variables
y l hng dn t tn bin theo chun Hungari. N c nh ngha cc tin t cho cc tn
bin.
Data type prefix
Description
Natural number
Integer number
bi
Bit
by
Byte
dw
Double words
Boolean
Character
Pointer
lp
Long pointer
ifp
efp
Page 7 of 15
ar Array bf Buffer h
Handle
str String regardless
of type
st Structure em
Enumeration
un Union
The type of a variable
member variable.
Description
g_
Global variable
s_
Static variable
m_
Class member variable
The name part can contains one or more words. The first letter of each word in the name part is
capitalized.
For example:
szUserName: Mt user name l 1 xu kt thc bng zero
lpszPath: L mt con tr Path , c tr vo xu c kt thc bng zero
g_pData: L 1 con tr ton cc tr vo block data
Note: Some popular variables may not follow this convention.
For example:
Variables
Description
i, j, k, l, m, n, x, y, z
Char variable
String variable
3.4. Types
Ngi lp trnh c th nh ngha kiu d liu mi thng qua t kha typedef. Tt c cc k t
ca n phi c vit hoa v khng c space hoc gch di.
For example:
DWORD: a double word data type
Page 8 of 15
Version 1.0
LPSTR: a long pointer string data type
DEVICEINFO: a structure contains information about a device
3.5. Marcos
Tt c cc k t khi nh ngha macros phi c vit hoa v ngn cch cc t bng k t _
For example:
#define DEV_MEMORY 1500
#define GET_ITEM(p, l, c, w) ((p) + (l)*(w) + (c))
Module
ZB_
ZigBee
PLC_
PLC
LCD_
LCD
RTC_
Real-time clock
USB_
3.6. Functions
The first letter in the identifier and the first letter of each subsequent concatenated word are
capitalized.
K t u tin ca t c vit hoa.
trnh conflicts th c th them tn module vo u tn hm <Module name>_<Function
name>.
Tn hm thng thc hin 1 tc v nn thng gm 1 ng t i tip theo l 1 danh t.
For example:
DisplayString(LPSTR lpszMessage)
Usart_SendBytes(LPBYTE lpBuffer)
GetDateFromDB()
ZB_SendData()
4. Source formatting
4.1. Indentation
Sa dng 1 tab hoc 4 space cho cch khi lnh dng lnh cho tng level
Page 9 of 15
Version 1.0
Nhng i tng phi t u tin trong file source:
-
Preprocessors
Function prototypes
Function implementations
4.2. Spaces
Spaces c s dng sau:
-
Gia tn hm v du (
Trc ( v sau ).
4.3. Comments
Trong ANSI C tt c ch thch c t trong /* v */ cn // khng phi l ANSI v khng
c h tr trn tt c cc trnh bin dch.
Khi comment nn s dung quy tc thi tht theo code pha trn. c vit bng ch
thng bng ting anh. L m t ngn gn ch nng, phng thc hot ng
Dng ch thch c th c t cng dng vi macros hoc code.
For example:
#define STATE_SELECTED
#define STATE_READY
#define STATE_STARTED
0
1
2
Page 10 of 15
Version 1.0
Page 11 of 15
Version 1.0
4.7. Example
/********************************************************************************
Copyright (C) 2014 - 2016 DangNQ . All rights reserved.
Product: EXAMPLE
Module: uart.c
Version: 1.0
Author: Nguyen Quoc Dang
Created: 29-Sep-08
Modified:
<Name>
<Date>
<Change>
Released: 01-Oct-08
Description: Including functions for UART communication
Note: <Note>
********************************************************************************/
/*-----------------------------------------------------------------------------*/
/* Header inclusions
*/
/*-----------------------------------------------------------------------------*/
#include <string.h>
#include <uart.h>
/*-----------------------------------------------------------------------------*/
/* Local Data type definitions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Local Constant definitions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Local Macro definitions
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Global variables
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Function prototypes
*/
Page 12 of 15
Version 1.0
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Function implementations
*/ /*----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------Function: void Uart_SetBaudrate(USHORT uSpeed);
Purpose: Set baudrate speed for serial communication.
Parameters: uSpeed
Return: none
Comments: The baudrate speed depends on frequency of crystal.
This project uses 11.0592 MHz crystal
Modified:
<Modified by>
<Date>
<Change>
--------------------------------------------------------------------------------*/
void Uart_SetBaudrate(USHORT uSpeed)
{
switch (uSpeed)
{
case 9600:
{
TH1 = LOBYTE(-3);
break;
}
case 4800:
{
TH1 = LOBYTE(-6);
break;
}
case 2400:
{
TH1
= LOBYTE(-12);
break;
}
}
TR1 = ON;
}
/*------------------------------------------------------------------------------Function: void Uart_Transmit(PSTR pszTransmit);
Purpose: Transmit a string via serial communication
Parameters: pszTransmit
Return: none
Comments: <Any addition comments>
Modified:
Page 13 of 15
Version 1.0
<Modified by>
<Date>
<Change>
-------------------------------------------------------------------------------*/
void Uart_Transmit(PSTR pszTransmit)
{
BYTE nCount, nLength;
nLength = strlen(pszTransmit);
Page 14 of 15
Version 1.0
for (nCount = 0; nCount < nLength; nCount++)
{
TI = OFF;
SBUF = pszTransmit[nCount];
while (TI == OFF);
TI = OFF;
}
}
6. Appendix
Page 15 of 15