-
Notifications
You must be signed in to change notification settings - Fork 13
/
install-piface-real-time-clock.sh
executable file
·139 lines (129 loc) · 4.04 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/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() {
revision=$(grep "Revision" /proc/cpuinfo | sed -e "s/Revision\t: //")
RPI2_REVISION=$((16#a01041))
RPI3_REVISION=$((16#a02082))
RPI4_REVISION=$((16#a03111))
if [ "$((16#$revision))" -ge "$RPI4_REVISION" ]; then
RPI_REVISION="4"
elif [ "$((16#$revision))" -ge "$RPI3_REVISION" ]; then
RPI_REVISION="3"
elif [ "$((16#$revision))" -ge "$RPI2_REVISION" ]; then
RPI_REVISION="2"
else
RPI_REVISION="1"
fi
}
#=======================================================================
# NAME: start_on_boot
# DESCRIPTION: Load the I2C modules and send magic number to RTC, on boot.
#=======================================================================
start_on_boot() {
echo "Create a new pifacertc init script to load time from PiFace RTC."
echo "Adding /etc/init.d/pifacertc ."
if [[ $RPI_REVISION == "4" ]]; then
i=1 # i2c-1
elif [[ $RPI_REVISION == "3" ]]; then
i=1 # i2c-1
elif [[ $RPI_REVISION == "2" ]]; then
i=1 # i2c-1
else
i=0 # i2c-0
fi
cat > /etc/init.d/pifacertc << EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: pifacertc
# Required-Start: udev mountkernfs \$remote_fs raspi-config
# Required-Stop:
# Default-Start: S
# Default-Stop:
# Short-Description: Add the PiFace RTC
# Description: Add the PiFace RTC
### END INIT INFO
. /lib/lsb/init-functions
case "\$1" in
start)
log_success_msg "Probe the i2c-dev"
modprobe i2c-dev
# Calibrate the clock (default: 0x47). See datasheet for MCP7940N
log_success_msg "Calibrate the clock"
i2cset -y $i 0x6f 0x08 0x47
log_success_msg "Probe the mcp7941x driver"
modprobe i2c:mcp7941x
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-$i/device/new_device
log_success_msg "Synchronise the system clock and hardware RTC"
hwclock --hctosys
;;
stop)
;;
restart)
;;
force-reload)
;;
*)
echo "Usage: \$0 start" >&2
exit 3
;;
esac
EOF
chmod +x /etc/init.d/pifacertc
echo "Install the pifacertc init script"
update-rc.d pifacertc defaults
}
#=======================================================================
# 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 " sudo hwclock --systohc"
echo ""
echo "Enable auto-sync on boot by enabling the service"
echo " sudo systemctl enable pifacertc"
echo ""
echo "Check service status"
echo " sudo systemctl status pifacertc"
echo ""