Skip to content

Instantly share code, notes, and snippets.

@vch42
Created February 4, 2019 22:30
Show Gist options
  • Save vch42/a0fd4db0da289015ee0a0c0616eaadd6 to your computer and use it in GitHub Desktop.
Save vch42/a0fd4db0da289015ee0a0c0616eaadd6 to your computer and use it in GitHub Desktop.
#!/bin/sh
. /lib/lsb/init-functions
#set_revision_var() {
# revision=$(grep "Revision" /proc/cpuinfo | sed -e "s/Revision\t: //")
# RPI2_REVISION=$((16#a01041))
# RPI3_REVISION=$((16#a02082))
# if [ "$((16#$revision))" -ge "$RPI3_REVISION" ]; then
# RPI_REVISION="3"
# elif [ "$((16#$revision))" -ge "$RPI2_REVISION" ]; then
# RPI_REVISION="2"
# else
# RPI_REVISION="1"
# fi
#}
case "$1" in
start)
#check if i2c-tools is installed
dpkg -s i2c-tools > /dev/null 2>&1
if [ $? -eq 1 ]; then
log_failure_msg "The package `i2c-tools` is not installed"
exit 1
fi
#find the valid i2c bus number
i2cdetect -y 0 > /dev/null 2>&1
if [ $? -eq 0 ]; then
i2cbus=0 #i2c-0
else
i2cdetect -y 1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
i2cbus=1 #i2c-1
else
log_failure_msg "Cannot communicate on I2C bus 0 or 1. Check that I2C is enabled in raspi-config"
exit 1
fi
fi
log_success_msg "Found I2C bus i2c-$i2cbus"
#make sure i2c-dev kernel module is loaded
log_success_msg "Probing i2c-dev driver"
modprobe i2c-dev
if [ $? -ne 0 ]; then
log_failure_msg "Probing i2c-dev driver failed, aborting"
exit 1
fi
# Calibrate the RTC (default: 0x47). See datasheet for MCP7940N
log_success_msg "Calibrating the RTC module"
i2cset -y $i2cbus 0x6f 0x08 0x47
if [ $? -ne 0 ]; then
log_failure_msg "Calibrating the RTC module failed, aborting"
exit 1
fi
#load the mcp7941x driver (will show up as rtc_ds1307 in lsmod)
log_success_msg "Probing the mcp7941x driver"
modprobe i2c:mcp7941x
if [ $? -ne 0 ]; then
log_failure_msg "Probing the mcp7941x driver failed, aborting"
exit 1
fi
#instantiating the rtc device
log_success_msg "Add the mcp7941x device in the sys filesystem"
# https://www.kernel.org/doc/Documentation/i2c/instantiating-devices
echo mcp7941x 0x6f > /sys/class/i2c-dev/i2c-$i2cbus/device/new_device
if [ $? -ne 0 ]; then
log_failure_msg "Adding the mcp7941x device failed, aborting"
exit 1
fi
#pushing the RTC time in the system clock (assuming RTC is in UTC)
log_success_msg "Synchronise the system clock to the hardware RTC"
hwclock --hctosys -u
if [ $? -ne 0 ]; then
log_failure_msg "Something wrong happened while syncing the RTC to the sys clock!"
exit 1
fi
;;
stop)
#check if i2c-tools is installed
dpkg -s i2c-tools > /dev/null 2>&1
if [ $? -eq 1 ]; then
log_failure_msg "The package `i2c-tools` is not installed"
exit 1
fi
i2cdetect -y 0 > /dev/null 2>&1
if [ $? -eq 0 ]; then
i2cbus=0 #i2c-0
else
i2cdetect -y 1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
i2cbus=1 #i2c-1
else
log_failure_msg "Cannot communicate on I2C bus 0 or 1. Check that I2C is enabled in raspi-config"
exit 1
fi
fi
log_success_msg "Found I2C bus i2c-$i2cbus"
if [ -e /dev/rtc ]; then
log_success_msg "Removing the mcp7941x device from the sys filesystem"
# https://www.kernel.org/doc/Documentation/i2c/instantiating-devices
echo 0x6f > /sys/class/i2c-dev/i2c-$i2cbus/device/delete_device
if [ $? -ne 0 ]; then
log_failure_msg "Removing the mcp7941x device failed"
fi
else
log_failure_msg "Finding device node /dev/rtc "
fi
log_success_msg "Unloading driver"
rmmod rtc_ds1307
if [ ! $? -eq 0 ]; then
log_failure_msg "Unloading the driver"
fi
;;
restart)
;;
*)
echo "Usage: \$0 [start][stop]" >&2
exit 3
;;
esac
[Unit]
Description=PiFace RTC module init
Before=multi-user.target graphical.target
After=mountkernfs.service udev.service remote-fs.target raspi-config.service
Requires=udev.service remote-fs.target raspi-config.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/PiFace/pifacertc start
ExecStop=/opt/PiFace/pifacertc stop
[Install]
WantedBy=multi-user.target graphical.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment