110 lines
3.1 KiB
C
110 lines
3.1 KiB
C
#ifndef _ERPC_CORE_H_
|
||
#define _ERPC_CORE_H_
|
||
#include "config.h"
|
||
#include "list.h"
|
||
#define ERPC_VERSION_MAJOR 1
|
||
#define ERPC_VERSION_MINOR 0
|
||
#define ERPC_VERSION_PATCH 0
|
||
|
||
#define ERPC_MSG_HEADER_SIZE 9
|
||
#define ERPC_MSG_DATA_MAX_SIZE (256 - ERPC_MSG_HEADER_SIZE + 2)
|
||
#define ERPC_MSG_CACHE_SIZE 16 // Maximum number of messages in cache
|
||
|
||
#define ERPC_SEND_TIMEOUT 30 // 发送超时时间,单位tick
|
||
typedef struct erpc_msg_t
|
||
{
|
||
u8 src_id;
|
||
u8 dest_id;
|
||
u8 type;
|
||
u16 port;
|
||
u16 len;
|
||
u16 crc;
|
||
u8 data[ERPC_MSG_DATA_MAX_SIZE];
|
||
} erpc_msg_t;
|
||
|
||
typedef struct erpc_cache_t
|
||
{
|
||
u8 state;
|
||
erpc_msg_t msg;
|
||
} erpc_cache_t;
|
||
typedef enum erpc_msg_type
|
||
{
|
||
ERPC_MSG_TYPE_REQUEST = 0,
|
||
ERPC_MSG_TYPE_ACK,
|
||
ERPC_MSG_TYPE_RESPONSE,
|
||
}erpc_msg_type;
|
||
typedef enum erpc_cache_state
|
||
{
|
||
CACHE_FREE = 0,
|
||
// 占用
|
||
CACHE_IN_USE,
|
||
// 发送cache
|
||
CACHE_WAIT_SEND,
|
||
CACHE_SEND_OK,
|
||
CACHE_SEND_HW_ERR,
|
||
// 接收cache
|
||
CACHE_GET_DATA, // 发现数据
|
||
CACHE_GET_MSG, // 发现完整消息
|
||
CACHE_REC_ACK, // 收到ACK
|
||
CACHE_GET_RESP, // 收到RESP数据包
|
||
CACHE_WAIT_HANDLE, // 等待处理
|
||
} erpc_cache_state;
|
||
typedef enum erpc_error
|
||
{
|
||
ERPC_OK = 0,
|
||
// 发送REQ
|
||
ERPC_ERR_NO_FREE_SEND_CACHE, // 没有空闲的发送缓存
|
||
ERPC_ERR_SEND_TIMEOUT, // 发送REQ超时
|
||
ERPC_ERR_SEND_ACK_TIMEOUT, // 发送REQ成功,但是没有收到ACK
|
||
ERPC_ERR_SEND_PORT_IS_INUSE, // 发送失败,端口已被占用
|
||
ERPC_ERR_SEND_HW_ERR, // 发送失败,硬件错误
|
||
// 接收REQ
|
||
ERPC_ERR_NO_CMD_RETURN, // 发生REQ成功,但是没有收到RESP包
|
||
} erpc_error;
|
||
typedef struct erpc_hw_t
|
||
{
|
||
u8 local_id;
|
||
void (*lock)(void);
|
||
void (*unlock)(void);
|
||
void (*sleep)(u32 ms);
|
||
u32 (*send)(u8 *data, u16 len);
|
||
erpc_cache_t send_cache[ERPC_MSG_CACHE_SIZE];
|
||
erpc_cache_t recv_cache[ERPC_MSG_CACHE_SIZE];
|
||
list_t handler_list;
|
||
} erpc_hw_t;
|
||
|
||
typedef struct handler_list_t
|
||
{
|
||
u16 port;
|
||
void (*handle)(erpc_hw_t *hw, erpc_cache_t *cache, u8 *data, u16 len);
|
||
} handler_list_t;
|
||
|
||
#define CHECK_DEAL(condition, error) \
|
||
if ((condition)) \
|
||
return error;
|
||
/**
|
||
* 内核函数,需要手动sleep
|
||
*/
|
||
void send_core(erpc_hw_t *hw);
|
||
void recv_core(erpc_hw_t *hw);
|
||
void recv_handle(erpc_hw_t *hw);
|
||
|
||
/**
|
||
* 用户函数
|
||
*/
|
||
u32 send_request(erpc_hw_t *hw, u8 dest_id, u16 port, u8 *data, u16 len, u16 timeout, u8 retry);
|
||
u32 send_request_wait_response(erpc_hw_t *hw, u8 dest_id, u16 port, u8 *data_out, u16 len_out, u8 *data_in, u16 *len_in, u16 send_timeout, u16 recv_timeout, u8 retry);
|
||
u32 send_response(erpc_hw_t *hw, erpc_cache_t *cache, u8 *data, u16 len, u16 timeout, u8 retry);
|
||
u32 write_rec_cache(erpc_hw_t *hw, u8 *data, u16 len);
|
||
/**
|
||
* 注册处理函数
|
||
*/
|
||
#define EXPORT_ERPC_HANDLE(hw, _port, _handle) \
|
||
static handler_list_t _A##_handle = \
|
||
{ \
|
||
.port = _port,\
|
||
.handle = _handle,\
|
||
};\
|
||
EXPORT_LIST_NODE(hw.list,_A##_handle);
|
||
|
||
#endif /* _ERPC_CORE_H_ */ |