fix(simple cmd) 完成简单通讯
This commit is contained in:
parent
2627e20b32
commit
8357f41a75
|
@ -1,7 +0,0 @@
|
||||||
{
|
|
||||||
// 使用 IntelliSense 了解相关属性。
|
|
||||||
// 悬停以查看现有属性的描述。
|
|
||||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": []
|
|
||||||
}
|
|
|
@ -13,6 +13,16 @@
|
||||||
"erpc_core.h": "c",
|
"erpc_core.h": "c",
|
||||||
"type_traits": "c",
|
"type_traits": "c",
|
||||||
"list": "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": []
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
45
erpc_core.c
45
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)
|
if (hw_cfg->send_cache[i].state == ERPC_CMD_NO_ERROR)
|
||||||
{
|
{
|
||||||
|
printf("find send cache %d\n", i);
|
||||||
cache_ord = i;
|
cache_ord = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -169,12 +170,28 @@ u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len)
|
||||||
|
|
||||||
wait_time--;
|
wait_time--;
|
||||||
}
|
}
|
||||||
CHECK_IF_ERROR(wait_time == 0, ERPC_ERR_SEND_TIMEOUT);
|
u32 ret = ERPC_NO_ERROR;
|
||||||
CHECK_IF_ERROR(hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_ONCE, ERPC_ERR_DEST_NO_RESPONSE);
|
do
|
||||||
CHECK_IF_ERROR(hw_cfg->send_cache[cache_ord].state == ERPC_CMD_SEND_REPEAT, ERPC_ERR_DEST_NO_RESPONSE);
|
{
|
||||||
return ERPC_NO_ERROR;
|
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)
|
u32 erpc_send_data(erpc_hw_cfg_t *hw)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAX_SEND_CMD_CACHE_NUM; i++)
|
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)
|
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);
|
memcpy(hw_cfg->rec_cache[i].data, data, len);
|
||||||
hw_cfg->rec_cache[i].state = ERPC_CMD_WAIT_SERVER_DEAL;
|
hw_cfg->rec_cache[i].state = ERPC_CMD_WAIT_SERVER_DEAL;
|
||||||
return ERPC_NO_ERROR;
|
return ERPC_NO_ERROR;
|
||||||
|
@ -352,6 +370,7 @@ void erpc_rev_deal_core()
|
||||||
|
|
||||||
for (int j = 0; j < MAX_REC_CMD_CACHE_NUM; j++)
|
for (int j = 0; j < MAX_REC_CMD_CACHE_NUM; j++)
|
||||||
{
|
{
|
||||||
|
// 发现等待处理的数据包
|
||||||
if (hw->rec_cache[j].state == ERPC_CMD_WAIT_TASK_DEAL)
|
if (hw->rec_cache[j].state == ERPC_CMD_WAIT_TASK_DEAL)
|
||||||
{
|
{
|
||||||
// 多线程处理
|
// 多线程处理
|
||||||
|
@ -368,30 +387,32 @@ void erpc_rev_deal_core()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
hw->deal_unlock();
|
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;
|
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;
|
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;
|
hw->rec_cache[j].state = ERPC_CMD_NO_ERROR;
|
||||||
}
|
}
|
||||||
|
cmd_list = cmd_list->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
list = list->next;
|
||||||
}
|
}
|
||||||
if (erpc_sleep_tick != NULL)
|
if (erpc_sleep_tick != NULL)
|
||||||
{
|
{
|
||||||
|
|
21
erpc_core.h
21
erpc_core.h
|
@ -56,7 +56,7 @@ typedef enum erpc_status
|
||||||
ERPC_CMD_SEND_REPEAT, // 发送指令重发
|
ERPC_CMD_SEND_REPEAT, // 发送指令重发
|
||||||
ERPC_CMD_SEND_OK, // 发送指令成功
|
ERPC_CMD_SEND_OK, // 发送指令成功
|
||||||
ERPC_CMD_DEST_BUSY, // 目标设备忙
|
ERPC_CMD_DEST_BUSY, // 目标设备忙
|
||||||
}erpc_status;
|
} erpc_status;
|
||||||
|
|
||||||
typedef struct erpc_data_cache_t
|
typedef struct erpc_data_cache_t
|
||||||
{
|
{
|
||||||
|
@ -71,7 +71,11 @@ typedef struct erpc_cmd_list_t
|
||||||
u16 cmd;
|
u16 cmd;
|
||||||
u32 (*handle)(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len);
|
u32 (*handle)(u8 hw, u8 src_id, u8 dest_id, u16 port, u8 *data, u16 len);
|
||||||
} erpc_cmd_list_t;
|
} erpc_cmd_list_t;
|
||||||
|
typedef enum ERPC_ROLE
|
||||||
|
{
|
||||||
|
ERPC_ROLE_MASTER,
|
||||||
|
ERPC_ROLE_SLAVE,
|
||||||
|
} ERPC_ROLE;
|
||||||
typedef struct erpc_hw_cfg_t
|
typedef struct erpc_hw_cfg_t
|
||||||
{
|
{
|
||||||
u8 ord;
|
u8 ord;
|
||||||
|
@ -91,7 +95,12 @@ typedef struct erpc_hw_cfg_t
|
||||||
{ \
|
{ \
|
||||||
return error; \
|
return error; \
|
||||||
}
|
}
|
||||||
|
#define CHECK_IF_ERROR_AND_DO(condition, error, action) \
|
||||||
|
if (condition) \
|
||||||
|
{ \
|
||||||
|
action; \
|
||||||
|
return error; \
|
||||||
|
}
|
||||||
typedef enum erpc_error
|
typedef enum erpc_error
|
||||||
{
|
{
|
||||||
ERPC_NO_ERROR = 0,
|
ERPC_NO_ERROR = 0,
|
||||||
|
@ -105,21 +114,17 @@ typedef enum erpc_error
|
||||||
ERPC_ERR_HW_EXIST, // 硬件已经存在
|
ERPC_ERR_HW_EXIST, // 硬件已经存在
|
||||||
ERPC_ERR_HW_NOT_EXIST, // 硬件不存在
|
ERPC_ERR_HW_NOT_EXIST, // 硬件不存在
|
||||||
ERPC_ERR_REC_CACHE_FULL, // 接收缓存满
|
ERPC_ERR_REC_CACHE_FULL, // 接收缓存满
|
||||||
}erpc_error;
|
} erpc_error;
|
||||||
typedef void (*erpc_sleep)(u32 tick);
|
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); // 写入接受缓存
|
u32 erpc_set_rev_cahce(u8 hw, u8 *data, u16 len); // 写入接受缓存
|
||||||
void erpc_rev_package_core(); // 处理接受到的包,通讯层面处理
|
void erpc_rev_package_core(); // 处理接受到的包,通讯层面处理
|
||||||
void erpc_rev_deal_core(); // 处理接受到的指令,业务层面处理
|
void erpc_rev_deal_core(); // 处理接受到的指令,业务层面处理
|
||||||
extern erpc_sleep erpc_sleep_tick;
|
extern erpc_sleep erpc_sleep_tick;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
u32 erpc_hw_add(erpc_hw_cfg_t *hw);
|
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_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);
|
u32 erpc_send(u8 hw, u8 dest_id, u16 port, u8 *data, u16 len);
|
||||||
#endif /* ERPC_CORE_H_ */
|
#endif /* ERPC_CORE_H_ */
|
BIN
erpc_core.o
BIN
erpc_core.o
Binary file not shown.
6
makefile
6
makefile
|
@ -15,13 +15,13 @@ SRCS = erpc_core.c \
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
# 可执行文件
|
# 可执行文件
|
||||||
TARGET = port_self.exe
|
Target = port_self.exe
|
||||||
|
|
||||||
# 默认目标
|
# 默认目标
|
||||||
all: $(TARGET)
|
all: $(Target)
|
||||||
|
|
||||||
# 链接目标
|
# 链接目标
|
||||||
$(TARGET): $(OBJS)
|
$(Target): $(OBJS)
|
||||||
$(CC) -o $@ $^
|
$(CC) -o $@ $^
|
||||||
|
|
||||||
# 编译源文件
|
# 编译源文件
|
||||||
|
|
|
@ -80,6 +80,8 @@ DWORD WINAPI rev_task(LPVOID lpParam)
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
// printf("rev_task\n");
|
||||||
|
erpc_rev_package_core();
|
||||||
erpc_rev_deal_core();
|
erpc_rev_deal_core();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,7 +91,7 @@ DWORD WINAPI test_task(LPVOID lpParam)
|
||||||
printf("test_task\n");
|
printf("test_task\n");
|
||||||
while (1)
|
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);
|
u32 ret = erpc_send(self_hw_cfg.ord, self_hw_cfg.local_id, 0x01, NULL, 0);
|
||||||
printf("[test_task]send ret:%d\n", ret);
|
printf("[test_task]send ret:%d\n", ret);
|
||||||
sys_sleep(1000);
|
sys_sleep(1000);
|
||||||
|
@ -129,5 +131,6 @@ int main(int argc, char *argv[])
|
||||||
WaitForSingleObject(threadHandle[2], INFINITE);
|
WaitForSingleObject(threadHandle[2], INFINITE);
|
||||||
CloseHandle(threadHandle[0]);
|
CloseHandle(threadHandle[0]);
|
||||||
CloseHandle(threadHandle[1]);
|
CloseHandle(threadHandle[1]);
|
||||||
|
CloseHandle(threadHandle[2]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
BIN
port_self.exe
BIN
port_self.exe
Binary file not shown.
BIN
port_self.o
BIN
port_self.o
Binary file not shown.
Loading…
Reference in New Issue