From 461ea3f207f6e011a9b3fcbe04a110271e225171 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 12 Nov 2026 14:15:51 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E5=87=BD=E6=95=B0=E8=BF=94?= =?utf8?q?=E5=9B=9E=E5=80=BC=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- APP/BSP/dev_can.c | 9 +++-- APP/BSP/dev_can.h | 2 +- APP/dispboard.h | 2 + APP/err.h | 87 ++++++++++++++++++++++++++++++++++++++++++++ APP/mb/mb_phy.h | 2 +- APP/mb/mb_phy_can.c | 9 +++-- APP/mb/mb_phy_uart.c | 5 ++- APP/mb/mb_task.c | 2 +- APP/mb/mb_task.h | 12 ++---- APP/user_export.c | 28 +++++++------- APP/user_iap.h | 9 ++--- APP/user_rtc.c | 10 ++--- 12 files changed, 132 insertions(+), 45 deletions(-) create mode 100644 APP/err.h diff --git a/APP/BSP/dev_can.c b/APP/BSP/dev_can.c index f55caf2..13ad3eb 100644 --- a/APP/BSP/dev_can.c +++ b/APP/BSP/dev_can.c @@ -16,6 +16,7 @@ #include #include "dev_can.h" +#include "err.h" /* CAN 标识符 */ #define CAN_ID_MASTER 0x001 @@ -79,13 +80,13 @@ int can_send_frame(uint32_t id, const uint8_t *data, uint8_t len) if (len > CAN_MAX_DATA) len = CAN_MAX_DATA; if (id > 0x7FF) - return -1; + return ERR_PARAM; for (i = 0; i < len; i++) tx_data[i] = data[i]; if (CAN_TXBufferReady(CAN1) == 0) - return -1; + return ERR_BUSY; CAN_Transmit(CAN1, CAN_FRAME_STD, id, tx_data, len, 1); @@ -93,7 +94,7 @@ int can_send_frame(uint32_t id, const uint8_t *data, uint8_t len) while (CAN_TXComplete(CAN1) == 0) { if (CAN1->SR & CAN_SR_BUSOFF_Msk) { CAN_Open(CAN1); /* Bus Off 恢复 */ - return -1; + return ERR_COMM; } __NOP(); } @@ -102,7 +103,7 @@ int can_send_frame(uint32_t id, const uint8_t *data, uint8_t len) can_log_frame("TX", id, tx_data, len); #endif - return 0; + return ERR_OK; } void can_register_rx_callback(can_rx_callback_t cb) diff --git a/APP/BSP/dev_can.h b/APP/BSP/dev_can.h index 81dabfe..6e16826 100644 --- a/APP/BSP/dev_can.h +++ b/APP/BSP/dev_can.h @@ -40,7 +40,7 @@ void can_init(void); * @param id CAN 标识符 (11 位标准帧) * @param data 数据指针 (最多 8 字节) * @param len 数据长度 (0-8) - * @return 0 成功, -1 失败 + * @return ERR_OK 成功; <0 错误码 (ERR_PARAM / ERR_BUSY / ERR_COMM) */ int can_send_frame(uint32_t id, const uint8_t *data, uint8_t len); diff --git a/APP/dispboard.h b/APP/dispboard.h index 888f824..0b05058 100644 --- a/APP/dispboard.h +++ b/APP/dispboard.h @@ -4,6 +4,8 @@ #include #include +#include "err.h" + #include "mb_api.h" #include "device_model.h" diff --git a/APP/err.h b/APP/err.h new file mode 100644 index 0000000..983828d --- /dev/null +++ b/APP/err.h @@ -0,0 +1,87 @@ +/** + * @file err.h + * @brief 全工程统一错误码 / 返回码定义 + * + * 统一约定: + * - 函数返回值 >= 0 表示成功 / 有效数据(如读取数量、字节数) + * - 函数返回值 < 0 表示错误, 具体错误见下方 ERR_* 宏 + * - 所有错误的数值统一在本文件定义, 各模块原有的 *_OK / *_ERR_* / + * USER_IAP_* 命名为方便阅读的别名, 全部映射到本文件的统一错误码, + * 避免全局数值冲突。 + * + * 命名规则: + * - ERR_* : 统一错误码(唯一数值来源) + * - MB_ERR_* : Modbus 模块别名 + * - USER_IAP_* : IAP 升级模块别名 + */ + +#ifndef __ERR_H__ +#define __ERR_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** 统一的返回 / 错误码类型 */ +typedef int err_t; + +/* ======================== 通用错误码 ======================== */ +#define ERR_OK ( 0) /* 成功 */ +#define ERR_FAIL ( -1) /* 通用失败 */ +#define ERR_PARAM ( -2) /* 参数错误 */ +#define ERR_NULL ( -3) /* 空指针 / 非法参数 */ +#define ERR_STATE ( -4) /* 非法状态 / 状态不匹配 */ +#define ERR_INVALID ( -5) /* 数据 / 内容非法 */ +#define ERR_EXIST ( -6) /* 已存在 */ +#define ERR_NOT_FOUND ( -7) /* 未找到 */ +#define ERR_NO_DATA ( -8) /* 无数据 */ +#define ERR_NO_MEM ( -9) /* 内存不足 */ +#define ERR_BUSY (-10) /* 资源忙 */ +#define ERR_TIMEOUT (-11) /* 超时 */ +#define ERR_LOCKED (-12) /* 已锁定 / 不可用 */ + +/* ======================== IO / 文件 / 磁盘 ======================== */ +#define ERR_IO (-20) /* IO 错误 */ +#define ERR_DEV (-21) /* 设备错误 */ +#define ERR_OPEN (-22) /* 打开失败 */ +#define ERR_CLOSE (-23) /* 关闭失败 */ +#define ERR_READ (-24) /* 读取失败 */ +#define ERR_WRITE (-25) /* 写入失败 */ +#define ERR_MKDIR (-26) /* 创建目录失败 */ +#define ERR_MOUNT (-27) /* 挂载失败 */ +#define ERR_NO_DISK (-28) /* 磁盘未就绪 / 未挂载 */ +#define ERR_NO_FILE (-29) /* 文件不存在 */ +#define ERR_CRC (-30) /* 校验失败 */ +#define ERR_PROG (-31) /* 烧录 / 写入从站失败 */ +#define ERR_VERIFY (-32) /* 校验比对失败 */ + +/* ======================== 通信 / 协议 ======================== */ +#define ERR_COMM (-40) /* 通信错误 */ +#define ERR_PROTO (-41) /* 协议错误 */ +#define ERR_NO_RESP (-42) /* 无响应 */ +#define ERR_EXCEPTION (-43) /* 从站异常响应 */ +#define ERR_DOWNLOAD (-44) /* 下载 / 传输失败 */ +#define ERR_BAD_FORMAT (-45) /* 格式错误 */ + +/* ======================== Modbus 模块别名 ======================== */ +#define MB_OK ERR_OK +#define MB_ERR_TIMEOUT ERR_TIMEOUT +#define MB_ERR_CRC ERR_CRC +#define MB_ERR_PARAM ERR_PARAM +#define MB_ERR_EXCEPTION ERR_EXCEPTION +#define MB_ERR_BUSY ERR_BUSY +#define MB_ERR_NO_RESP ERR_NO_RESP + +/* ======================== IAP 升级模块别名 ======================== */ +#define USER_IAP_OK ERR_OK +#define USER_IAP_BUSY ERR_BUSY +#define USER_IAP_NO_FILE ERR_NO_FILE +#define USER_IAP_NO_UFDISK ERR_NO_DISK +#define USER_IAP_DOWNLOAD ERR_DOWNLOAD +#define USER_IAP_BAD_FORMAT ERR_BAD_FORMAT + +#ifdef __cplusplus +} +#endif + +#endif /* __ERR_H__ */ \ No newline at end of file diff --git a/APP/mb/mb_phy.h b/APP/mb/mb_phy.h index 959173f..ae93879 100644 --- a/APP/mb/mb_phy.h +++ b/APP/mb/mb_phy.h @@ -22,7 +22,7 @@ * @brief 发送一整帧字节流 (阻塞直到发送完成) * @param data 数据指针 * @param len 长度 - * @return 0 成功, -1 失败 + * @return ERR_OK 成功; <0 错误码 (ERR_*) */ int mb_phy_send(const uint8_t *data, uint16_t len); diff --git a/APP/mb/mb_phy_can.c b/APP/mb/mb_phy_can.c index 0b1573c..ec77020 100644 --- a/APP/mb/mb_phy_can.c +++ b/APP/mb/mb_phy_can.c @@ -23,6 +23,7 @@ #include "mb_phy.h" #include "mb_config.h" +#include "err.h" #include #include @@ -160,7 +161,7 @@ int mb_phy_send(const uint8_t *data, uint16_t len) uint16_t offset = 0; if (!data || len == 0) - return -1; + return ERR_PARAM; /* 分片发送: 每包 6 字节, 最后一包按实际长度 */ if (s_rx_q) @@ -187,9 +188,9 @@ int mb_phy_send(const uint8_t *data, uint16_t len) offset += chunk_len; if (can_send_frame(s_tx_id, can_data, 2 + chunk_len) < 0) - return -1; + return ERR_COMM; } - return 0; + return ERR_OK; } uint16_t mb_phy_rx_copy(uint8_t *dst, uint16_t max) @@ -229,4 +230,4 @@ void mb_phy_stop(void) can_register_rx_callback(NULL); reassembly_reset(); } -#endif /* MB_PHY_CAN_EN */ +#endif /* MB_PHY_CAN_EN */ \ No newline at end of file diff --git a/APP/mb/mb_phy_uart.c b/APP/mb/mb_phy_uart.c index 22b2ad3..ec8aada 100644 --- a/APP/mb/mb_phy_uart.c +++ b/APP/mb/mb_phy_uart.c @@ -11,6 +11,7 @@ #include "mb_phy.h" #include "mb_config.h" +#include "err.h" #include #include @@ -33,7 +34,7 @@ static EventBits_t s_rx_bit = 0; int mb_phy_send(const uint8_t *data, uint16_t len) { if (!data || len == 0) - return -1; + return ERR_PARAM; for (uint16_t i = 0; i < len; i++) { while (UART_IsTXFIFOFull(MB_UART_X)) @@ -42,7 +43,7 @@ int mb_phy_send(const uint8_t *data, uint16_t len) while (UART_IsTXBusy(MB_UART_X)) ; } - return 0; + return ERR_OK; } /* ======================== UART1 中断 (参考 vcom_serial.c UART0_Handler) ======================== */ diff --git a/APP/mb/mb_task.c b/APP/mb/mb_task.c index bcd6596..b482c9d 100644 --- a/APP/mb/mb_task.c +++ b/APP/mb/mb_task.c @@ -222,7 +222,7 @@ bool mb_task_busy(void) { return s_waiting; } int mb_task_slave_reply(const uint8_t *data, uint16_t len) { if (!data || len == 0) - return -1; + return ERR_PARAM; return mb_phy_send(data, len); } diff --git a/APP/mb/mb_task.h b/APP/mb/mb_task.h index 69ca827..b4a56b5 100644 --- a/APP/mb/mb_task.h +++ b/APP/mb/mb_task.h @@ -27,13 +27,9 @@ extern "C" { #endif /* ======================== 错误码 ======================== */ -#define MB_OK 0 -#define MB_ERR_TIMEOUT -1 /* 等待响应超时 */ -#define MB_ERR_CRC -2 /* CRC 校验失败 */ -#define MB_ERR_PARAM -3 /* 参数错误 */ -#define MB_ERR_EXCEPTION -4 /* 从站返回异常响应 */ -#define MB_ERR_BUSY -5 /* 总线忙(前一请求未完成) */ -#define MB_ERR_NO_RESP -6 /* 未收到期望响应 */ +/* 错误码统一在 err.h 中定义: MB_OK / MB_ERR_* 为 err.h 中 + * ERR_OK / ERR_* 的别名, 数值来源唯一。 */ +#include "err.h" /* ======================== 队列元素 ======================== */ /* 待发送请求帧 */ @@ -95,7 +91,7 @@ void mb_task_set_iap_cb(void (*cb)(uint8_t ack)); /** * @brief 从模式回复: 直接经物理层发送一帧响应 (在 slave cb 处理指令时调用) * 设备地址/目标由上层在帧内编码, 物理层仅负责拆包发送。 - * @return 0 成功, -1 失败 + * @return ERR_OK 成功; <0 错误码 (ERR_*) */ int mb_task_slave_reply(const uint8_t *data, uint16_t len); diff --git a/APP/user_export.c b/APP/user_export.c index 7da182b..80904f5 100644 --- a/APP/user_export.c +++ b/APP/user_export.c @@ -4,6 +4,8 @@ #include "SWM341.h" +#include "err.h" + #include "version.h" #include "bsp_init.h" #include "lvgl.h" @@ -343,18 +345,18 @@ int user_file_create( FIL *fp, const char * dir, const char *filename, uint8_t m char path[128]; if( !dir || !filename || !fp ){ - return -1; + return ERR_PARAM; } if (fs_mount(USB) != 0) - return -1; + return ERR_MOUNT; sprintf( path, "usb:/%s", device_equip_tag_get() ); fres = f_mkdir(path); if (fres != FR_OK && fres != FR_EXIST) { fs_unmount(USB); - return -2; + return ERR_MKDIR; } strcat( path, "/" ); @@ -362,7 +364,7 @@ int user_file_create( FIL *fp, const char * dir, const char *filename, uint8_t m fres = f_mkdir(path); if (fres != FR_OK && fres != FR_EXIST) { fs_unmount(USB); - return -2; + return ERR_MKDIR; } strcat( path, "/" ); strcat( path, filename); @@ -370,9 +372,9 @@ int user_file_create( FIL *fp, const char * dir, const char *filename, uint8_t m fres = f_open(fp, path, mode); if (fres != FR_OK) { fs_unmount(USB); - return -3; + return ERR_OPEN; } - return 0; + return ERR_OK; } #include "reg_cache.h" @@ -567,7 +569,7 @@ int user_param_load( uint16_t *param_buff, uint16_t buff_size ) /* 检查 U 盘是否就绪并挂载 */ if (fs_mount(USB) != 0) - return -1; + return ERR_MOUNT; //1. 读取位号,生成文件名 memset( tag, 0, sizeof(tag) ); @@ -580,7 +582,7 @@ int user_param_load( uint16_t *param_buff, uint16_t buff_size ) ret = user_param_file_open( &fp, "usb:/BD_SET/BD_BDM.set", FA_READ ); if( ret < 0 ){ fs_unmount(USB); - return -2; + return ERR_OPEN; } } @@ -589,7 +591,7 @@ int user_param_load( uint16_t *param_buff, uint16_t buff_size ) if( ret < 0 ){ f_close( &fp ); fs_unmount(USB); - return -3; + return ERR_READ; } f_close( &fp ); fs_unmount(USB); @@ -856,7 +858,7 @@ int user_soe_write_act( FIL *fp, int idx, uint16_t *act_evt ) fres = f_write( fp, buf, strlen(buf), &bw ); if( fres != FR_OK ){ printf( "f_write failed, err:%d\n", fres ); - return -1; + return ERR_WRITE; } //todo write event info @@ -875,7 +877,7 @@ int user_soe_write_act( FIL *fp, int idx, uint16_t *act_evt ) fres = f_write( fp, buf, strlen(buf), &bw ); if( fres != FR_OK ){ printf( "f_write failed, err:%d\n", fres ); - return -1; + return ERR_WRITE; } return 0; } @@ -894,7 +896,7 @@ int user_soe_write_chg( FIL *fp, int idx, uint16_t *evt ) fres = f_write( fp, buf, strlen(buf), &bw ); if( fres != FR_OK ){ printf( "f_write failed, err:%d\n", fres ); - return -1; + return ERR_WRITE; } //todo write event info @@ -904,7 +906,7 @@ int user_soe_write_chg( FIL *fp, int idx, uint16_t *evt ) fres = f_write( fp, buf, strlen(buf), &bw ); if( fres != FR_OK ){ printf( "f_write failed, err:%d\n", fres ); - return -1; + return ERR_WRITE; } return 0; } diff --git a/APP/user_iap.h b/APP/user_iap.h index 8e3c3f8..290eb3f 100644 --- a/APP/user_iap.h +++ b/APP/user_iap.h @@ -4,12 +4,9 @@ #include /* ==== IAP 升级返回码 ==== */ -#define USER_IAP_OK ( 0) -#define USER_IAP_BUSY ( -1) /* 已有升级在进行 / IAP 状态寄存器非零 */ -#define USER_IAP_NO_FILE ( -2) /* U盘上未找到升级固件 */ -#define USER_IAP_NO_UFDISK ( -3) /* U盘未就绪或挂载失败 */ -#define USER_IAP_DOWNLOAD ( -4) /* 固件下载/写入 从站失败 */ -#define USER_IAP_BAD_FORMAT ( -5) /* 固件文件格式错误 (项目名/板子ID/长度非法) */ +/* 返回码数值统一在 err.h 中定义: USER_IAP_OK / USER_IAP_* + * 为 err.h 中 ERR_OK / ERR_* 的别名。 */ +#include "err.h" /* ==== IAP 内部状态机 ==== */ #define IAP_ST_IDLE 0 // 空闲状态 diff --git a/APP/user_rtc.c b/APP/user_rtc.c index 0a36b5c..9dd462b 100644 --- a/APP/user_rtc.c +++ b/APP/user_rtc.c @@ -4,6 +4,8 @@ #include "SWM341.h" +#include "err.h" + #include "version.h" #include "bsp_init.h" #include "lvgl.h" @@ -74,9 +76,9 @@ int32_t user_rtc_time_get( struct tm *tm ) tm->tm_hour = date.Hour; tm->tm_min = date.Minute; tm->tm_sec = date.Second; - return 0; + return ERR_OK; } - return -1; + return ERR_INVALID; } @@ -92,6 +94,4 @@ time_t time( time_t *timep ) *timep = now; } return now; -} - - +} \ No newline at end of file -- 2.34.1