Skip to content

Instantly share code, notes, and snippets.

@ugate ugate/setup.sh
Last active Jan 25, 2018

Embed
What would you like to do?
NTC C.H.I.P. setup script
#!/bin/bash
LC_ALL=en_US.UTF-8
# configures/installs common CHIP features (including enabling disabled ones)
# run 'sudo chmod +x setup.sh && ./setup.sh' to run the installation
clear
res=
dtc=
rboot=
uname -a
cat << _EOF_
===========================================================================
▒▒▒ ▒▒▒ ▒▒░
▒▒▒ ▒▒▒ ▒▒░
▒▒▒ ▒▒▒ ▒▒░
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
▒▒░ ░▒▒
▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒
▒▒░ ▒▒▒▒ ░▒▒
▒▒░ ▒▒▒▒▒▒▒▒ ░▒▒
▒▒░ ▒▒▒▒▒▒▒▒░ ░▒▒
▒▒░ ▒▒▒▒▒▒▒▒▒▒ ░▒▒
▒▒▒▒▒▒▒▒░ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒░ ░▒▒
▒▒░ ░▒▒
▒▒░ ░▒▒
▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒
▒▒░ ░▒▒
▒▒░ ░▒▒
▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒ ▒▒▒ ▒▒▒
▒▒▒ ▒▒▒ ▒▒▒
▒▒▒ ▒▒▒ ▒▒▒
____ _ _ ___ ____
/ ___| | | | | |_ _| | _ \
| | | |_| | | | | |_) |
| |___ _ | _ | _ | | | |__/
\____| (_)|_| |_| (_)|___| (_) |_| (_)
===========================================================================
_______ _______ _______ _ _ _____
|______ |______ | | | |_____]
______| |______ | |_____| |
===========================================================================
_EOF_
# password for the current user
while [[ -z "$pwo" ]]; do
read -s -p "Enter a CURRENT password for \"$USER\" (default=\"chip\"): " pwo
if [[ -z "$pwo" ]]; then
pwo=chip
fi
echo "*** validating password ***"
sudo -k
if sudo -lS &> /dev/null <<- _EOF_
$pwo
_EOF_
then
echo 'Password confirmed!'
else
echo '☠ Invalid password!'
pwo=""
fi
done
# change $USER password
echo "==========================================================================="
cpw () {
yn=("*** Skip ***" "Change password for \"$1\"")
yna=
pwx=
pwc=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
while [[ -z "$pwx" || "$pwx" != "$pwc" || ${#pwx} -lt 8 ]]; do
if [ "$pwx" != "$pwc" ]; then
echo ""
echo "☠ New pasword and retyped password do not match!"
elif [[ -n "$pwx" && ${#pwx} -lt 8 ]]; then
echo ""
echo "☠ New password is too short (min 8 characters)!"
fi
read -s -p "Enter a NEW password for \"$1\": " pwx
echo ""
read -s -p "Retype NEW password for \"$1\": " pwc
done
echo ""
if [[ "$1" == "$USER" ]]; then
pwu=$pwx
else
pwr=$pwx
fi
if sudo passwd $1 &> /dev/null <<- _EOF_
$pwx
$pwx
_EOF_
then
echo "Password changed for $1!"
else
echo "☠ Failed to change password for $1! "$?
fi
fi
}
cpw "$USER"
echo "---------------------------------------------------------------------------"
cpw "root"
# update host files
echo "==========================================================================="
hn=$(grep -o -m 1 '\b[a-z]*\b' /etc/hosts)
yn=("*** Skip ***" "Change host name from \"$hn\"")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
read -p "Enter a NEW hostname for CHIP: " res
if [[ -n "$res" ]]; then
yn=("*** Skip ***" "Replace \"$hn\" with \"$res\" in \"/etc/hostname\" and \"/etc/hosts\"")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
echo "---------------------------------------------------------------------------"
echo "/etc/hostname contents:"
sudo sed -i "s/${hn}/${res}/ig" /etc/hostname
cat /etc/hostname
echo "---------------------------------------------------------------------------"
echo "/etc/hosts contents:"
sudo sed -i "s/${hn}/${res}/ig" /etc/hosts
cat /etc/hosts
hn=$res
fi
fi
fi
# WiFi setup
echo "==========================================================================="
nmcli device wifi list
echo "---------------------------------------------------------------------------"
ssid=
ssidp=
readarray -t ssids < <(nmcli -f ssid device wifi list ifname wlan0 | grep -o '^\S*')
# first element in array is "SSID"
ssids[0]="*** Skip ***"
PS3="Please select a WiFi SSID to connect to (use list #): "
select ssid in "${ssids[@]}"; do
for item in "${ssids[@]}"; do
if [[ $item == $ssid ]]; then
break 2
fi
done
done
if [ "$ssid" != "${ssids[0]}" ]; then
while [[ -z "$ssidp" || ${#ssidp} -lt 8 ]]; do
if [[ -n "$ssidp" && ${#ssidp} -lt 8 ]]; then
echo "☠ Pasword for \"$ssid\" is too short (${#ssid} is less than min of 8 characters)!"
fi
read -s -p "Enter the password to connect to \"$ssid\": " ssidp
done
echo ""
# remove existing (if present) and add
#sudo nmcli device disconnect wlan0
sudo nmcli con delete id "$ssid" &> /dev/null
sudo nmcli device wifi connect "$ssid" password "$ssidp" ifname wlan0
echo ""
fi
# update/upgrade system
echo "==========================================================================="
yn=("*** Skip ***" "Update/Upgrade system using apt-get")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
sudo apt-get update && sudo apt-get -y upgrade
fi
# regenerate new SSH server keys
echo "==========================================================================="
yn=("*** Skip ***" "Regenerate SSH server keys (for SSH, SFTP, etc.)")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna || -z "$yna" ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
sudo ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
sudo ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa -b 521
sudo /etc/init.d/ssh restart
fi
# install build-essential
echo "==========================================================================="
yn=("*** Skip ***" "Install required \"build-essential\" for C/C++ compilation")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna || -z "$yna" ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
sudo apt-get -y install build-essential
fi
# enable device tree overlay hw
echo "==========================================================================="
dtcInstall() {
if [[ $dtc != 1 ]]; then
sudo apt install device-tree-compiler
# sudo dtc -I dtb -O dts -o /boot/sun5i-r8-chip.dts /boot/sun5i-r8-chip.dtb # decompile DTB
sudo cp /boot/sun5i-r8-chip.dtb /boot/sun5i-r8-chip.dtb.bak.$(date -d "today" +"%Y%m%d%H%M")
dtc=1
fi
}
# enable /dev/i2c-1
echo "---------------------------------------------------------------------------"
echo "By default /dev/i2c-1 (I2C on bus 1) is \"disabled\" in CHIP's DTB."
echo "Enabling i2c-1 will allow you to use the TWI1-SDA/TWI1-SCK pins on your CHIP."
echo "This process requires an update to /boot/sun5i-r8-chip.dtb"
yn=("*** Skip ***" "Update CHIP's DTB to enable /dev/i2c-1")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna || -z "$yna" ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
read -p "Enter the clock-frequency for /dev/i2c-1 between 3814-400000 (default 100000): " res
if [[ -z "$res" || ! "$res" =~ ^[0-9]*$ ]]; then
res=100000
elif [[ $res -lt 3814 ]]; then
res=3814
elif [[ $res -gt 400000 ]]; then
res=400000
fi
echo "Setting clock-frequency to $res"
dtcInstall "i2c1"
sudo fdtput -t s /boot/sun5i-r8-chip.dtb "/aliases" "i2c1" "/soc@01c00000/i2c@01c2b000"
sudo fdtput -t s /boot/sun5i-r8-chip.dtb "/soc@01c00000/i2c@01c2b000" "status" "okay"
sudo fdtput -t s /boot/sun5i-r8-chip.dtb "/soc@01c00000/i2c@01c2b000" "pinctrl-names" "default"
sudo fdtput -t x /boot/sun5i-r8-chip.dtb "/soc@01c00000/i2c@01c2b000" "pinctrl-0" "0x38" # 0x5e for older v4.4
sudo fdtput -t i /boot/sun5i-r8-chip.dtb "/soc@01c00000/i2c@01c2b000" "clock-frequency" "${res}";
rboot=1
fi
# enable pwm0
echo "---------------------------------------------------------------------------"
echo "By default /sys/class/pwm/pwmchip0 is \"disabled\" in CHIP's DTB."
echo "Enabling pwm0 will allow you to use the PWM0 pin on your CHIP."
echo "This process requires an update to /boot/sun5i-r8-chip.dtb"
yn=("*** Skip ***" "Update CHIP's DTB to enable PWM0")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna || -z "$yna" ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
dtcInstall "pwm0"
sudo fdtput -t s /boot/sun5i-r8-chip.dtb "/soc@01c00000/pwm@01c20e00" "status" "okay"
sudo fdtput -t s /boot/sun5i-r8-chip.dtb "/soc@01c00000/pwm@01c20e00" "pinctrl-names" "default"
sudo fdtput -t x /boot/sun5i-r8-chip.dtb "/soc@01c00000/pwm@01c20e00" "pinctrl-0" "0x67" # 0x63 for older v4.4
rboot=1
fi
# install node.js
echo "==========================================================================="
yn=("*** Skip ***" "Install/Update Node.js")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
echo "Detecting latest Node.js version..."
nodever=
nodevernew=
nodepth=$(which node)
nodeurl="https://nodejs.org/dist/latest/"
nodeurl=$nodeurl$(sudo curl -sS -L "$nodeurl" | grep -o -m 1 'node-v[0-9]*\.[0-9]*\.[0-9]*-linux-armv7l\.tar\.xz' | head -1)
nodeop="Install"
if [[ -n "$nodepth" ]]; then
nodever=$(node -v)
nodever=${nodever//[!0-9\.]/}
nodevernew=$(grep -o '[0-9]*\.[0-9]*\.[0-9]*' <<< $nodeurl)
nodeop="Upgrade Node.js from $nodever to"
nodepth=${nodepth%"bin/node"}
else
nodepth=/usr/local/
fi
if [[ -n "$nodevernew" && "$nodever" == "$nodevernew" ]]; then
echo "Node.js is up to date with version $nodever"
else
yn=("*** Skip ***" "$nodeop \"$nodeurl\"")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
if [[ -n "$nodepth" && -n "$nodever" ]]; then
# remove existing node and npm
sudo rm -rf $nodepth{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
fi
sudo curl -o /tmp/nodejs.tar.xz "$nodeurl"
echo "Installing Node.js to $nodepth"
sudo tar -C $nodepth --strip-components 1 -xJf /tmp/nodejs.tar.xz
echo "Installed Node.js "$(node -v)" with npm "$(npm -v)
fi
fi
fi
# generate self-signed certificate for node.js HTTPS/TLS connections
yn=("*** Skip ***" "Generate required self-signed certificate for HTTPS/TLS")
yna=
echo "==========================================================================="
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
sudo openssl req -x509 -sha256 -newkey rsa:2048 -keyout /etc/ssl/private/key.pem -out /etc/ssl/certs/cert.pem -days 18250 -nodes
echo "Generated /etc/ssl/private/key.pem and /etc/ssl/certs/cert.pem"
fi
# reboot?
echo "==========================================================================="
if [[ $rboot == 1 ]]; then
echo "CHIP needs to reboot for setup to complete..."
yn=("*** Skip ***" "Reboot CHIP")
yna=
PS3="Enter 1 or 2: "
select yna in "${yn[@]}"; do
for item in "${yn[@]}"; do
if [[ $item == $yna || -z "$yna" ]]; then
break 2
fi
done
done
if [[ $yna == "${yn[1]}" ]]; then
sudo reboot
fi
else
echo "Setup is complete and your CHIP is ready for use!"
fi
echo "==========================================================================="
exit 0 #Exit with success
@ugate

This comment has been minimized.

Copy link
Owner Author

ugate commented Aug 10, 2016

Performs the following setup changes on C.H.I.P.

  1. Change chip password (optional)
  2. Change root password (optional)
  3. Change /etc/hostname and /etc/hosts to a new name (optional)
  4. Connect to WiFi using the list of available SSIDs (optional)
  5. Update/Upgrade system using apt-get (optional)
  6. Install latest version of build-essential for C/C++ compilation (optional)
  7. Enable /dev/i2c-1 via DTB that's disabled by default as of 4.4 (optional)
  8. Enable /sys/class/pwm/pwmchip0 via DTB that's disabled by default as of 4.4 (optional)
  9. Install/Update Node.js from the latest ARMv7 binaries (optional)
  10. Generate self-signed certificate for HTTPS/TLS connections (optional)
  11. Reboot when the above changes require to do so (optional)
@ugate

This comment has been minimized.

Copy link
Owner Author

ugate commented Aug 10, 2016

PWM0 Test

Once enabled in the DTB using the PWM option in the scritpt and a reboot has completed you can test PWM0 by running the following script as sudo:

#!/bin/bash
echo "Testing LED PWM blink..."
# request device- must be done before any of the following settings can be changed
echo 0 > /sys/class/pwm/pwmchip0/export
sleep 1
# list available PWMs, the driver (if any), and its state
cat /sys/kernel/debug/pwm
# set the polarity to active HIGH (echo normal) or active LOW (echo inversed)- must be done before enabled
echo normal > /sys/class/pwm/pwmchip0/pwm0/polarity
# enable PWM channel using set- must be done before period or duty_cycle are set)
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
# set the period to 1 second (adjust as needed in nanoseconds)
echo 1000000000 > /sys/class/pwm/pwmchip0/pwm0/period
# set the duty cycle to 500 ms (adjust as needed in nanoseconds)
echo 500000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
# set the power control to auto/on/off
echo auto > /sys/class/pwm/pwmchip0/pwm0/power/control
echo "LED should be blink 11x?"
sleep 11
# disable device
echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
# free device
echo 0 > /sys/class/pwm/pwmchip0/unexport
echo "done!"
@vi

This comment has been minimized.

Copy link

vi commented Jan 19, 2018

What to do with sun4i-pwm 1c20e00.pwm: prop pinctrl-0 index 0 invalid phandle?
I tried both pinctrl-0" "0x67" and pinctrl-0" "0x63"...

Kernel version is 4.3.0-ntc

Where to get these numbers? Why are they different based on kernel version?

Update: Found out myself. It should match phandle of pwm0. If there is no phandle at all there, creating it using some new unused value works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.