• Linux学习第36天:Linux RTC 驱动实验:时间是一条流淌的河


    Linux版本号4.1.15   芯片I.MX6ULL                                    大叔学Linux    品人间百味  思文短情长 


            RTC就是实时时钟。

            本笔记主要学习Linux RTC驱动试验,主要内容包括Linux内核RTC驱动简介、I.MX6U内部RTC分析、RTC时间查看与设置。因为Linux内核已经由NXP写了驱动,所以本节的重点内容只是对其进行的分析。思维导图如下:

            

    一、Linux内核RTC驱动简介

            Linux 内核将 RTC 设备抽象为 rtc_device 结构体,因此 RTC 设备驱动就是申请并初始化
    rtc_device,最后将 rtc_device 注册到 Linux 内核里面,这样 Linux 内核就有一个 RTC 设备的。

            RTC设备的操作是用一个操作集合(结构体)来表示的。

            ops成员变量是一个rtc_class_ops类型的指针变量。rtc_class_ops为RTC设备最底层操作函数集合,包括从RTC设备中读取时间、向RTC设备写入新的时间值等。

            rtc_dev.c文件提供了所有RTC设备共用的file_operation函数操作集。

            应用程序可以通过ioctl函数来设置/读取时间、设置/读取闹钟的操作。

            rtc_dev_ioctl最终会通过操作rtc_class_ops中的read_time、set_time等函数来对具体RTC设备的读写操作。

            rec_read_time会调用_rtc_read_time函数。

            _rtc_read_time会调用rtc_class_ops中的read_time来从RTC设备中获取当前时间。

            Linux内核中RTC驱动调用流程:

            rtc_device_register会申请一个rtc_device并初始化这个rec_device,最后向调用者返回这个rtc_device。

    1. struct rtc_device *rtc_device_register(const char *name,//设备名
    2. struct device *dev,//设备
    3. const struct rtc_class_ops *ops,//RTC底层驱动函数集
    4. struct module *owner)//RTC拥有者
    5. //注册成功的话返回rtc_device,失败的话会返回一个负值。

            rtc_device_unregister注销注册的rtc_device.

    void rtc_device_unregister(struct rtc_device *rtc)// rtc是要注销的rtc_device

    二、I.MX6U内部RTC驱动分析

            RTC驱动NXP已经给写好了。

            分析驱动从设备树入手,找到设备节点snvs_rtc:

    1. 1 snvs_rtc: snvs-rtc-lp {
    2. 2 compatible = "fsl,sec-v4.0-mon-rtc-lp";
    3. 3 regmap = <&snvs>;
    4. 4 offset = <0x34>;
    5. 5 interrupts = 19 IRQ_TYPE_LEVEL_HIGH>, 20
    6. IRQ_TYPE_LEVEL_HIGH>;
    7. 6 };

            对应驱动文件为drivers/rtc/rtc-snvs.c:

    1. 380 static const struct of_device_id snvs_dt_ids[] = {
    2. 381 { .compatible = "fsl,sec-v4.0-mon-rtc-lp", },// imx6ull.dtsi 中的 snvs_rtc 设备节点会和此 驱动匹配。
    3. 382 { /* sentinel */ }
    4. 383 };
    5. 384 MODULE_DEVICE_TABLE(of, snvs_dt_ids);
    6. 385
    7. 386 static struct platform_driver snvs_rtc_driver = {//标准的 platform 驱动框架,当设备和驱动匹配成功以后 snvs_rtc_probe 函
    8. 数就会执行。
    9. 387 .driver = {
    10. 388 .name = "snvs_rtc",
    11. 389 .pm = SNVS_RTC_PM_OPS,
    12. 390 .of_match_table = snvs_dt_ids,
    13. 391 },
    14. 392 .probe = snvs_rtc_probe,
    15. 393 };
    16. 394 module_platform_driver(snvs_rtc_driver);
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

            调用 platform_get_resource 函数从设备树中获取到 RTC 外设寄存器基地址。

    mmio = devm_ioremap_resource(&pdev->dev, res);

            调用函数 devm_ioremap_resource 完成内存映射,得到 RTC 外设寄存器物理基
    地址对应的虚拟地址。

    1. data->regmap = devm_regmap_init_mmio(&pdev->dev, mmio,
    2. &snvs_rtc_config);

            Linux3.1 引入了一个全新的 regmap 机制, regmap 用于提供一套方便的 API 函
    数去操作底层硬件寄存器,以提高代码的可重用性。 snvs-rtc.c 文件会采用 regmap 机制来读写
    RTC 底层硬件寄存器。这里使用 devm_regmap_init_mmio 函数将 RTC 的硬件寄存器转化为
    regmap 形式,这样 regmap 机制的 regmap_write、 regmap_read 等 API 函数才能操作寄存器。

    data->irq = platform_get_irq(pdev, 0);

            从设备树中获取 RTC 的中断号。

    1. /* Initialize glitch detect */
    2. regmap_write(data->regmap, data->offset + SNVS_LPPGDR,
    3. SNVS_LPPGDR_INIT);

            设置 RTC_ LPPGDR 寄存器值为 SNVS_LPPGDR_INIT= 0x41736166,这里就是
    用的 regmap 机制的 regmap_write 函数完成对寄存器进行写操作。

    1. /* Clear interrupt status */
    2. regmap_write(data->regmap, data->offset + SNVS_LPSR,
    3. 0xffffffff);

            设置 RTC_LPSR 寄存器,写入 0xffffffff, LPSR 是 RTC 状态寄存器,写 1 清零,因此这一步就是清除 LPSR 寄存器。

    1. /* Enable RTC */
    2. snvs_rtc_enable(data, true);

            调用 snvs_rtc_enable 函数使能 RTC,此函数会设置 RTC_LPCR 寄存器。

    1. ret = devm_request_irq(&pdev->dev, data->irq,
    2. snvs_rtc_irq_handler,
    3. IRQF_SHARED, "rtc alarm", &pdev->dev);

            调用 devm_request_irq函数请求 RTC中断,中断服务函数为 snvs_rtc_irq_handler,用于 RTC 闹钟中断。

    1. data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
    2. &snvs_rtc_ops, THIS_MODULE);

            调用 devm_rtc_device_register 函数向系统注册 rtc_devcie, RTC 底层驱动集为snvs_rtc_ops。snvs_rtc_ops操作集包含了读取/设置 RTC时间,读取/设置闹钟等函数。

    1. 126 static int snvs_rtc_read_time(struct device *dev,
    2. struct rtc_time *tm)
    3. 127 {
    4. 128 struct snvs_rtc_data *data = dev_get_drvdata(dev);
    5. 129 unsigned long time = rtc_read_lp_counter(data);
    6. 130
    7. 131 rtc_time_to_tm(time, tm);
    8. 132
    9. 133 return 0;
    10. 134 }

    调用 rtc_read_lp_counter 获取 RTC 计数值,这个时间值是秒数。

    调用 rtc_time_to_tm 函数将获取到的秒数转换为时间值,也就是 rtc_time 结构体类型.

    rtc_read_lp_counter 函数,此函数用于读取 RTC 计数值:

    1. 50 static u32 rtc_read_lp_counter(struct snvs_rtc_data *data)
    2. 51 {
    3. 52 u64 read1, read2;
    4. 53 u32 val;
    5. 54
    6. 55 do {
    7. 56 regmap_read(data->regmap, data->offset + SNVS_LPSRTCMR,
    8. &val);
    9. 57 read1 = val;
    10. 58 read1 <<= 32;
    11. 59 regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR,
    12. &val);
    13. 60 read1 |= val;
    14. 61
    15. 62 regmap_read(data->regmap, data->offset + SNVS_LPSRTCMR,
    16. &val);
    17. 63 read2 = val;
    18. 64 read2 <<= 32;
    19. 65 regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR,
    20. &val);
    21. 66 read2 |= val;
    22. 67 /*
    23. 68 * when CPU/BUS are running at low speed, there is chance that
    24. 69 * we never get same value during two consecutive read, so here
    25. 70 * we only compare the second value.
    26. 71 */
    27. 72 } while ((read1 >> CNTR_TO_SECS_SH) != (read2 >>
    28. CNTR_TO_SECS_SH));
    29. 73
    30. 74 /* Convert 47-bit counter to 32-bit raw second count */
    31. 75 return (u32) (read1 >> CNTR_TO_SECS_SH);
    32. 76 }

            第 56~72 行,读取 RTC_LPSRTCMR 和 RTC_LPSRTCLR 这两个寄存器,得到 RTC 的计数值,单位为秒,这个秒数就是当前时间。这里读取了两次 RTC 计数值,因为要读取两个寄存器,因此可能存在读取第二个寄存器的时候时间数据更新了,导致时间不匹配,因此这里连续读两
    次,如果两次的时间值相等那么就表示时间数据有效。
            第 75 行,返回时间值,注意这里将前面读取到的 RTC 计数值右移了 15 位。

    三、RTC时间查看与设置

    1.时间RTC查看

    如果要查看时间的话输入“ date”命令即可。

    2.设置RTC时间

            现在我要设置当前时间为 2019 年 8 月 31 日 18:13:00,因此输入如下命令:
    date -s "2019-08-31 18:13:00"

            将当前的时间写入到 RTC 里面,这里要用到 hwclock 命令,输入如下命令将系统时间写入到 RTC里面:
    hwclock -w //将当前系统时间写入到 RTC 里面
            时间写入到 RTC 里面以后就不怕系统重启以后时间丢失了,如果 I.MX6U-ALPHA 开发板
    底板接了纽扣电池,那么开发板即使断电了时间也不会丢失。

    四、总结

            本笔记主要学习Linux RTC驱动试验,主要内容包括Linux内核RTC驱动简介、I.MX6U内部RTC分析、RTC时间查看与设置。


    本文为参考正点原子开发板配套教程整理而得,仅用于学习交流使用,不得用于商业用途。

  • 相关阅读:
    js — 原生轮播图的制作
    【Docker】实现JMeter分布式压测
    二叉树练习
    腾讯云服务器CVM_云主机_云计算服务器_弹性云服务器
    从零开始学Spring Boot系列-前言
    SendKeys.SendWait 函数模拟键盘输入
    HR人才测评,提高员工和岗位的适配度
    java计算机毕业设计重工教师职称管理系统源码+mysql数据库+系统+lw文档+部署
    AJAX之概述
    002_Anaconda的安装与使用
  • 原文地址:https://blog.csdn.net/jiage987450/article/details/134267804