rtt学习笔记二:I/O设备管理

I/O设备管理

I/O管理模块为应用提供了一个对设备进行访问的通用接口,并通过定义的数据结构对设备驱动程序和设备信息进行管理。

I/O管理模块实现了对设备驱动程序的封装:设备驱动程序的实现与I/O管理模块独立,提高了模块的可移植性。应用程序通过I/O管理模块提供的标准接口访问底层设备,设备驱动程序的升级对上层应用没有影响。这种方式使得与设备的硬件操作相关的代码与应用相隔离,双方只需各自关心自己的功能,这降低了代码的复杂性,提高了系统的可靠性。

rtt中,设备也被认为是一类对象,被纳入对象管理器范畴,每个设备对象都是由基对象派生而来,每个具体设备都可以继承其父类对象的属性,并派生出其私有属性。

 

 I/O设备控制块:

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 (*cloase)(rt_device_t 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_size_t (*control)(rt_device_t dev, rt_uint8_t   cmd, void *args);

 

#ifdef RT_USING_DEVICE_SUSPEND

Rt_err_t (*suspend)(rt_device_t dev);

Rt_err_t (*resume)(rt_device_t dev);

#endif

 

//设备私有数据

Void *private;

};

当前rtt支持的设备类型包括:

Enum rt_device_class_type

{

RT_DEVICE_CLASS_Char = 0,

RT_DEVICE_CLASS_Block,

RT_DEVICE_CLASS_Netif,

RT_DEVICE_CLASSS_MTD,

RT_DEVIE_CLASS_CAN,

RT_DEVICE_CLASS_Unknown

};

 

I/O设备管理接口:

注册设备:

Rt_err_t rt_device_register(rt_device_t  dev, const char *name, rt_uint8_t flag)

 

Flag参数:

#define RT_DEVICE_FLAG_DEACTIVATE 0x000  //未初始化设备

 

#define RT_DEVICE_FLAG_RDONLY 0x001 //只读设备

#define RT_DEVICE_FLAG_WRONLY 0x002 //只写设备

#define RT_DEVICE_FLAG_RDWR 0x003 //读写设备

 

#define RT_DEVICE_FLAG_REMOVEABLE 0x004 //可移除设备

#define  RT_DEVICE_FLAG_STANDALONE 0x008 //独立设备

#define  RT_DEVICE_FLAG_ACTIVATED 0x010 //已激活设备

#define  RT_DEVICE_FLAG_SUSPEND 0x020 //挂起设备

#define  RT_DEVICE_FLAG_STREAM 0x040 //设备处于流模式

 

#define RT_DEVICE_FLAG_INT_RX 0x100 //设备处于中断接受模式

#define   RT_DEVICE_FLAG_INT_TX 0x400 //设备处于中断发送模式

#define  RT_DEVICE_FLAG_DMA_RX 0x200 //设备处于DMA接受模式

#define RT_DEVICE_FALG_DMA_TX 0x800  //设备处于DMA发送模式

 

 

 

卸载设备:

Rt_err_t rt_device_unregister(rt_device_t dev)

卸载设备并不会释放设备控制块所占用的内存

 

初始化所有设备:

Rt_err_t rt_device_init_all(void)

如果设备的flags域已经是RT_DEVICE_GLAF_ACTIVATED,,调用这个接口将不再重复做初始化,一个设备初始化完成后它的flagsRT_DEVICE_FLAG_ACTIVATED应该被置位。

 

查找设备:

Rt_device_t rt_device_find(const char *name)

在设备对象类型所对应的对象容器中遍历寻找设备对象,然后返回该设备,如果没有找到相应的设备对象,则返回RT_NULL

 

打开设备:

Rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflags)

 

Oflag参数:

#define RT_DEVICE_OFLAG_RDONLY 0x001 //只读模式访问

#define RT_DEVICE_OFLAG_WRONLY 0x002 //只写模式访问

#define RT_DEVICE_OFLAG_RDWR 0x003 //读写模式访问

 

关闭设备:

Rt_err_t rt_device_close(rt_device_t dev)

 

读设备:

Rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);

 

写设备:

Rt_size_t  rt_device_write(rt_device_t dev, rt_off_t pos,const void *buffer, rt_size_t size);

 

控制设备:

Rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd,  void *arg)

 

设置数据接收指示:

Rt_err_t rt_device_set_rx_indicate(rt_device_t dev,rt_err_t (*rx_ind)(rt_device_t dev,rt_size_t size))

 

设置发送完成指示:

Rt_err_t rt_device_t_set_tx_complete(rt_device_t dev,rt_err_t (*tx_done)(rt_device_t dev, void *buffer))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值