Ubuntu 23.10,Raspberry Pi 5-如何添加 RTC?

Ubuntu 23.10,Raspberry Pi 5-如何添加 RTC?

虽然前几代 RPi 没有板载实时时钟,但 RPi 5 确实包含一个。但是,它没有电池备份,因此如果 R Pi 断电,时间就会丢失。我需要一个带有电池备份的 RTC。我开发了一个 HAT,其中包括一个 PCF85063AT RTC 模块和电池。现在,我正在尝试弄清楚如何让 Ubuntu 使用它。

i2cdetectRTC 模块确实使用地址 0x68正确显示。

我尝试遵循本指南:https://dev.to/dev_neil_a/setup-an-rtc-module-with-a-raspberry-pi-4-and-ubuntu-server-2004-8e6#configure-the-rtc-module-in-ubuntu,但它已经过时了,并且按照这些步骤对我来说不起作用。

这是我当前的脚本:

#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status
# Steps modified from https://dev.to/dev_neil_a/setup-an-rtc-module-with-a-raspberry-pi-4-and-ubuntu-server-2004-8e6#configure-the-rtc-module-in-ubuntu
# Install utilities that include hwclock
apt install util-linux-extra -y
# Register the RTC module
echo pcf85063 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
# Disable linesi n /lib/udev/hwclock-set
if ! grep --quiet "# if [ -e /run/systemd/system ] ; then" /lib/udev/hwclock-set; then
cp /lib/udev/hwclock-set /lib/udev/hwclock-set.original
sed -i '/if \[ -e \/run\/systemd\/system \] ; then/,/fi/s/^/# /' /lib/udev/hwclock-set
sed -i 's|/sbin/hwclock --rtc=\$dev --systz --badyear|# /sbin/hwclock --rtc=$dev --systz --badyear|' /lib/udev/hwclock-set
sed -i 's|/sbin/hwclock --rtc=\$dev --hctosys|# &|' /lib/udev/hwclock-set
fi
# Ensures the RTC module is correctly loaded at boot time
if ! grep --quiet "dtoverlay=i2c-rtc,pcf85063,addr=0x68" /boot/firmware/config.txt; then
echo dtoverlay=i2c-rtc,pcf85063,addr=0x68 >> /boot/firmware/config.txt
fi
# Ensures the RTC module is correctly loaded at boot time
if ! grep --quiet "rtc-pcf85063" /etc/modules; then
echo rtc-pcf85063 >> /etc/modules
fi

运行这些命令并重新启动后,我看到的内容如下:

root@GATEWAY-0x0000001E:~# cat /proc/modules | grep rtc
rtc_pcf85063 24576 0 - Live 0xffffa000717a4000
regmap_i2c 12288 1 rtc_pcf85063, Live 0xffffa0007179a000
rtc_rpi 12288 1 - Live 0xffffa000710c4000
root@GATEWAY-0x0000001E:~# lsmod | grep rtc
rtc_pcf85063 24576 0
regmap_i2c 12288 1 rtc_pcf85063
rtc_rpi 12288 1
root@GATEWAY-0x0000001E:~# ll /dev/rt*
lrwxrwxrwx 1 root root 4 Apr 4 18:59 /dev/rtc -> rtc0
crw------- 1 root root 251, 0 Apr 4 18:59 /dev/rtc0
root@GATEWAY-0x0000001E:~# hwclock --rtc=/dev/rtc0 --show
1970-01-01 01:20:09.284950+00:00
root@GATEWAY-0x0000001E:~# date
Thu Apr 4 19:53:41 UTC 2024
root@GATEWAY-0x0000001E:~# hwclock --rtc=/dev/rtc1 --show
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --verbose option to see the details of our search for an access method.
root@GATEWAY-0x0000001E:~# hwclock --rtc=/dev/rtc1 --show --verbose
hwclock from util-linux 2.39.1
System Time: 1712260435.095222
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.
root@GATEWAY-0x0000001E:~# i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- 42 -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

我认为 /dev/rtc0 是板载 RTC。我的 RTC 是否未正确注册?

i2cdetect仍然将地址显示为68而不是UU。如果我再次运行echo pcf85063 0x68 > /sys/class/i2c-adapter/i2c-1/new_device,则我得到UU。似乎这个“new_device”在重启后不是持久的?

任何想法都值得感激。谢谢!

编辑: dmesg全新重启后:

root@GATEWAY-0x0000001E:~# dmesg | grep -i rtc
[    0.000000] Kernel command line: coherent_pool=1M 8250.nr_uarts=1 pci=pcie_bus_safe snd_bcm2835.enable_compat_alsa=0 snd_bcm2835.enable_hdmi=1 bcm2708_fb.fbwidth=640 bcm2708_fb.fbheight=480 bcm2708_fb.fbdepth=16 bcm2708_fb.fbswap=1 smsc95xx.macaddr=D8:3A:DD:AB:BE:42 vc_mem.mem_base=0x3fc00000 vc_mem.mem_size=0x40000000  console=ttyAMA10,115200 multipath=off dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable rootfstype=ext4 rootwait fixrtc
[    0.000000] Unknown kernel command line parameters "fixrtc multipath=off", will be passed to user space.
[    1.807444]     fixrtc
[    2.101483] rpi-rtc soc:rpi_rtc: registered as rtc0
[    2.148523] rpi-rtc soc:rpi_rtc: setting system clock to 1970-01-01T01:35:11 UTC (5711)

再次运行后echo pcf85063 0x68 > /sys/class/i2c-adapter/i2c-1/new_device,我得到:

root@GATEWAY-0x0000001E:~# hwclock --rtc=/dev/rtc1 --show --verbose
hwclock from util-linux 2.39.1
System Time: 1712257289.018692
Using the rtc interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
ioctl(4, RTC_UIE_ON, 0): Invalid argument
Waiting in loop for time from /dev/rtc1 to change
hwclock: ioctl(RTC_RD_NAME) to /dev/rtc1 to read the time failed: Invalid argument
...synchronization failed

dmesg 中还有以下几行:

[  102.669335] rtc-pcf85063 1-0068: registered as rtc1
[  102.669362] i2c i2c-1: new_device: Instantiated device pcf85063 at 0x68

相关内容