![Pi--Zero2详情页--1GB_09.jpg Pi--Zero2详情页--1GB_09.jpg](https://static.assets-stash.eet-china.com/forum/202103/10/161124n1jtfnwjptfktkzm.jpg)
zero2开发板在Linux下如需开启自动同步时间,目前是通过网络实现的,因为板子默认没有RTC。如果需要不联网实现时间同步,需要外接RTC模块。下面以香橙派DS1307 RTC 时钟模块为例,说明下时钟模块的连接使用方法:
1) 香橙派 DS1307 RTC 时钟模块如下图所示,使用 i2c 接口和开发板通信,i2c 设备 地址为 0x68。RTC 模块默认不配电池,使用前需要准备一块纽扣电池
![图1.png 图1.png](https://static.assets-stash.eet-china.com/forum/202103/10/161138n9oa1xd9yd92xvzy.png)
2) 首先将 RTC 模块接到开发板的 26pin 上,接线方式如下所示
![图2.png 图2.png](https://static.assets-stash.eet-china.com/forum/202103/10/161151rwufjfm2muf2uffu.png)
3) 接好 RTC 模块后,先用 i2cdetect 命令查看下是否能检测到 RTC 模块的设备地址
![图3.png 图3.png](https://static.assets-stash.eet-china.com/forum/202103/10/161220hlalabrrvq7kayqa.png)
4) RTC 模块使用的 ds1307 型号的芯片,首先需要确认 RTC_DRV_DS1307 内核配 置已经打开
![图4.png 图4.png](https://static.assets-stash.eet-china.com/forum/202103/10/161232b3f4yy12j1jhknqs.png)
5) 确认内核配置 OK 口,还需要在 dts 中添加 rtc-ds1307 模块的配置。最新版本的 linux 系统中预装了一个名为 orangepi-add-overlay 的脚本,通过这个脚本我们可以 使用 DT overlay 来动态的添加某些 dts 中没有的功能。首先编写 rtc-ds1307 模块的 dts 文件,内容如下所示
![图5.png 图5.png](https://static.assets-stash.eet-china.com/forum/202103/10/161247cceifp5epdpc7wpd.png)
a. 然后使用 orangepi-add-overlay 将 i2c-ds1307.dts 编译成 i2c-ds1307.dtbo,并 且设置好相关的启动变量
![图5-a.png 图5-a.png](https://static.assets-stash.eet-china.com/forum/202103/10/161259qza0u6706nw766i6.png)
b. i2c-ds1307.dtbo 会被复制到/boot/overlay-user 中,运行完 orangepi-add-overlay 后可以查看下/boot/overlay-user 中是否有 i2c-ds1307.dtbo 这个文件
![图5-b.png 图5-b.png](https://static.assets-stash.eet-china.com/forum/202103/10/161315qjci1qzqxzjk8ud6.png)
c. orangepi-add-overlay 还会在/boot/orangepiEnv 中添加 user_overlays 变量, 并设置值为 i2c-ssd1307
![图5-c.png 图5-c.png](https://static.assets-stash.eet-china.com/forum/202103/10/161327i221m8j2z2i4ne68.png)
d. 然后重启 linux 系统,启动时,在 u-boot 的 log 中可以看到 DT overlay 相关的输出
![图5-d.png 图5-d.png](https://static.assets-stash.eet-china.com/forum/202103/10/161339qcok3cu36maa3cfm.png)
6) 重启后,从 dmesg 输出的 log 中可看到 ds1307 模块的加载信息,ds1307 对应的设备节点为 rtc0
![图6.png 图6.png](https://static.assets-stash.eet-china.com/forum/202103/10/161351wzdbxxzzvvnyvx1f.png)
7) linux 系统启动时,如果开发板连接了网络,linux 系统会通过网络自动同步系统 时间为正确的时间,linux 系统默认时间为世界标准时间 UTC,在中国,需要将时区 修改为 Asia/Shanghai,使用 data 命令获取到的时间才正确,方法如下
a. 执行下面的命令
![图7-a.png 图7-a.png](https://static.assets-stash.eet-china.com/forum/202103/10/161404m8ki0m5mrt8oguc0.png)
b. 然后选择地理区域为 Asia
![图7-b.png 图7-b.png](https://static.assets-stash.eet-china.com/forum/202103/10/161422maj5c0tjl2tdctrk.png)
c. 再选择时区为 Shanghai
![图7-c.png 图7-c.png](https://static.assets-stash.eet-china.com/forum/202103/10/161436vourg090u0cwctg9.png)
d. 配置完后再使用 date 命令查看时间就会正常了
![图7-d.png 图7-d.png](https://static.assets-stash.eet-china.com/forum/202103/10/161447glt0smijcleazzve.png)
8) 如果系统当前时间不正确,首先请连接网络,然后使用下面的命令同步时间,这 里之所以先要将系统时间设置正确,是为了后面同步 RTC 模块的时间做准备
![图8.png 图8.png](https://static.assets-stash.eet-china.com/forum/202103/10/161459gt1bxz4iitix9xl9.png)
9) 查看 RTC 模块当前时间的命令如下所示
![图9.png 图9.png](https://static.assets-stash.eet-china.com/forum/202103/10/161510dj0ibebajz34b60o.png)
10) 第一次使用 RTC 模块读取到的时间肯定是不对的,可以通过下面的命令将系统 当前的时间同步到 RTC 模块,同步前,需要保证系统当前的时间是正确的
![图10.png 图10.png](https://static.assets-stash.eet-china.com/forum/202103/10/161522lyhhbd5nl3swya5d.png)
11) 此时就可以断开开发板所有的网络连接,然后等待几分钟,再重启系统,然后 查看系统时间就会发现即使没有网络,系统的时间也是正确的