Discussion:
Activate a Real Time Clock chip on I2C in Raspberry Pi 4B with Debian (not Raspbian)
(too old to reply)
Rick Thomas
2023-04-16 03:10:01 UTC
Permalink
I've got a Raspberry Pi 4B (4GB) with a DS3231 RTC module. I can make the combo work with Ubuntu and RaspberryPI-OS. I'd like to try it with the plain-vanilla Debian from <https://raspi.debian.net/tested-images/> but I can't find instructions for activating the I2C and RTC hardware in that OS.

Any suggestions? Thanks!

Rick
Diederik de Haas
2023-04-16 08:10:01 UTC
Permalink
Post by Rick Thomas
I've got a Raspberry Pi 4B (4GB) with a DS3231 RTC module. I can make the
combo work with Ubuntu and RaspberryPI-OS. I'd like to try it with the
plain-vanilla Debian from <https://raspi.debian.net/tested-images/> but I
can't find instructions for activating the I2C and RTC hardware in that OS.
Documentation/devicetree/bindings/rtc/rtc-ds1307.yaml in the kernel source
describes how one could use it ('compatible = "maxim,ds3231"' for DS3231).
I think you should make a DeviceTree Overlay, but I don't know how to do that.
Georg Gast
2023-04-16 09:30:01 UTC
Permalink
Am Sun, 16 Apr 2023 10:01:08 +0200
Post by Diederik de Haas
Post by Rick Thomas
I've got a Raspberry Pi 4B (4GB) with a DS3231 RTC module. I can
make the combo work with Ubuntu and RaspberryPI-OS. I'd like to
try it with the plain-vanilla Debian from
<https://raspi.debian.net/tested-images/> but I can't find
instructions for activating the I2C and RTC hardware in that OS.
Documentation/devicetree/bindings/rtc/rtc-ds1307.yaml in the kernel
source describes how one could use it ('compatible = "maxim,ds3231"'
for DS3231). I think you should make a DeviceTree Overlay, but I
don't know how to do that.
I got this working with that systemd unit on a Rpi3 but should also
work on a RPi4. Maybe you need to adjust the modprobe sequence. I guess
the 2708 is 2835 on the Pi4.

cat /etc/systemd/system/hwclock-pi.service
[Unit]
Description=HWClock at the I2C Bus of the Raspberry pi
Before=basic.target
Conflicts=shutdown.target
DefaultDependencies=no

[Service]
Type=oneshot
ExecStart=/bin/sh -c "modprobe i2c_bcm2835 && modprobe i2c_bcm2708 &&
modprobe rtc_ds1307 && sleep 1 && echo ds1307 0x68 >
/sys/class/i2c-adapter/i2c-1/new_device && /sbin/hwclock -s"
ExecStop=/sbin/hwclock -w RemainAfterExit=yes

[Install]
WantedBy=basic.target
Diederik de Haas
2023-04-16 12:20:01 UTC
Permalink
Post by Georg Gast
ExecStart=/bin/sh -c "modprobe i2c_bcm2835 && modprobe i2c_bcm2708 &&
modprobe rtc_ds1307 && sleep 1 && echo ds1307 0x68 >
/sys/class/i2c-adapter/i2c-1/new_device && /sbin/hwclock -s"
Nice :-)
I wonder whether this (essentially) is what a DTO does.
I'm pretty sure that based on the 'compatible' the appropriate kernel module
gets loaded (rtc_ds1307 in this case) and what you described above, seems
reasonable to be the rest of what it does/triggers.
Rick Thomas
2023-04-17 02:10:03 UTC
Permalink
Thanks for your response, Georg!

Is there somewhere that these modules are documented that I can read-up for a more detailed understanding?

As an experiment, I tried running the following (It didn't work, but maybe you can suggest a modification that will. I'm probably not fully understanding something!):
modprobe i2c_bcm2835 &&
modprobe rtc_ds1307 &&
sleep 1 &&
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device &&
/sbin/hwclock -r
This is what I got when I tried it:
hwclock: Cannot access the Hardware Clock via any known method.

and when I tried:
i2cdetect -y 1
I got"
Error: Could not open file `/dev/i2c-1' or `/dev/i2c/1': No such file or directory

