`
jiagou
  • 浏览: 2525902 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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接口进行注册.


完!


分享到:
评论

相关推荐

    在STM32L051C8上使用 RT-Thread Nano 实例项目源码

    本资源是我的RT-Thread专栏应用篇《RT-Thread 应用篇 — 在STM32L051上使用 RT-Thread》的工程源码: 一个简单的应用:无线温湿度传感器 一个小内存的芯片:STM32L051C8T6 一个小而美丽的物联网操作系统:RT-Thread ...

    rt-thread源码

    硬实时内核,这层是RT-Thread的核心,包括了内核系统中对象的实现,例如多线程及其调度,信号量,邮箱,消息队列,内存管理,定时器等实现。 组件层,这些是基于RT-Thread核心基础上的外围组件,例如文件系统,...

    RT-Thread API参考手册.pdf

    RT-Thread 嵌入式实时操作系统 API参考手册 多线程及其调度、信号量、邮箱、消息队列、内存管理、定时器等

    C语言开发基于RT-Thread家庭安全环境检测系统源码.zip

    基于RT-Thread家庭安全环境检测系统源码。主要包含如下功能: 1、基于RT-Thread操作系统的按键组件,音频播放组件等; 2、基于AB32VG1开发板的语音播放功能; 3、基于Node-Red的串口功能与AB32VG1通讯; 4、连接腾讯...

    解决RT-Thread Studio包管理失败的更新文件

    RT-Thread Studio包管理失败,原因就是\platform\env_released\env\tools\Python27\DLLs的_ssl.pyd文件有bug,用该更新文件替换后将会解决问题。

    RT-Thread实时操作系统编程指南

    • RT-Thread快速入门,在无硬件平台的情况下,如何迅速地了解RT-Thread实时操作系统, 如何使用RT-Thread实时操作系统最基本的一些元素; • RT-Thread作为一个完整的实时操作系统,它能够满足各种实时系统的需求,...

    STM32+Nano版RT-thread+LWIP移植源码

    主要提供STM32 基于nano版本的-RT-thread操作系统基础上对LWIP协议栈进行移植,并实现网络通讯功能,提供源码以及测试例程,以及说明文档

    RT-THREAD 编程指南 中文手册

    RT-THREAD 编程指南,RT-THREAD是一个很好用的操作系统。学习它可以了解操作系统的相关知识,对于学习单片机的朋友很有帮助。

    rt-thread-v3.0.2 源代码

    RT-Thread 是一款主要由中国开源社区主导开发的开源实时操作系统。实时线程操作系统不仅仅是一个单一的实时操作系统内核,它也是一个完整的应用系统,包含了实时、嵌入式系统相关的各个组件:TCP/IP协议栈,文件系统...

    rt-thread入门教程PPT

    rt-thread入门教程PPT

    RealThread.RT-Thread.3.1.3.pack

    RealThread.RT-Thread.3.1.3.pack的离线安装包,官方下载。直接下载安装就可以使用了。移植RT-Thread使用。

    rt-thread-3.1.3_rtthread_RT-Thread_nano_rtthreadopenocd_RT-Threa

    rtthread nano的模板,有基本的内核部分,可以完成线程调度、信号量传递等等

    RT-THREAD 编程指南.pdf

    RT-THREAD 编程指南

    RT-thread系统GPRS远程升级

    根据rt-thread系统来进行编码,通过单片机控制GPRS模块来进行远程升级

    2022年RT-Thread全球技术大会国内专场PPT合集(31份).zip

    RT-Thread 电源管理组件 RT-Thread 构建配置系统 RT-Thread 上的单元测试 RT-Thread 中的 POSIX 支持 RT-Thread开源社区蓝牙调试分享 RT-Thread在摄像头及IoT设备上的实践经验分享 高性能RISCV MCU在以太网以及CAN ...

    RealThread.RT-Thread.3.1.5.zip

    Keil RT-Thread Pack Installer File

    rt-thread-nano-3.1.3.rar

    rt-thread-nano-3.1.3,RT-Thread微内核,包含STM32的移植程序及应用程序,以及线程、队列、事件的应用

    RT-Thread最全入门教程

    RT-Thread是一个实时的内核(全抢占优先级调度,调度器时间复杂度O(1)),但在发展过程中,RT-Thread实时操作系统得到了来自全国嵌入式开发工程师的鼎力支持,为RT-Thread添砖加瓦,现在它不仅仅是一款高效、稳定的...

    AN0001-RT-Thread-串口设备应用笔记.pdf

    RT-THREAD串口设备应用笔记,本文首先给出使用 RT-Thread 的设备操作接口开发串口收、发数据程序的示例代码,并在正点原子 STM32F4 ...接着分析了示例代码的实现,最后深入地描述了 RT-Thread 设备管理框架与串口的联系

    RT-Thread Studio-v2.2.6-setup-x86-64-202305191040

    RT-Thread,全称是 Real Time-Thread,顾名思义,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,允许多个任务同时运行并不意味着...而对于资源丰富的物联网设备,RT-Thread 又能使用在线的软件包管理工

Global site tag (gtag.js) - Google Analytics