OpenWRT添加RTC时钟模块(DS1307)

目的:自动运行有保障,不受断电困扰。
机器:GL-INET v1
有关该路由器的详细参数:https://revspace.nl/GL-iNet
OpenWRT版本:Barrier Breaker
TinyRTC模块,DS1307
需要四个脚:SDA(18),SCL(19),+3.3V,GND
=============两个思路(本文用第二种)===========
I.编译源码,将kmod-rtc-ds1307模块直接编入内核并生成ipk包;
(可能会碰到内核兼容问题,适合量产)
II.将rtc-ds1307.ko内核模块放入/lib/modules/3*/内,调用i2c工具挂载RTC设备。(碰到内核兼容问题的几率小,适合个人)
III.直接找kmod-rtc-ds1307的包,附后(可能会碰到内核兼容问题,成功安装过一次)
================准备工作===================
1.增加内核对RTC模块的支持
kernel_menuconfig
–>Device Drivers
–>Real Time Clock
+Set the RTC time based on NTP synchronization
+/sys/class/rtc/rtcN (sysfs)
+/proc/driver/rtc (procfs for rtcN)
+/dev/rtcN (character devices)
+Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025
*tips:
-Set system time from RTC on startup and resume
不启用此功能,为了避免以下报错:
[ 2.960000] drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
启动顺序很靠前,无奈还不能一步到位。
2.增加i2c设备支持
menuconfig
–>kernel support
–>I2C support
+kmod-i2c-core
+algo-bit,i2c-gpio;
+kmod-i2c-gpio-custom
3.检测工具
menuconfig
–>Utilities
+dmesg 查看内核日志
+hwclock 时钟管理
+i2c-tools i2c工具集,检测、读取i2c设备数据
================转折================
查查看,menuconfig菜单中有无以下选项:
–>kernel modules–>other modules
kmod-rtc-ds1307
tips:有你就直接选中吧,万事大吉,直接编译。
============如果没有,继续看============
1.选好其他自己想要的功能后,先编译;
2.完成后,可以得到rtc-ds1307.ko(关键);
路径:/openwrt/barrier_breaker/build_dir/target-mips_34kc_uClibc-0.9.33.2/linux-ar71xx_generic/linux-3.10.49/drivers/rtc
感谢其他开发者的努力,我才能顺利获悉此路径:
https://dev.openwrt.org/changeset/42791
tips:如果你想获知如何创建kernel modules packages,可继续参阅如下网站:
https://downloads.openwrt.org/kamikaze/docs/openwrt.html#x1-470002.1.3
3.机器刷好固件,将rtc-ds1307.ko文件放入/lib/modules/3*/目录下;
【OpenWRT添加RTC时钟模块(DS1307)】4.载入i2c模块insmod i2c-dev
5.载入设备:insmod i2c-gpio-custom bus0=0,18,19
18指代GPIO18,SDA接口;
19指代GPIO19,SCL接口;
*应根据实际连接情况对指令做出相应的调整,
insmod i2c-gpio-custom bus0=0,SDA,SCL
6.检查系统是否已经探测到DS1307硬件:i2cdetect -y 0
正常情况:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – – – – – – – – – – – – –
10: – – – – – – – – – – – – – – – –
20: – – – – – – – – – – – – – – – –
30: – – – – – – – – – – – – – – – –
40: – – – – – – – – – – – – – – – –
50: 50 – – – – – – – – – – – – – – –
60: – – – – – – – – 68 – – – – – – –
70: – – – – – – – –
DS1307 i2c设备地址:0x68
7.注册新设备
echo ds1307 0x68 > ‘/sys/class/i2c-dev/i2c-0/device/new_device’
8.如果一切正常,可以使用hwclock -r 读取到rtc模块硬件时间,也可以在/dev目录下看到rtc0;
9.同步浏览器时间,用hwclock -w 将时间写入rtc硬件中;
10.添加开机启动脚本,用于开机自动注册i2c设备并将系统时间修正为硬件时间,内容如下:
vi /etc/rc.local

# Put your custom commands here that should be executed once # the system init finished. By default this file does nothing. # sign the rtc device insmod i2c-dev insmod i2c-gpio-custom bus0=0,18,19 echo ds1307 0x68 > '/sys/class/i2c-dev/i2c-0/device/new_device' hwclock -sexit 0

重启
END
========================================
kmod-rtc-ds1307内核包
http://openwrt.jaru.eu.org/barrier_breaker/ar71xx/packages/
不知是谁,但我暂时还不能做出来。
========================================
经过近2个月的测试,
1.模块稳定性高;
2.ds1307模块走得快,宜联网后定时使用hwclock -w 进行校正。
参考:
http://blog.oldcomputerjunk.net/tag/embedded/
Using an i2c RTC with the Carambola2 (or any OpenWRT modified router)
https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi?view=all
adding-a-real-time-clock-to-raspberry-pi
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
configuring-i2c
http://blog.csdn.net/manfeel/article/details/43165589
为OpenWrt添加实时钟(RTC)–以MT7620为例
https://dev.openwrt.org/changeset/42791
kernel: add rtc-ds1307 module support
http://kernelreloaded.blog385.com/index.php/archives/adding-rtc-to-embedded-devices-running-openwrt/
虽只有一半,但很有用。

    推荐阅读