Skip to content

Instantly share code, notes, and snippets.

@h0tw1r3
Last active July 9, 2022 14:37
Show Gist options
  • 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
@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