fix(simple cmd) 完成简单通讯

This commit is contained in:
chenyf 2024-11-21 00:12:48 +08:00
parent 2627e20b32
commit 8357f41a75
9 changed files with 82 additions and 50 deletions

7
.vscode/launch.json vendored
View File

@ -1,7 +0,0 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

12
.vscode/settings.json vendored
View File

@ -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": []
}
]
}

View File

@ -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)
{

View File

@ -56,7 +56,7 @@ typedef enum erpc_status
ERPC_CMD_SEND_REPEAT, // 发送指令重发
ERPC_CMD_SEND_OK, // 发送指令成功
ERPC_CMD_DEST_BUSY, // 目标设备忙
}erpc_status;
} 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,
@ -105,21 +114,17 @@ typedef enum erpc_error
ERPC_ERR_HW_EXIST, // 硬件已经存在
ERPC_ERR_HW_NOT_EXIST, // 硬件不存在
ERPC_ERR_REC_CACHE_FULL, // 接收缓存满
}erpc_error;
} erpc_error;
typedef void (*erpc_sleep)(u32 tick);
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(); // 处理接受到的指令,业务层面处理
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_ */

Binary file not shown.

View File

@ -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 $@ $^
# 编译源文件

View File

@ -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,7 +91,7 @@ DWORD WINAPI test_task(LPVOID lpParam)
printf("test_task\n");
while (1)
{
printf("[test_task]send\n");
// 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;
}

Binary file not shown.

Binary file not shown.