参考
- https://forum.armbian.com/topic/4074-real-time-clock-ds3231/
- https://learn.adafruit.com/adding-a-real-time-clock-to-beaglebone-black/set-rtc-time
- https://blog.csdn.net/nathan_csdn/article/details/116232481
环境
主板: OrangePi PC
系统: Armbian
使用镜像: Armbian_23.02.2_Orangepipc_jammy_current_5.15.93.img
一、OrangePi开启i2c功能
参考官方手册: OrangePi_PC_H3_用户手册_v3.2.pdf
在/boot/armbianEnv.txt
的overlays
字段添加i2c0 i2c1
, 再重启即可。
我的配置实例:
verbosity=7
bootlogo=true
console=serial
disp_mode=1280x800p60
overlay_prefix=sun8i-h3
overlays=uart2 i2c0 i2c1
rootdev=UUID=705b28eb-7ba5-481e-ac44-cb93d2cfd612
rootfstype=ext4
usbstoragequirks=0x2537:0x1066:u,0x2537:0x1068:u
二、连接DS3231M
这里,我把DS3231M连接到OrangePi的i2c1
。
然后安装i2c-tools
sudo apt-get install i2c-tools
执行命令 sudo i2cdetect -y 1
查看到有地址0x68
即是连接成功。
这里的1
是指i2c1
,如果使用i2c0
,请使用命令sudo i2cdetect -y 0
。
air@orangepipc:~$ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
三、启用本地RTC
使用命令timedatectl status
查看当前配置。
现在我们执行以下命令设置本地RTC:
timedatectl set-local-rtc 1
set-local-rtc
是:Control whether RTC is in local time, 默认是关闭的,我们把它打开
set-ntp
是:Enable or disable network time synchronization(是否开启网络时间同步)默认是打开的,
若set-ntp
是关闭的,可以用timedatectl set-ntp 1
打开它。
重启OrangePi
再执行: dmesg | grep rtc
, 将获得以下结果
air@orangepipc:~$ dmesg | grep rtc
[ 2.895745] sun6i-rtc 1f00000.rtc: registered as rtc0
[ 2.900855] sun6i-rtc 1f00000.rtc: setting system clock to 1970-01-01T00:00:16 UTC (16)
[ 2.909004] sun6i-rtc 1f00000.rtc: RTC enabled
执行:ls -l /dev/rtc*
, 得到以下结果
air@orangepipc:~$ ls -l /dev/rtc*
lrwxrwxrwx 1 root root 4 Apr 22 15:51 /dev/rtc -> rtc0
crw------- 1 root root 253, 0 Apr 22 15:51 /dev/rtc0
说明RTC并没有使用我们的DS3232M。
四、配置DS3232M为硬件时钟
执行: (0x68
是DS3231M的I2C地址, i2c-1
是S3231M连接OrangePi主板的I2C接口)
su root
echo 'ds3231 0x68' | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
执行 ls -l /dev/rtc*
能看到: /dev/rtc1
出现了
root@orangepipc:/home/air# ls -l /dev/rtc*
lrwxrwxrwx 1 root root 4 Apr 22 15:51 /dev/rtc -> rtc0
crw------- 1 root root 253, 0 Apr 22 15:51 /dev/rtc0
crw------- 1 root root 253, 1 Apr 22 15:56 /dev/rtc1
使用命令: dmesg | grep rtc
, 将获得以下结果(关注最后2行)
root@orangepipc:/home/air# dmesg | grep rtc
[ 2.898913] sun6i-rtc 1f00000.rtc: registered as rtc0
[ 2.904025] sun6i-rtc 1f00000.rtc: setting system clock to 2023-04-22T17:17:06 UTC (1682183826)
[ 2.912871] sun6i-rtc 1f00000.rtc: RTC enabled
[ 475.421290] rtc-ds1307 1-0068: registered as rtc1
执行以下命令:(把当前正确的时间写入DS3231M, 然后再读出来测试下)
su root
hwclock -w -f /dev/rtc1
hwclock -r -f /dev/rtc1
应该能有正确的时间输出:
2023-04-22 15:59:35.204101+08:00
我们执行 sudo i2cdetect -y 1
, 能看到地址0x68
变成了UU
, 表明通过i2c1
, DS3231M已经被系统使用。
root@orangepipc:/home/air# sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
重启
重启完成后,使用命令: dmesg | grep rtc
, 将获得以下结果
air@orangepipc:~$ dmesg | grep rtc
[ 2.898913] sun6i-rtc 1f00000.rtc: registered as rtc0
[ 2.904025] sun6i-rtc 1f00000.rtc: setting system clock to 2023-04-22T17:17:06 UTC (1682183826)
[ 2.912871] sun6i-rtc 1f00000.rtc: RTC enabled
没错,又变回去了,我们得执行一次下面指令才能把DM3221的时间同步过来(注意参数变成-s
了, 不是-r
或-w
了!):
su root
echo 'ds3231 0x68' | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s -f /dev/rtc1
那么,解决方法一:
在 /etc/rc.local
的 exit 0
的上一行新增以下2行,达到开机启动的目的。
echo 'ds3231 0x68' | tee /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s -f /dev/rtc1
解决方法二(推荐):
执行命令:
su root
apt-get install -y ntpdate
mkdir /usr/share/rtc_ds3231
创建并编辑: vim /usr/share/rtc_ds3231/clock_init.sh
#!/bin/bash
sleep 18
echo 'ds3231 0x68' | tee /sys/class/i2c-adapter/i2c-1/new_device
sleep 3
hwclock -s -f /dev/rtc1
hwclock -w -f /dev/rtc1
ntpdate 0.asia.pool.ntp.org
hwclock -w -f /dev/rtc1
创建并编辑: vim /lib/systemd/system/rtc-ds3231.service
[Unit]
Description=DS3231 RTC Service
[Service]
Type=simple
WorkingDirectory=/usr/share/rtc_ds3231
ExecStart=/bin/bash clock_init.sh
SyslogIdentifier=rtc_ds3231
[Install]
WantedBy=multi-user.target
执行命令
systemctl enable rtc-ds3231.service
systemctl stop rtc-ds3231.service
systemctl start rtc-ds3231.service
注意hwclock
命令都要带上 -f /dev/rtc1
, 不然就是默认使用自带的硬件时钟/dev/rtc0
,
当然,系统自带的断电就不会继续计时了,所以时间才会不准确,除非连上网络。
完结!
####################<几个命令的help>####################
air@orangepipc:~$ date
2023年 04月 22日 星期六 14:46:24 CST
air@orangepipc:~$ hwclock --help
Usage:
hwclock [function] [option...]
Time clocks utility.
Functions:
-r, --show display the RTC time
--get display drift corrected RTC time
--set set the RTC according to --date
-s, --hctosys set the system time from the RTC
-w, --systohc set the RTC from the system time
--systz send timescale configurations to the kernel
-a, --adjust adjust the RTC to account for systematic drift
--predict predict the drifted RTC time according to --date
Options:
-u, --utc the RTC timescale is UTC
-l, --localtime the RTC timescale is Local
-f, --rtc <file> use an alternate file to /dev/rtc0
--directisa use the ISA bus instead of /dev/rtc0 access
--date <time> date/time input for --set and --predict
--delay <sec> delay used when set new RTC time
--update-drift update the RTC drift factor
--noadjfile do not use /etc/adjtime
--adjfile <file> use an alternate file to /etc/adjtime
--test dry run; implies --verbose
-v, --verbose display more details
-h, --help display this help
-V, --version display version
For more details see hwclock(8).
air@orangepipc:~$ timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
Commands:
status Show current time settings
show Show properties of systemd-timedated
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization
systemd-timesyncd Commands:
timesync-status Show status of systemd-timesyncd
show-timesync Show properties of systemd-timesyncd
Options:
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
--monitor Monitor status of systemd-timesyncd
-p --property=NAME Show only properties by this name
-a --all Show all properties, including empty ones
--value When showing properties, only print the value
See the timedatectl(1) man page for details.