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

rt-thread的IO设备管理模块为应用提供了一个对设备进行访问的通用接口,,并通过定义的数据结构对设备驱动程序和设备信息进行管理。从系统整体位置来说I/O管理模块相当于设备驱动程序和上层应用之间的一个中间层。

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

1 IO设备管理控制块

[cpp]  view plain  copy
  1. typedef struct rt_device *rt_device_t;  
  2. /** 
  3.  * Device structure 
  4.  */  
  5. struct rt_device  
  6. {  
  7.     struct rt_object          parent;                   /**< inherit from rt_object *///内核对象  
  8.   
  9.     enum rt_device_class_type type;                     /**< device type *///IO设备类型  
  10.     rt_uint16_t               flag;                     /**< device flag *///设备标志  
  11.     rt_uint16_t               open_flag;                /**< device open flag *///打开标志  
  12.   
  13.     rt_uint8_t                device_id;                /**< 0 - 255 *///设备ID  
  14.   
  15.     /* device call back */  
  16.     rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);//数据接收回调函数  
  17.     rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);//数据发送完回调函数  
  18.   
  19.     /* common device interface */  
  20.     rt_err_t  (*init)   (rt_device_t dev);//初始化通用接口  
  21.     rt_err_t  (*open)   (rt_device_t dev, rt_uint16_t oflag);//打开通用接口  
  22.     rt_err_t  (*close)  (rt_device_t dev);//关闭通用接口  
  23.     rt_size_t (*read)   (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);//读通用接口  
  24.     rt_size_t (*write)  (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);//写通用接口  
  25.     rt_err_t  (*control)(rt_device_t dev, rt_uint8_t cmd, void *args);//控制通用接口  
  26.   
  27. #ifdef RT_USING_DEVICE_SUSPEND  
  28.     rt_err_t (*suspend) (rt_device_t dev);//挂起设备  
  29.     rt_err_t (*resumed) (rt_device_t dev);//还原设备  
  30. #endif  
  31.   
  32.     void                     *user_data;                /**< device private data *///私有数据  
  33. };  

其中设备类型type为一枚举类型,有如下定义:

[cpp]  view plain  copy
  1. /** 
  2.  * @addtogroup Device 
  3.  */  
  4.   
  5. /*@{*/  
  6.   
  7. /** 
  8.  * device (I/O) class type 
  9.  */  
  10. enum rt_device_class_type  
  11. {  
  12.     RT_Device_Class_Char = 0,                           /**< character device */  
  13.     RT_Device_Class_Block,                              /**< block device */  
  14.     RT_Device_Class_NetIf,                              /**< net interface */  
  15.     RT_Device_Class_MTD,                                /**< memory device */  
  16.     RT_Device_Class_CAN,                                /**< CAN device */  
  17.     RT_Device_Class_RTC,                                /**< RTC device */  
  18.     RT_Device_Class_Sound,                              /**< Sound device */  
  19.     RT_Device_Class_Graphic,                            /**< Graphic device */  
  20.     RT_Device_Class_I2CBUS,                             /**< I2C bus device */  
  21.     RT_Device_Class_USBDevice,                          /**< USB slave device */  
  22.     RT_Device_Class_USBHost,                            /**< USB host bus */  
  23.     RT_Device_Class_SPIBUS,                             /**< SPI bus device */  
  24.     RT_Device_Class_SPIDevice,                          /**< SPI device */  
  25.     RT_Device_Class_SDIO,                               /**< SDIO bus device */  
  26.     RT_Device_Class_PM,                                 /**< PM pseudo device */  
  27.     RT_Device_Class_Unknown                             /**< unknown device */  
  28. };  

2 接口源码分析

2.1 注册设备

在一个设备能够被上层应用访问前,需要先把这个设备注册到系统中,并添加一些相应的属性。这些注册的设备均可以采用“查找设备接口”通过设备名来查找设备,获得该设备控制块.

其源码如下:

[cpp]  view plain  copy
  1. /** 
  2.  * This function registers a device driver with specified name. 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * @param name the device driver's name 
  6.  * @param flags the flag of device 
  7.  * 
  8.  * @return the error code, RT_EOK on initialization successfully. 
  9.  */  
  10. rt_err_t rt_device_register(rt_device_t dev,  
  11.                             const char *name,  
  12.                             rt_uint16_t flags)  
  13. {  
  14.     if (dev == RT_NULL)  
  15.         return -RT_ERROR;  
  16.   
  17.     if (rt_device_find(name) != RT_NULL)//尝试通过设备名查找该设备,如果查到,则说明已经注册过,所以返回错误  
  18.         return -RT_ERROR;  
  19.   
  20.     rt_object_init(&(dev->parent), RT_Object_Class_Device, name);//初始化内核对象,此过程会对内核对象添加到内核对象管理系统中,见之前的文章  
  21.     dev->flag = flags;  
  22.   
  23.     return RT_EOK;  
  24. }  

