当前位置:编程学习 > wap >>

rt-thread的IO设备管理系统源码分析

rt-thread的IO设备管理模块为应用提供了一个对设备进行访问的通用接口,,并通过定义的数据结构对设备驱动程序和设备信息进行管理。从系统整体位置来说I/O管理模块相当于设备驱动程序和上层应用之间的一个中间层。
I/O管理模块实现了对设备驱动程序的封装:设备驱动程序的实现与I/O管理模块独立,提高了模块的可移植性。应用程序通过I/O管理模块提供的标准接口访问底层设备,设备驱动程序的升级不会对上层应用产生影响。这种方式使得与设备的硬件操作相关的代码与应用相隔离,双方只需各自关注自己的功能,这降低了代码的复杂性,提高了系统的可靠性。
1 IO设备管理控制块
[cpp] 
typedef struct rt_device *rt_device_t;  
/** 
 * Device structure 
 */  
struct rt_device  
{  
    struct rt_object          parent;                   /**< inherit from rt_object *///内核对象  
  
    enum rt_device_class_type type;                     /**< device type *///IO设备类型  
    rt_uint16_t               flag;                     /**< device flag *///设备标志  
    rt_uint16_t               open_flag;                /**< device open flag *///打开标志  
  
    rt_uint8_t                device_id;                /**< 0 - 255 *///设备ID  
  
    /* device call back */  
    rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);//数据接收回调函数  
    rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);//数据发送完回调函数  
  
    /* common device interface */  
    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_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_err_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 (*resumed) (rt_device_t dev);//还原设备  
#endif  
  
    void                     *user_data;                /**< device private data *///私有数据  
};  
 
其中设备类型type为一枚举类型,有如下定义:
[cpp] 
/** 
 * @addtogroup Device 
 */  
  
/*@{*/  
  
/** 
 * device (I/O) class type 
 */  
enum rt_device_class_type  
{  
    RT_Device_Class_Char = 0,                           /**< character device */  
    RT_Device_Class_Block,                              /**< block device */  
    RT_Device_Class_NetIf,                              /**< net interface */  
    RT_Device_Class_MTD,                                /**< memory device */  
    RT_Device_Class_CAN,                                /**< CAN device */  
    RT_Device_Class_RTC,                                /**< RTC device */  
    RT_Device_Class_Sound,                              /**< Sound device */  
    RT_Device_Class_Graphic,                            /**< Graphic device */  
    RT_Device_Class_I2CBUS,                             /**< I2C bus device */  
    RT_Device_Class_USBDevice,                          /**< USB slave device */  
    RT_Device_Class_USBHost,                            /**< USB host bus */  
    RT_Device_Class_SPIBUS,                             /**< SPI bus device */  
    RT_Device_Class_SPIDevice,                          /**< SPI device */  
    RT_Device_Class_SDIO,                               /**< SDIO bus device */  
    RT_Device_Class_PM,                                 /**< PM pseudo device */  
    RT_Device_Class_Unknown                             /**< unknown device */  
};  
 
2 接口源码分析
2.1 注册设备
在一个设备能够被上层应用访问前,需要先把这个设备注册到系统中,并添加一些相应的属性。这些注册的设备均可以采用“查找设备接口”通过设备名来查找设备,获得该设备控制块.
其源码如下:
[cpp]  
/** 
 * This function registers a device driver with specified name. 
 * 
 * @param dev the pointer of device driver structure 
 * @param name the device driver's name 
 * @param flags the flag of device 
 * 
 * @return the error code, RT_EOK on initialization successfully. 
 */  
rt_err_t rt_device_register(rt_device_t dev,  
                            const char *name,  
                            rt_uint16_t flags)  
{  
    if (dev == RT_NULL)  
        return -RT_ERROR;  
  
补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,