From 4501fcbd6584fb49efdcf1f999951ce9a14fa8fa Mon Sep 17 00:00:00 2001 From: ZHANG Yuntian <95260730+RadxaYuntian@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:38:09 +0800 Subject: [PATCH] hardware: add GPIO test functions --- src/usr/lib/rsetup/tui/hardware/gpio.sh | 114 ++++++++++++++++++++ src/usr/lib/rsetup/tui/hardware/hardware.sh | 4 + 2 files changed, 118 insertions(+) create mode 100644 src/usr/lib/rsetup/tui/hardware/gpio.sh diff --git a/src/usr/lib/rsetup/tui/hardware/gpio.sh b/src/usr/lib/rsetup/tui/hardware/gpio.sh new file mode 100644 index 00000000..eb23484e --- /dev/null +++ b/src/usr/lib/rsetup/tui/hardware/gpio.sh @@ -0,0 +1,114 @@ +# shellcheck shell=bash + +__hardware_gpio_fmt() { + local i="$1" + if (( i < 4 )) + then + # shellcheck disable=SC2028 + echo "%s %s %s %s\\n" + elif (( i == 4 )) + then + # shellcheck disable=SC2028 + echo "%s %s %s %s\\n" + else + # shellcheck disable=SC2028 + echo "%s %s %s %s\\n" + fi +} + +__hardware_gpio_set() { + local i level="$1" gpio=() states=() + + for i in {1..40} + do + if gpiofind "PIN_$i" >/dev/null + then + read -ra gpio < <(gpiofind "PIN_$i") + if gpioset "${gpio[0]}" "${gpio[1]}=$level" + then + gpioset -m signal "${gpio[0]}" "${gpio[1]}=$level" & + states+=("$level") + else + states+=("E") + fi + else + states+=(" ") + fi + done + + msgbox "Following GPIO pins have their state changed temporarily: + +0: Low | 1: High | E: Error + +State Pin State +$(for i in {0..19} +do + # shellcheck disable=SC2059 + printf "$(__hardware_gpio_fmt "$i")" \ + "${states[i * 2]}" "$(( i * 2 + 1 ))" \ + "$(( i * 2 + 2 ))" "${states[i * 2 + 1]}" +done)" #" # Workaround VS Code incorrect code highlighting + + jobs -r -p | xargs -I{} kill -- {} +} + +__hardware_gpio_set_high() { + __hardware_gpio_set 1 +} + +__hardware_gpio_set_low() { + __hardware_gpio_set 0 +} + +__hardware_gpio_get() { + local i level gpio=() states=() + + for i in {1..40} + do + if gpiofind "PIN_$i" >/dev/null + then + read -ra gpio < <(gpiofind "PIN_$i") + if level="$(gpioget "${gpio[@]}" 2>/dev/null)" + then + states+=("$level") + else + states+=("E") + fi + else + states+=(" ") + fi + done + + msgbox "Following is the current reading of all supported GPIO pins: + +0: Low | 1: High | E: Error + +State Pin State +$(for i in {0..19} +do + # shellcheck disable=SC2059 + printf "$(__hardware_gpio_fmt "$i")" \ + "${states[i * 2]}" "$(( i * 2 + 1 ))" \ + "$(( i * 2 + 2 ))" "${states[i * 2 + 1]}" +done)" #" # Workaround VS Code incorrect code highlighting +} + +__hardware_gpio() { + if loginctl | grep -e ttyS -e ttyAML -e ttyFIQ >/dev/null && \ + ! yesno "A serial session has been detected. + +Testing GPIO with provided functions may cause the serial console to malfunction. +It will only return to normal working condition after a system reboot. + +Do you want to continue? +" + then + return + fi + + menu_init + menu_add __hardware_gpio_set_high "Set all GPIO to High" + menu_add __hardware_gpio_set_low "Set all GPIO to Low" + menu_add __hardware_gpio_get "Get all GPIO state" + menu_show "Please select the test case:" +} diff --git a/src/usr/lib/rsetup/tui/hardware/hardware.sh b/src/usr/lib/rsetup/tui/hardware/hardware.sh index 502231d3..9545934a 100644 --- a/src/usr/lib/rsetup/tui/hardware/hardware.sh +++ b/src/usr/lib/rsetup/tui/hardware/hardware.sh @@ -3,6 +3,9 @@ # shellcheck source=src/usr/lib/rsetup/mod/config.sh source "/usr/lib/rsetup/mod/config.sh" +# shellcheck source=src/usr/lib/rsetup/tui/hardware/gpio.sh +source "/usr/lib/rsetup/tui/hardware/gpio.sh" + __hardware_gstreamer_test_picture() { local temp temp="$(mktemp tmp.XXXXXXXXXX.jpg)" @@ -240,6 +243,7 @@ __hardware() { menu_add __hardware_gpio_leds "GPIO LEDs" menu_add __hardware_thermal "Thermal governor" menu_add __hardware_dsi_mirror "Configure DSI display mirroring" + menu_add __hardware_gpio "40-pin GPIO" if $DEBUG then menu_add __hardware_rgb_leds "RGB LEDs"