Last active
July 9, 2022 14:37
-
-
Save h0tw1r3/11191428 to your computer and use it in GitHub Desktop.
Initializing I2C RTC (DS3231) on Raspberry PI bootup with Systemd without recompiling the kernel or devicetree support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/conf.d/rtc-i2c | |
# | |
# My chip is actually a ds3231n, but ds1307 driver works fine (ds3232 does not!) | |
# | |
CHIP="ds1307" | |
ADDRESS="0x68" | |
BUS="1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/modules-load.d/rtc-i2c.conf | |
i2c-dev | |
i2c_bcm2708 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/udev/rules.d/rtc-i2c.rules | |
# | |
# I would prefer to put SUBSYSTEMS="i2c", | |
# but for some reason it's not working on my system. | |
# | |
ACTION=="add", SUBSYSTEM="rtc", ATTRS{hctosys}=="0", RUN+="/usr/bin/hwclock -s --utc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /lib/systemd/system/rtc-i2c.service | |
[Unit] | |
Description=Initialize i2c hardware RTC device driver | |
DefaultDependencies=no | |
Requires=systemd-modules-load.service | |
After=systemd-modules-load.service | |
Before=sysvinit.target | |
ConditionPathExists=/sys/class/i2c-adapter | |
Conflicts=shutdown.target | |
[Service] | |
Type=oneshot | |
RemainAfterExit=yes | |
EnvironmentFile=/etc/conf.d/rtc-i2c | |
ExecStart=/bin/sh -c "echo ${CHIP} ${ADDRESS} > /sys/class/i2c-adapter/i2c-${BUS}/new_device" | |
[Install] | |
WantedBy=sysinit.target |
Hi,
With such dependencies in .service :
Requires=systemd-modules-load.service
After=systemd-modules-load.service
You can have rtc udev rules run, whereas i2c module not yet fully loaded as you can see in logs :
i2c_rtc_1.service - Initialize i2c hardware RTC device driver
Loaded: loaded (/etc/systemd/system/i2c_rtc_1.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 1970-01-01 01:00:07 CET; 45 years 10 months ago
Process: 113 ExecStart=/bin/sh -c echo ${CHIP} ${ADDRESS} > /sys/class/i2c-adapter/i2c-${BUS}/new_device (code=exited, status=1/FAILURE)
Main PID: 113 (code=exited, status=1/FAILURE)
Jan 01 01:00:07 smart-learning-blue sh[113]: /bin/sh: /sys/class/i2c-adapter/i2c-0/new_device: No such file or directory
Jan 01 01:00:07 smart-learning-blue systemd[1]: i2c_rtc_1.service: Main process exited, code=exited, status=1/FAILURE
Jan 01 01:00:07 smart-learning-blue systemd[1]: Failed to start Initialize i2c hardware RTC device driver.
Jan 01 01:00:07 smart-learning-blue systemd[1]: i2c_rtc_1.service: Unit entered failed state.
Jan 01 01:00:07 smart-learning-blue systemd[1]: i2c_rtc_1.service: Failed with result 'exit-code'.
* systemd-modules-load.service - Load Kernel Modules
Loaded: loaded (/usr/lib/systemd/system/systemd-modules-load.service; static; vendor preset: disabled)
Active: active (exited) since Thu 1970-01-01 01:00:07 CET; 45 years 10 months ago
Docs: man:systemd-modules-load.service(8)
man:modules-load.d(5)
Process: 109 ExecStart=/usr/lib/systemd/systemd-modules-load (code=exited, status=0/SUCCESS)
Main PID: 109 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/systemd-modules-load.service
Jan 01 01:00:07 smart-learning-blue systemd-modules-load[109]: Inserted module 'bcm2708_rng'
Jan 01 01:00:07 smart-learning-blue systemd-modules-load[109]: Inserted module 'snd_bcm2835'
Jan 01 01:00:07 smart-learning-blue systemd-modules-load[109]: Inserted module 'i2c_dev'
Jan 01 01:00:07 smart-learning-blue systemd-modules-load[109]: Inserted module 'rtc_ds1307'
Jan 01 01:00:07 smart-learning-blue systemd[1]: Started Load Kernel Modules.
remi@smart-learning-blue ~ $ less /tmp/logs_start
remi@smart-learning-blue ~ $ dmesg |grep i2c
[ 7.430214] i2c /dev entries driver
[ 10.576283] bcm2708_i2c 20205000.i2c: BSC0 Controller at 0x20205000 (irq 79) (baudrate 100000)
After investigating, I found very useful TAG attribute of udev rules http://www.freedesktop.org/software/systemd/man/systemd.device.html
And finally got it working :
https://gist.github.com/Lahorde/2bc5e4a3b69fc6ca5797
I link to my setup/fork. Here is what is working for me: https://gist.github.com/Lahorde/2bc5e4a3b69fc6ca5797#gistcomment-2825294
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Jeffrey,
thank you for your solution. In rtc-i2c.rules is an error:
SUBSYSTEM="rtc" -> SUBSYSTEM=="rtc"
On my system (raspbian jessie) hwclock is located in /sbin/hwclock.
Sincerly,
Andreas