I found this in journalctl :
Apr 16 18:23:33 pi kernel: rtc-ds1307: probe of 1-0068 failed with error -121
Apr 16 18:23:33 pi kernel: i2c i2c-1: new_device: Instantiated device ds1307 at 0x68

Is rtc_ds1307 the wrong module for my DS3231 rtc hardware?

Thanks!
Rick

=================================================================
Post by Georg Gast
Am Sun, 16 Apr 2023 10:01:08 +0200
Post by Diederik de Haas
Post by Rick Thomas
I've got a Raspberry Pi 4B (4GB) with a DS3231 RTC module. I can
make the combo work with Ubuntu and RaspberryPI-OS. I'd like to
try it with the plain-vanilla Debian from
<https://raspi.debian.net/tested-images/> but I can't find
instructions for activating the I2C and RTC hardware in that OS.
Documentation/devicetree/bindings/rtc/rtc-ds1307.yaml in the kernel
source describes how one could use it ('compatible = "maxim,ds3231"'
for DS3231). I think you should make a DeviceTree Overlay, but I
don't know how to do that.
I got this working with that systemd unit on a Rpi3 but should also
work on a RPi4. Maybe you need to adjust the modprobe sequence. I guess
the 2708 is 2835 on the Pi4.
cat /etc/systemd/system/hwclock-pi.service
[Unit]
Description=HWClock at the I2C Bus of the Raspberry pi
Before=basic.target
Conflicts=shutdown.target
DefaultDependencies=no
[Service]
Type=oneshot
ExecStart=/bin/sh -c "modprobe i2c_bcm2835 && modprobe i2c_bcm2708 &&
modprobe rtc_ds1307 && sleep 1 && echo ds1307 0x68 >
/sys/class/i2c-adapter/i2c-1/new_device && /sbin/hwclock -s"
ExecStop=/sbin/hwclock -w RemainAfterExit=yes
[Install]
WantedBy=basic.target
Reco
2023-04-17 08:40:01 UTC
Permalink
Hi.
Post by Rick Thomas
Apr 16 18:23:33 pi kernel: rtc-ds1307: probe of 1-0068 failed with error -121
Apr 16 18:23:33 pi kernel: i2c i2c-1: new_device: Instantiated device ds1307 at 0x68
Is rtc_ds1307 the wrong module for my DS3231 rtc hardware?
No, because ds1307 kernel module should work with DS3231.
But the address of the device (0x68) on I2C bus could be different,
or the device is actually connected to another I2C bus (i.e. - not 1st).

Consider running i2cdetect to clarify things.

Reco
Rick Thomas
2023-04-17 10:10:01 UTC
Permalink
Sadly, when I do:
i2cdetect -l
I get nothing back. Leading me to conclude that there are no busses available.

FWIW, when I do:
/sbin/hwclock -vr
I get:
hwclock from util-linux 2.36.1
System Time: 1681724077.614748
Trying to open: /dev/rtc0
Trying to open: /dev/rtc
Trying to open: /dev/misc/rtc
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.

Also FWIW, when I do:
ls -l /dev/i2c*
I get:
ls: cannot access '/dev/i2c*': No such file or directory

Further confirming that there are no available busses.

Is it possible that I need to modprobe a different (or additional) module than "i2c_bcm2835"?

Thanks!
Rick

PS: I'm still wondering if there is some kind of documentation of these modules and what they do that would give me a bit of background for this project.
Post by Reco
Hi.
Post by Rick Thomas
Apr 16 18:23:33 pi kernel: rtc-ds1307: probe of 1-0068 failed with error -121
Apr 16 18:23:33 pi kernel: i2c i2c-1: new_device: Instantiated device ds1307 at 0x68
Is rtc_ds1307 the wrong module for my DS3231 rtc hardware?
No, because ds1307 kernel module should work with DS3231.
But the address of the device (0x68) on I2C bus could be different,
or the device is actually connected to another I2C bus (i.e. - not 1st).
Consider running i2cdetect to clarify things.
Reco
Reco
2023-04-17 10:40:01 UTC
Permalink
Hi.
Post by Rick Thomas
i2cdetect -l
I get nothing back. Leading me to conclude that there are no busses available.
"modprobe i2c-dev" should fix that.

