Skip to content

Instantly share code, notes, and snippets.

@davidebettio
Forked from h0tw1r3/rtc-i2c
Created July 12, 2016 12:26
Show Gist options
  • Save davidebettio/5590c071dc214bf2b715c1695865aaa9 to your computer and use it in GitHub Desktop.
Save davidebettio/5590c071dc214bf2b715c1695865aaa9 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment