rtt的io设备框架面向对象学习-硬件定时器设备

本文详细介绍了STM32硬件定时器设备类的定义、构造函数以及它们在rtt框架下的工作原理,包括设备驱动层、STM32_hwtimer类、基类rt_hwtimer_device和基类rt_device的交互过程。
摘要由CSDN通过智能技术生成

目录

    • 1.硬件定时器设备类对象图全貌
    • 2.stm32硬件定时器设备类及其构造函数
    • 3 硬件定时器设备基类及其构造函数
    • 4 设备基类及其构造函数
    • 5.总结
    • 6.内部调用流程
    • 7.应用程序使用流程

1.硬件定时器设备类对象图全貌

请添加图片描述
设备驱动层的硬件定时器类实现类,是需要各个BSP实现的。其继承自设备驱动框架层的硬件定时器设备基类。一般设备驱动框架层及以上的类都是rtt官方写好的抽象类

上图采用c++的纯虚函数来表示需要子类实现的父类方法——或者说是父类对子类的约束,当然在rtt的oopc中是通过函数指针实现的。

可以看到设备驱动层是stm32硬件定时器或者别的芯片厂家的硬件定时器类的所在。
rtt的oopc中每类都有对应的构造函数(初始化函数)。

以对象图中stm32硬件定时器设备类为起点分析其类定义和构造函数。

2.stm32硬件定时器设备类及其构造函数

如第1节对象图,在/ bsp / stm32 / libraries / HAL_Drivers / drivers /drv_tim.c中定义了stm32硬件定时器设备类及其构造函数,如下

类定义:

struct stm32_hwtimer
{
    rt_hwtimer_t time_device;
    TIM_HandleTypeDef    tim_handle;
    IRQn_Type tim_irqn;
    char *name;
};

类实例化

static struct stm32_hwtimer stm32_hwtimer_obj[] =

其构造函数

static int stm32_hwtimer_init(void)
{
    int i = 0;
    int result = RT_EOK;

    for (i = 0; i < sizeof(stm32_hwtimer_obj) / sizeof(stm32_hwtimer_obj[0]); i++)
    {
        stm32_hwtimer_obj[i].time_device.info = &_info;
        stm32_hwtimer_obj[i].time_device.ops  = &_ops;//重写父类方法
        if (rt_device_hwtimer_register(&stm32_hwtimer_obj[i].time_device,
            stm32_hwtimer_obj[i].name, &stm32_hwtimer_obj[i].tim_handle) == RT_EOK)
        {
            LOG_D("%s register success", stm32_hwtimer_obj[i].name);
        }
        else
        {
            LOG_E("%s register failed", stm32_hwtimer_obj[i].name);
            result = -RT_ERROR;
        }
    }

    return result;
}
INIT_BOARD_EXPORT(stm32_hwtimer_init);

可以看到,stm32_hwtimer_init是stm32硬件定时器设备类的构造函数,它所干的事,主要是重写父类的方法,如下定义

static const struct rt_hwtimer_ops _ops =
{
    .init = timer_init,
    .start = timer_start,
    .stop = timer_stop,
    .count_get = timer_counter_get,
    .control = timer_ctrl,
};

然后调用其父类——硬件定时器设备基类——的构造函数rt_device_hwtimer_register完成接下来的构造流程。

3 硬件定时器设备基类及其构造函数

在/ components / drivers / include / drivers /hwtimer.h中定义了硬件定时器设备基类:

typedef struct rt_hwtimer_device
{
    struct rt_device parent;
    const struct rt_hwtimer_ops *ops;
    const struct rt_hwtimer_info *info;

    rt_int32_t freq;                /* counting frequency set by the user */
    rt_int32_t overflow;            /* timer overflows */
    float period_sec;
    rt_int32_t cycles;              /* how many times will generate a timeout event after overflow */
    rt_int32_t reload;              /* reload cycles(using in period mode) */
    rt_hwtimer_mode_t mode;         /* timing mode(oneshot/period) */
} rt_hwtimer_t;

其中需要子类实现的方法定义如下

struct rt_hwtimer_ops
{   
     void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);    
     rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);    
     void (*stop)(struct rt_hwtimer_device *timer);    
     rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);    
     rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
 };

可以看到第一个形参是本类的指针——这就模拟了面向对象的this指针

在/ components / drivers /hwtimer/hwtimer.c中实现了该类的构造函数rt_device_hwtimer_register,如下:

rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data)
{
    struct rt_device *device;

    RT_ASSERT(timer != RT_NULL);
    RT_ASSERT(timer->ops != RT_NULL);
    RT_ASSERT(timer->info != RT_NULL);

    device = &(timer->parent);

    device->type        = RT_Device_Class_Timer;
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;

#ifdef RT_USING_DEVICE_OPS
    device->ops         = &hwtimer_ops;
#else
    device->init        = rt_hwtimer_init;
    device->open        = rt_hwtimer_open;
    device->close       = rt_hwtimer_close;
    device->read        = rt_hwtimer_read;
    device->write       = rt_hwtimer_write;
    device->control     = rt_hwtimer_control;
#endif
    device->user_data   = user_data;

    return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}

同样的套路,该类的构造函数主要是重写其父类(设备基类)的方法,硬件定时器的父类方法rtt写好了,

device->init        = rt_hwtimer_init;
device->open        = rt_hwtimer_open; 
device->close       = rt_hwtimer_close; 
device->read        = rt_hwtimer_read;   
device->write       = rt_hwtimer_write;

其都是被父类管理接口调用所以其函数的第一个形参都是父类的指针——模拟面向对象的this指针

然后调用其父类——
设备基类——的构造函rt_device_register来继续接下来的构造流程。

4 设备基类及其构造函数

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

5.总结

参见rtt的oopc实现特点

6.内部调用流程

参见内部调用流程

7.应用程序使用流程

参见官方文档等路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值