diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index f980ab9..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 964e7d2..c9ee9c3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,16 @@ "erpc_core.h": "c", "type_traits": "c", "list": "c", - "list.h": "c" - } + "list.h": "c", + "array": "c", + "string": "c", + "string_view": "c" + }, + "makefile.launchConfigurations": [ + { + "cwd": "d:\\WORK\\erpc", + "binaryPath": "d:\\WORK\\erpc\\port_self.exe", + "binaryArgs": [] + } + ] } \ No newline at end of file diff --git a/erpc_core.c b/erpc_core.c index fa0db9d..570de48 100644 --- a/erpc_core.c +++ b/erpc_core.c @@ -132,6 +132,7 @@ u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len) { if (hw_cfg->send_cache[i].state == ERPC_CMD_NO_ERROR) { + printf("find send cache %d\n", i); cache_ord = i; break; } @@ -169,12 +170,28 @@ u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len) wait_time--; } - CHECK_IF_ERROR(wait_time == 0, ERPC_ERR_SEND_TIMEOUT); - CHECK_IF_ERROR(hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_ONCE, ERPC_ERR_DEST_NO_RESPONSE); - CHECK_IF_ERROR(hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_REPEAT, ERPC_ERR_DEST_NO_RESPONSE); - return ERPC_NO_ERROR; + u32 ret = ERPC_NO_ERROR; + do + { + if(wait_time == 0){ + ret = ERPC_ERR_SEND_TIMEOUT; + break; + } + if(hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_ONCE){ + ret = ERPC_ERR_DEST_NO_RESPONSE; + break; + } + if(hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_REPEAT){ + ret = ERPC_ERR_DEST_NO_RESPONSE; + break; + } + } while (0); + hw_cfg->send_cache[cache_ord].state = ERPC_CMD_NO_ERROR; + return ret; } + + u32 erpc_send_data(erpc_hw_cfg_t *hw) { for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++) @@ -314,6 +331,7 @@ u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len) { if (hw_cfg->rec_cache[i].state == ERPC_CMD_NO_ERROR) { + printf("set rev cache %d\r\n", i); memcpy(hw_cfg->rec_cache[i].data, data, len); hw_cfg->rec_cache[i].state = ERPC_CMD_WAIT_SERVER_DEAL; return ERPC_NO_ERROR; @@ -352,6 +370,7 @@ void erpc_rev_deal_core() for (int j = 0; j < MAX_REC_CMD_CACHE_NUM; j++) { + // 发现等待处理的数据包 if (hw->rec_cache[j].state == ERPC_CMD_WAIT_TASK_DEAL) { // 多线程处理 @@ -368,30 +387,32 @@ void erpc_rev_deal_core() continue; } hw->deal_unlock(); - - list_t *list = &hw->cmd_list; + // 搜索指令列表 + list_t *cmd_list = &hw->cmd_list; // 链表是空的 - if (list->data == NULL) + if (cmd_list->data == NULL) { continue; } - while (list != NULL) + while (cmd_list != NULL) { - erpc_cmd_list_t *cmd_list = (erpc_cmd_list_t *)list->data; + erpc_cmd_list_t *cmd_obj = (erpc_cmd_list_t *)cmd_list->data; erpc_cmd_def_t *cmd_def = (erpc_cmd_def_t *)hw->rec_cache[j].data; - if (cmd_list->cmd == cmd_def->head.port) + if (cmd_obj->cmd == cmd_def->head.port) { - if (cmd_list->handle != NULL) + if (cmd_obj->handle != NULL) { // 指令调用 - cmd_list->handle(hw->ord, cmd_def->head.src_id, cmd_def->head.dest_id, cmd_def->head.port, cmd_def->data, cmd_def->head.msg_len); + cmd_obj->handle(hw->ord, cmd_def->head.src_id, cmd_def->head.dest_id, cmd_def->head.port, cmd_def->data, cmd_def->head.msg_len); } // 处理完成 丢弃缓存 hw->rec_cache[j].state = ERPC_CMD_NO_ERROR; } + cmd_list = cmd_list->next; } } } + list = list->next; } if (erpc_sleep_tick != NULL) { diff --git a/erpc_core.h b/erpc_core.h index cfbe330..a32907d 100644 --- a/erpc_core.h +++ b/erpc_core.h @@ -46,17 +46,17 @@ typedef struct erpc_cmd_def_t #define MAX_SEND_CMD_CACHE_NUM 10 // 最大发送指令缓存数量,发送指令优先级>接收指令优先级 typedef enum erpc_status { - ERPC_CMD_NO_ERROR, // 发送cache,接收cache空闲 - ERPC_CMD_DATA_DEAL, // 发送cache数据组织中 - ERPC_CMD_WAIT_SERVER_DEAL, // 等待服务线程处理接受cache - ERPC_CMD_WAIT_TASK_DEAL, // 等待任务线程处理指令 + ERPC_CMD_NO_ERROR, // 发送cache,接收cache空闲 + ERPC_CMD_DATA_DEAL, // 发送cache数据组织中 + ERPC_CMD_WAIT_SERVER_DEAL, // 等待服务线程处理接受cache + ERPC_CMD_WAIT_TASK_DEAL, // 等待任务线程处理指令 ERPC_CMD_WAIT_TASK_DEAL_FINISH, // 等待任务线程处理指令完成 - ERPC_CMD_WAIT_SEND, // 等待服务线程处理发送cache - ERPC_CMD_SEND_ONCE, // 发送指令成功 - ERPC_CMD_SEND_REPEAT, // 发送指令重发 - ERPC_CMD_SEND_OK, // 发送指令成功 - ERPC_CMD_DEST_BUSY, // 目标设备忙 -}erpc_status; + ERPC_CMD_WAIT_SEND, // 等待服务线程处理发送cache + ERPC_CMD_SEND_ONCE, // 发送指令成功 + ERPC_CMD_SEND_REPEAT, // 发送指令重发 + ERPC_CMD_SEND_OK, // 发送指令成功 + ERPC_CMD_DEST_BUSY, // 目标设备忙 +} erpc_status; typedef struct erpc_data_cache_t { @@ -71,7 +71,11 @@ typedef struct erpc_cmd_list_t u16 cmd; u32 (*handle)(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len); } erpc_cmd_list_t; - +typedef enum ERPC_ROLE +{ + ERPC_ROLE_MASTER, + ERPC_ROLE_SLAVE, +} ERPC_ROLE; typedef struct erpc_hw_cfg_t { u8 ord; @@ -91,7 +95,12 @@ typedef struct erpc_hw_cfg_t { \ return error; \ } - +#define CHECK_IF_ERROR_AND_DO(condition, error, action) \ + if (condition) \ + { \ + action; \ + return error; \ + } typedef enum erpc_error { ERPC_NO_ERROR = 0, @@ -101,25 +110,21 @@ typedef enum erpc_error ERPC_ERR_HW_SEND_FAIL, // 硬件层发来的错误 erpc_hw_cfg_t.write返回错误 ERPC_ERR_DEST_BUSY, ERPC_ERR_DEST_NO_RESPONSE, // 目标设备无响应 - ERPC_ERR_MALLOC_ERROR, // malloc失败,内存不足 - ERPC_ERR_HW_EXIST, // 硬件已经存在 - ERPC_ERR_HW_NOT_EXIST, // 硬件不存在 - ERPC_ERR_REC_CACHE_FULL, // 接收缓存满 -}erpc_error; + ERPC_ERR_MALLOC_ERROR, // malloc失败,内存不足 + ERPC_ERR_HW_EXIST, // 硬件已经存在 + ERPC_ERR_HW_NOT_EXIST, // 硬件不存在 + ERPC_ERR_REC_CACHE_FULL, // 接收缓存满 +} erpc_error; typedef void (*erpc_sleep)(u32 tick); - -void erpc_send_deal_core(); // 发送指令处理线程 +void erpc_send_deal_core(); // 发送指令处理线程 u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len); // 写入接受缓存 -void erpc_rev_package_core(); // 处理接受到的包,通讯层面处理 -void erpc_rev_deal_core(); // 处理接受到的指令,业务层面处理 +void erpc_rev_package_core(); // 处理接受到的包,通讯层面处理 +void erpc_rev_deal_core(); // 处理接受到的指令,业务层面处理 extern erpc_sleep erpc_sleep_tick; - - u32 erpc_hw_add(erpc_hw_cfg_t *hw); u32 erpc_add_cmd_list(erpc_hw_cfg_t *hw, erpc_cmd_list_t *cmd_list); - u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len); #endif /* ERPC_CORE_H_ */ \ No newline at end of file diff --git a/erpc_core.o b/erpc_core.o index afdbadc..d9d50ac 100644 Binary files a/erpc_core.o and b/erpc_core.o differ diff --git a/makefile b/makefile index 9460005..61c0a50 100644 --- a/makefile +++ b/makefile @@ -15,13 +15,13 @@ SRCS = erpc_core.c \ OBJS = $(SRCS:.c=.o) # 可执行文件 -TARGET = port_self.exe +Target = port_self.exe # 默认目标 -all: $(TARGET) +all: $(Target) # 链接目标 -$(TARGET): $(OBJS) +$(Target): $(OBJS) $(CC) -o $@ $^ # 编译源文件 diff --git a/port_self.c b/port_self.c index adc5278..7ccedb9 100644 --- a/port_self.c +++ b/port_self.c @@ -80,6 +80,8 @@ DWORD WINAPI rev_task(LPVOID lpParam) while (1) { + // printf("rev_task\n"); + erpc_rev_package_core(); erpc_rev_deal_core(); } } @@ -89,8 +91,8 @@ DWORD WINAPI test_task(LPVOID lpParam) printf("test_task\n"); while (1) { - printf("[test_task]send\n"); - u32 ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x01, NULL, 0); + // printf("[test_task]send\n"); + u32 ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x01, NULL, 0); printf("[test_task]send ret:%d\n", ret); sys_sleep(1000); } @@ -129,5 +131,6 @@ int main(int argc, char *argv[]) WaitForSingleObject(threadHandle[2], INFINITE); CloseHandle(threadHandle[0]); CloseHandle(threadHandle[1]); + CloseHandle(threadHandle[2]); return 0; } \ No newline at end of file diff --git a/port_self.exe b/port_self.exe index 3894270..0ea715d 100644 Binary files a/port_self.exe and b/port_self.exe differ diff --git a/port_self.o b/port_self.o index 5b4f9b4..ed6933e 100644 Binary files a/port_self.o and b/port_self.o differ