Reco
Rick Thomas
2023-04-17 11:50:01 UTC
Permalink
That helps some...

***@pi:~# i2cdetect -l
i2c-3 i2c bcm2835 (***@7e804000) I2C adapter
i2c-1 i2c Broadcom STB : I2C adapter
i2c-2 i2c bcm2835 (***@7e205000) I2C adapter
i2c-0 i2c Broadcom STB : I2C adapter

and:
***@pi:~# i2cdetect -y 3
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: -- -- -- -- -- -- -- --

All other busses (0 1 and 2 as arguments to i2cdetect give no devices)

So, if I change "i2c-1" to "i2c-3" in the "echo > /sys/class ..." I find that the hwclock command now works!

Now all I have to do is get all that into a systemd service file so it is done at boot time.

It's late now. I'll try that tomorrow!

Thanks very much to all who contributed!
Rick

PS: Anybody who can explain why it has to be bus 3 -- and how I could have predicted that -- will get a hundred Internet guru-points!
Post by Reco
Hi.
Post by Rick Thomas
i2cdetect -l
I get nothing back. Leading me to conclude that there are no busses available.
"modprobe i2c-dev" should fix that.
Reco
Paul Wise
2023-04-18 03:00:02 UTC
Permalink
Post by Reco
    i2cdetect -l
I get nothing back.  Leading me to conclude that there are no busses available.
"modprobe i2c-dev" should fix that.
This seems like a usability issue, so I have sent a patch that
adds a bunch of error messages in situations of silent failure:

https://lore.kernel.org/linux-i2c/20230418023248.250685-1-***@bonedaddy.net/T/
--
bye,
pabs

https://wiki.debian.org/PaulWise
Rick Thomas
2023-04-18 05:40:01 UTC
Permalink
Post by Paul Wise
Post by Reco
    i2cdetect -l
I get nothing back.  Leading me to conclude that there are no busses available.
"modprobe i2c-dev" should fix that.
This seems like a usability issue, so I have sent a patch that
That would have been great while I was trying to track down the solution to this problem.
What would have been even greater is if the error messages include references to some documentation that explained the problem and gave some clues to the larger context in which the problem arose.

I'm still hoping that someone here can point me to documentation that can give me answers to questions like these: For example, I'd like to know why it chose bus3 rather than one of the other busses (0,1,2,4)? I'd also like to know what the various modules do and what kinds of parameters there are to influence the detailed behavior? And where is the documentation for the magic writing into /sys ?

Big Thanks! to all who have contributed to this discussion.
Rick
Paul Wise
2023-04-19 00:00:02 UTC
Permalink
Post by Rick Thomas
What would have been even greater is if the error messages include
references to some documentation that explained the problem and gave
some clues to the larger context in which the problem arose.
The messages I sent that patch adding are for very simple issues;
"/proc not mounted", "/sys not mounted" and "i2c-dev not loaded" and
since they all have very simple solutions, I just added the commands
to fix them instead of pointers to documentation about each error.

Probably the i2c manual pages should get some links to the Linux kernel
documentation and wiki for I2C. I'm unlikely to work on that though,
hopefully someone else is willing to do that.
Post by Rick Thomas
For example, I'd like to know why it chose bus3 rather than one of
the other busses (0,1,2,4)?
I expect that depends on the bus driver code for your device,
those do not seem to document their ordering though and it
looks like your particular i2c bus driver has no documentation.

https://www.kernel.org/doc/html/latest/i2c/busses/index.html
Post by Rick Thomas
I'd also like to know what the various modules do and what kinds of
parameters there are to influence the detailed behavior?
Looks like the devices are defined by DeviceTree, ACPI, board files,
dynamically by drivers for other devices, hardware probing or sysfs:

https://www.kernel.org/doc/html/latest/i2c/instantiating-devices.html
Post by Rick Thomas
And where is the documentation for the magic writing into /sys ?
Seems to be here:

https://www.kernel.org/doc/html/latest/i2c/instantiating-devices.html#method-4-instantiate-from-user-space
https://www.kernel.org/doc/html/latest/i2c/i2c-sysfs.html