2.2 卸载设备

与注册设备相反,卸载设备是将原先注册好的一个设备从设备管理系统中移除:

[cpp]  view plain  copy
  1. /** 
  2.  * This function removes a previously registered device driver 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * 
  6.  * @return the error code, RT_EOK on successfully. 
  7.  */  
  8. rt_err_t rt_device_unregister(rt_device_t dev)  
  9. {  
  10.     RT_ASSERT(dev != RT_NULL);  
  11.   
  12.     rt_object_detach(&(dev->parent));//脱离内核对象,该过程会将内核对象从内核对象系统中移除  
  13.   
  14.     return RT_EOK;  
  15. }  

2.3 初始化所有设备

初始化所有已经注册到系统中的设备,该函数在rt-thread的启动中会被调用.

[cpp]  view plain  copy
  1. /** 
  2.  * This function initializes all registered device driver 
  3.  * 
  4.  * @return the error code, RT_EOK on successfully. 
  5.  */  
  6. rt_err_t rt_device_init_all(void)  
  7. {  
  8.     struct rt_device *device;  
  9.     struct rt_list_node *node;  
  10.     struct rt_object_information *information;  
  11.     register rt_err_t result;  
  12.   
  13.     extern struct rt_object_information rt_object_container[];  
  14.   
  15.     information = &rt_object_container[RT_Object_Class_Device];//通过类型找到对应的内核对象容器  
  16.   
  17.     /* for each device */  
  18.     for (node  = information->object_list.next;//依次扫描各个已经注册的设备  
  19.          node != &(information->object_list);  
  20.          node  = node->next)  
  21.     {  
  22.         rt_err_t (*init)(rt_device_t dev);  
  23.         device = (struct rt_device *)rt_list_entry(node,//获取设备控制块  
  24.                                                    struct rt_object,  
  25.                                                    list);  
  26.   
  27.         /* get device init handler */  
  28.         init = device->init;  
  29.         if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))//如果设备控制块中已设置了初始化函数,则调用初始化函数进行初始化  
  30.         {  
  31.             result = init(device);  
  32.             if (result != RT_EOK)  
  33.             {  
  34.                 rt_kprintf("To initialize device:%s failed. The error code is %d\n",  
  35.                            device->parent.name, result);  
  36.             }  
  37.             else  
  38.             {  
  39.                 device->flag |= RT_DEVICE_FLAG_ACTIVATED;//设置设备标志为已激活标志  
  40.             }  
  41.         }  
  42.     }  
  43.   
  44.     return RT_EOK;  
  45. }  

从上面源码可知,此函数会从内核对象容器中逐个扫描注册好的设备,然后调用其初始化函数进行初始化。


2.4 查找设备

此函数实现通过指定设备名找到对应的设备结构控制块。

[cpp]  view plain  copy
  1. /** 
  2.  * This function finds a device driver by specified name. 
  3.  * 
  4.  * @param name the device driver's name 
  5.  * 
  6.  * @return the registered device driver on successful, or RT_NULL on failure. 
  7.  */  
  8. rt_device_t rt_device_find(const char *name)  
  9. {  
  10.     struct rt_object *object;  
  11.     struct rt_list_node *node;  
  12.     struct rt_object_information *information;  
  13.   
  14.     extern struct rt_object_information rt_object_container[];  
  15.   
  16.     /* enter critical */  
  17.     if (rt_thread_self() != RT_NULL)//如果当前正在有线程在运行,则进入临界区,即停止线程调度  
  18.         rt_enter_critical();  
  19.   
  20.     /* try to find device object */  
  21.     information = &rt_object_container[RT_Object_Class_Device];//获取对应类型的内核对象容器  
  22.     for (node  = information->object_list.next;//依次扫描各个注册好的设备  
  23.          node != &(information->object_list);  
  24.          node  = node->next)  
  25.     {  
  26.         object = rt_list_entry(node, struct rt_object, list);//得到设备内核对象  
  27.         if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)//比较名字  
  28.         {  
  29.             /* leave critical */  
  30.             if (rt_thread_self() != RT_NULL)//离开临界区,即使用调度器  
  31.                 rt_exit_critical();  
  32.   
  33.             return (rt_device_t)object;//返回当前设备控制块  
  34.         }  
  35.     }  
  36.   
  37.     /* leave critical */  
  38.     if (rt_thread_self() != RT_NULL)//离开临界区,即使用调度器  
  39.         rt_exit_critical();  
  40.   
  41.     /* not found */  
  42.     return RT_NULL;  
  43. }  

