目录
- 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.应用程序使用流程
参见官方文档。