diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8f9ce70 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,16 @@ +{ + "files.associations": { + "spi_port.h": "c", + "cmd_def.h": "c", + "config.h": "c", + "crc16.h": "c", + "comdef.h": "c", + "stddef.h": "c", + "core.h": "c", + "stdint.h": "c", + "cor.h": "c", + "eprc_core.h": "c", + "erpc_core.h": "c", + "type_traits": "c" + } +} \ No newline at end of file diff --git a/config.h b/config.h new file mode 100644 index 0000000..b9c966d --- /dev/null +++ b/config.h @@ -0,0 +1,11 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#define ERPC_VERSION "0.0.1" + + +#define u8 unsigned char +#define u16 unsigned short +#define u32 unsigned int + +#endif /* _CONFIG_H_ */ \ No newline at end of file diff --git a/crc16.c b/crc16.c new file mode 100644 index 0000000..7cfea11 --- /dev/null +++ b/crc16.c @@ -0,0 +1,68 @@ +#include "crc16.h" + +static const uint16_t crc16tab[256] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, + 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, + 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, + 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, + 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, + 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, + 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, + 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, + 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, + 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, + 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, + 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, + 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, + 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, + 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, + 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, + 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, + 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, + 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, + 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, + 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, + 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, + 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, + 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, + 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, + 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0}; + +uint16_t crc16(const unsigned char *buf, size_t len) { + int counter; + uint16_t crc = 0; + for (counter = 0; counter < len; counter++) + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ *buf++) & 0x00FF]; + return crc; +} + +u16 cmd_cal_crc16(erpc_cmd_def_t *cmd) { + u16 crc = 0; + u32 counter = 0; + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ cmd->src_id) & 0x00FF]; + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ cmd->dest_id) & 0x00FF]; + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ cmd->msg_len) & 0x00FF]; + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ cmd->type) & 0x00FF]; + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ cmd->port) & 0x00FF]; + for (counter = 0; counter < cmd->msg_len; counter++) + crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ cmd->data[counter]) & 0x00FF]; + cmd->crc_16[0] = (u8)(crc >> 8); + cmd->crc_16[1] = (u8)(crc & 0x00FF); + return crc; +} +/** + * 检查crc16校验 + * return 0:校验成功 + * return 1:校验失败 + */ +u8 cmd_check_crc16(erpc_cmd_def_t *cmd) { + u16 crc = cmd_cal_crc16(cmd); + if (cmd->crc_16[0] != (u8)(crc >> 8) || + cmd->crc_16[1] != (u8)(crc & 0x00FF)) { + return 1; + } + return 0; +} \ No newline at end of file diff --git a/crc16.h b/crc16.h new file mode 100644 index 0000000..adb86fb --- /dev/null +++ b/crc16.h @@ -0,0 +1,18 @@ +#ifndef _CRC_CRC16_H +#define _CRC_CRC16_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include +#include +#include "config.h" +#include "erpc_core.h" +uint16_t crc16(const unsigned char *buf, size_t len); +u8 cmd_check_crc16(erpc_cmd_def_t *cmd); +u16 cmd_cal_crc16(erpc_cmd_def_t *cmd); +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif diff --git a/erpc_core.c b/erpc_core.c new file mode 100644 index 0000000..4526e07 --- /dev/null +++ b/erpc_core.c @@ -0,0 +1,71 @@ +#include "erpc_core.h" + +#include "crc16.h" + +u32 erpc_report_node_self(node_cfg_t *node_cfg) { + erpc_cmd_def_t cmd; + cmd.src_id = node_cfg->local_id; + cmd.dest_id = 0xFF; + cmd.msg_len = 0; + cmd.type = PACKAGE_TYPE_DEV_POWER_ON; + cmd_cal_crc16(&cmd); + return node_cfg->write((u8 *)&cmd, sizeof(erpc_cmd_def_t)); +} + +u32 erpc_search_all_node(node_cfg_t *node_cfg) { + erpc_cmd_def_t cmd; + cmd.src_id = node_cfg->local_id; + cmd.dest_id = 0xFF; + cmd.msg_len = 0; + cmd.type = PACKAGE_TYPE_CMD_SERCH_DEV_ONLINE; + cmd_cal_crc16(&cmd); + return node_cfg->write((u8 *)&cmd, sizeof(erpc_cmd_def_t)); +} + +u32 erpc_send_cmd(node_cfg_t *node_cfg, u8 dest_id, u8 *data, u32 len) { + erpc_cmd_def_t cmd; + cmd.src_id = node_cfg->local_id; + cmd.dest_id = dest_id; + cmd.msg_len = len; + cmd.type = PACKAGE_TYPE_CMD_REQ; + cmd.data = data; + cmd_cal_crc16(&cmd); + return node_cfg->write((u8 *)&cmd, sizeof(erpc_cmd_def_t) + len); +} + +u32 erpc_send_cmd_ack(node_cfg_t *node_cfg, erpc_cmd_def_t *cmd_rec, + u8 is_ack) { + erpc_cmd_def_t cmd_send; + cmd_send.src_id = node_cfg->local_id; + cmd_send.dest_id = cmd_rec->src_id; + if (is_ack) + cmd_send.type = PACKAGE_TYPE_CMD_REQ_ACK; + else + cmd_send.type = PACKAGE_TYPE_CMD_RESP_NACK; + cmd_send.msg_len = 0; + cmd_cal_crc16(&cmd_send); + return node_cfg->write((u8 *)&cmd_send, sizeof(erpc_cmd_def_t)); +} + +u32 erpc_send_repeat(node_cfg_t *node_cfg,u8 dest_id) +{ + erpc_cmd_def_t cmd; + cmd.src_id = node_cfg->local_id; + cmd.dest_id = dest_id; + cmd.type = PACKAGE_TYPE_CMD_REPEAT; + cmd.msg_len = 0; + cmd_cal_crc16(&cmd); + return node_cfg->write((u8 *)&cmd, sizeof(erpc_cmd_def_t)); +} +u32 erpc_send_msg(node_cfg_t *node_cfg, u8 dest_id, u8 *data, u32 len) { + int retry = CMD_MAX_RETRY; + node_cfg->bus_lock(); + u32 ret = 0; + while (retry-- > 0) { + u32 ret = erpc_send_cmd(node_cfg, dest_id, data, len); + + } + return ret; +} +u32 erpc_rec_msg_core(node_cfg_t *node_cfg, u8 *data, u32 len) { +} \ No newline at end of file diff --git a/erpc_core.h b/erpc_core.h new file mode 100644 index 0000000..9ebbfc9 --- /dev/null +++ b/erpc_core.h @@ -0,0 +1,57 @@ +#ifndef ERPC_CORE_H_ +#define ERPC_CORE_H_ + +#include "config.h" + +#define MAX_CMD_LEN (256 - 5) // crc16最大支持256字节 +#define CMD_MAX_RETRY 5 // 最大重发次数 +#define CMD_TIMEOUT 30 // 超时时间 tick +#define MAX_COM 20 // 最大支持端口打开数量,可认为是同时通讯的号 + +typedef enum erpc_error { + DEST_SYS_BUSY = 0, // 目标系统繁忙,接收到nack + DEST_SYS_TIMEOUT, // 目标系统处理超时 + BUS_CRC_ERROR, // 总线CRC错误,可能是总线通讯质量不佳 +}; +typedef enum package_type { + PACKAGE_TYPE_DEV_POWER_ON = 0, + PACKAGE_TYPE_CMD_REQ, + PACKAGE_TYPE_CMD_REQ_ACK, + PACKAGE_TYPE_CMD_RESP, + PACKAGE_TYPE_CMD_RESP_ACK, + PACKAGE_TYPE_CMD_RESP_NACK, + PACKAGE_TYPE_CMD_REPEAT, // crc校验失败,请求重发 + PACKAGE_TYPE_DEV_POWER_OFF, + PACKAGE_TYPE_CMD_SERCH_DEV_ONLINE, +} package_type; + +typedef struct node_cfg_t { + u8 local_id; + struct port_t { + u16 com_id; + volatile u32 time_out; + void (*pre_deal)(u8 *data, u32 len); // 数据预处理接口 + u8 send_ack : 2; // 0:没有ack返回,1:ack返回 2:nack返回 + u8 rec_crc : 1; // 0:crc没有故障, 1: crc有故障 + u8 req_resend : 1; // 1:客户端请求重发 + } port[MAX_COM]; + u32 (*write)(u8 *data, u32 len); + void (*bus_lock)(void); + void (*bus_unlock)(void); + void (*sleep)(void); +} node_cfg_t; + +// 广播ID +#define ERPC_BOARDCAST_ID 0xff + +typedef struct erpc_cmd_def_t { + u8 src_id; + u8 dest_id; + package_type type; + u16 port; // 指令号,指令号=端口号 + u8 msg_len; + u8 crc_16[2]; + u8 *data; +} erpc_cmd_def_t; + +#endif /* ERPC_CORE_H_ */ \ No newline at end of file diff --git a/erpc设计文档.md b/erpc设计文档.md index e69de29..74a6089 100644 --- a/erpc设计文档.md +++ b/erpc设计文档.md @@ -0,0 +1,58 @@ + + + +CRC16 校验长度为 16-256字节较为合适。 + +ACK丢了不重发 + + + + +# 发送指令流程 +1.发送数据后,始终要等待ack +2.接收数据后,始终要回复ack +3.获得ack不代表指令成功 +4.crc校验不成功,发送repeat包 +5.指令号=端口号 + +## 正常传输 +| host | slave | +| ---------------- | ---------------- | +| 发送指令 | 等待指令 | +| 等待ACK回复 | 发送ACK | +| 等待指令处理完成 | 处理指令 | +| 等待指令处理完成 | 发送resp | +| 发送resp_ack | 等待resp_ack回复 | +| 处理完成 | 处理完成 | +## 指令丢失 +| host | slave | +| ---------------- | --------------------- | +| 发送指令 | 等待指令 | +| 发送指令丢失 | 等待指令 | +| 超时重发指令 | 等待指令 | +| 等待ACK回复 | 指令接收成功,发送ACK | +| 等待指令处理完成 | 处理指令 | +| 等待指令处理完成 | 发送resp | +| 发送resp_ack | 等待resp_ack回复 | +| 处理完成 | 处理完成 | +## cmd ack 丢失 +| host | slave | +| ---------------- | -------------------------- | +| 发送指令 | 等待指令 | +| 等待ACK回复 | 指令接收成功,发送ACK | +| 超时重发 | 指令已经开始行,发现重发包,发送ACK| +| 等待ACK回复 | 处理指令 | +| 等待指令处理完成 | 处理指令 | +| 等待指令处理完成 | 发送resp | +| 发送resp_ack | 等待resp_ack回复 | +| 处理完成 | 处理完成 | +## resp ack 丢失 +| host | slave | +| ---------------- | ---------------- | +| 发送指令 | 等待指令 | +| 等待ACK回复 | 发送ACK | +| 等待指令处理完成 | 处理指令 | +| 等待指令处理完成 | 发送resp | +| 发送resp_ack | 等待resp_ack回复 | +| 处理完成 |发送resp | +| 发送resp_ack | 处理完成 | \ No newline at end of file diff --git a/spi_port.c b/spi_port.c new file mode 100644 index 0000000..e2c613d --- /dev/null +++ b/spi_port.c @@ -0,0 +1,3 @@ +#include "spi_port.h" + + diff --git a/spi_port.h b/spi_port.h new file mode 100644 index 0000000..d6fb834 --- /dev/null +++ b/spi_port.h @@ -0,0 +1 @@ +#include "config.h" \ No newline at end of file