No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2c.sh 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #!/bin/bash
  2. : <<'DISCLAIMER'
  3. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  4. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  5. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  6. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  7. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  8. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  9. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  10. OTHER DEALINGS IN THE SOFTWARE.
  11. This script is licensed under the terms of the MIT license.
  12. Unless otherwise noted, code reproduced herein
  13. was written for this script.
  14. Originally written by the - The Pimoroni Crew -
  15. DISCLAIMER
  16. productname="na" # the name of the product to install
  17. baseurl="http://www.adafru.it"
  18. scriptname="i2c" # the name of this script
  19. spacereq=1 # minimum size required on root partition in MB
  20. debugmode="no" # whether the script should use debug routines
  21. debuguser="none" # optional test git user to use in debug mode
  22. debugpoint="none" # optional git repo branch or tag to checkout
  23. forcesudo="yes" # whether the script requires to be ran with root privileges
  24. promptreboot="no" # whether the script should always prompt user to reboot
  25. customcmd="yes" # whether to execute commands specified before exit
  26. armhfonly="yes" # whether the script is allowed to run on other arch
  27. armv6="yes" # whether armv6 processors are supported
  28. armv7="yes" # whether armv7 processors are supported
  29. armv8="yes" # whether armv8 processors are supported
  30. raspbianonly="no" # whether the script is allowed to run on other OSes
  31. macosxsupport="no" # whether Mac OS X is supported by the script
  32. osreleases=( "Raspbian" ) # list os-releases supported
  33. oswarning=( "Debian" "Ubuntu" "Mate" ) # list experimental os-releases
  34. squeezesupport="no" # whether Squeeze is supported
  35. wheezysupport="yes" # whether Wheezy is supported
  36. jessiesupport="yes" # whether Jessie is supported
  37. FORCE=$1
  38. DEVICE_TREE=true
  39. ASK_TO_REBOOT=false
  40. CURRENT_SETTING=false
  41. UPDATE_DB=false
  42. BOOTCMD=/boot/cmdline.txt
  43. CONFIG=/boot/config.txt
  44. APTSRC=/etc/apt/sources.list
  45. INITABCONF=/etc/inittab
  46. BLACKLIST=/etc/modprobe.d/raspi-blacklist.conf
  47. LOADMOD=/etc/modules
  48. DTBODIR=/boot/overlays
  49. confirm() {
  50. if [ "$FORCE" == '-y' ]; then
  51. true
  52. else
  53. read -r -p "$1 [y/N] " response < /dev/tty
  54. if [[ $response =~ ^(yes|y|Y)$ ]]; then
  55. true
  56. else
  57. false
  58. fi
  59. fi
  60. }
  61. prompt() {
  62. read -r -p "$1 [y/N] " response < /dev/tty
  63. if [[ $response =~ ^(yes|y|Y)$ ]]; then
  64. true
  65. else
  66. false
  67. fi
  68. }
  69. success() {
  70. echo "$(tput setaf 2)$1$(tput sgr0)"
  71. }
  72. warning() {
  73. echo "$(tput setaf 1)$1$(tput sgr0)"
  74. }
  75. newline() {
  76. echo ""
  77. }
  78. sudocheck() {
  79. if [ $(id -u) -ne 0 ]; then
  80. echo -e "Install must be run as root. Try 'sudo ./$scriptname'\n"
  81. exit 1
  82. fi
  83. }
  84. sysclean() {
  85. sudo apt-get clean && sudo apt-get autoclean
  86. sudo apt-get -y autoremove &> /dev/null
  87. }
  88. sysupdate() {
  89. if ! $UPDATE_DB; then
  90. sudo apt-get update
  91. UPDATE_DB=true
  92. fi
  93. }
  94. sysupgrade() {
  95. sudo apt-get update && sudo apt-get upgrade
  96. sudo apt-get clean && sudo apt-get autoclean
  97. sudo apt-get -y autoremove &> /dev/null
  98. }
  99. sysreboot() {
  100. warning "Some changes made to your system require"
  101. warning "your computer to reboot to take effect."
  102. newline
  103. if prompt "Would you like to reboot now?"; then
  104. sync && sudo reboot
  105. fi
  106. }
  107. arch_check() {
  108. IS_ARMHF=false
  109. IS_ARMv6=false
  110. if uname -m | grep "armv.l" > /dev/null; then
  111. IS_ARMHF=true
  112. if uname -m | grep "armv6l" > /dev/null; then
  113. IS_ARMv6=true
  114. fi
  115. fi
  116. }
  117. os_check() {
  118. IS_RASPBIAN=false
  119. IS_MACOSX=false
  120. IS_SUPPORTED=false
  121. IS_EXPERIMENTAL=false
  122. if [ -f /etc/os-release ]; then
  123. if cat /etc/os-release | grep "Raspbian" > /dev/null; then
  124. IS_RASPBIAN=true && IS_SUPPORTED=true
  125. fi
  126. if command -v apt-get > /dev/null; then
  127. for os in ${osreleases[@]}; do
  128. if cat /etc/os-release | grep "$os" > /dev/null; then
  129. IS_SUPPORTED=true
  130. fi
  131. done
  132. for os in ${oswarning[@]}; do
  133. if cat /etc/os-release | grep "$os" > /dev/null; then
  134. IS_EXPERIMENTAL=true
  135. fi
  136. done
  137. fi
  138. elif uname -s | grep "Darwin" > /dev/null; then
  139. IS_MACOSX=true
  140. if [ $macosxsupport == "yes" ]; then
  141. IS_SUPPORTED=true
  142. fi
  143. fi
  144. }
  145. raspbian_check() {
  146. IS_SQUEEZE=false
  147. IS_WHEEZY=false
  148. IS_JESSIE=false
  149. if [ -f /etc/os-release ]; then
  150. if cat /etc/os-release | grep "jessie" > /dev/null; then
  151. IS_JESSIE=true
  152. elif cat /etc/os-release | grep "wheezy" > /dev/null; then
  153. IS_WHEEZY=true
  154. elif cat /etc/os-release | grep "squeeze" > /dev/null; then
  155. IS_SQUEEZE=true
  156. else
  157. echo "Unsupported distribution"
  158. exit 1
  159. fi
  160. fi
  161. }
  162. raspbian_old() {
  163. if $IS_SQUEEZE || $IS_WHEEZY ;then
  164. true
  165. else
  166. false
  167. fi
  168. }
  169. dt_check() {
  170. if [ -e $CONFIG ] && grep -q "^device_tree=$" $CONFIG; then
  171. DEVICE_TREE=false
  172. fi
  173. }
  174. i2c_check() {
  175. if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
  176. CURRENT_SETTING=true
  177. else
  178. CURRENT_SETTING=false
  179. fi
  180. }
  181. spi_check() {
  182. if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*spi(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
  183. CURRENT_SETTING=true
  184. else
  185. CURRENT_SETTING=false
  186. fi
  187. }
  188. get_init_sys() {
  189. if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
  190. SYSTEMD=1
  191. elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
  192. SYSTEMD=0
  193. else
  194. echo "Unrecognised init system" && exit 1
  195. fi
  196. }
  197. get_hw_rev() {
  198. hwrid=$(grep "^Revision" /proc/cpuinfo | rev | cut -c 2-3 | rev)
  199. if [ "$hwrid" == "00" ] || [ "$hwrid" == "01" ];then # Pi 1
  200. hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-4 | rev)"
  201. if [ "$hwrid" == "00" ];then
  202. hwgen="126" # P1
  203. elif [ "$hwrid" == "01" ];then
  204. hwgen="140" # J8
  205. fi
  206. else
  207. hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-6 | rev)"
  208. if [ "$hwrid" == "04" ];then # Pi 2
  209. hwgen="240"
  210. elif [ "$hwrid" == "08" ];then # Pi 3
  211. hwgen="340"
  212. elif [ "$hwrid" == "09" ];then # Pi 0
  213. hwgen="040"
  214. else # Unknown
  215. hwgen="000"
  216. fi
  217. fi
  218. }
  219. : <<'MAINSTART'
  220. Perform all global variables declarations as well as function definition
  221. above this section for clarity, thanks!
  222. MAINSTART
  223. dt_check
  224. arch_check
  225. os_check
  226. if ! $IS_ARMHF; then
  227. warning "This hardware is not supported, sorry!"
  228. warning "Config files have been left untouched"
  229. newline && exit 1
  230. fi
  231. if $IS_RASPBIAN; then
  232. raspbian_check
  233. if [ $wheezysupport == "no" ] && raspbian_old; then
  234. newline && warning "--- Warning ---" && newline
  235. echo "The $productname installer"
  236. echo "does not work on this version of Raspbian."
  237. echo "Check https://github.com/$gitusername/$gitreponame"
  238. echo "for additional information and support"
  239. newline && exit 1
  240. fi
  241. elif [ $raspbianonly == "yes" ];then
  242. warning "This script is intended for Raspbian on a Raspberry Pi!"
  243. newline && exit 1
  244. else
  245. if ! $IS_SUPPORTED && ! $IS_EXPERIMENTAL; then
  246. warning "Your operating system is not supported, sorry!"
  247. warning "Config files have been left untouched"
  248. newline && exit 1
  249. fi
  250. fi
  251. if [ $forcesudo == "yes" ]; then
  252. sudocheck
  253. fi
  254. newline
  255. echo "This script will enable I2C on your Raspberry Pi"
  256. newline
  257. warning "--- Warning ---"
  258. newline
  259. echo "Always be careful when running scripts and commands"
  260. echo "copied from the internet. Ensure they are from a"
  261. echo "trusted source."
  262. newline
  263. echo "If you want to see what this script does before"
  264. echo "running it, you should run:"
  265. echo " curl -sS $baseurl/$scriptname"
  266. newline
  267. if confirm "Do you wish to continue?"; then
  268. i2c_check
  269. if $DEVICE_TREE && ! $CURRENT_SETTING; then
  270. newline
  271. echo "Device Tree Detected"
  272. echo "Enabling I2C..."
  273. newline
  274. if grep -q "^dtparam=i2c_arm=off" $CONFIG; then
  275. sudo sed -i "s|^dtparam=i2c_arm=off|dtparam=i2c_arm=on|" $CONFIG &> /dev/null
  276. elif grep -q "^#dtparam=i2c_arm=on" $CONFIG; then
  277. sudo sed -i "s|^#dtparam=i2c_arm=on|dtparam=i2c_arm=on|" $CONFIG &> /dev/null
  278. else
  279. echo "dtparam=i2c_arm=on" | sudo tee -a $CONFIG &> /dev/null
  280. fi
  281. echo "$BLACKLIST"
  282. if ! [ -e $BLACKLIST ]; then
  283. touch $BLACKLIST
  284. fi
  285. sudo sed -i "s|^\(blacklist[[:space:]]*i2c[-_]bcm2708\)|#\1|" $BLACKLIST &> /dev/null
  286. dtparam i2c_arm=on &> /dev/null
  287. newline
  288. success "I2C Enabled"
  289. warning "Reboot required!"
  290. newline
  291. ASK_TO_REBOOT=true
  292. elif ! $DEVICE_TREE && [ -e $BLACKLIST ] && grep -q "^blacklist[[:space:]]*i2c-bcm2708" $BLACKLIST; then
  293. newline
  294. echo "No Device Tree Detected"
  295. echo "Enabling I2C..."
  296. newline
  297. echo "Commenting out Blacklist entry in "
  298. echo "$BLACKLIST"
  299. sudo sed -i "s|^blacklist[[:space:]]*i2c-bcm2708.*|#blacklist i2c-bcm2708|" $BLACKLIST &> /dev/null
  300. newline && success "I2C Enabled" && newline
  301. else
  302. success "I2C Already Enabled" && newline
  303. fi
  304. if ! [ -e $LOADMOD ]; then
  305. touch $LOADMOD
  306. fi
  307. if ! grep -q "^i2c[-_]dev" $LOADMOD; then
  308. echo "i2c-dev" | sudo tee -a $LOADMOD &> /dev/null
  309. fi
  310. if ! grep -qe "i2c[-_]bcm2708" $LOADMOD; then
  311. echo "i2c-bcm2708" | sudo tee -a $LOADMOD &> /dev/null
  312. fi
  313. sudo modprobe -a i2c-bcm2708 i2c-dev
  314. else
  315. newline && echo "Aborting..." && newline
  316. fi
  317. exit 0