Initialize the SATA HAT on startup

This commit is contained in:
coolneng 2021-05-15 16:51:46 +02:00
parent 8392ac213b
commit 57f5952f8a
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
3 changed files with 74 additions and 1 deletions

View File

@ -9,6 +9,8 @@
"console=TTYAMA0,115200" "console=TTYAMA0,115200"
"console=tty1" "console=tty1"
"8250.nr_uarts=1" "8250.nr_uarts=1"
"iomem=relaxed"
"strict-devmem=0"
]; ];
boot.loader.raspberryPi = { boot.loader.raspberryPi = {
enable = true; enable = true;

View File

@ -28,7 +28,22 @@ in {
startAt = "02:00:00"; startAt = "02:00:00";
}; };
wantedBy = [ "default.target" ]; # Enable SATA HAT
systemd.services.sata-hat = {
description = "Enable software support for SATA Hat";
wantedBy = [ "zfs-import.target" ];
script = ''
${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh on"
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
ExecStop = ''
${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh off"
'';
}; };
before = [ "zfs-import.target" "zfs-import-vault.service" ];
requires = [ "systemd-udev-settle.service" ];
after = [ "systemd-udev-settle.service" ];
}; };
} }

56
scripts/SATA-hat.sh Executable file
View File

@ -0,0 +1,56 @@
#!/bin/sh
BASE_PATH=/sys/class/gpio
# GPIO pins
SATA0=26
SATA1=25
CPU_FAN=12
# Values
LOW=0
HIGH=1
export_pin() {
if [ ! -e $BASE_PATH/gpio"$1" ]; then
echo "$1" >$BASE_PATH/export
fi
}
unexport_pin() {
if [ -e $BASE_PATH/gpio"$1" ]; then
echo "$1" >$BASE_PATH/unexport
fi
}
set_mode() {
export_pin "$1"
echo "out" >$BASE_PATH/gpio"$1"/direction
echo "$2" >$BASE_PATH/gpio"$1"/value
if [ "$3" = "clean" ]; then
unexport_pin "$1"
fi
}
turn_on() {
set_mode $SATA0 $HIGH
sleep 1
set_mode $SATA1 $HIGH
set_mode $CPU_FAN $LOW
}
turn_off() {
set_mode $SATA0 $LOW clean
set_mode $SATA1 $LOW clean
set_mode $CPU_FAN $LOW clean
}
trap turn_off INT
if [ "$1" = "on" ]; then
turn_on
else
turn_off
fi
exit 0