2.5 设备初始化

[cpp]  view plain  copy
  1. /** 
  2.  * This function will initialize the specified device 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  *  
  6.  * @return the result 
  7.  */  
  8. rt_err_t rt_device_init(rt_device_t dev)  
  9. {  
  10.     rt_err_t result = RT_EOK;  
  11.   
  12.     RT_ASSERT(dev != RT_NULL);  
  13.   
  14.     /* get device init handler */  
  15.     if (dev->init != RT_NULL)  
  16.     {  
  17.         if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))//如果当前设备没有激活  
  18.         {  
  19.             result = dev->init(dev);//调用其初始化函数进行初始化  
  20.             if (result != RT_EOK)  
  21.             {  
  22.                 rt_kprintf("To initialize device:%s failed. The error code is %d\n",  
  23.                            dev->parent.name, result);  
  24.             }  
  25.             else  
  26.             {  
  27.                 dev->flag |= RT_DEVICE_FLAG_ACTIVATED;//设备设备激活标志  
  28.             }  
  29.         }  
  30.     }  
  31.   
  32.     return result;  
  33. }  

2.6 打开设备

[cpp]  view plain  copy
  1. /** 
  2.  * This function will open a device 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * @param oflag the flags for device open 
  6.  * 
  7.  * @return the result 
  8.  */  
  9. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)  
  10. {  
  11.     rt_err_t result = RT_EOK;  
  12.   
  13.     RT_ASSERT(dev != RT_NULL);  
  14.   
  15.     /* if device is not initialized, initialize it. */  
  16.     if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))//如果当前设备没有激活  
  17.     {  
  18.         if (dev->init != RT_NULL)  
  19.         {  
  20.             result = dev->init(dev);//调用其初始化函数进行初始化  
  21.             if (result != RT_EOK)  
  22.             {  
  23.                 rt_kprintf("To initialize device:%s failed. The error code is %d\n",  
  24.                            dev->parent.name, result);  
  25.   
  26.                 return result;  
  27.             }  
  28.         }  
  29.   
  30.         dev->flag |= RT_DEVICE_FLAG_ACTIVATED;//设备激活标志  
  31.     }  
  32.   
  33.     /* device is a stand alone device and opened *///如果设备已经打开  
  34.     if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&  
  35.         (dev->open_flag & RT_DEVICE_OFLAG_OPEN))  
  36.     {  
  37.         return -RT_EBUSY;  
  38.     }  
  39.   
  40.     /* call device open interface */  
  41.     if (dev->open != RT_NULL)  
  42.     {  
  43.         result = dev->open(dev, oflag);//打开设备  
  44.     }  
  45.   
  46.     /* set open flag */  
  47.     if (result == RT_EOK || result == -RT_ENOSYS)  
  48.         dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;//设备打开标志  
  49.   
  50.     return result;  
  51. }  

2.7 关闭设备

[cpp]  view plain  copy
  1. /** 
  2.  * This function will close a device 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * 
  6.  * @return the result 
  7.  */  
  8. rt_err_t rt_device_close(rt_device_t dev)  
  9. {  
  10.     rt_err_t result = RT_EOK;  
  11.   
  12.     RT_ASSERT(dev != RT_NULL);  
  13.   
  14.     /* call device close interface */  
  15.     if (dev->close != RT_NULL)  
  16.     {  
  17.         result = dev->close(dev);//关闭设备  
  18.     }  
  19.   
  20.     /* set open flag */  
  21.     if (result == RT_EOK || result == -RT_ENOSYS)  
  22.         dev->open_flag = RT_DEVICE_OFLAG_CLOSE;//设置打开标志为关闭状态  
  23.   
  24.     return result;  
  25. }  

2.8 读设备

[cpp]  view plain  copy
  1. /** 
  2.  * This function will read some data from a device. 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * @param pos the position of reading 
  6.  * @param buffer the data buffer to save read data 
  7.  * @param size the size of buffer 
  8.  * 
  9.  * @return the actually read size on successful, otherwise negative returned. 
  10.  * 
  11.  * @note since 0.4.0, the unit of size/pos is a block for block device. 
  12.  */  
  13. rt_size_t rt_device_read(rt_device_t dev,  
  14.                          rt_off_t    pos,  
  15.                          void       *buffer,  
  16.                          rt_size_t   size)  
  17. {  
  18.     RT_ASSERT(dev != RT_NULL);  
  19.   
  20.     /* call device read interface */  
  21.     if (dev->read != RT_NULL)//如果当前存在读取接口  
  22.     {  
  23.         return dev->read(dev, pos, buffer, size);//调用读接口进行读操作  
  24.     }  
  25.   
  26.     /* set error code */  
  27.     rt_set_errno(-RT_ENOSYS);//如果当前不存在读取接口,则设置错误码为-RT_ENOSYS  
  28.   
  29.     return 0;  
  30. }  