These may also be useful:

https://www.kernel.org/doc/html/latest/i2c/
https://archive.kernel.org/oldwiki/i2c.wiki.kernel.org/

PS: I am subscribed, no need to CC me, please respect Reply-To.
--
bye,
pabs

https://wiki.debian.org/PaulWise
Rick Thomas
2023-04-19 06:30:01 UTC
Permalink
Thanks very much, Pabs! I will follow up on those links and hopefully get a better understanding of what is going on and why. Perhaps the next time someone has a question like mine, I'll be able to be as helpful to them as the folks here have been to me!

Rick
Post by Paul Wise
Post by Rick Thomas
What would have been even greater is if the error messages include
references to some documentation that explained the problem and gave
some clues to the larger context in which the problem arose.
The messages I sent that patch adding are for very simple issues;
"/proc not mounted", "/sys not mounted" and "i2c-dev not loaded" and
since they all have very simple solutions, I just added the commands
to fix them instead of pointers to documentation about each error.
Probably the i2c manual pages should get some links to the Linux kernel
documentation and wiki for I2C. I'm unlikely to work on that though,
hopefully someone else is willing to do that.
Post by Rick Thomas
For example, I'd like to know why it chose bus3 rather than one of
the other busses (0,1,2,4)?
I expect that depends on the bus driver code for your device,
those do not seem to document their ordering though and it
looks like your particular i2c bus driver has no documentation.
https://www.kernel.org/doc/html/latest/i2c/busses/index.html
Post by Rick Thomas
I'd also like to know what the various modules do and what kinds of
parameters there are to influence the detailed behavior?
Looks like the devices are defined by DeviceTree, ACPI, board files,
https://www.kernel.org/doc/html/latest/i2c/instantiating-devices.html
Post by Rick Thomas
And where is the documentation for the magic writing into /sys ?
https://www.kernel.org/doc/html/latest/i2c/instantiating-devices.html#method-4-instantiate-from-user-space
https://www.kernel.org/doc/html/latest/i2c/i2c-sysfs.html
https://www.kernel.org/doc/html/latest/i2c/
https://archive.kernel.org/oldwiki/i2c.wiki.kernel.org/
PS: I am subscribed, no need to CC me, please respect Reply-To.
--
bye,
pabs
https://wiki.debian.org/PaulWise
* signature.asc
Rick Thomas
2023-05-12 09:00:01 UTC
Permalink
Turns out that the PiHut has a tutorial webpage for just this problem!

https://thepihut.com/blogs/raspberry-pi-tutorials/17209332-adding-a-real-time-clock-to-your-raspberry-pi

Thanks for all the help!
Rick
Post by Rick Thomas
Thanks very much, Pabs! I will follow up on those links and hopefully
get a better understanding of what is going on and why. Perhaps the
next time someone has a question like mine, I'll be able to be as
helpful to them as the folks here have been to me!
Rick
Post by Paul Wise
Post by Rick Thomas
What would have been even greater is if the error messages include
references to some documentation that explained the problem and gave
some clues to the larger context in which the problem arose.
The messages I sent that patch adding are for very simple issues;
"/proc not mounted", "/sys not mounted" and "i2c-dev not loaded" and
since they all have very simple solutions, I just added the commands
to fix them instead of pointers to documentation about each error.
Probably the i2c manual pages should get some links to the Linux kernel
documentation and wiki for I2C. I'm unlikely to work on that though,
hopefully someone else is willing to do that.
Post by Rick Thomas
For example, I'd like to know why it chose bus3 rather than one of
the other busses (0,1,2,4)?
I expect that depends on the bus driver code for your device,
those do not seem to document their ordering though and it
looks like your particular i2c bus driver has no documentation.
https://www.kernel.org/doc/html/latest/i2c/busses/index.html
Post by Rick Thomas
I'd also like to know what the various modules do and what kinds of
parameters there are to influence the detailed behavior?
Looks like the devices are defined by DeviceTree, ACPI, board files,
https://www.kernel.org/doc/html/latest/i2c/instantiating-devices.html
Post by Rick Thomas
And where is the documentation for the magic writing into /sys ?
https://www.kernel.org/doc/html/latest/i2c/instantiating-devices.html#method-4-instantiate-from-user-space
https://www.kernel.org/doc/html/latest/i2c/i2c-sysfs.html
https://www.kernel.org/doc/html/latest/i2c/
https://archive.kernel.org/oldwiki/i2c.wiki.kernel.org/
PS: I am subscribed, no need to CC me, please respect Reply-To.
--
bye,
pabs
https://wiki.debian.org/PaulWise
* signature.asc
Paul Wise
2023-04-18 00:00:01 UTC
Permalink
Post by Georg Gast
ExecStart=/bin/sh -c "modprobe i2c_bcm2835 && modprobe i2c_bcm2708 &&
modprobe rtc_ds1307 && sleep 1 && echo ds1307 0x68 >
/sys/class/i2c-adapter/i2c-1/new_device && /sbin/hwclock -s"
I feel like there should be a better way to do this than manually doing
/sys changes? Is there no i2c command for wrapping /sys i2c changes?
--
bye,
pabs

