【本帖内容】RT-thread学习之设备层框架设计浅析
RT-thread学习之一直走下去
RT-thread学习之RTT 的与众不同
RT-thread学习之object对象管理机制
RT-thread学习之Thread线程机制及应用
RT-thread学习之线程间的同步
用户接口部分处理
/*串口数据接收处理结构*/
struct rcv_deal_pro_st
{
char rcv_buff[1024];
int total_len;
int port;
void *result;
int (*resolve_pro_func)(unsigned char *, int, void *);
int (*deal_pro_func)(int, void *);
};
用户接口实现细节
/*
*函数功能:启动串口数据发送功能
* des:要写入参数的地址
* len:要写入参数的长度
* port:要写入的串口号
* 返回值:实际写入的数据长度,这个参数有点鸡肋啊。。。
*/
int write_serial(unsigned char *des,int len,int port)
{
int i,temp_len=0;
SERIAL_STR *serial_snd = &serial_snd_g[port -1];
if(serial_snd!=0)
{
for(i=0;i
*serial_snd->write_pointer = des[temp_len++];
serial_snd->write_pointer++;
if((serial_snd->write_pointer-serial_snd->datas_buffer)>MAX_REC_SIZE-1)
{
serial_snd->write_pointer=serial_snd->datas_buffer;
serial_snd->write_full_flag=1;
}
}
if(port==1)
{
USART_ITConfig( USART1, USART_IT_TC, ENABLE );
}
else if(port==2)
{
USART_ITConfig( USART2, USART_IT_TXE, ENABLE );
}
else if(port==3)
{
USART_ITConfig( USART3, USART_IT_TXE, ENABLE );
}
}
return temp_len;
}
int rcv_deal_datas(void *datas, void *param, void *flag)
{
struct rcv_deal_pro_st *pro = (struct rcv_deal_pro_st *)datas;
int len = 0;
int type = 0;
len=read_serial( (unsigned char *)(pro->rcv_buff+pro->total_len), sizeof(pro->rcv_buff)-len-1, pro->port);
if(len)
{
pro->total_len += len;
}
else if(pro->total_len)
{
unsigned char *point = (unsigned char *)pro->rcv_buff;
while(point < pro->rcv_buff + pro->total_len)
{
type = pro->resolve_pro_func(pro->rcv_buff, pro->total_len, pro->result);
//清除缓存数据
if(type && pro->deal_pro_func)
{
pro->deal_pro_func(type, pro->result);
}
point = (unsigned char *)&pro->result;
memset(pro->rcv_buff,0,1024);
}
pro->total_len=0;
}
return 1;
}
接下来看下RTT内部对于设备层的管理是如何定义的:
struct rt_device
{
struct rt_object parent;
/*设备类型*/
enum rt_device_class_type type;
/*设备参数及打开参数*/
rt_uint16_t flag,open_flag;
/*提供给上层应用的回调函数*/
rt_err_t (*rx_indicate)(rt_device_t dev,rt_size_t size);
rt_err_t (*tx_complete)(rt_device_t dev,void* buffer);
/*公共的设备接口(由驱动程序提供)*/
rt_err_t (*init)(rt_device_t dev);
rt_err_t (*open)(rt_device_t dev,rt_uint16_t oflag);
rt_err_t (*close)(rt_device dev);
rt_size_t (*read)(rt_device_t dev,rt_off_t pos,void* buffer,rt_size_t size);
rt_size_t (*write)(rt_device_t dev,rt_off_t pos,const void* buffer,rt_size_t size);
rt_err_t (*control)(rt_device_t dev,rt_uint8_t cmd,void* args);
/*设备的私有数据*/
void* user_data;
};
typedef struct rt_device* rt_device_t;
【本帖内容】RT-thread学习之设备层框架设计浅析
RT-thread学习之一直走下去
RT-thread学习之RTT 的与众不同
RT-thread学习之object对象管理机制
RT-thread学习之Thread线程机制及应用
RT-thread学习之线程间的同步