Add means to control STM power-on from userland 16/189516/6
authorMichal Sidor <m.sidor@samsung.com>
Tue, 18 Sep 2018 08:21:27 +0000 (10:21 +0200)
committerAlexander Mazuruk <a.mazuruk@samsung.com>
Wed, 10 Oct 2018 07:00:43 +0000 (09:00 +0200)
This patch adds a shell script `muxpi-power` which can power the STM on
or off. The patch also contains a systemd service that runs `muxpi-power
on` during system boot. It is a fairly universal (while there's systemd,
there's a way) and convenient (compared to formerly used u-boot script
modification) way of powering STM on.

Change-Id: Ia96e5ef75bc70a5b25ef34633157939e7f11554a
Signed-off-by: Michal Sidor <m.sidor@samsung.com>
sw/nanopi/power/muxpi-power [new file with mode: 0755]
sw/nanopi/power/systemd/muxpi-power.service [new file with mode: 0644]
sw/nanopi/stm/systemd/stm.service

diff --git a/sw/nanopi/power/muxpi-power b/sw/nanopi/power/muxpi-power
new file mode 100755 (executable)
index 0000000..5054d45
--- /dev/null
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+set -e
+
+MODE_PIN=203
+MODE_NORMAL=0
+MODE_FLASHING=1
+
+POWER_PIN=3
+POWER_OFF=0
+POWER_ON=1
+
+check_gpio_sysfs() {
+  test -e /sys/class/gpio
+}
+
+export_gpio_out() {
+  GPIO_NUM=$1
+  test -d "/sys/class/gpio/gpio$GPIO_NUM" \
+    || echo "$GPIO_NUM" > /sys/class/gpio/export
+  echo out > "/sys/class/gpio/gpio$GPIO_NUM/direction"
+}
+
+prepare_gpios() {
+  export_gpio_out $MODE_PIN
+  export_gpio_out $POWER_PIN
+}
+
+gpio_set() {
+  GPIO_NUM=$1
+  GPIO_VALUE=$2
+  echo "$GPIO_VALUE" > "/sys/class/gpio/gpio$GPIO_NUM/value"
+}
+
+stm_power_off() {
+  prepare_gpios
+  gpio_set $POWER_PIN $POWER_OFF
+  gpio_set $MODE_PIN $MODE_NORMAL
+}
+
+stm_power_on() {
+  prepare_gpios
+  gpio_set $POWER_PIN $POWER_OFF
+  gpio_set $MODE_PIN $MODE_NORMAL
+  gpio_set $POWER_PIN $POWER_ON
+}
+
+stm_flash_mode() {
+  prepare_gpios
+  gpio_set $POWER_PIN $POWER_OFF
+  gpio_set $MODE_PIN $MODE_FLASHING
+  gpio_set $POWER_PIN $POWER_ON
+}
+
+SUBCOMMAND="$1"
+case "$SUBCOMMAND" in
+  "off")
+    stm_power_off
+  ;;
+  "on")
+    stm_power_on
+  ;;
+  "flashing")
+    stm_flash_mode
+  ;;
+  *)
+    echo "unknown command"
+    printf "usage: %s {off,on,flashing}\n" "$0"
+    exit 1
+  ;;
+esac
diff --git a/sw/nanopi/power/systemd/muxpi-power.service b/sw/nanopi/power/systemd/muxpi-power.service
new file mode 100644 (file)
index 0000000..139c666
--- /dev/null
@@ -0,0 +1,11 @@
+[Unit]
+Description=MuxPi microcontroller power
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/muxpi-power on
+ExecStop=/usr/bin/muxpi-power off
+RemainAfterExit=yes
+
+[Install]
+WantedBy=basic.target
index 582bdd2..ec0866b 100644 (file)
@@ -2,6 +2,7 @@
 Description=STM RPC
 Requires=stm-user.socket
 Requires=stm.socket
+Requires=muxpi-power.service
 
 [Service]
 ExecStart=/usr/bin/stm -serve