#include <stdio.h>
#include "dev_can.h"
+#include "err.h"
/* CAN 标识符 */
#define CAN_ID_MASTER 0x001
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);
while (CAN_TXComplete(CAN1) == 0) {
if (CAN1->SR & CAN_SR_BUSOFF_Msk) {
CAN_Open(CAN1); /* Bus Off 恢复 */
- return -1;
+ return ERR_COMM;
}
__NOP();
}
can_log_frame("TX", id, tx_data, len);
#endif
- return 0;
+ return ERR_OK;
}
void can_register_rx_callback(can_rx_callback_t cb)
* @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);
#include <stdint.h>
#include <stdbool.h>
+#include "err.h"
+
#include "mb_api.h"
#include "device_model.h"
--- /dev/null
+/**
+ * @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
* @brief 发送一整帧字节流 (阻塞直到发送完成)
* @param data 数据指针
* @param len 长度
- * @return 0 成功, -1 失败
+ * @return ERR_OK 成功; <0 错误码 (ERR_*)
*/
int mb_phy_send(const uint8_t *data, uint16_t len);
#include "mb_phy.h"
#include "mb_config.h"
+#include "err.h"
#include <string.h>
#include <stdbool.h>
uint16_t offset = 0;
if (!data || len == 0)
- return -1;
+ return ERR_PARAM;
/* 分片发送: 每包 6 字节, 最后一包按实际长度 */
if (s_rx_q)
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)
can_register_rx_callback(NULL);
reassembly_reset();
}
-#endif /* MB_PHY_CAN_EN */
+#endif /* MB_PHY_CAN_EN */
\ No newline at end of file
#include "mb_phy.h"
#include "mb_config.h"
+#include "err.h"
#include <string.h>
#include <stdbool.h>
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))
while (UART_IsTXBusy(MB_UART_X))
;
}
- return 0;
+ return ERR_OK;
}
/* ======================== UART1 中断 (参考 vcom_serial.c UART0_Handler) ======================== */
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
#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"
/* ======================== 队列元素 ======================== */
/* 待发送请求帧 */
/**
* @brief 从模式回复: 直接经物理层发送一帧响应 (在 slave cb 处理指令时调用)
* 设备地址/目标由上层在帧内编码, 物理层仅负责拆包发送。
- * @return 0 成功, -1 失败
+ * @return ERR_OK 成功; <0 错误码 (ERR_*)
*/
int mb_task_slave_reply(const uint8_t *data, uint16_t len);
#include "SWM341.h"
+#include "err.h"
+
#include "version.h"
#include "bsp_init.h"
#include "lvgl.h"
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, "/" );
fres = f_mkdir(path);
if (fres != FR_OK && fres != FR_EXIST) {
fs_unmount(USB);
- return -2;
+ return ERR_MKDIR;
}
strcat( path, "/" );
strcat( path, filename);
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"
/* 检查 U 盘是否就绪并挂载 */
if (fs_mount(USB) != 0)
- return -1;
+ return ERR_MOUNT;
//1. 读取位号,生成文件名
memset( tag, 0, sizeof(tag) );
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;
}
}
if( ret < 0 ){
f_close( &fp );
fs_unmount(USB);
- return -3;
+ return ERR_READ;
}
f_close( &fp );
fs_unmount(USB);
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
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;
}
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
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;
}
#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 // 空闲状态
#include "SWM341.h"
+#include "err.h"
+
#include "version.h"
#include "bsp_init.h"
#include "lvgl.h"
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;
}
*timep = now;
}
return now;
-}
-
-
+}
\ No newline at end of file