zion/scripts/SATA-hat.sh

75 lines
1.2 KiB
Bash
Raw Normal View History

2021-05-15 16:51:46 +02:00
#!/bin/sh
BASE_PATH=/sys/class
GPIO_PATH="$BASE_PATH"/gpio
PWM_PATH="$BASE_PATH"/pwm/pwmchip0
2021-05-15 16:51:46 +02:00
# GPIO pins
SATA0=26
SATA1=25
CPU_FAN=12
# Values
LOW=0
HIGH=1
export_pin() {
if [ ! -e $GPIO_PATH/gpio"$1" ]; then
echo "$1" >$GPIO_PATH/export
fi
2021-05-15 16:51:46 +02:00
}
unexport_pin() {
if [ -e $GPIO_PATH/gpio"$1" ]; then
echo "$1" >$GPIO_PATH/unexport
fi
2021-05-15 16:51:46 +02:00
}
set_gpio() {
export_pin "$1"
echo "out" >$GPIO_PATH/gpio"$1"/direction
echo "$2" >$GPIO_PATH/gpio"$1"/value
if [ "$3" = "clean" ]; then
unexport_pin "$1"
fi
2021-05-15 16:51:46 +02:00
}
enable_pwm_channel() {
echo "$1" >$PWM_PATH/export
echo 40000 >$PWM_PATH/pwm"$1"/period
echo 30000 >$PWM_PATH/pwm"$1"/duty_cycle
echo 1 >$PWM_PATH/pwm"$1"/enable
}
set_pwm() {
enable_pwm_channel 1
if [ "$1" = "clean" ]; then
echo 1 >$PWM_PATH/unexport
fi
}
2021-05-15 16:51:46 +02:00
turn_on() {
set_gpio $SATA0 $HIGH
sleep 1
set_gpio $SATA1 $HIGH
set_gpio $CPU_FAN $HIGH
set_pwm
2021-05-15 16:51:46 +02:00
}
turn_off() {
set_gpio $SATA0 $LOW clean
set_gpio $SATA1 $LOW clean
set_gpio $CPU_FAN $LOW clean
set_pwm clean
2021-05-15 16:51:46 +02:00
}
trap turn_off INT
if [ "$1" = "on" ]; then
turn_on
2021-05-15 16:51:46 +02:00
else
turn_off
2021-05-15 16:51:46 +02:00
fi
exit 0