树莓派


上文中讨论了通过sysfs来实例化i2c设备 (rtc ds3231)

本文继续看看如何通过 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[] = {{patible = "dallas,ds1307",.data = (void *)ds_1307},{patible = "dallas,ds1308",.data = (void *)ds_1308},{patible = "dallas,ds1337",.data = (void *)ds_1337},{patible = "dallas,ds1338",.data = (void *)ds_1338},{patible = "dallas,ds1339",.data = (void *)ds_1339},{patible = "dallas,ds1388",.data = (void *)ds_1388},{patible = "dallas,ds1340",.data = (void *)ds_1340},{patible = "dallas,ds1341",.data = (void *)ds_1341},{patible = "maxim,ds3231",.data = (void *)ds_3231},{patible = "st,m41t0",.data = (void *)m41t00},{patible = "st,m41t00",.data = (void *)m41t00},{patible = "microchip,mcp7940x",.data = (void *)mcp794xx},{patible = "microchip,mcp7941x",.data = (void *)mcp794xx},{patible = "pericom,pt7c4338",.data = (void *)ds_1307},{patible = "epson,rx8025",.data = (void *)rx_8025},{patible = "isil,isl12057",.data = (void *)ds_1337},{ }
};
MODULE_DEVICE_TABLE(of, ds1307_of_match);
#endif

转载于:.html