2.9 写设备

[cpp]  view plain  copy
  1. /** 
  2.  * This function will write some data to a device. 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * @param pos the position of written 
  6.  * @param buffer the data buffer to be written to device 
  7.  * @param size the size of buffer 
  8.  * 
  9.  * @return the actually written size on successful, otherwise negative returned. 
  10.  * 
  11.  * @note since 0.4.0, the unit of size/pos is a block for block device. 
  12.  */  
  13. rt_size_t rt_device_write(rt_device_t dev,  
  14.                           rt_off_t    pos,  
  15.                           const void *buffer,  
  16.                           rt_size_t   size)  
  17. {  
  18.     RT_ASSERT(dev != RT_NULL);  
  19.   
  20.     /* call device write interface */  
  21.     if (dev->write != RT_NULL)//如果当前存在写接口  
  22.     {  
  23.         return dev->write(dev, pos, buffer, size);//调用写接口进行写操作  
  24.     }  
  25.   
  26.     /* set error code */  
  27.     rt_set_errno(-RT_ENOSYS);//如果当前不存在写接口,则设备当前错误码为-RT_ENOSYS  
  28.   
  29.     return 0;  
  30. }  

2.10 控制设备

[cpp]  view plain  copy
  1. /** 
  2.  * This function will perform a variety of control functions on devices. 
  3.  * 
  4.  * @param dev the pointer of device driver structure 
  5.  * @param cmd the command sent to device 
  6.  * @param arg the argument of command 
  7.  * 
  8.  * @return the result 
  9.  */  
  10. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)  
  11. {  
  12.     RT_ASSERT(dev != RT_NULL);  
  13.   
  14.     /* call device write interface */  
  15.     if (dev->control != RT_NULL)  
  16.     {  
  17.         return dev->control(dev, cmd, arg);//调用控制接口进行操作  
  18.     }  
  19.   
  20.     return RT_EOK;  
  21. }  

2.11 设备接收回调函数

如果设备接收到数据时,将会主动调用此回调函数进行处理,但一般只是进行些简单的操作,如发送信号量,而让另一个接收线程来处理接收到的数据.该接口只是给设备控制块设置此回调函数

[cpp]  view plain  copy
  1. /** 
  2.  * This function will set the indication callback function when device receives 
  3.  * data. 
  4.  * 
  5.  * @param dev the pointer of device driver structure 
  6.  * @param rx_ind the indication callback function 
  7.  * 
  8.  * @return RT_EOK 
  9.  */  
  10. rt_err_t  
  11. rt_device_set_rx_indicate(rt_device_t dev,  
  12.                           rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))  
  13. {  
  14.     RT_ASSERT(dev != RT_NULL);  
  15.   
  16.     dev->rx_indicate = rx_ind;  
  17.   
  18.     return RT_EOK;  
  19. }  

2.12 设备发送回调函数

与接收回调函数对应,但设备发送完数据时,也会调用此回调函数。此接口只是用来给设备控制块设备此回调函数。

[cpp]  view plain  copy
  1. /** 
  2.  * This function will set the indication callback function when device has  
  3.  * written data to physical hardware. 
  4.  * 
  5.  * @param dev the pointer of device driver structure 
  6.  * @param tx_done the indication callback function 
  7.  * 
  8.  * @return RT_EOK 
  9.  */  
  10. rt_err_t  
  11. rt_device_set_tx_complete(rt_device_t dev,  
  12.                           rt_err_t (*tx_done)(rt_device_t dev, void *buffer))  
  13. {  
  14.     RT_ASSERT(dev != RT_NULL);  
  15.   
  16.     dev->tx_complete = tx_done;  
  17.   
  18.     return RT_EOK;  
  19. }  

3 设备驱动实现的步骤

上述内容已经比较详细地介绍了设备控制块的数据结构及其相关的操作接口,那么在具体实现中,又是如何实现一个设备的驱动的呢?

步骤1:根据rt_device定义的结构定义一设备变量,根据设备公共接口,实现各个接口,当然也可以是空函数。

步骤2:根据自己的设备类型定义自己的私有数据域。特别是可以有多个相同设备的情况下,设备接口可以用同一套,不同的只是各自的数据域(例如寄存器基地址)。

步骤3: 按照RT-Thread的对象模型,扩展一个对象有两种方式:
           (a) 定义自己的私有数据结构,然后赋值到RT-Thread设备控制块的private指针上。
           (b) 从struct rt device结构中进行派生。

步骤4: 根据设备的类型,注册到RT-Thread设备框架中,即调用rt_device_register接口进行注册.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值