-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-piface-real-time-clock.sh
executable file
·89 lines (83 loc) · 2.65 KB
/
install-piface-real-time-clock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
#: Description: Enables the required modules for PiFace Clock.
#=======================================================================
# NAME: check_for_i2c_tools
# DESCRIPTION: Checks if i2c-tools is installed.
#=======================================================================
check_for_i2c_tools() {
dpkg -s i2c-tools > /dev/null 2>&1
if [[ $? -eq 1 ]]; then
echo "The package `i2c-tools` is not installed. Install it with:"
echo ""
echo " sudo apt-get install i2c-tools"
echo ""
exit 1
fi
}
#=======================================================================
# NAME: set_revision_var
# DESCRIPTION: Stores the revision number of this Raspberry Pi into
# $RPI_REVISION
#=======================================================================
set_revision_var() {
rev_str=$(grep "Revision" /proc/cpuinfo)
# get the last character
len_rev_str=${#rev_str}
chr_index=$(($len_rev_str-1))
chr=${rev_str:$chr_index:$len_rev_str}
if [[ $chr == "2" || $chr == "3" ]]; then
RPI_REVISION="1"
else
RPI_REVISION="2"
fi
}
#=======================================================================
# NAME: start_on_boot
# DESCRIPTION: Load the I2C modules and send magic number to RTC, on boot.
#=======================================================================
start_on_boot() {
echo "Changing /etc/rc.local to load time from PiFace Clock."
# remove exit 0
sed -i "s/exit 0//" /etc/rc.local
if [[ $RPI_REVISION == "1" ]]; then
i=0 # i2c-0
else
i=1 # i2c-1
fi
cat >> /etc/rc.local << EOF
modprobe i2c-dev
# Calibrate the clock (default: 0x47). See datasheet for MCP7940N
i2cset -y $i 0x6f 0x08 0x47
modprobe i2c:mcp7941x
echo mcp7941x 0x6f > /sys/class/i2c-dev/i2c-$i/device/new_device
hwclock --hctosys
EOF
}
#=======================================================================
# MAIN
#=======================================================================
# check if the script is being run as root
if [[ $EUID -ne 0 ]]
then
printf 'This script must be run as root.\nExiting..\n'
exit 1
fi
RPI_REVISION=""
check_for_i2c_tools &&
set_revision_var &&
start_on_boot &&
if [[ ! -e /sys/class/i2c-dev/i2c-$i ]]; then
echo "Enable I2C by using:"
echo ""
echo " raspi-config"
echo ""
echo "Then navigate to 'Advanced Options' > 'I2C' and select 'yes' to "
echo "enable the ARM I2C interface. Then *reboot* and set your clock "
echo "with:"
else
echo "Now *reboot* and set your clock with:"
fi
echo ""
echo ' sudo date -s "14 JAN 2014 10:10:30"'
echo " hwclock --systohc"
echo ""