rtt的io设备框架面向对象学习-脉冲编码器设备

本文详细介绍了脉冲编码器设备基类及其子类,从设备驱动层到设备IO管理层的构造流程,以STM32为例,包括初始化方法、驱动框架接口和应用程序使用步骤。
摘要由CSDN通过智能技术生成

目录

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

1.脉冲编码器设备基类

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

在/ components / drivers / include / drivers 下的pulse_encoder.h定义了如下脉冲编码器设备基类

struct rt_pulse_encoder_device
{
struct rt_device parent;
const struct rt_pulse_encoder_ops *ops;
enum rt_pulse_encoder_type type;
};

脉冲编码器设备基类的方法定义如下

struct rt_pulse_encoder_ops
{
rt_err_t (*init)(struct rt_pulse_encoder_device *pulse_encoder);
rt_int32_t (*get_count)(struct rt_pulse_encoder_device *pulse_encoder);
rt_err_t (*clear_count)(struct rt_pulse_encoder_device *pulse_encoder);
rt_err_t (*control)(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args);
};

抽象出来共性成为脉冲编码器设备基类的方法。
在这里插入图片描述

2.脉冲编码器设备基类的子类

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

3.初始化/构造流程

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

3.1设备驱动层

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

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

定义了stm32脉冲编码器类
struct stm32_pulse_encoder_device
{
struct rt_pulse_encoder_device pulse_encoder;
TIM_HandleTypeDef tim_handler;
IRQn_Type encoder_irqn;
rt_int32_t over_under_flowcount; char *name;
};

实例化了stm32的脉冲编码器设备:
static struct stm32_pulse_encoder_device stm32_pulse_encoder_obj[] ;

重写了脉冲编码器设备基类的方法:
static const struct rt_pulse_encoder_ops _ops =
{
.init = pulse_encoder_init,
.get_count = pulse_encoder_get_count,
.clear_count = pulse_encoder_clear_count,
.control = pulse_encoder_control,
};

hw_pulse_encoder_init中开启stm32脉冲编码器设备的初始化:
调用/ components / drivers / misc /pulse_encoder.c的rt_device_pulse_encoder_register函数来初始化脉冲编码器设备基类对象:
stm32_pulse_encoder_obj[i].pulse_encoder.ops = &_ops;
rt_device_pulse_encoder_register(&stm32_pulse_encoder_obj[i].pulse_encoder, stm32_pulse_encoder_obj[i].name, RT_NULL) ;

注意,重写了脉冲编码器设备基类的方法。
请添加图片描述

3.2 设备驱动框架层

/ components / drivers / misc 下的pulse_encoder.c实现了设备驱动框架层接口rt_device_pulse_encoder_register,它是脉冲编码器设备驱动框架层的入口,开启脉宽编码器设备基类的构造流程。

它主要重写了脉冲编码设备基类的父类——设备基类——的方法:
#ifdef RT_USING_DEVICE_OPS
device->ops = &pulse_encoder_ops;
#else
device->init = rt_pulse_encoder_init;
device->open = rt_pulse_encoder_open;
device->close = rt_pulse_encoder_close;
device->read = rt_pulse_encoder_read;
device->write = RT_NULL;
device->control = rt_pulse_encoder_control;
#endif
在这里插入图片描述

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

3.3 设备io管理层

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

详细参见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、付费专栏及课程。

余额充值