Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
install & setup of an I2C RTC on RaspberryPi (both Arch Linux ARM and Raspbian)
#!/bin/bash
#-----------------------------------------------------------
# Copyright (C) 2014-2016 GRUBERND http://grubernd.at
# released under a FreeBSD License
#-----------------------------------------------------------
# CATE installer module :: device RealTimeClock RPi
#
# !! needs to be run as root / sudo !!
#
# works both for DS1307 and DS3231 chips.
# this script is also designed to run standalone for
# both Arch Linux ARM and Raspbian installs.
#-----------------------------------------------------------
#_CATECONF__ # RTC service requires no configuration
if [[ "$(which pacman)" != "" ]]; then
echo -e ""
echo -e ".. installing I2C RTC for Arch Linux ARM"
echo -e ""
modules_file="/etc/modules-load.d/raspberrypi.conf"
pacman -S --needed --noconfirm i2c-tools
fi
if [[ "$(which apt-get)" != "" ]]; then
echo -e ""
echo -e ".. installing I2C RTC for Raspbian"
echo -e ""
modules_file="/etc/modules"
apt-get install --yes i2c-tools
fi
echo -e ""
echo -e ".. activating I2C devicetree in /boot/config.txt"
sed --in-place 's/^#device_tree_param=i2c_arm=on/device_tree_param=i2c_arm=on/' "/boot/config.txt"
#-----------------------------------------------------------
echo -e ""
echo -e ".. adding i2c and rtc modules to the conf"
cp "$modules_file" /tmp/rtc.tmp
echo rtc-ds1307 >> /tmp/rtc.tmp
echo i2c-dev >> /tmp/rtc.tmp
sort /tmp/rtc.tmp | uniq > "$modules_file"
#-----------------------------------------------------------
echo -e ""
echo -e ".. creating init script for the RTC"
mkdir -p /usr/lib/systemd/scripts/
cat > /usr/lib/systemd/scripts/rtc << ENDRTCSCRIPT
#!/bin/bash
# create an i2c device DS1307 (works also for DS3231)
# set systemclock from external i2c-rtc
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s
ENDRTCSCRIPT
chmod 755 /usr/lib/systemd/scripts/rtc
echo -e ""
echo -e ".. creating service for the RTC"
cat > /etc/systemd/system/rtc.service << ENDRTCSERVICE
[Unit]
Description=RTClock
Before=network.target
[Service]
ExecStart=/usr/lib/systemd/scripts/rtc
Type=oneshot
[Install]
WantedBy=multi-user.target
ENDRTCSERVICE
#-----------------------------------------------------------
echo -e ""
echo -e ".. enabling and starting RTC service"
systemctl enable rtc
systemctl start rtc
#-----------------------------------------------------------
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. systemctl status rtc"
#_CATETEST__ systemctl status rtc
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. testing i2c activation in /boot/config.txt"
#_CATETEST__ grep "^device_tree_param=i2c_arm=on" /boot/config.txt
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. testing connection of i2c devices"
#_CATETEST__ i2cdetect -y 1
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. testing rtc hwclock"
#_CATETEST__ hwclock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.