Skip to content

Instantly share code, notes, and snippets.

@8
Created May 2, 2013 10:13
Show Gist options
  • Save 8/5501329 to your computer and use it in GitHub Desktop.
Save 8/5501329 to your computer and use it in GitHub Desktop.
shell script for configuring raspicomm rtc clock on startup
#! /bin/sh
# The content of the raspberry pi revision
# Content of the file /etc/init.d/rtc.sh
RTC_SCRIPT_HEADER="#! /bin/sh\n\n### BEGIN INIT INFO\n# Provides: rtc\n# Required-Start: $remote_fs $syslog\n# Required-Stop: $local_fs\n# Default-Start: 2 3 4 5\n# Default-Stop: 0 1 6\n# Short-Description: Creates the real time clock i2c device when executed\n### END INIT INFO\n\n# Activate the real time clock\n"
RTC_SCRIPT_BODY_REVISION1="echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device"
RTC_SCRIPT_BODY_REVISION2="echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"
RTC_SCRIPT_REVISION1=$RTC_SCRIPT_HEADER$RTC_SCRIPT_BODY_REVISION1
RTC_SCRIPT_REVISION2=$RTC_SCRIPT_HEADER$RTC_SCRIPT_BODY_REVISION2
revision=0
validinput=0
# Repeat the question
while [ $validinput -eq 0 ]; do
# Ask the user which revision of the raspberry pi he is using
echo -n "Please specify the revision of the raspberry pi you are using [1,2]: "
# read user input
read revision
if [ $revision -eq 1 ] ; then
validinput=1
elif [ $revision -eq 2 ] ; then
validinput=1
fi
done
i2cMissing=0
# check if the i2c-devices exist before
if [ $revision -eq 1 ] ; then
if [ ! -w "/sys/class/i2c-adapter/i2c-0/new_device" ] ; then
echo "Error: /sys/class/i2c-adapter/i2c-0/new_device does not exist or not writable."
i2cMissing=1
fi
elif [ $revision -eq 2 ]; then
if [ ! -w "/sys/clr,,pass/i2c-adapter/i2c-1/new_device" ] ; then
echo "Error: /sys/class/i2c-adapter/i2c-1/new_device does not exist or not writeable."
i2cMissing=1
fi
fi
if [ $i2cMissing -eq 1 ] ; then
echo "It seems that your kernel is not configured for i2c device support, which is required for accessing the real time clock."
enableI2C="n"
echo -n "Do you want to enable i2c support? [y,n]: "
read enableI2C
if [ "$enableI2C" = "y" ]; then
echo "trying to enable i2c support..."
kernel=`uname -r`
echo 'You using the kernel version' $kernel
echo "loading i2c_bcm2708 module"
modprobe i2c_bcm2708
# check if i2c_bcm2708 is already added to /etc/modules
installed=`cat /etc/modules | grep i2c_bcm2708 -c`
if [ $installed = 0 ] ; then
# if not, add i2c_bcm2708 to /etc/modules
echo "adding i2c_bcm2708 module to startup"
echo "i2c_bcm2708" >> /etc/modules
else
echo "i2c_bcm2708 already added to /etc/modules"
fi
else
echo "exiting..."
exit
fi
fi
# Tell the user what we're doing
echo "deploying real time clock startup script /etc/init.d/rtc.sh for raspberry pi revision $revision"
# Depending on the revision deploy a startup script for the real time clock
if [ $revision -eq 1 ]; then
echo $RTC_SCRIPT_REVISION1 > /etc/init.d/rtc.sh
else
echo $RTC_SCRIPT_REVISION2 > /etc/init.d/rtc.sh
fi
echo Set /etc/init.d/rtc.sh to autostart
# Make the file executable
chmod +x /etc/init.d/rtc.sh
# Set the real time clock to be automatically started
update-rc.d rtc.sh defaults 80
# Execute the script right now to create the rtc i2c device
/etc/init.d/rtc.sh
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment