I have an mcp79310 RTC chip installed on gpio pins 22 & 23, and I am attempting to use it with dtoverlay=i2c-rtc-gpio,mcp7941x,i2c_gpio_sda=22,i2c_gpio_scl=23
in /boot/config.txt
and adding rtc-mcp7941x
to /etc/modules
.
After rebooting, upon checking with $ timedatectl
I get this result:
Failed to query server: Failed to read RTC: Invalid argument
Running $ i2cdetect -y 11
shows:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- UU
70: -- -- -- -- -- -- -- --
So nothing seems to be wrong so far. Checking $ dmesg|grep rtc
shows:
[ 4.299290] i2c-gpio i2c-gpio-rtc@0: using lines 22 (SDA) and 23 (SCL)
[ 5.733573] rtc-ds1307 11-006f: registered as rtc0
With this failure to read the RTC, I removed the dtoverlay for it and added the i2c-gpio
dtoverlay in /boot/config.txt:
dtoverlay=i2c-gpio,i2c_gpio_sda=22,i2c_gpio_scl=23
After rebooting, I tested the RTC chip directly using i2cget & i2cset (per this article - https://tom.meinlschmidt.org/2014/02/19/mcp7940-rtc-with-pi/), and it works perfectly.
After further experimentation, if I start up the Raspi with the dtoverlay=i2c-gpio
enabled, but dtoverlay=i2c-rtc-gpio
disabled, then disable the first dtoverlay and re-enable the second, and then reboot, the RTC works perfectly when queried by timedatectl
.
In other words, if I booting, starting the RTC chip without tying it to the rtc dtoverlay, then reboot with the rtc dtoverlay it works. If I remove the RTC battery and power down, then on power-up I have to go through the same two-step boot process to get it working again.
Any ideas on how to fix this problem?
UPDATE: The problem seems to resolve after about 30 secs from the finish of boot up. I believe it may happen when timedatectl syncs its system clock (I am using chrony for NTP). If I try setting it up manually after startup:
# echo mcp7941x 0x6f > /sys/class/i2c-adapter/i2c-11/new_device
It starts working right away after I call hwclock -r
. If I put the same code into /etc/rc.local
it still takes about 30 secs before the RTC works.
Here's a schematic: