如何进行RT-Thread中设备模型rt_device的理解

发布时间:2021-12-17 15:29:25 作者:柒染
来源:亿速云 阅读:557

如何进行RT-Thread中设备模型rt_device的理解,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

前言

rt_device的结构

如何进行RT-Thread中设备模型rt_device的理解

rt_device_class_type.png

rt_device的使用

pin设备模型:结构如下:

/* pin device and operations for RT-Thread */
struct rt_device_pin
{
    struct rt_device parent; /* 派生于rt_device */
    const struct rt_pin_ops *ops; /* 设备特有的操作接口,还可以根据需要增加其他成员 */
};

如:

/* 使用时,需要把设备名称、操作接口等,传入 */
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
{
    _hw_pin.parent.type         = RT_Device_Class_Miscellaneous; /* 设备类型,为了区分设备种类 */
    _hw_pin.parent.rx_indicate  = RT_NULL; /* 接收回调,串口、CAN一般会有 */
    _hw_pin.parent.tx_complete  = RT_NULL; /* 发送回调,串口、CAN一般会有 */

#ifdef RT_USING_DEVICE_OPS
    _hw_pin.parent.ops          = &pin_ops;
#else
    _hw_pin.parent.init         = RT_NULL; /* 以下标准的rt_device设备操作接口,根据需要实现 */
    _hw_pin.parent.open         = RT_NULL;
    _hw_pin.parent.close        = RT_NULL;
    _hw_pin.parent.read         = _pin_read;
    _hw_pin.parent.write        = _pin_write;
    _hw_pin.parent.control      = _pin_control;
#endif

    _hw_pin.ops                 = ops;  /* 操作接口,设备的特有操作接口 */
    _hw_pin.parent.user_data    = user_data; /* 不是必要的用户数据 */

    /* register a character device */
    rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);  /* 设备注册接口:注册为具体设备 */

    return 0;
}
/* 具体设备的OPS 实现 */
const static struct rt_pin_ops _stm32_pin_ops =
{
    stm32_pin_mode,
    stm32_pin_write,
    stm32_pin_read,
    stm32_pin_attach_irq,
    stm32_pin_dettach_irq,
    stm32_pin_irq_enable,
};

/* 实际设备的注册方法 */
rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);

其他

关于如何进行RT-Thread中设备模型rt_device的理解问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 如何使用RT-Thread的FinSH对硬件进行编程
  2. Linux平台总线驱动设备模型是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

rt-thread

上一篇:RT-Thread中的内核对象操作API怎么理解

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》