修复规范矛盾并定案5项规则,保护宏统一XXX_H

规则定案:常量在右(a>5)、保护宏XXX_H(禁双下划线)、
头文件常量用#define、存储类前缀(g_/s_)优先、允许void返回值
全库32个.h头文件保护宏 __XXX_H → XXX_H 改名(字节级替换)
S1检查规则同步更新(拒绝双下划线),Inc/gpio.h 反向验证被正确拦截
规范示例矛盾修复:K&R括号、标准类型、变量初始化、命名常量、注释字段补全
This commit is contained in:
2026-09-01 16:07:00 +08:00
parent 2b66f96d01
commit bb76cfd779
35 changed files with 158 additions and 138 deletions
+3 -3
View File
@@ -8,8 +8,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __APP_CFG_H #ifndef APP_CFG_H
#define __APP_CFG_H #define APP_CFG_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -33,4 +33,4 @@ extern "C" {
} }
#endif #endif
#endif /* __APP_CFG_H */ #endif /* APP_CFG_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __APP_MAIN_H #ifndef APP_MAIN_H
#define __APP_MAIN_H #define APP_MAIN_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -24,4 +24,4 @@ void app_main_run(void);
} }
#endif #endif
#endif /* __APP_MAIN_H */ #endif /* APP_MAIN_H */
+3 -3
View File
@@ -9,8 +9,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __SYS_CLOCK_H #ifndef SYS_CLOCK_H
#define __SYS_CLOCK_H #define SYS_CLOCK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -72,4 +72,4 @@ int sys_clock_set_unix(uint32_t unix_secs);
} }
#endif #endif
#endif /* __SYS_CLOCK_H */ #endif /* SYS_CLOCK_H */
+3 -3
View File
@@ -9,8 +9,8 @@
* 创建日期:2026-08-29 * 创建日期:2026-08-29
*/ */
#ifndef __SYS_EVENTS_H #ifndef SYS_EVENTS_H
#define __SYS_EVENTS_H #define SYS_EVENTS_H
#include "cmsis_os2.h" #include "cmsis_os2.h"
@@ -32,4 +32,4 @@ extern osEventFlagsId_t sys_evtHandle;
} }
#endif #endif
#endif /* __SYS_EVENTS_H */ #endif /* SYS_EVENTS_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __ADC_TASK_H #ifndef ADC_TASK_H
#define __ADC_TASK_H #define ADC_TASK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -22,4 +22,4 @@ void adc_task_func(void const *arg);
} }
#endif #endif
#endif /* __ADC_TASK_H */ #endif /* ADC_TASK_H */
+3 -3
View File
@@ -9,8 +9,8 @@
* v1.1 2026-08-24 王建锋 提取 net_stack_bring_up() 供硬件层/NET 层两种测试模式共用 * v1.1 2026-08-24 王建锋 提取 net_stack_bring_up() 供硬件层/NET 层两种测试模式共用
*/ */
#ifndef __NET_TASK_H #ifndef NET_TASK_H
#define __NET_TASK_H #define NET_TASK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -28,4 +28,4 @@ void net_task_func(void const *arg);
} }
#endif #endif
#endif /* __NET_TASK_H */ #endif /* NET_TASK_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __RS485_TASK_H #ifndef RS485_TASK_H
#define __RS485_TASK_H #define RS485_TASK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -22,4 +22,4 @@ void rs485_task_func(void const *arg);
} }
#endif #endif
#endif /* __RS485_TASK_H */ #endif /* RS485_TASK_H */
+3 -3
View File
@@ -7,8 +7,8 @@
* 修改记录: * 修改记录:
* 2026-08-29 王建锋 创建初始版本 * 2026-08-29 王建锋 创建初始版本
*/ */
#ifndef __UI_TASK_H #ifndef UI_TASK_H
#define __UI_TASK_H #define UI_TASK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -29,4 +29,4 @@ void ui_task_func(void *argument);
} }
#endif #endif
#endif /* __UI_TASK_H */ #endif /* UI_TASK_H */
+3 -3
View File
@@ -10,8 +10,8 @@
* v1.1 2026-08-28 王建锋 改为 TCP Server,迁入独立任务 timeSyncTask * v1.1 2026-08-28 王建锋 改为 TCP Server,迁入独立任务 timeSyncTask
*/ */
#ifndef __TIME_SYNC_H #ifndef TIME_SYNC_H
#define __TIME_SYNC_H #define TIME_SYNC_H
#include <stdint.h> #include <stdint.h>
@@ -37,4 +37,4 @@ void timeSyncTask(void *argument);
} }
#endif #endif
#endif /* __TIME_SYNC_H */ #endif /* TIME_SYNC_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __CRC_H #ifndef CRC_H
#define __CRC_H #define CRC_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -25,4 +25,4 @@ uint8_t crc8(const uint8_t *data, size_t len);
} }
#endif #endif
#endif /* __CRC_H */ #endif /* CRC_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* v1.0 2026-07-19 王建锋 创建初始版本 * v1.0 2026-07-19 王建锋 创建初始版本
*/ */
#ifndef __RINGBUF_H #ifndef RINGBUF_H
#define __RINGBUF_H #define RINGBUF_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -39,4 +39,4 @@ void ringbuf_reset(ringbuf_t *rb);
} }
#endif #endif
#endif /* __RINGBUF_H */ #endif /* RINGBUF_H */
+3 -3
View File
@@ -9,8 +9,8 @@
* 2026-06-06 王建锋 创建初始版本,按代码规范重构 * 2026-06-06 王建锋 创建初始版本,按代码规范重构
*/ */
#ifndef __CH395F_H #ifndef CH395F_H
#define __CH395F_H #define CH395F_H
/* /*
* 头文件包含区 - 仅包含本模块必需的头文件 * 头文件包含区 - 仅包含本模块必需的头文件
@@ -719,4 +719,4 @@ void ch395f_get_unreach_info(uint8_t *p_buf);
} }
#endif #endif
#endif /* __CH395F_H */ #endif /* CH395F_H */
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef __GD5F2GQ5UE_H #ifndef GD5F2GQ5UE_H
#define __GD5F2GQ5UE_H #define GD5F2GQ5UE_H
/* /*
* 模块名称:GD5F2GQ5UE SPI NAND Flash 驱动 * 模块名称:GD5F2GQ5UE SPI NAND Flash 驱动
@@ -235,4 +235,4 @@ int32_t gd5f_check_ecc(void);
} }
#endif #endif
#endif /* __GD5F2GQ5UE_H */ #endif /* GD5F2GQ5UE_H */
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef __NAND_FTL_H #ifndef NAND_FTL_H
#define __NAND_FTL_H #define NAND_FTL_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
+3 -3
View File
@@ -8,8 +8,8 @@
* 修改记录: * 修改记录:
*/ */
#ifndef __NET_CONFIG_H #ifndef NET_CONFIG_H
#define __NET_CONFIG_H #define NET_CONFIG_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -101,4 +101,4 @@ extern "C" {
} }
#endif #endif
#endif /* __NET_CONFIG_H */ #endif /* NET_CONFIG_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* 修改记录: * 修改记录:
*/ */
#ifndef __NET_SELECT_H #ifndef NET_SELECT_H
#define __NET_SELECT_H #define NET_SELECT_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -111,4 +111,4 @@ int net_poll_events(net_pollfd_t *fds, int nfds, int timeout);
} }
#endif #endif
#endif /* __NET_SELECT_H */ #endif /* NET_SELECT_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* 修改记录: * 修改记录:
*/ */
#ifndef __NET_SOCKET_H #ifndef NET_SOCKET_H
#define __NET_SOCKET_H #define NET_SOCKET_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -340,4 +340,4 @@ int net_listen_locked(int sockfd);
} }
#endif #endif
#endif /* __NET_SOCKET_H */ #endif /* NET_SOCKET_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* 修改记录: * 修改记录:
*/ */
#ifndef __NET_TYPES_H #ifndef NET_TYPES_H
#define __NET_TYPES_H #define NET_TYPES_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -175,4 +175,4 @@ typedef struct {
} }
#endif #endif
#endif /* __NET_TYPES_H */ #endif /* NET_TYPES_H */
+3 -3
View File
@@ -11,8 +11,8 @@
* 2026-07-18 王建锋 创建初始版本 * 2026-07-18 王建锋 创建初始版本
*/ */
#ifndef __RS485_H #ifndef RS485_H
#define __RS485_H #define RS485_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -123,4 +123,4 @@ void rs485_on_rx(uint8_t *buf, uint16_t size);
} }
#endif #endif
#endif /* __RS485_H */ #endif /* RS485_H */
+3 -3
View File
@@ -9,8 +9,8 @@
* 2026-07-17 王建锋 创建初始版本,参考 SD2506API-G Ver2.0 手册 * 2026-07-17 王建锋 创建初始版本,参考 SD2506API-G Ver2.0 手册
*/ */
#ifndef __SD2506_H #ifndef SD2506_H
#define __SD2506_H #define SD2506_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -293,4 +293,4 @@ uint8_t sd2506_dec_to_bcd(uint8_t dec);
} }
#endif #endif
#endif /* __SD2506_H */ #endif /* SD2506_H */
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef __TPAFE5160_H #ifndef TPAFE5160_H
#define __TPAFE5160_H #define TPAFE5160_H
/* /*
* 模块名称:TPAFE5160 16位8通道同步采样ADC驱动 * 模块名称:TPAFE5160 16位8通道同步采样ADC驱动
@@ -230,4 +230,4 @@ void tpafe5160_exti_handler(uint16_t GPIO_Pin);
} }
#endif #endif
#endif /* __TPAFE5160_H */ #endif /* TPAFE5160_H */
+3 -3
View File
@@ -7,8 +7,8 @@
* 修改记录: * 修改记录:
* 2026-08-29 王建锋 创建初始版本 * 2026-08-29 王建锋 创建初始版本
*/ */
#ifndef __UI_MENU_H #ifndef UI_MENU_H
#define __UI_MENU_H #define UI_MENU_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -29,4 +29,4 @@ int ui_menu_init(void);
} }
#endif #endif
#endif /* __UI_MENU_H */ #endif /* UI_MENU_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* 2026-08-29 王建锋 创建初始版本 * 2026-08-29 王建锋 创建初始版本
* 2026-08-29 王建锋 修正监听端口为 7003,与 DTU-RemoteLCD 客户端默认端口对齐 * 2026-08-29 王建锋 修正监听端口为 7003,与 DTU-RemoteLCD 客户端默认端口对齐
*/ */
#ifndef __UI_NET_SINK_H #ifndef UI_NET_SINK_H
#define __UI_NET_SINK_H #define UI_NET_SINK_H
#include <stdint.h> #include <stdint.h>
@@ -54,4 +54,4 @@ void ui_net_sink_poll(void);
} }
#endif #endif
#endif /* __UI_NET_SINK_H */ #endif /* UI_NET_SINK_H */
+3 -3
View File
@@ -7,8 +7,8 @@
* 修改记录: * 修改记录:
* 2026-08-29 王建锋 创建初始版本 * 2026-08-29 王建锋 创建初始版本
*/ */
#ifndef __UI_PORT_H #ifndef UI_PORT_H
#define __UI_PORT_H #define UI_PORT_H
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
@@ -76,4 +76,4 @@ void ui_port_step(uint32_t tick_ms);
} }
#endif #endif
#endif /* __UI_PORT_H */ #endif /* UI_PORT_H */
+3 -3
View File
@@ -15,8 +15,8 @@
* 2026-07-18 王建锋 创建初始版本 * 2026-07-18 王建锋 创建初始版本
*/ */
#ifndef __DBG_CFG_H #ifndef DBG_CFG_H
#define __DBG_CFG_H #define DBG_CFG_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -43,4 +43,4 @@ extern "C" {
} }
#endif #endif
#endif /* __DBG_CFG_H */ #endif /* DBG_CFG_H */
+3 -3
View File
@@ -14,8 +14,8 @@
* 2026-07-18 王建锋 创建初始版本 * 2026-07-18 王建锋 创建初始版本
*/ */
#ifndef __DBG_LOG_H #ifndef DBG_LOG_H
#define __DBG_LOG_H #define DBG_LOG_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -135,4 +135,4 @@ extern "C" {
} }
#endif #endif
#endif /* __DBG_LOG_H */ #endif /* DBG_LOG_H */
+1 -1
View File
@@ -126,7 +126,7 @@ cppcheck 是开源 C/C++ 静态分析工具,**不运行代码**,通过语法
| 编号 | 规范条款 | 规则 | | 编号 | 规范条款 | 规则 |
|---|---|---| |---|---|---|
| S1 | 2.3.1 | .h 文件必须有 `__XXX_H` 格式头文件保护宏 | | S1 | 2.3.1 | .h 文件必须有 `XXX_H` 格式头文件保护宏(禁止 `__` 前缀) |
| S2 | 5.2 | 文件开头必须有文件头注释(含 模块名称/作者) | | S2 | 5.2 | 文件开头必须有文件头注释(含 模块名称/作者) |
| S3 | 3.4.1 | `#define` 宏名必须全大写+下划线+数字 | | S3 | 3.4.1 | `#define` 宏名必须全大写+下划线+数字 |
| S4 | 3.5.1 | typedef 类型名必须以 `_t` 结尾 | | S4 | 3.5.1 | typedef 类型名必须以 `_t` 结尾 |
@@ -8,12 +8,13 @@
| 版本号 | 修订日期 | 修订内容 | 修订人 | | 版本号 | 修订日期 | 修订内容 | 修订人 |
| ---- | ---------- | ------ | --- | | ---- | ---------- | ------ | --- |
| V1.0 | 2026-05-11 | 创建初始版本 | 王建锋 | | V1.0 | 2026-05-11 | 创建初始版本 | 王建锋 |
| V1.0.1 | 2026-09-01 | 修复示例与规则矛盾(括号风格、示例类型、变量初始化、硬编码、注释补全);规则定案:保护宏改`XXX_H`、常量在右、头文件常量用`#define`、存储类前缀优先、允许`void`返回值 | 王建锋 |
## 2. 文件结构规范 ## 2. 文件结构规范
### 2.1 头文件(.h)结构 ### 2.1 头文件(.h)结构
```c ```c
#ifndef __MODULE_NAME_H #ifndef MODULE_NAME_H
#define __MODULE_NAME_H #define MODULE_NAME_H
/* /*
* 模块名称:模块英文名称 * 模块名称:模块英文名称
@@ -30,7 +31,7 @@ extern "C" {
#endif #endif
/* 头文件包含区 - 仅包含本模块必需的头文件 */ /* 头文件包含区 - 仅包含本模块必需的头文件 */
#include "stdint.h" #include <stdint.h>
/* 宏定义区 */ /* 宏定义区 */
#define MODULE_NAME_CONSTANT 100 #define MODULE_NAME_CONSTANT 100
@@ -48,7 +49,7 @@ void module_name_init(void);
} }
#endif #endif
#endif /* __MODULE_NAME_H */ #endif /* MODULE_NAME_H */
``` ```
### 2.2 源文件(.c)结构 ### 2.2 源文件(.c)结构
```c ```c
@@ -74,10 +75,10 @@ typedef struct {
} module_name_private_t; } module_name_private_t;
/* 全局变量定义区 - 尽量避免使用全局变量 */ /* 全局变量定义区 - 尽量避免使用全局变量 */
uint8_t g_module_name_global_var = 0; uint8_t g_module_name_global_var = 0; /* 全局状态变量(示例) */
/* 静态变量定义区 */ /* 静态变量定义区 */
static module_name_private_t s_module_name_private_var; static module_name_private_t s_module_name_private_var; /* 私有状态变量(示例) */
/* 私有函数声明区 */ /* 私有函数声明区 */
static void module_name_private_function(void); static void module_name_private_function(void);
@@ -92,7 +93,7 @@ static void module_name_private_function(void) {
} }
``` ```
### 2.3 头文件包含规则 ### 2.3 头文件包含规则
1. 头文件必须包含头文件保护宏,格式为`__MODULE_NAME_H` 1. 头文件必须包含头文件保护宏,格式为`MODULE_NAME_H`(不得使用`__`双下划线前缀,其为C标准保留标识符)
2. 头文件中只包含本模块接口必需的其他头文件 2. 头文件中只包含本模块接口必需的其他头文件
3. 源文件中先包含系统头文件,再包含自定义头文件 3. 源文件中先包含系统头文件,再包含自定义头文件
4. 禁止在头文件中定义变量和函数体 4. 禁止在头文件中定义变量和函数体
@@ -111,13 +112,14 @@ static void module_name_private_function(void) {
4. 指针变量以`p_`前缀开头 4. 指针变量以`p_`前缀开头
5. 数组变量以`a_`前缀开头 5. 数组变量以`a_`前缀开头
6. 布尔变量以`is_`、`has_`、`can_`等前缀开头 6. 布尔变量以`is_`、`has_`、`can_`等前缀开头
7. 前缀冲突时,存储类前缀(`g_`/`s_`)优先于类型前缀(`p_`/`a_`),如静态数组`s_uart_rx_buffer`
**示例:** **示例:**
```c ```c
uint8_t g_system_status; /* 全局系统状态变量 */ uint8_t g_system_status = 0; /* 全局系统状态变量 */
static uint16_t s_timer_count; /* 静态定时器计数变量 */ static uint16_t s_timer_count = 0; /* 静态定时器计数变量 */
uint8_t *p_data_buffer; /* 数据缓冲区指针 */ uint8_t *p_data_buffer = NULL; /* 数据缓冲区指针 */
uint16_t a_adc_value[10]; /* ADC采样值数组 */ uint16_t a_adc_value[10] = { 0 }; /* ADC采样值数组 */
bool is_button_pressed; /* 按钮是否按下标志 */ bool is_button_pressed = false; /* 按钮是否按下标志 */
``` ```
### 3.3 函数命名 ### 3.3 函数命名
1. 采用**小写字母+下划线**命名法 1. 采用**小写字母+下划线**命名法
@@ -136,13 +138,15 @@ static void uart_private_handle_interrupt(void);
### 3.4 宏和常量命名 ### 3.4 宏和常量命名
1. 采用**大写字母+下划线**命名法 1. 采用**大写字母+下划线**命名法
2. 以**模块名**作为前缀 2. 以**模块名**作为前缀
3. 常量优先使用`const`定义,而非`#define` 3. 头文件对外常量使用`#define`(见2.3.4);源文件内私有常量可用`const`,命名遵循3.2(小写+下划线)
**示例:** **示例:**
```c ```c
/* 头文件对外常量:使用 #define */
#define UART_BAUD_RATE_9600 9600 #define UART_BAUD_RATE_9600 9600
#define UART_BUFFER_SIZE 128 #define UART_BUFFER_SIZE 128
const uint8_t UART_DEFAULT_DATA_BITS = 8; /* 源文件内私有常量:可用 const,命名遵循 3.2(小写) */
const uint8_t uart_default_data_bits = 8;
``` ```
### 3.5 类型定义命名 ### 3.5 类型定义命名
1. 采用**小写字母+下划线**命名法 1. 采用**小写字母+下划线**命名法
@@ -176,7 +180,11 @@ typedef struct {
5. 一元运算符前后不加空格 5. 一元运算符前后不加空格
**正确示例:** **正确示例:**
```c ```c
int a = 10; int32_t a = 10;
int32_t b = 0;
int32_t i = 0;
uint8_t c[10] = { 0 };
if (a > 5) { if (a > 5) {
b = a + 3; b = a + 3;
} }
@@ -186,7 +194,10 @@ for (i = 0; i < 10; i++) {
``` ```
**错误示例:** **错误示例:**
```c ```c
int a=10; int32_t a=10;
int32_t b=0;
int32_t i=0;
uint8_t c[10]={0};
if(a>5){ if(a>5){
b=a+3; b=a+3;
} }
@@ -202,8 +213,8 @@ for(i=0;i<10;i++){
5. 长表达式应在运算符处换行,新行与运算符对齐 5. 长表达式应在运算符处换行,新行与运算符对齐
**示例:** **示例:**
```c ```c
int calculate_sum(int a, int b, int c, int d) { int32_t calculate_sum(int32_t a, int32_t b, int32_t c, int32_t d) {
int sum; int32_t sum = 0;
sum = a + b sum = a + b
+ c + c
@@ -260,21 +271,25 @@ while (1) ;
``` ```
**示例:** **示例:**
```c ```c
#define DELAY_INNER_LOOP 123 /* 延时内层循环次数,按12MHz时钟整定 */
/* /*
* 函数功能:毫秒级软件延时函数 * 函数功能:毫秒级软件延时函数
* 入口参数:ms - 需要延时的毫秒数 unsigned int 0 - 65535 * 入口参数:ms - 需要延时的毫秒数 uint32_t 0 - 65535
* 出口参数:无
* 返回值:无(void)
* 限定条件:0 <= ms <= 65535 * 限定条件:0 <= ms <= 65535
* 函数说明:1. 采用空指令循环方式实现延时,会阻塞CPU运行 * 函数说明:1. 采用空指令循环方式实现延时,会阻塞CPU运行
* 2. 延时精度依赖系统时钟,默认适配12MHz时钟(12T模式) * 2. 延时精度依赖系统时钟,默认适配12MHz时钟
* 3. 系统时钟改变时,需重新调整内层循环次数 * 3. 系统时钟改变时,需重新调整内层循环次数(DELAY_INNER_LOOP)
* 4. 当ms为0时,函数立即返回 * 4. 当ms为0时,函数立即返回
*/ */
void delay_ms(unsigned int ms) { void delay_ms(uint32_t ms) {
unsigned int i; uint32_t i;
unsigned int j; uint32_t j;
for (i = 0; i < ms; i++) { for (i = 0; i < ms; i++) {
for (j = 0; j < 123; j++) { for (j = 0; j < DELAY_INNER_LOOP; j++) {
} }
} }
} }
@@ -293,13 +308,16 @@ static uint8_t s_uart_rx_buffer[UART_BUFFER_SIZE]; /* UART接收缓冲区 */
### 5.5 代码行注释 ### 5.5 代码行注释
1. **关键逻辑代码每一行都要添加注释** 1. **关键逻辑代码每一行都要添加注释**
2. 复杂的算法和逻辑必须添加详细注释 2. 复杂的算法和逻辑必须添加详细注释
3. 注释应单独占一行,与被注释代码缩进级别相同 3. 注释应单独占一行(变量定义的行内注释除外,见5.4.3),与被注释代码缩进级别相同
**示例:** **示例:**
```c ```c
#define CRC16_INITIAL 0xFFFF /* CRC-16初值 */
#define CRC16_POLY_REFLECTED 0xA001 /* CRC-16/MCC反射多项式 */
#define BITS_PER_BYTE 8 /* 每字节的位数 */
/* 计算CRC校验值 */ /* 计算CRC校验值 */
uint16_t crc_calculate(uint8_t *data, uint16_t length) uint16_t crc_calculate(uint8_t *data, uint16_t length) {
{ uint16_t crc = CRC16_INITIAL;
uint16_t crc = 0xFFFF;
uint16_t i; uint16_t i;
uint16_t j; uint16_t j;
@@ -309,11 +327,11 @@ uint16_t crc_calculate(uint8_t *data, uint16_t length)
crc ^= data[i]; crc ^= data[i];
/* 对每个位进行处理 */ /* 对每个位进行处理 */
for (j = 0; j < 8; j++) { for (j = 0; j < BITS_PER_BYTE; j++) {
/* 检查最低位是否为1 */ /* 检查最低位是否为1 */
if (crc & 0x0001) { if (crc & 0x0001) {
/* 最低位为1,右移并与多项式异或 */ /* 最低位为1,右移并与多项式异或 */
crc = (crc >> 1) ^ 0xA001; crc = (crc >> 1) ^ CRC16_POLY_REFLECTED;
} else { } else {
/* 最低位为0,直接右移 */ /* 最低位为0,直接右移 */
crc = crc >> 1; crc = crc >> 1;
@@ -334,12 +352,14 @@ uint16_t crc_calculate(uint8_t *data, uint16_t length)
5. 使用标准数据类型(`uint8_t`、`int32_t`等),避免使用`char`、`int`等不确定长度的类型 5. 使用标准数据类型(`uint8_t`、`int32_t`等),避免使用`char`、`int`等不确定长度的类型
**示例:** **示例:**
```c ```c
#define DATA_BUF_SIZE 100 /* 数据缓冲区大小(字节) */
void function(void) { void function(void) {
uint8_t status = 0; uint8_t status = 0;
uint16_t count = 0; uint16_t count = 0;
uint8_t *p_data = NULL; uint8_t *p_data = NULL;
p_data = (uint8_t *)malloc(100); p_data = (uint8_t *)malloc(DATA_BUF_SIZE);
if (p_data == NULL) { if (p_data == NULL) {
return; return;
} }
@@ -354,17 +374,17 @@ void function(void) {
1. 函数应遵循"单一职责原则",一个函数只做一件事 1. 函数应遵循"单一职责原则",一个函数只做一件事
2. 函数长度不宜过长,一般不超过50行 2. 函数长度不宜过长,一般不超过50行
3. 函数参数不宜过多,一般不超过5个 3. 函数参数不宜过多,一般不超过5个
4. 函数必须有明确的返回值,用于表示执行状态 4. 有失败路径的函数必须有明确的返回值,用于表示执行状态;无失败路径的纯输出函数可返回`void`
5. 避免使用函数参数作为返回值 5. 避免使用函数参数作为返回值
6. 私有函数必须声明为`static` 6. 私有函数必须声明为`static`
### 6.3 控制结构 ### 6.3 控制结构
1. `if`语句中,常量应写在比较运算符的左边 1. `if`语句中,常量应写在比较运算符的右边
2. `switch`语句必须包含`default`分支 2. `switch`语句必须包含`default`分支
3. 避免使用`goto`语句,除非用于错误处理 3. 避免使用`goto`语句,除非用于错误处理
4. 循环嵌套不宜超过3层 4. 循环嵌套不宜超过3层
**示例:** **示例:**
```c ```c
if (0 == status) { if (status == 0) {
do_something(); do_something();
} }
@@ -423,8 +443,8 @@ uint8_t uart_send_data(uint8_t *data, uint16_t length) {
## 9. 附录 ## 9. 附录
### 9.1 完整示例代码 ### 9.1 完整示例代码
```c ```c
#ifndef __LED_H #ifndef LED_H
#define __LED_H #define LED_H
/* /*
* 模块名称:LED控制模块 * 模块名称:LED控制模块
@@ -440,7 +460,7 @@ uint8_t uart_send_data(uint8_t *data, uint16_t length) {
extern "C" { extern "C" {
#endif #endif
#include "stdint.h" #include <stdint.h>
/* LED编号定义 */ /* LED编号定义 */
#define LED_NUM_1 0 #define LED_NUM_1 0
@@ -491,5 +511,5 @@ uint8_t led_toggle(uint8_t led_num);
} }
#endif #endif
#endif /* __LED_H */ #endif /* LED_H */
``` ```
+3 -3
View File
@@ -5,7 +5,7 @@ C 语言代码规范检查(《嵌入式C语言代码规范(V1.0)》可机
由 pre-commit 调用:python scripts/check_coding_standard.py <file...>,发现违规退出码 1。 由 pre-commit 调用:python scripts/check_coding_standard.py <file...>,发现违规退出码 1。
检查项(括号内为规范条款号): 检查项(括号内为规范条款号):
S1 (2.3.1) .h 文件必须有 __XXX_H 格式的头文件保护宏 S1 (2.3.1) .h 文件必须有 XXX_H 格式的头文件保护宏(禁止 __ 双下划线前缀)
S2 (5.2) 文件开头必须有文件头注释(含 模块名称 / 作者) S2 (5.2) 文件开头必须有文件头注释(含 模块名称 / 作者)
S3 (3.4.1) #define 宏名必须全大写 + 下划线 + 数字 S3 (3.4.1) #define 宏名必须全大写 + 下划线 + 数字
S4 (3.5.1) typedef 类型名必须以 _t 结尾 S4 (3.5.1) typedef 类型名必须以 _t 结尾
@@ -35,8 +35,8 @@ def check_file(path):
# S1 (2.3.1) .h 头文件保护宏 # S1 (2.3.1) .h 头文件保护宏
if path.endswith('.h') and \ if path.endswith('.h') and \
not re.search(r'#\s*ifndef\s+__[A-Z0-9_]+_H\b', text): not re.search(r'#\s*ifndef\s+(?<!_)[A-Z][A-Z0-9_]*_H\b', text):
v.append(('-', 'S1 2.3.1', '缺少 __XXX_H 格式的头文件保护宏')) v.append(('-', 'S1 2.3.1', '缺少 XXX_H 格式的头文件保护宏(禁止 __ 前缀)'))
# S2 (5.2) 文件头注释 # S2 (5.2) 文件头注释
head = '\n'.join(lines[:60]) head = '\n'.join(lines[:60])
+3 -3
View File
@@ -25,8 +25,8 @@
* Phase 11 | NET 层 | testTask:KeepAlive 超时断开测试 * Phase 11 | NET 层 | testTask:KeepAlive 超时断开测试
*/ */
#ifndef __CH395F_TEST_H #ifndef CH395F_TEST_H
#define __CH395F_TEST_H #define CH395F_TEST_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -104,4 +104,4 @@ extern test_stats_t g_test_stats;
} }
#endif #endif
#endif /* __CH395F_TEST_H */ #endif /* CH395F_TEST_H */
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef __CH395F_TEST_TASK_H #ifndef CH395F_TEST_TASK_H
#define __CH395F_TEST_TASK_H #define CH395F_TEST_TASK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -12,4 +12,4 @@ void ch395f_test_run(void);
} }
#endif #endif
#endif /* __CH395F_TEST_TASK_H */ #endif /* CH395F_TEST_TASK_H */
+3 -3
View File
@@ -22,8 +22,8 @@
* Phase 10 | 边界条件(offset 越界 / size=0 / 未擦除写入 / 写保护解除) * Phase 10 | 边界条件(offset 越界 / size=0 / 未擦除写入 / 写保护解除)
*/ */
#ifndef __GD5F_TEST_H #ifndef GD5F_TEST_H
#define __GD5F_TEST_H #define GD5F_TEST_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -86,4 +86,4 @@ void gd5f_test_run(void);
} }
#endif #endif
#endif /* __GD5F_TEST_H */ #endif /* GD5F_TEST_H */
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef __NET_TEST_TASK_H #ifndef NET_TEST_TASK_H
#define __NET_TEST_TASK_H #define NET_TEST_TASK_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -19,4 +19,4 @@ void net_layer_client_test_main(void);
} }
#endif #endif
#endif /* __NET_TEST_TASK_H */ #endif /* NET_TEST_TASK_H */
+3 -3
View File
@@ -8,8 +8,8 @@
* 创建日期:2026-08-26 * 创建日期:2026-08-26
*/ */
#ifndef __STORAGE_TEST_H #ifndef STORAGE_TEST_H
#define __STORAGE_TEST_H #define STORAGE_TEST_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -54,4 +54,4 @@ void storage_test_run(void);
} }
#endif #endif
#endif /* __STORAGE_TEST_H */ #endif /* STORAGE_TEST_H */
+3 -3
View File
@@ -13,8 +13,8 @@
* TEST_SUITE_GD5F : GD5F2GQ5UE NAND 驱动测试 * TEST_SUITE_GD5F : GD5F2GQ5UE NAND 驱动测试
*/ */
#ifndef __TEST_CONFIG_H #ifndef TEST_CONFIG_H
#define __TEST_CONFIG_H #define TEST_CONFIG_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -76,4 +76,4 @@ extern "C" {
} }
#endif #endif
#endif /* __TEST_CONFIG_H */ #endif /* TEST_CONFIG_H */