https://wiki.debian.org/PaulWise
Georg Gast
2023-04-18 08:30:01 UTC
Permalink
I searched for other ways but this was the only way to do it from userspace without device tree changes ... The online device tree overlays don't work without kernel support and a DT that got compiled with the "-@" option. That is not the case with stock Debian kernels. In my opinion, dtcs are nowadays more like firmware as they change more or less often between kernel versions. In the original meaning they were thought to stay the same even among different os like Linux and FreeBSD.

Bye

Georg
Post by Paul Wise
Post by Georg Gast
ExecStart=/bin/sh -c "modprobe i2c_bcm2835 && modprobe i2c_bcm2708 &&
modprobe rtc_ds1307 && sleep 1 && echo ds1307 0x68 >
/sys/class/i2c-adapter/i2c-1/new_device && /sbin/hwclock -s"
I feel like there should be a better way to do this than manually doing
/sys changes? Is there no i2c command for wrapping /sys i2c changes?
--
bye,
pabs
https://wiki.debian.org/PaulWise
gene heskett
2023-04-16 11:20:01 UTC
Permalink
Post by Diederik de Haas
Post by Rick Thomas
I've got a Raspberry Pi 4B (4GB) with a DS3231 RTC module. I can make the
combo work with Ubuntu and RaspberryPI-OS. I'd like to try it with the
plain-vanilla Debian from <https://raspi.debian.net/tested-images/> but I
can't find instructions for activating the I2C and RTC hardware in that OS.
Documentation/devicetree/bindings/rtc/rtc-ds1307.yaml in the kernel source
describes how one could use it ('compatible = "maxim,ds3231"' for DS3231).
I think you should make a DeviceTree Overlay, but I don't know how to do that.
The last time I made a realtime preempt kernel for an armhf, the
makefile did that. Std output from a make menuconfig, an rpi4b runs it
just as if it doesn't know any better, has been since Feb 20, 2020.

Cheers, Gene Heskett.
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.
- Louis D. Brandeis
Genes Web page <http://geneslinuxbox.net:6309/>
Uwe Kleine-König
2023-04-18 15:30:01 UTC
Permalink
Post by Diederik de Haas
Post by Rick Thomas
I've got a Raspberry Pi 4B (4GB) with a DS3231 RTC module. I can make the
combo work with Ubuntu and RaspberryPI-OS. I'd like to try it with the
plain-vanilla Debian from <https://raspi.debian.net/tested-images/> but I
can't find instructions for activating the I2C and RTC hardware in that OS.
Documentation/devicetree/bindings/rtc/rtc-ds1307.yaml in the kernel source
describes how one could use it ('compatible = "maxim,ds3231"' for DS3231).
I think you should make a DeviceTree Overlay, but I don't know how to do that.
An overlay is the nice way to add the RTC to the system. With the device
trees provided by Debian this is a bit more complicated than with
RaspberryPI-OS, because they are not compiled using dtc -@.

If the sysfs stuff that Georg Gast suggested works, I can help you to
create a dt overlay.

Best regards
Uwe
Loading...