Browse Source

Initialize the SATA HAT on startup

master
coolneng 2 months ago
parent
commit
57f5952f8a
Signed by: coolneng GPG Key ID: 9893DA236405AF57
  1. 2
      configuration.nix
  2. 17
      modules/periodic.nix
  3. 56
      scripts/SATA-hat.sh

2
configuration.nix

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

17
modules/periodic.nix

@ -28,7 +28,22 @@ in {
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

@ -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
Loading…
Cancel
Save