rt-thread的IO设备管理模块为应用提供了一个对设备进行访问的通用接口,,并通过定义的数据结构对设备驱动程序和设备信息进行管理。从系统整体位置来说I/O管理模块相当于设备驱动程序和上层应用之间的一个中间层。
I/O管理模块实现了对设备驱动程序的封装:设备驱动程序的实现与I/O管理模块独立,提高了模块的可移植性。应用程序通过I/O管理模块提供的标准接口访问底层设备,设备驱动程序的升级不会对上层应用产生影响。这种方式使得与设备的硬件操作相关的代码与应用相隔离,双方只需各自关注自己的功能,这降低了代码的复杂性,提高了系统的可靠性。
1 IO设备管理控制块
- 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为一枚举类型,有如下定义:
- /**
- * @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 注册设备
在一个设备能够被上层应用访问前,需要先把这个设备注册到系统中,并添加一些相应的属性。这些注册的设备均可以采用“查找设备接口”通过设备名来查找设备,获得该设备控制块.
其源码如下:
- /**
- * 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;
- if (rt_device_find(name) != RT_NULL)//尝试通过设备名查找该设备,如果查到,则说明已经注册过,所以返回错误
- return -RT_ERROR;
- rt_object_init(&(dev->parent), RT_Object_Class_Device, name);//初始化内核对象,此过程会对内核对象添加到内核对象管理系统中,见之前的文章
- dev->flag = flags;
- return RT_EOK;
- }
2.2 卸载设备
与注册设备相反,卸载设备是将原先注册好的一个设备从设备管理系统中移除:
- /**
- * This function removes a previously registered device driver
- *
- * @param dev the pointer of device driver structure
- *
- * @return the error code, RT_EOK on successfully.
- */
- rt_err_t rt_device_unregister(rt_device_t dev)
- {
- RT_ASSERT(dev != RT_NULL);
- rt_object_detach(&(dev->parent));//脱离内核对象,该过程会将内核对象从内核对象系统中移除
- return RT_EOK;
- }
2.3 初始化所有设备
初始化所有已经注册到系统中的设备,该函数在rt-thread的启动中会被调用.
- /**
- * This function initializes all registered device driver
- *
- * @return the error code, RT_EOK on successfully.
- */
- rt_err_t rt_device_init_all(void)
- {
- struct rt_device *device;
- struct rt_list_node *node;
- struct rt_object_information *information;
- register rt_err_t result;
- extern struct rt_object_information rt_object_container[];
- information = &rt_object_container[RT_Object_Class_Device];//通过类型找到对应的内核对象容器
- /* for each device */
- for (node = information->object_list.next;//依次扫描各个已经注册的设备
- node != &(information->object_list);
- node = node->next)
- {
- rt_err_t (*init)(rt_device_t dev);
- device = (struct rt_device *)rt_list_entry(node,//获取设备控制块
- struct rt_object,
- list);
- /* get device init handler */
- init = device->init;
- if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))//如果设备控制块中已设置了初始化函数,则调用初始化函数进行初始化
- {
- result = init(device);
- if (result != RT_EOK)
- {
- rt_kprintf("To initialize device:%s failed. The error code is %d\n",
- device->parent.name, result);
- }
- else
- {
- device->flag |= RT_DEVICE_FLAG_ACTIVATED;//设置设备标志为已激活标志
- }
- }
- }
- return RT_EOK;
- }
从上面源码可知,此函数会从内核对象容器中逐个扫描注册好的设备,然后调用其初始化函数进行初始化。
2.4 查找设备
此函数实现通过指定设备名找到对应的设备结构控制块。
- /**
- * This function finds a device driver by specified name.
- *
- * @param name the device driver's name
- *
- * @return the registered device driver on successful, or RT_NULL on failure.
- */
- rt_device_t rt_device_find(const char *name)
- {
- struct rt_object *object;
- struct rt_list_node *node;
- struct rt_object_information *information;
- extern struct rt_object_information rt_object_container[];
- /* enter critical */
- if (rt_thread_self() != RT_NULL)//如果当前正在有线程在运行,则进入临界区,即停止线程调度
- rt_enter_critical();
- /* try to find device object */
- information = &rt_object_container[RT_Object_Class_Device];//获取对应类型的内核对象容器
- for (node = information->object_list.next;//依次扫描各个注册好的设备
- node != &(information->object_list);
- node = node->next)
- {
- object = rt_list_entry(node, struct rt_object, list);//得到设备内核对象
- if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)//比较名字
- {
- /* leave critical */
- if (rt_thread_self() != RT_NULL)//离开临界区,即使用调度器
- rt_exit_critical();
- return (rt_device_t)object;//返回当前设备控制块
- }
- }
- /* leave critical */
- if (rt_thread_self() != RT_NULL)//离开临界区,即使用调度器
- rt_exit_critical();
- /* not found */
- return RT_NULL;
- }
2.5 设备初始化
- /**
- * This function will initialize the specified device
- *
- * @param dev the pointer of device driver structure
- *
- * @return the result
- */
- rt_err_t rt_device_init(rt_device_t dev)
- {
- rt_err_t result = RT_EOK;
- RT_ASSERT(dev != RT_NULL);
- /* get device init handler */
- if (dev->init != RT_NULL)
- {
- if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))//如果当前设备没有激活
- {
- result = dev->init(dev);//调用其初始化函数进行初始化
- if (result != RT_EOK)
- {
- rt_kprintf("To initialize device:%s failed. The error code is %d\n",
- dev->parent.name, result);
- }
- else
- {
- dev->flag |= RT_DEVICE_FLAG_ACTIVATED;//设备设备激活标志
- }
- }
- }
- return result;
- }
2.6 打开设备
- /**
- * This function will open a device
- *
- * @param dev the pointer of device driver structure
- * @param oflag the flags for device open
- *
- * @return the result
- */
- rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
- {
- rt_err_t result = RT_EOK;
- RT_ASSERT(dev != RT_NULL);
- /* if device is not initialized, initialize it. */
- if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))//如果当前设备没有激活
- {
- if (dev->init != RT_NULL)
- {
- result = dev->init(dev);//调用其初始化函数进行初始化
- if (result != RT_EOK)
- {
- rt_kprintf("To initialize device:%s failed. The error code is %d\n",
- dev->parent.name, result);
- return result;
- }
- }
- dev->flag |= RT_DEVICE_FLAG_ACTIVATED;//设备激活标志
- }
- /* device is a stand alone device and opened *///如果设备已经打开
- if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
- (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
- {
- return -RT_EBUSY;
- }
- /* call device open interface */
- if (dev->open != RT_NULL)
- {
- result = dev->open(dev, oflag);//打开设备
- }
- /* set open flag */
- if (result == RT_EOK || result == -RT_ENOSYS)
- dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;//设备打开标志
- return result;
- }
2.7 关闭设备
- /**
- * This function will close a device
- *
- * @param dev the pointer of device driver structure
- *
- * @return the result
- */
- rt_err_t rt_device_close(rt_device_t dev)
- {
- rt_err_t result = RT_EOK;
- RT_ASSERT(dev != RT_NULL);
- /* call device close interface */
- if (dev->close != RT_NULL)
- {
- result = dev->close(dev);//关闭设备
- }
- /* set open flag */
- if (result == RT_EOK || result == -RT_ENOSYS)
- dev->open_flag = RT_DEVICE_OFLAG_CLOSE;//设置打开标志为关闭状态
- return result;
- }
2.8 读设备
- /**
- * This function will read some data from a device.
- *
- * @param dev the pointer of device driver structure
- * @param pos the position of reading
- * @param buffer the data buffer to save read data
- * @param size the size of buffer
- *
- * @return the actually read size on successful, otherwise negative returned.
- *
- * @note since 0.4.0, the unit of size/pos is a block for block device.
- */
- rt_size_t rt_device_read(rt_device_t dev,
- rt_off_t pos,
- void *buffer,
- rt_size_t size)
- {
- RT_ASSERT(dev != RT_NULL);
- /* call device read interface */
- if (dev->read != RT_NULL)//如果当前存在读取接口
- {
- return dev->read(dev, pos, buffer, size);//调用读接口进行读操作
- }
- /* set error code */
- rt_set_errno(-RT_ENOSYS);//如果当前不存在读取接口,则设置错误码为-RT_ENOSYS
- return 0;
- }
2.9 写设备
- /**
- * This function will write some data to a device.
- *
- * @param dev the pointer of device driver structure
- * @param pos the position of written
- * @param buffer the data buffer to be written to device
- * @param size the size of buffer
- *
- * @return the actually written size on successful, otherwise negative returned.
- *
- * @note since 0.4.0, the unit of size/pos is a block for block device.
- */
- rt_size_t rt_device_write(rt_device_t dev,
- rt_off_t pos,
- const void *buffer,
- rt_size_t size)
- {
- RT_ASSERT(dev != RT_NULL);
- /* call device write interface */
- if (dev->write != RT_NULL)//如果当前存在写接口
- {
- return dev->write(dev, pos, buffer, size);//调用写接口进行写操作
- }
- /* set error code */
- rt_set_errno(-RT_ENOSYS);//如果当前不存在写接口,则设备当前错误码为-RT_ENOSYS
- return 0;
- }
2.10 控制设备
- /**
- * This function will perform a variety of control functions on devices.
- *
- * @param dev the pointer of device driver structure
- * @param cmd the command sent to device
- * @param arg the argument of command
- *
- * @return the result
- */
- rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
- {
- RT_ASSERT(dev != RT_NULL);
- /* call device write interface */
- if (dev->control != RT_NULL)
- {
- return dev->control(dev, cmd, arg);//调用控制接口进行操作
- }
- return RT_EOK;
- }
2.11 设备接收回调函数
如果设备接收到数据时,将会主动调用此回调函数进行处理,但一般只是进行些简单的操作,如发送信号量,而让另一个接收线程来处理接收到的数据.该接口只是给设备控制块设置此回调函数
- /**
- * This function will set the indication callback function when device receives
- * data.
- *
- * @param dev the pointer of device driver structure
- * @param rx_ind the indication callback function
- *
- * @return RT_EOK
- */
- 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_ASSERT(dev != RT_NULL);
- dev->rx_indicate = rx_ind;
- return RT_EOK;
- }
2.12 设备发送回调函数
与接收回调函数对应,但设备发送完数据时,也会调用此回调函数。此接口只是用来给设备控制块设备此回调函数。
- /**
- * This function will set the indication callback function when device has
- * written data to physical hardware.
- *
- * @param dev the pointer of device driver structure
- * @param tx_done the indication callback function
- *
- * @return RT_EOK
- */
- rt_err_t
- rt_device_set_tx_complete(rt_device_t dev,
- rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
- {
- RT_ASSERT(dev != RT_NULL);
- dev->tx_complete = tx_done;
- return RT_EOK;
- }
3 设备驱动实现的步骤
上述内容已经比较详细地介绍了设备控制块的数据结构及其相关的操作接口,那么在具体实现中,又是如何实现一个设备的驱动的呢?
步骤1:根据rt_device定义的结构定义一设备变量,根据设备公共接口,实现各个接口,当然也可以是空函数。
步骤2:根据自己的设备类型定义自己的私有数据域。特别是可以有多个相同设备的情况下,设备接口可以用同一套,不同的只是各自的数据域(例如寄存器基地址)。
步骤3: 按照RT-Thread的对象模型,扩展一个对象有两种方式:
(a) 定义自己的私有数据结构,然后赋值到RT-Thread设备控制块的private指针上。
(b) 从struct rt device结构中进行派生。
步骤4: 根据设备的类型,注册到RT-Thread设备框架中,即调用rt_device_register接口进行注册.