diff --git a/App/app_cfg.h b/App/app_cfg.h index 1e2054ea..7d89e76d 100644 --- a/App/app_cfg.h +++ b/App/app_cfg.h @@ -8,8 +8,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __APP_CFG_H -#define __APP_CFG_H +#ifndef APP_CFG_H +#define APP_CFG_H #ifdef __cplusplus extern "C" { @@ -33,4 +33,4 @@ extern "C" { } #endif -#endif /* __APP_CFG_H */ +#endif /* APP_CFG_H */ diff --git a/App/app_main.h b/App/app_main.h index 2f7b4450..83b952b4 100644 --- a/App/app_main.h +++ b/App/app_main.h @@ -8,8 +8,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __APP_MAIN_H -#define __APP_MAIN_H +#ifndef APP_MAIN_H +#define APP_MAIN_H #ifdef __cplusplus extern "C" { @@ -24,4 +24,4 @@ void app_main_run(void); } #endif -#endif /* __APP_MAIN_H */ +#endif /* APP_MAIN_H */ diff --git a/App/sys_clock.h b/App/sys_clock.h index 3e28c101..24fb4ec5 100644 --- a/App/sys_clock.h +++ b/App/sys_clock.h @@ -9,8 +9,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __SYS_CLOCK_H -#define __SYS_CLOCK_H +#ifndef SYS_CLOCK_H +#define SYS_CLOCK_H #ifdef __cplusplus extern "C" { @@ -72,4 +72,4 @@ int sys_clock_set_unix(uint32_t unix_secs); } #endif -#endif /* __SYS_CLOCK_H */ +#endif /* SYS_CLOCK_H */ diff --git a/App/sys_events.h b/App/sys_events.h index 1ba5c839..f930f3cd 100644 --- a/App/sys_events.h +++ b/App/sys_events.h @@ -9,8 +9,8 @@ * 创建日期:2026-08-29 */ -#ifndef __SYS_EVENTS_H -#define __SYS_EVENTS_H +#ifndef SYS_EVENTS_H +#define SYS_EVENTS_H #include "cmsis_os2.h" @@ -32,4 +32,4 @@ extern osEventFlagsId_t sys_evtHandle; } #endif -#endif /* __SYS_EVENTS_H */ +#endif /* SYS_EVENTS_H */ diff --git a/App/task/adc_task.h b/App/task/adc_task.h index 674ba3a7..7e5ccfa3 100644 --- a/App/task/adc_task.h +++ b/App/task/adc_task.h @@ -8,8 +8,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __ADC_TASK_H -#define __ADC_TASK_H +#ifndef ADC_TASK_H +#define ADC_TASK_H #ifdef __cplusplus extern "C" { @@ -22,4 +22,4 @@ void adc_task_func(void const *arg); } #endif -#endif /* __ADC_TASK_H */ +#endif /* ADC_TASK_H */ diff --git a/App/task/net_task.h b/App/task/net_task.h index 18a3db49..7baba844 100644 --- a/App/task/net_task.h +++ b/App/task/net_task.h @@ -9,8 +9,8 @@ * v1.1 2026-08-24 王建锋 提取 net_stack_bring_up() 供硬件层/NET 层两种测试模式共用 */ -#ifndef __NET_TASK_H -#define __NET_TASK_H +#ifndef NET_TASK_H +#define NET_TASK_H #ifdef __cplusplus extern "C" { @@ -28,4 +28,4 @@ void net_task_func(void const *arg); } #endif -#endif /* __NET_TASK_H */ +#endif /* NET_TASK_H */ diff --git a/App/task/rs485_task.h b/App/task/rs485_task.h index f4bde233..e224e968 100644 --- a/App/task/rs485_task.h +++ b/App/task/rs485_task.h @@ -8,8 +8,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __RS485_TASK_H -#define __RS485_TASK_H +#ifndef RS485_TASK_H +#define RS485_TASK_H #ifdef __cplusplus extern "C" { @@ -22,4 +22,4 @@ void rs485_task_func(void const *arg); } #endif -#endif /* __RS485_TASK_H */ +#endif /* RS485_TASK_H */ diff --git a/App/task/ui_task.h b/App/task/ui_task.h index 8048ad57..546b5670 100644 --- a/App/task/ui_task.h +++ b/App/task/ui_task.h @@ -7,8 +7,8 @@ * 修改记录: * 2026-08-29 王建锋 创建初始版本 */ -#ifndef __UI_TASK_H -#define __UI_TASK_H +#ifndef UI_TASK_H +#define UI_TASK_H #ifdef __cplusplus extern "C" { @@ -29,4 +29,4 @@ void ui_task_func(void *argument); } #endif -#endif /* __UI_TASK_H */ +#endif /* UI_TASK_H */ diff --git a/App/time_sync.h b/App/time_sync.h index 8cbe711a..e1b5c7ef 100644 --- a/App/time_sync.h +++ b/App/time_sync.h @@ -10,8 +10,8 @@ * v1.1 2026-08-28 王建锋 改为 TCP Server,迁入独立任务 timeSyncTask */ -#ifndef __TIME_SYNC_H -#define __TIME_SYNC_H +#ifndef TIME_SYNC_H +#define TIME_SYNC_H #include @@ -37,4 +37,4 @@ void timeSyncTask(void *argument); } #endif -#endif /* __TIME_SYNC_H */ +#endif /* TIME_SYNC_H */ diff --git a/App/util/crc.h b/App/util/crc.h index b0cb46b7..bc3e6787 100644 --- a/App/util/crc.h +++ b/App/util/crc.h @@ -8,8 +8,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __CRC_H -#define __CRC_H +#ifndef CRC_H +#define CRC_H #ifdef __cplusplus extern "C" { @@ -25,4 +25,4 @@ uint8_t crc8(const uint8_t *data, size_t len); } #endif -#endif /* __CRC_H */ +#endif /* CRC_H */ diff --git a/App/util/ringbuf.h b/App/util/ringbuf.h index f7c52ecb..4b256c34 100644 --- a/App/util/ringbuf.h +++ b/App/util/ringbuf.h @@ -8,8 +8,8 @@ * v1.0 2026-07-19 王建锋 创建初始版本 */ -#ifndef __RINGBUF_H -#define __RINGBUF_H +#ifndef RINGBUF_H +#define RINGBUF_H #ifdef __cplusplus extern "C" { @@ -39,4 +39,4 @@ void ringbuf_reset(ringbuf_t *rb); } #endif -#endif /* __RINGBUF_H */ +#endif /* RINGBUF_H */ diff --git a/Drivers/BSP/CH395F/ch395f.h b/Drivers/BSP/CH395F/ch395f.h index c0526db6..81015ce1 100644 --- a/Drivers/BSP/CH395F/ch395f.h +++ b/Drivers/BSP/CH395F/ch395f.h @@ -9,8 +9,8 @@ * 2026-06-06 王建锋 创建初始版本,按代码规范重构 */ -#ifndef __CH395F_H -#define __CH395F_H +#ifndef CH395F_H +#define CH395F_H /* * 头文件包含区 - 仅包含本模块必需的头文件 @@ -719,4 +719,4 @@ void ch395f_get_unreach_info(uint8_t *p_buf); } #endif -#endif /* __CH395F_H */ +#endif /* CH395F_H */ diff --git a/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.h b/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.h index bb2f4237..8f1192e3 100644 --- a/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.h +++ b/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.h @@ -1,5 +1,5 @@ -#ifndef __GD5F2GQ5UE_H -#define __GD5F2GQ5UE_H +#ifndef GD5F2GQ5UE_H +#define GD5F2GQ5UE_H /* * 模块名称:GD5F2GQ5UE SPI NAND Flash 驱动 @@ -235,4 +235,4 @@ int32_t gd5f_check_ecc(void); } #endif -#endif /* __GD5F2GQ5UE_H */ +#endif /* GD5F2GQ5UE_H */ diff --git a/Drivers/BSP/GD5F2GQ5UE/nand_ftl.h b/Drivers/BSP/GD5F2GQ5UE/nand_ftl.h index 0735acd1..183c07ae 100644 --- a/Drivers/BSP/GD5F2GQ5UE/nand_ftl.h +++ b/Drivers/BSP/GD5F2GQ5UE/nand_ftl.h @@ -1,5 +1,5 @@ -#ifndef __NAND_FTL_H -#define __NAND_FTL_H +#ifndef NAND_FTL_H +#define NAND_FTL_H #ifdef __cplusplus extern "C" { diff --git a/Drivers/BSP/NET/net_config.h b/Drivers/BSP/NET/net_config.h index 853258a4..3d61cb85 100644 --- a/Drivers/BSP/NET/net_config.h +++ b/Drivers/BSP/NET/net_config.h @@ -8,8 +8,8 @@ * 修改记录: */ -#ifndef __NET_CONFIG_H -#define __NET_CONFIG_H +#ifndef NET_CONFIG_H +#define NET_CONFIG_H #ifdef __cplusplus extern "C" { @@ -101,4 +101,4 @@ extern "C" { } #endif -#endif /* __NET_CONFIG_H */ +#endif /* NET_CONFIG_H */ diff --git a/Drivers/BSP/NET/net_select.h b/Drivers/BSP/NET/net_select.h index fe31ca5d..a8e7c00a 100644 --- a/Drivers/BSP/NET/net_select.h +++ b/Drivers/BSP/NET/net_select.h @@ -8,8 +8,8 @@ * 修改记录: */ -#ifndef __NET_SELECT_H -#define __NET_SELECT_H +#ifndef NET_SELECT_H +#define NET_SELECT_H #ifdef __cplusplus extern "C" { @@ -111,4 +111,4 @@ int net_poll_events(net_pollfd_t *fds, int nfds, int timeout); } #endif -#endif /* __NET_SELECT_H */ +#endif /* NET_SELECT_H */ diff --git a/Drivers/BSP/NET/net_socket.h b/Drivers/BSP/NET/net_socket.h index 4a338d53..febd883d 100644 --- a/Drivers/BSP/NET/net_socket.h +++ b/Drivers/BSP/NET/net_socket.h @@ -8,8 +8,8 @@ * 修改记录: */ -#ifndef __NET_SOCKET_H -#define __NET_SOCKET_H +#ifndef NET_SOCKET_H +#define NET_SOCKET_H #ifdef __cplusplus extern "C" { @@ -340,4 +340,4 @@ int net_listen_locked(int sockfd); } #endif -#endif /* __NET_SOCKET_H */ +#endif /* NET_SOCKET_H */ diff --git a/Drivers/BSP/NET/net_types.h b/Drivers/BSP/NET/net_types.h index 7af37f89..c02690e8 100644 --- a/Drivers/BSP/NET/net_types.h +++ b/Drivers/BSP/NET/net_types.h @@ -8,8 +8,8 @@ * 修改记录: */ -#ifndef __NET_TYPES_H -#define __NET_TYPES_H +#ifndef NET_TYPES_H +#define NET_TYPES_H #ifdef __cplusplus extern "C" { @@ -175,4 +175,4 @@ typedef struct { } #endif -#endif /* __NET_TYPES_H */ +#endif /* NET_TYPES_H */ diff --git a/Drivers/BSP/RS485/rs485.h b/Drivers/BSP/RS485/rs485.h index 18964d5c..dbff9088 100644 --- a/Drivers/BSP/RS485/rs485.h +++ b/Drivers/BSP/RS485/rs485.h @@ -11,8 +11,8 @@ * 2026-07-18 王建锋 创建初始版本 */ -#ifndef __RS485_H -#define __RS485_H +#ifndef RS485_H +#define RS485_H #ifdef __cplusplus extern "C" { @@ -123,4 +123,4 @@ void rs485_on_rx(uint8_t *buf, uint16_t size); } #endif -#endif /* __RS485_H */ +#endif /* RS485_H */ diff --git a/Drivers/BSP/SD2506/sd2506.h b/Drivers/BSP/SD2506/sd2506.h index a34fee96..502cf331 100644 --- a/Drivers/BSP/SD2506/sd2506.h +++ b/Drivers/BSP/SD2506/sd2506.h @@ -9,8 +9,8 @@ * 2026-07-17 王建锋 创建初始版本,参考 SD2506API-G Ver2.0 手册 */ -#ifndef __SD2506_H -#define __SD2506_H +#ifndef SD2506_H +#define SD2506_H #ifdef __cplusplus extern "C" { @@ -293,4 +293,4 @@ uint8_t sd2506_dec_to_bcd(uint8_t dec); } #endif -#endif /* __SD2506_H */ +#endif /* SD2506_H */ diff --git a/Drivers/BSP/TPAFE5160/tpafe5160.h b/Drivers/BSP/TPAFE5160/tpafe5160.h index 6ec43090..62d78f7d 100644 --- a/Drivers/BSP/TPAFE5160/tpafe5160.h +++ b/Drivers/BSP/TPAFE5160/tpafe5160.h @@ -1,5 +1,5 @@ -#ifndef __TPAFE5160_H -#define __TPAFE5160_H +#ifndef TPAFE5160_H +#define TPAFE5160_H /* * 模块名称:TPAFE5160 16位8通道同步采样ADC驱动 @@ -230,4 +230,4 @@ void tpafe5160_exti_handler(uint16_t GPIO_Pin); } #endif -#endif /* __TPAFE5160_H */ +#endif /* TPAFE5160_H */ diff --git a/Drivers/BSP/UI/ui_menu.h b/Drivers/BSP/UI/ui_menu.h index b7515368..d1c1c42e 100644 --- a/Drivers/BSP/UI/ui_menu.h +++ b/Drivers/BSP/UI/ui_menu.h @@ -7,8 +7,8 @@ * 修改记录: * 2026-08-29 王建锋 创建初始版本 */ -#ifndef __UI_MENU_H -#define __UI_MENU_H +#ifndef UI_MENU_H +#define UI_MENU_H #ifdef __cplusplus extern "C" { @@ -29,4 +29,4 @@ int ui_menu_init(void); } #endif -#endif /* __UI_MENU_H */ +#endif /* UI_MENU_H */ diff --git a/Drivers/BSP/UI/ui_net_sink.h b/Drivers/BSP/UI/ui_net_sink.h index b7d95c1d..d3193b0a 100644 --- a/Drivers/BSP/UI/ui_net_sink.h +++ b/Drivers/BSP/UI/ui_net_sink.h @@ -8,8 +8,8 @@ * 2026-08-29 王建锋 创建初始版本 * 2026-08-29 王建锋 修正监听端口为 7003,与 DTU-RemoteLCD 客户端默认端口对齐 */ -#ifndef __UI_NET_SINK_H -#define __UI_NET_SINK_H +#ifndef UI_NET_SINK_H +#define UI_NET_SINK_H #include @@ -54,4 +54,4 @@ void ui_net_sink_poll(void); } #endif -#endif /* __UI_NET_SINK_H */ +#endif /* UI_NET_SINK_H */ diff --git a/Drivers/BSP/UI/ui_port.h b/Drivers/BSP/UI/ui_port.h index 26cc3881..786737ae 100644 --- a/Drivers/BSP/UI/ui_port.h +++ b/Drivers/BSP/UI/ui_port.h @@ -7,8 +7,8 @@ * 修改记录: * 2026-08-29 王建锋 创建初始版本 */ -#ifndef __UI_PORT_H -#define __UI_PORT_H +#ifndef UI_PORT_H +#define UI_PORT_H #include #include @@ -76,4 +76,4 @@ void ui_port_step(uint32_t tick_ms); } #endif -#endif /* __UI_PORT_H */ +#endif /* UI_PORT_H */ diff --git a/Drivers/BSP/dbg_cfg.h b/Drivers/BSP/dbg_cfg.h index f3ea1c6f..c39395f7 100644 --- a/Drivers/BSP/dbg_cfg.h +++ b/Drivers/BSP/dbg_cfg.h @@ -15,8 +15,8 @@ * 2026-07-18 王建锋 创建初始版本 */ -#ifndef __DBG_CFG_H -#define __DBG_CFG_H +#ifndef DBG_CFG_H +#define DBG_CFG_H #ifdef __cplusplus extern "C" { @@ -43,4 +43,4 @@ extern "C" { } #endif -#endif /* __DBG_CFG_H */ +#endif /* DBG_CFG_H */ diff --git a/Drivers/BSP/dbg_log.h b/Drivers/BSP/dbg_log.h index 9cd7abf5..1b51e5bf 100644 --- a/Drivers/BSP/dbg_log.h +++ b/Drivers/BSP/dbg_log.h @@ -14,8 +14,8 @@ * 2026-07-18 王建锋 创建初始版本 */ -#ifndef __DBG_LOG_H -#define __DBG_LOG_H +#ifndef DBG_LOG_H +#define DBG_LOG_H #ifdef __cplusplus extern "C" { @@ -135,4 +135,4 @@ extern "C" { } #endif -#endif /* __DBG_LOG_H */ +#endif /* DBG_LOG_H */ diff --git a/docs/代码规范/pre-commit使用说明.md b/docs/代码规范/pre-commit使用说明.md index 594077bc..25454b98 100644 --- a/docs/代码规范/pre-commit使用说明.md +++ b/docs/代码规范/pre-commit使用说明.md @@ -126,7 +126,7 @@ cppcheck 是开源 C/C++ 静态分析工具,**不运行代码**,通过语法 | 编号 | 规范条款 | 规则 | |---|---|---| -| S1 | 2.3.1 | .h 文件必须有 `__XXX_H` 格式头文件保护宏 | +| S1 | 2.3.1 | .h 文件必须有 `XXX_H` 格式头文件保护宏(禁止 `__` 前缀) | | S2 | 5.2 | 文件开头必须有文件头注释(含 模块名称/作者) | | S3 | 3.4.1 | `#define` 宏名必须全大写+下划线+数字 | | S4 | 3.5.1 | typedef 类型名必须以 `_t` 结尾 | diff --git a/docs/代码规范/嵌入式C语言代码规范(V1.0).md b/docs/代码规范/嵌入式C语言代码规范(V1.0).md index 91cb4ceb..712a129a 100644 --- a/docs/代码规范/嵌入式C语言代码规范(V1.0).md +++ b/docs/代码规范/嵌入式C语言代码规范(V1.0).md @@ -8,12 +8,13 @@ | 版本号 | 修订日期 | 修订内容 | 修订人 | | ---- | ---------- | ------ | --- | | V1.0 | 2026-05-11 | 创建初始版本 | 王建锋 | +| V1.0.1 | 2026-09-01 | 修复示例与规则矛盾(括号风格、示例类型、变量初始化、硬编码、注释补全);规则定案:保护宏改`XXX_H`、常量在右、头文件常量用`#define`、存储类前缀优先、允许`void`返回值 | 王建锋 | ## 2. 文件结构规范 ### 2.1 头文件(.h)结构 ```c -#ifndef __MODULE_NAME_H -#define __MODULE_NAME_H +#ifndef MODULE_NAME_H +#define MODULE_NAME_H /* * 模块名称:模块英文名称 @@ -30,7 +31,7 @@ extern "C" { #endif /* 头文件包含区 - 仅包含本模块必需的头文件 */ -#include "stdint.h" +#include /* 宏定义区 */ #define MODULE_NAME_CONSTANT 100 @@ -48,7 +49,7 @@ void module_name_init(void); } #endif -#endif /* __MODULE_NAME_H */ +#endif /* MODULE_NAME_H */ ``` ### 2.2 源文件(.c)结构 ```c @@ -74,10 +75,10 @@ typedef struct { } 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); @@ -92,7 +93,7 @@ static void module_name_private_function(void) { } ``` ### 2.3 头文件包含规则 -1. 头文件必须包含头文件保护宏,格式为`__MODULE_NAME_H` +1. 头文件必须包含头文件保护宏,格式为`MODULE_NAME_H`(不得使用`__`双下划线前缀,其为C标准保留标识符) 2. 头文件中只包含本模块接口必需的其他头文件 3. 源文件中先包含系统头文件,再包含自定义头文件 4. 禁止在头文件中定义变量和函数体 @@ -111,13 +112,14 @@ static void module_name_private_function(void) { 4. 指针变量以`p_`前缀开头 5. 数组变量以`a_`前缀开头 6. 布尔变量以`is_`、`has_`、`can_`等前缀开头 +7. 前缀冲突时,存储类前缀(`g_`/`s_`)优先于类型前缀(`p_`/`a_`),如静态数组`s_uart_rx_buffer` **示例:** ```c -uint8_t g_system_status; /* 全局系统状态变量 */ -static uint16_t s_timer_count; /* 静态定时器计数变量 */ -uint8_t *p_data_buffer; /* 数据缓冲区指针 */ -uint16_t a_adc_value[10]; /* ADC采样值数组 */ -bool is_button_pressed; /* 按钮是否按下标志 */ +uint8_t g_system_status = 0; /* 全局系统状态变量 */ +static uint16_t s_timer_count = 0; /* 静态定时器计数变量 */ +uint8_t *p_data_buffer = NULL; /* 数据缓冲区指针 */ +uint16_t a_adc_value[10] = { 0 }; /* ADC采样值数组 */ +bool is_button_pressed = false; /* 按钮是否按下标志 */ ``` ### 3.3 函数命名 1. 采用**小写字母+下划线**命名法 @@ -136,13 +138,15 @@ static void uart_private_handle_interrupt(void); ### 3.4 宏和常量命名 1. 采用**大写字母+下划线**命名法 2. 以**模块名**作为前缀 -3. 常量优先使用`const`定义,而非`#define` +3. 头文件对外常量使用`#define`(见2.3.4);源文件内私有常量可用`const`,命名遵循3.2(小写+下划线) **示例:** ```c +/* 头文件对外常量:使用 #define */ #define UART_BAUD_RATE_9600 9600 #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 类型定义命名 1. 采用**小写字母+下划线**命名法 @@ -176,7 +180,11 @@ typedef struct { 5. 一元运算符前后不加空格 **正确示例:** ```c -int a = 10; +int32_t a = 10; +int32_t b = 0; +int32_t i = 0; +uint8_t c[10] = { 0 }; + if (a > 5) { b = a + 3; } @@ -186,7 +194,10 @@ for (i = 0; i < 10; i++) { ``` **错误示例:** ```c -int a=10; +int32_t a=10; +int32_t b=0; +int32_t i=0; +uint8_t c[10]={0}; if(a>5){ b=a+3; } @@ -202,8 +213,8 @@ for(i=0;i<10;i++){ 5. 长表达式应在运算符处换行,新行与运算符对齐 **示例:** ```c -int calculate_sum(int a, int b, int c, int d) { - int sum; +int32_t calculate_sum(int32_t a, int32_t b, int32_t c, int32_t d) { + int32_t sum = 0; sum = a + b + c @@ -260,21 +271,25 @@ while (1) ; ``` **示例:** ```c +#define DELAY_INNER_LOOP 123 /* 延时内层循环次数,按12MHz时钟整定 */ + /* * 函数功能:毫秒级软件延时函数 - * 入口参数:ms - 需要延时的毫秒数 unsigned int 0 - 65535 + * 入口参数:ms - 需要延时的毫秒数 uint32_t 0 - 65535 + * 出口参数:无 + * 返回值:无(void) * 限定条件:0 <= ms <= 65535 * 函数说明:1. 采用空指令循环方式实现延时,会阻塞CPU运行 - * 2. 延时精度依赖系统时钟,默认适配12MHz时钟(12T模式) - * 3. 系统时钟改变时,需重新调整内层循环次数 + * 2. 延时精度依赖系统时钟,默认适配12MHz时钟 + * 3. 系统时钟改变时,需重新调整内层循环次数(DELAY_INNER_LOOP) * 4. 当ms为0时,函数立即返回 */ -void delay_ms(unsigned int ms) { - unsigned int i; - unsigned int j; +void delay_ms(uint32_t ms) { + uint32_t i; + uint32_t j; 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 代码行注释 1. **关键逻辑代码每一行都要添加注释** 2. 复杂的算法和逻辑必须添加详细注释 -3. 注释应单独占一行,与被注释代码缩进级别相同 +3. 注释应单独占一行(变量定义的行内注释除外,见5.4.3),与被注释代码缩进级别相同 **示例:** ```c +#define CRC16_INITIAL 0xFFFF /* CRC-16初值 */ +#define CRC16_POLY_REFLECTED 0xA001 /* CRC-16/MCC反射多项式 */ +#define BITS_PER_BYTE 8 /* 每字节的位数 */ + /* 计算CRC校验值 */ -uint16_t crc_calculate(uint8_t *data, uint16_t length) -{ - uint16_t crc = 0xFFFF; +uint16_t crc_calculate(uint8_t *data, uint16_t length) { + uint16_t crc = CRC16_INITIAL; uint16_t i; uint16_t j; @@ -309,11 +327,11 @@ uint16_t crc_calculate(uint8_t *data, uint16_t length) crc ^= data[i]; /* 对每个位进行处理 */ - for (j = 0; j < 8; j++) { + for (j = 0; j < BITS_PER_BYTE; j++) { /* 检查最低位是否为1 */ if (crc & 0x0001) { /* 最低位为1,右移并与多项式异或 */ - crc = (crc >> 1) ^ 0xA001; + crc = (crc >> 1) ^ CRC16_POLY_REFLECTED; } else { /* 最低位为0,直接右移 */ crc = crc >> 1; @@ -334,12 +352,14 @@ uint16_t crc_calculate(uint8_t *data, uint16_t length) 5. 使用标准数据类型(`uint8_t`、`int32_t`等),避免使用`char`、`int`等不确定长度的类型 **示例:** ```c +#define DATA_BUF_SIZE 100 /* 数据缓冲区大小(字节) */ + void function(void) { uint8_t status = 0; uint16_t count = 0; uint8_t *p_data = NULL; - p_data = (uint8_t *)malloc(100); + p_data = (uint8_t *)malloc(DATA_BUF_SIZE); if (p_data == NULL) { return; } @@ -354,17 +374,17 @@ void function(void) { 1. 函数应遵循"单一职责原则",一个函数只做一件事 2. 函数长度不宜过长,一般不超过50行 3. 函数参数不宜过多,一般不超过5个 -4. 函数必须有明确的返回值,用于表示执行状态 +4. 有失败路径的函数必须有明确的返回值,用于表示执行状态;无失败路径的纯输出函数可返回`void` 5. 避免使用函数参数作为返回值 6. 私有函数必须声明为`static` ### 6.3 控制结构 -1. `if`语句中,常量应写在比较运算符的左边 +1. `if`语句中,常量应写在比较运算符的右边 2. `switch`语句必须包含`default`分支 3. 避免使用`goto`语句,除非用于错误处理 4. 循环嵌套不宜超过3层 **示例:** ```c -if (0 == status) { +if (status == 0) { do_something(); } @@ -423,8 +443,8 @@ uint8_t uart_send_data(uint8_t *data, uint16_t length) { ## 9. 附录 ### 9.1 完整示例代码 ```c -#ifndef __LED_H -#define __LED_H +#ifndef LED_H +#define LED_H /* * 模块名称:LED控制模块 @@ -440,7 +460,7 @@ uint8_t uart_send_data(uint8_t *data, uint16_t length) { extern "C" { #endif -#include "stdint.h" +#include /* LED编号定义 */ #define LED_NUM_1 0 @@ -491,5 +511,5 @@ uint8_t led_toggle(uint8_t led_num); } #endif -#endif /* __LED_H */ +#endif /* LED_H */ ``` diff --git a/scripts/check_coding_standard.py b/scripts/check_coding_standard.py index 71f9eb33..1990a803 100644 --- a/scripts/check_coding_standard.py +++ b/scripts/check_coding_standard.py @@ -5,7 +5,7 @@ C 语言代码规范检查(《嵌入式C语言代码规范(V1.0)》可机 由 pre-commit 调用:python scripts/check_coding_standard.py ,发现违规退出码 1。 检查项(括号内为规范条款号): - S1 (2.3.1) .h 文件必须有 __XXX_H 格式的头文件保护宏 + S1 (2.3.1) .h 文件必须有 XXX_H 格式的头文件保护宏(禁止 __ 双下划线前缀) S2 (5.2) 文件开头必须有文件头注释(含 模块名称 / 作者) S3 (3.4.1) #define 宏名必须全大写 + 下划线 + 数字 S4 (3.5.1) typedef 类型名必须以 _t 结尾 @@ -35,8 +35,8 @@ def check_file(path): # S1 (2.3.1) .h 头文件保护宏 if path.endswith('.h') and \ - not re.search(r'#\s*ifndef\s+__[A-Z0-9_]+_H\b', text): - v.append(('-', 'S1 2.3.1', '缺少 __XXX_H 格式的头文件保护宏')) + not re.search(r'#\s*ifndef\s+(?