rtt的io设备框架面向对象学习-dac设备

目录

        • 1.dac设备基类
        • 2.dac设备基类的子类
        • 3.初始化/构造流程
          • 3.1设备驱动层
          • 3.2 设备驱动框架层
          • 3.3 设备io管理层
        • 4.总结
        • 5.内部调用流程
        • 6.应用程序使用流程

1.dac设备基类

此层处于设备驱动框架层。也是抽象类。

在/ components / drivers / include / drivers 下的dac.h定义了如下dac设备基类
struct rt_dac_device
{
struct rt_device parent;
const struct rt_dac_ops *ops;
};

dac设备基类的方法定义如下
struct rt_dac_ops
{
rt_err_t (*disabled)(struct rt_dac_device *device, rt_uint32_t channel);
rt_err_t (*enabled)(struct rt_dac_device *device, rt_uint32_t channel);
rt_err_t (*convert)(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value);
rt_uint8_t (*get_resolution)(struct rt_dac_device *device);
};

抽象出来dac设备的共性成为dac设备基类的方法。
共性:失能dac,使能dac,转换,分辨率。
在这里插入图片描述

2.dac设备基类的子类

各个dac设备基类的子类已经是在bsp的驱动层来实现了,例如
/ bsp / stm32 / libraries / HAL_Drivers / drivers 下的drv_dac.c定义的stm32 dac类,这些都是可以实例化的终类。其他芯片厂家如此这般一样。

3.初始化/构造流程

以stm32为例,从设备驱动层、设备驱动框架层到设备io管理层从下到上的构造/初始化流程如下

3.1设备驱动层

此层是bsp层,可以实例化的终类地。

c文件:
/ bsp / stm32 / libraries / HAL_Drivers / drivers 下的drv_dac.c。

定义了stm32的dac类
struct stm32_dac
{
DAC_HandleTypeDef DAC_Handler;
struct rt_dac_device stm32_dac_device;
};
总感觉不舒服,和rtt设备io框架类继承机制不一致,应该改成这样
struct stm32_dac
{
struct rt_dac_device stm32_dac_device;
DAC_HandleTypeDef DAC_Handler;
};
这就是舒服许多了。

实例化了stm32的dac设备:
static struct stm32_dac stm32_dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];

重写了dac设备基类的方法:
static const struct rt_dac_ops stm_dac_ops =
{
.disabled = stm32_dac_disabled,
.enabled = stm32_dac_enabled,
.convert = stm32_set_dac_value,
.get_resolution = stm32_dac_get_resolution,
};
请添加图片描述

stm32_dac_init中开启stm32的dac设备的初始化:
调用/ components / drivers / misc /dac.c的rt_hw_dac_register函数来初始化adc设备基类对象: rt_hw_dac_register(&stm32_dac_obj[i].stm32_dac_device, name_buf, &stm_dac_ops, &stm32_dac_obj[i].DAC_Handler)
注意把重写的dac设备基类方法传递进去了。

3.2 设备驱动框架层

rt_hw_dac_register是dac设备驱动框架层的入口,开启dac设备基类的构造/初始化流程。
其主要是重写设备基类对象的方法,如下

/ components / drivers / misc 下的dac.c实现了设备驱动框架层接口。
重写dac设备基类的父类设备基类的方法如下
#ifdef RT_USING_DEVICE_OPS
device->parent.ops = &dac_ops;
#else
device->parent.init = RT_NULL;
device->parent.open = RT_NULL;
device->parent.close = RT_NULL;
device->parent.read = RT_NULL;
device->parent.write = _dac_write;
device->parent.control = _dac_control;
#endif

同时,重写dac设备基类的方法。
device->ops = ops;
在这里插入图片描述

并最终调用设备基类的初始化/构造函数rt_device_register。

3.3 设备io管理层

在/ components / drivers / core 下的device.c中实现了rt_device_register,它是io管理层的入口。
它将stm32 dac设备对象放到对象容器里管理。

详细参见io设备管理层。https://blog.csdn.net/yhb1206/article/details/136440373

4.总结

参见rtt的oopc实现特点

5.内部调用流程

参见内部调用流程

6.应用程序使用流程

参见官方文档等路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值