-
Notifications
You must be signed in to change notification settings - Fork 161
/
i2c.sh
executable file
·360 lines (317 loc) · 9.9 KB
/
i2c.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
: <<'DISCLAIMER'
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
This script is licensed under the terms of the MIT license.
Unless otherwise noted, code reproduced herein
was written for this script.
Originally written by the - The Pimoroni Crew -
DISCLAIMER
productname="na" # the name of the product to install
baseurl="http://www.adafru.it"
scriptname="i2c" # the name of this script
spacereq=1 # minimum size required on root partition in MB
debugmode="no" # whether the script should use debug routines
debuguser="none" # optional test git user to use in debug mode
debugpoint="none" # optional git repo branch or tag to checkout
forcesudo="yes" # whether the script requires to be ran with root privileges
promptreboot="no" # whether the script should always prompt user to reboot
customcmd="yes" # whether to execute commands specified before exit
armhfonly="yes" # whether the script is allowed to run on other arch
armv6="yes" # whether armv6 processors are supported
armv7="yes" # whether armv7 processors are supported
armv8="yes" # whether armv8 processors are supported
raspbianonly="no" # whether the script is allowed to run on other OSes
macosxsupport="no" # whether Mac OS X is supported by the script
osreleases=( "Raspbian" ) # list os-releases supported
oswarning=( "Debian" "Ubuntu" "Mate" ) # list experimental os-releases
squeezesupport="no" # whether Squeeze is supported
wheezysupport="yes" # whether Wheezy is supported
jessiesupport="yes" # whether Jessie is supported
FORCE=$1
DEVICE_TREE=true
ASK_TO_REBOOT=false
CURRENT_SETTING=false
UPDATE_DB=false
BOOTCMD=/boot/cmdline.txt
CONFIG=/boot/config.txt
APTSRC=/etc/apt/sources.list
INITABCONF=/etc/inittab
BLACKLIST=/etc/modprobe.d/raspi-blacklist.conf
LOADMOD=/etc/modules
DTBODIR=/boot/overlays
confirm() {
if [ "$FORCE" == '-y' ]; then
true
else
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
fi
}
prompt() {
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
}
success() {
echo "$(tput setaf 2)$1$(tput sgr0)"
}
warning() {
echo "$(tput setaf 1)$1$(tput sgr0)"
}
newline() {
echo ""
}
sudocheck() {
if [ $(id -u) -ne 0 ]; then
echo -e "Install must be run as root. Try 'sudo ./$scriptname'\n"
exit 1
fi
}
sysclean() {
sudo apt-get clean && sudo apt-get autoclean
sudo apt-get -y autoremove &> /dev/null
}
sysupdate() {
if ! $UPDATE_DB; then
sudo apt-get update
UPDATE_DB=true
fi
}
sysupgrade() {
sudo apt-get update && sudo apt-get upgrade
sudo apt-get clean && sudo apt-get autoclean
sudo apt-get -y autoremove &> /dev/null
}
sysreboot() {
warning "Some changes made to your system require"
warning "your computer to reboot to take effect."
newline
if prompt "Would you like to reboot now?"; then
sync && sudo reboot
fi
}
arch_check() {
IS_ARMHF=false
IS_ARMv6=false
if uname -m | grep "armv.l" > /dev/null; then
IS_ARMHF=true
if uname -m | grep "armv6l" > /dev/null; then
IS_ARMv6=true
fi
fi
}
os_check() {
IS_RASPBIAN=false
IS_MACOSX=false
IS_SUPPORTED=false
IS_EXPERIMENTAL=false
if [ -f /etc/os-release ]; then
if cat /etc/os-release | grep "Raspbian" > /dev/null; then
IS_RASPBIAN=true && IS_SUPPORTED=true
fi
if command -v apt-get > /dev/null; then
for os in ${osreleases[@]}; do
if cat /etc/os-release | grep "$os" > /dev/null; then
IS_SUPPORTED=true
fi
done
for os in ${oswarning[@]}; do
if cat /etc/os-release | grep "$os" > /dev/null; then
IS_EXPERIMENTAL=true
fi
done
fi
elif uname -s | grep "Darwin" > /dev/null; then
IS_MACOSX=true
if [ $macosxsupport == "yes" ]; then
IS_SUPPORTED=true
fi
fi
}
raspbian_check() {
IS_SQUEEZE=false
IS_WHEEZY=false
IS_JESSIE=false
if [ -f /etc/os-release ]; then
if cat /etc/os-release | grep "jessie" > /dev/null; then
IS_JESSIE=true
elif cat /etc/os-release | grep "wheezy" > /dev/null; then
IS_WHEEZY=true
elif cat /etc/os-release | grep "squeeze" > /dev/null; then
IS_SQUEEZE=true
else
echo "Unsupported distribution"
exit 1
fi
fi
}
raspbian_old() {
if $IS_SQUEEZE || $IS_WHEEZY ;then
true
else
false
fi
}
dt_check() {
if [ -e $CONFIG ] && grep -q "^device_tree=$" $CONFIG; then
DEVICE_TREE=false
fi
}
i2c_check() {
if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
CURRENT_SETTING=true
else
CURRENT_SETTING=false
fi
}
spi_check() {
if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*spi(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
CURRENT_SETTING=true
else
CURRENT_SETTING=false
fi
}
get_init_sys() {
if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
SYSTEMD=1
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
SYSTEMD=0
else
echo "Unrecognised init system" && exit 1
fi
}
get_hw_rev() {
hwrid=$(grep "^Revision" /proc/cpuinfo | rev | cut -c 2-3 | rev)
if [ "$hwrid" == "00" ] || [ "$hwrid" == "01" ];then # Pi 1
hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-4 | rev)"
if [ "$hwrid" == "00" ];then
hwgen="126" # P1
elif [ "$hwrid" == "01" ];then
hwgen="140" # J8
fi
else
hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-6 | rev)"
if [ "$hwrid" == "04" ];then # Pi 2
hwgen="240"
elif [ "$hwrid" == "08" ];then # Pi 3
hwgen="340"
elif [ "$hwrid" == "09" ];then # Pi 0
hwgen="040"
else # Unknown
hwgen="000"
fi
fi
}
: <<'MAINSTART'
Perform all global variables declarations as well as function definition
above this section for clarity, thanks!
MAINSTART
dt_check
arch_check
os_check
if ! $IS_ARMHF; then
warning "This hardware is not supported, sorry!"
warning "Config files have been left untouched"
newline && exit 1
fi
if $IS_RASPBIAN; then
raspbian_check
if [ $wheezysupport == "no" ] && raspbian_old; then
newline && warning "--- Warning ---" && newline
echo "The $productname installer"
echo "does not work on this version of Raspbian."
echo "Check https://github.com/$gitusername/$gitreponame"
echo "for additional information and support"
newline && exit 1
fi
elif [ $raspbianonly == "yes" ];then
warning "This script is intended for Raspbian on a Raspberry Pi!"
newline && exit 1
else
if ! $IS_SUPPORTED && ! $IS_EXPERIMENTAL; then
warning "Your operating system is not supported, sorry!"
warning "Config files have been left untouched"
newline && exit 1
fi
fi
if [ $forcesudo == "yes" ]; then
sudocheck
fi
newline
echo "This script will enable I2C on your Raspberry Pi"
newline
warning "--- Warning ---"
newline
echo "Always be careful when running scripts and commands"
echo "copied from the internet. Ensure they are from a"
echo "trusted source."
newline
echo "If you want to see what this script does before"
echo "running it, you should run:"
echo " curl -sS $baseurl/$scriptname"
newline
if confirm "Do you wish to continue?"; then
i2c_check
if $DEVICE_TREE && ! $CURRENT_SETTING; then
newline
echo "Device Tree Detected"
echo "Enabling I2C..."
newline
if grep -q "^dtparam=i2c_arm=off" $CONFIG; then
sudo sed -i "s|^dtparam=i2c_arm=off|dtparam=i2c_arm=on|" $CONFIG &> /dev/null
elif grep -q "^#dtparam=i2c_arm=on" $CONFIG; then
sudo sed -i "s|^#dtparam=i2c_arm=on|dtparam=i2c_arm=on|" $CONFIG &> /dev/null
else
echo "dtparam=i2c_arm=on" | sudo tee -a $CONFIG &> /dev/null
fi
echo "$BLACKLIST"
if ! [ -e $BLACKLIST ]; then
touch $BLACKLIST
fi
sudo sed -i "s|^\(blacklist[[:space:]]*i2c[-_]bcm2708\)|#\1|" $BLACKLIST &> /dev/null
dtparam i2c_arm=on &> /dev/null
newline
success "I2C Enabled"
warning "Reboot required!"
newline
ASK_TO_REBOOT=true
elif ! $DEVICE_TREE && [ -e $BLACKLIST ] && grep -q "^blacklist[[:space:]]*i2c-bcm2708" $BLACKLIST; then
newline
echo "No Device Tree Detected"
echo "Enabling I2C..."
newline
echo "Commenting out Blacklist entry in "
echo "$BLACKLIST"
sudo sed -i "s|^blacklist[[:space:]]*i2c-bcm2708.*|#blacklist i2c-bcm2708|" $BLACKLIST &> /dev/null
newline && success "I2C Enabled" && newline
else
success "I2C Already Enabled" && newline
fi
if ! [ -e $LOADMOD ]; then
touch $LOADMOD
fi
if ! grep -q "^i2c[-_]dev" $LOADMOD; then
echo "i2c-dev" | sudo tee -a $LOADMOD &> /dev/null
fi
if ! grep -qe "i2c[-_]bcm2708" $LOADMOD; then
echo "i2c-bcm2708" | sudo tee -a $LOADMOD &> /dev/null
fi
sudo modprobe -a i2c-bcm2708 i2c-dev
else
newline && echo "Aborting..." && newline
fi
exit 0