修改函数返回值定义
authorkyz <kongyuzhong@google.com>
Thu, 12 Nov 2026 06:15:51 +0000 (14:15 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 12 Nov 2026 06:15:51 +0000 (14:15 +0800)
12 files changed:
APP/BSP/dev_can.c
APP/BSP/dev_can.h
APP/dispboard.h
APP/err.h [new file with mode: 0644]
APP/mb/mb_phy.h
APP/mb/mb_phy_can.c
APP/mb/mb_phy_uart.c
APP/mb/mb_task.c
APP/mb/mb_task.h
APP/user_export.c
APP/user_iap.h
APP/user_rtc.c

index f55caf2319fc7749ee6d0a6c40ace3f050f3d136..13ad3ebd904a6b946b4ef0b919809bd2dc577f94 100644 (file)
@@ -16,6 +16,7 @@
 #include <stdio.h>
 
 #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)
index 81dabfef3a30025ecbef7adc640379b30f7f1098..6e1682640f8b5639fd4064768bb98efb1c48c2a5 100644 (file)
@@ -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);
 
index 888f824b2982affe3bd4642fc8c29c2c34378f1d..0b050589b8e6d7e49fb20e4187a9fb6e0369712f 100644 (file)
@@ -4,6 +4,8 @@
 #include <stdint.h>
 #include <stdbool.h>
 
+#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 (file)
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
index 959173ff31589fa67421bdd6b8fb1d3d5718af12..ae938798021aaae5325eff03b20ae3812f37b9e5 100644 (file)
@@ -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);
 
index 0b1573cb381b0a32eac65efad05fe4cdcca32b6a..ec77020a8960d2a0ac8df35246725aa7dc8ac1aa 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "mb_phy.h"
 #include "mb_config.h"
+#include "err.h"
 
 #include <string.h>
 #include <stdbool.h>
@@ -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
index 22b2ad3fe8390161d8eca3c555dbfeb64f693b47..ec8aada20f8d8ad8f5c3e5fd43190546bc6ad691 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "mb_phy.h"
 #include "mb_config.h"
+#include "err.h"
 
 #include <string.h>
 #include <stdbool.h>
@@ -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) ======================== */
index bcd6596fe7cbde90fdc51b1dd11d1b173a2c4f1b..b482c9d45d1097cab1573549b40fae6c7887274f 100644 (file)
@@ -222,7 +222,7 @@ bool mb_task_busy(void) { return s_waiting; }
 int mb_task_slave_reply(const uint8_t *data, uint16_t len)\r
 {\r
     if (!data || len == 0)\r
-        return -1;\r
+        return ERR_PARAM;\r
     return mb_phy_send(data, len);\r
 }\r
 \r
index 69ca827b6a0214e3e0e5d10b3ff45d7b387801e7..b4a56b561773f6670136fc6419ead6899118cb00 100644 (file)
@@ -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);
 
index 7da182b9152026579e35d41047cc72de3348d01d..80904f545abf954b1ad35eaad9ca518f9e5196a0 100644 (file)
@@ -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;
 }
index 8e3c3f83662660fe34b4faf4cff0be88ebc21639..290eb3f63c92ebd114abe85f0b49316eacd189b3 100644 (file)
@@ -4,12 +4,9 @@
 #include <stdint.h>
 
 /* ==== 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  // 空闲状态
index 0a36b5c60aa90e73dae781db71ea924d7a5c6beb..9dd462b5a0af2674c16e434e9a66461d3f17d279 100644 (file)
@@ -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