Skip to content

Instantly share code, notes, and snippets.

@h0tw1r3
Last active July 9, 2022 14:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save h0tw1r3/11191428 to your computer and use it in GitHub Desktop.
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.
# /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"
# /etc/modules-load.d/rtc-i2c.conf
i2c-dev
i2c_bcm2708
# /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"
# /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
@Lahorde
Copy link

Lahorde commented Nov 23, 2015

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

@solsticedhiver
Copy link

solsticedhiver commented Jan 31, 2019

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