为OpenWrt添加实时钟(RTC)

实时钟,英文名RTC(Real Time Clock),在PC里面很常见,在OpenWrt里面却鲜有提及,手头上有一个DS1307的TinyRTC实时钟模块,经过一番折腾,将其融合到了OpenWrt系统,遂将操作过程记录成文.

背景知识

DS1307在最新的OpenWrt中已经提供支持,却没有整合进ramips中,因此,本文的一个重点便是如何为ramips系统配置ds1307.另外,为ds1307编写合适的dts节点也是本文的一个重点.

实现过程 1.为ramips配置ds1307支持

系统在./scripts/medatata.pl中判断并处理RTC_SUPPORT开关,分析之后,原来是在 target/linux/ramips/mt7620/target.mk中,将

原始的内容:

FEATURES+=usb

修改为:

FEATURES+=usb rtc

即可打开mt7620对rtc的支持.

此时,make kernel_menuconfig进入配置菜单.同时应注意,由于ds1307是i2c接口的模块,因此,在device中需要配置i2c的支持.选中i2c之后,便可以在

Device Drivers -> Real Time Clock中看到ds1307

.config - Linux/mips 3.14.28 Kernel Configuration > Device Drivers > Real Time Clock ───────────────────────────────────── ┌──── Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025 ────┐ │ CONFIG_RTC_DRV_DS1307: │ │ │ │ If you say yes here you get support for various compatible RTC │ │ chips (often with battery backup) connected with I2C. This driver │ │ should handle DS1307, DS1337, DS1338, DS1339, DS1340, ST M41T00, │ │ EPSON RX-8025 and probably other chips. In some cases the RTC │ │ must already have been initialized (by manufacturing or a │ │ bootloader). │ │ │ │ The first seven registers on these chips hold an RTC, and other │ │ registers may add features such as NVRAM, a trickle charger for │ │ the RTC/NVRAM backup power, and alarms. NVRAM is visible in │ │ sysfs, but other chip features may not be available. │ │ │ │ This driver can also be built as a module. If so, the module │ │ will be called rtc-ds1307. │ │ │ │ Symbol: RTC_DRV_DS1307 [=y] │ │ Type : tristate │ │ Prompt: Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025 │ │ Location: │ │ -> Device Drivers │ │ -> Real Time Clock (RTC_CLASS [=y]) │ │ Defined at drivers/rtc/Kconfig:166 │ │ Depends on: RTC_CLASS [=y] && I2C [=y] │ │ │ │ │ │ │ ├───────────────────────────────────────────────────────────(100%)──┤ │ < Exit > │ └───────────────────────────────────────────────────────────────────┘ 2.为ds1307建立dts节点

配置好ds1307的编译开关后,接下来的工作就要在dts里面添加ds1307的设备节点.查阅ds1307的资料,其配置的i2c地址为0x68,因此,dts中可以添加如下内容:

i2c@0 { compatible = "i2c-gpio"; gpios = <&gpio2 0 0 /* sda = wan_led*/ &gpio3 0 0 /* scl = wlan_led*/ >; i2c-gpio,delay-us = <10>; /* ~20 kHz */ #address-cells = <1>; #size-cells = <0>; rtc@68 { compatible = "dallas,ds1307"; reg = <0x68>; }; };

完成这两步后,就可以make出支持ds1307的固件.

系统启动时,可以在TTL中看到如下内容:

[ 0.130000] i2c-gpio i2c.4: using pins 40 (SDA) and 72 (SCL) ... [ 0.790000] rtc-ds1307 0-0068: rtc core: registered ds1307 as rtc0 [ 0.810000] rtc-ds1307 0-0068: 56 bytes nvram ... [ 0.810000] rtc-ds1307 0-0068: setting system clock to 2015-01-26 22:31:15 UTC (1422311475)


说明RTC已经成功的整合到了mt7620的系统中.

ps:系统自带的hwclock命令可以很方便的对RTC进行相应的操作.

后记

kernel 3.14.28中的i2c-ralink驱动已经能够正常驱动7620的硬件i2c,只是在用i2cdetect的时候,需要加上-r参数,这样才能正确probe到设备.

OpenWrt:Ubuntu 12.04搭建OpenWrt编译环境

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/b4341339f525974d33ed33337c0dbb0a.html