树莓派 -- i2c学习 续(1) DeviceTree Overlay实例化rtc


上文中讨论了通过sysfs来实例化i2c设备 (rtc ds3231)
https://blog.csdn.net/feiwatson/article/details/81048616

本文继续看看如何通过 DeviceTree Overlays来实例化rtc ds3231

write dts

首先,写一个dts

/dts-v1/;
/plugin/;
/ {
       compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";

       fragment@0 {
          target = <&i2c1>;
          __overlay__ {
                #address-cells = <1>;
                #size-cells = <0>;
                rtc@68 {
                        compatible = "maxim,ds3231";
                        reg = <0x68>;
                        #address-cells = <2>;
                        #size-cells = <1>;
                };      
          };
       };
    };

注:
1. compatible = “maxim,ds3231”, 源码 drivers\rtc\rtc-ds1307.c中定义
2. i2c1在arch\arm\boot\dts\bcm283x.dtsi中定义

保存为文件rasp-rtc.dts

compile

copy dts到树莓派中并编译,

dtc -I dts -O dtb -o /boot/overlays/rasp-rtc.dtbo rasp-rtc.dts

生成的.dtbo到/boot/overlays

update /boot/config.txt

首先要确认i2c是使能的。如果没有使能,通过raspi-config命令来使能。
使能后在config.txt可以看到

dtparam=i2c_arm=on

增加下面内容至config.txt

# Uncomment this to enable the rtc ds3231 
dtoverlay=rasp-rtc

重启

reboot后,查看/dev可以看到rtc

pi@raspberrypi:/dev $ ls -la | grep rtc
lrwxrwxrwx  1 root root           4 Jul 16 13:49 rtc -> rtc0
crw-------  1 root root    253,   0 Jul 16 13:49 rtc0

测试

pi@raspberrypi:/dev $ sudo hwclock --debug
hwclock from util-linux 2.29.2
Using the /dev interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2018/07/16 14:13:13
Hw clock time : 2018/07/16 14:13:13 = 1531750393 seconds since 1969
Time since last adjustment is 1531750393 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2018-07-16 14:13:12.176793+0000

rtc driver Code

rtc-ds1307.c中定义compatible

#ifdef CONFIG_OF
static const struct of_device_id ds1307_of_match[] = {
    {
        .compatible = "dallas,ds1307",
        .data = (void *)ds_1307
    },
    {
        .compatible = "dallas,ds1308",
        .data = (void *)ds_1308
    },
    {
        .compatible = "dallas,ds1337",
        .data = (void *)ds_1337
    },
    {
        .compatible = "dallas,ds1338",
        .data = (void *)ds_1338
    },
    {
        .compatible = "dallas,ds1339",
        .data = (void *)ds_1339
    },
    {
        .compatible = "dallas,ds1388",
        .data = (void *)ds_1388
    },
    {
        .compatible = "dallas,ds1340",
        .data = (void *)ds_1340
    },
    {
        .compatible = "dallas,ds1341",
        .data = (void *)ds_1341
    },
    {
        .compatible = "maxim,ds3231",
        .data = (void *)ds_3231
    },
    {
        .compatible = "st,m41t0",
        .data = (void *)m41t00
    },
    {
        .compatible = "st,m41t00",
        .data = (void *)m41t00
    },
    {
        .compatible = "microchip,mcp7940x",
        .data = (void *)mcp794xx
    },
    {
        .compatible = "microchip,mcp7941x",
        .data = (void *)mcp794xx
    },
    {
        .compatible = "pericom,pt7c4338",
        .data = (void *)ds_1307
    },
    {
        .compatible = "epson,rx8025",
        .data = (void *)rx_8025
    },
    {
        .compatible = "isil,isl12057",
        .data = (void *)ds_1337
    },
    { }
};
MODULE_DEVICE_TABLE(of, ds1307_of_match);
#endif

转载于:https://www.cnblogs.com/feiwatson/p/9478205.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值