From 2f52fa27e5e119dd8d4490e82221341e0ff58364 Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Fri, 16 May 2025 18:31:28 +0200 Subject: [PATCH] #264 gpio wfis & unit test --- gpio/gpio.c | 39 ++++--- .../{gpio_test7_wfi.sh => gpio_test6_wfi.sh} | 100 +++++++++++++++++- 2 files changed, 126 insertions(+), 13 deletions(-) rename gpio/test/{gpio_test7_wfi.sh => gpio_test6_wfi.sh} (52%) diff --git a/gpio/gpio.c b/gpio/gpio.c index 9fa5bf6..2f6b683 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -181,7 +181,7 @@ static void wfi (void) { globalCounter++; if(globalCounter>=globalIterations) { printgpio("finished\n"); - exit (0) ; + exit(0); } else { printgpioflush("I"); } @@ -193,15 +193,25 @@ static void wfi2(struct WPIWfiStatus wfiStatus, void* userdata) { (void)userdata; globalCounter++; if (globalCounter>=globalIterations) { - printgpio("finished\n"); - exit(0); + switch(wfiStatus.edge) { + case INT_EDGE_FALLING: + printgpio("finished falling\n"); + break; + case INT_EDGE_RISING: + printgpio("finished rising\n"); + break; + default: + printgpio("finished\n"); + break; + } + exit(wfiStatus.edge); } else { printgpioflush("I"); } } -int get_wfi_edge(const char* arg_cmd, const char* arg_mode) { +int get_wfi_edge(const char* arg_cmd, const char* arg_mode, int exitcode) { if (strcasecmp (arg_mode, "rising") == 0) { return INT_EDGE_RISING ; } else if (strcasecmp (arg_mode, "falling") == 0) { @@ -210,7 +220,7 @@ int get_wfi_edge(const char* arg_cmd, const char* arg_mode) { return INT_EDGE_BOTH ; } else { fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", arg_cmd, arg_mode) ; - exit (1); + exit(exitcode); } } @@ -236,7 +246,7 @@ void doWfiInternal(const char* cmd, int pin, int mode, int interations, int time printgpio("wait for interrupt function call\n"); for (int Sec=0; Sec=5) { interations = atoi(argv[4]); } @@ -272,12 +282,12 @@ void doWfi2(int argc, char *argv []) int timeoutSec = 2147483647; if (argc != 4 && argc != 5 && argc != 6 && argc != 7) { - fprintf (stderr, "Usage: %s wfidb pin mode [debounce period microsec.] [interations] [timeout sec.]\n", argv [0]) ; - exit(1); + fprintf (stderr, "Usage: %s wfis pin mode [debounce period microsec.] [interations] [timeout sec.]\n", argv [0]); + exit(-2); } pin = atoi (argv[2]) ; - mode = get_wfi_edge(argv[1], argv[3]); + mode = get_wfi_edge(argv[1], argv[3], -1); if (argc>=5) { debounce = atoi(argv[4]); } @@ -287,8 +297,13 @@ void doWfi2(int argc, char *argv []) if (argc>=7) { timeoutSec = atoi(argv[6]); } + if (timeoutSec<0 || interations<0 || debounce<0) { + fprintf (stderr, " invalid parameter\n"); + exit(-2); + } doWfiInternal(argv[1], pin, mode, interations, timeoutSec, debounce); + exit(-1); // timeout } @@ -1193,7 +1208,7 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "rbx" ) == 0) doReadByte (argc, argv, TRUE) ; else if (strcasecmp (argv [1], "rbd" ) == 0) doReadByte (argc, argv, FALSE) ; else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ; - else if (strcasecmp (argv [1], "wfidb" ) == 0) doWfi2 (argc, argv) ; + else if (strcasecmp (argv [1], "wfis" ) == 0) doWfi2 (argc, argv) ; else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ; else if (strcasecmp (argv [1], "is40pin" ) == 0) doIs40Pin () ; else diff --git a/gpio/test/gpio_test7_wfi.sh b/gpio/test/gpio_test6_wfi.sh similarity index 52% rename from gpio/test/gpio_test7_wfi.sh rename to gpio/test/gpio_test6_wfi.sh index a5d2298..89f37e0 100755 --- a/gpio/test/gpio_test7_wfi.sh +++ b/gpio/test/gpio_test6_wfi.sh @@ -9,6 +9,7 @@ TIMEOUT=8 iteration=0 GPIOIN=26 GPIOOUT=19 +DEBOUNCE=100000 wait_with_timeout() { local pid=$1 @@ -27,6 +28,7 @@ wait_with_timeout() { toggle=$((1 - toggle)) gpio -g write $GPIOOUT $toggle echo -n ${toggle} + sleep 0.2 iteration=$((iteration + 1)) done @@ -39,7 +41,7 @@ wfi_test() { local set_iter=$2 local set_iterOK=$3 - #wfi test + #wfi call if [ $iteration -eq 0 ]; then gpio -g wfi $GPIOIN "$edge" & else @@ -66,6 +68,48 @@ wfi_test() { fi } +wfis_test() { + local edge=$1 + local set_iter=$2 + local set_iterOK=$3 + local set_EXITCODE=$4 + + #wfis call + if [ $iteration -eq 0 ]; then + gpio -g wfis $GPIOIN "$edge" $DEBOUNCE & + else + gpio -g wfis $GPIOIN "$edge" $DEBOUNCE "$set_iter" & + fi + GPIO_PID=$! + wait_with_timeout "$GPIO_PID" "$TIMEOUT" + EXIT_CODE=$? + echo + if [ $EXIT_CODE -lt 3 ]; then + + if [ $EXIT_CODE -eq "$set_EXITCODE" ]; then + + if [ "$set_iter" -gt 0 ]; then + if [ "$iteration" -eq "$set_iterOK" ]; then + echo -e "${GREEN}wfis "$edge" $set_iter iteration passt (exit code $EXIT_CODE)${NC}" + else + echo -e "${RED}wfis "$edge" failed, $iteration iterations of $set_iter is wrong${NC}" + UNITTEST_OK=false + fi + else + echo -e "${GREEN}wfi "$edge" passt (exit code $EXIT_CODE)${NC}" + fi + + else + echo -e "${RED}wfis "$edge" failed - wrong exit code $EXIT_CODE${NC}" + UNITTEST_OK=false + fi + + else # negativ + echo -e "${RED}wfis "$edge" failed (exit code $EXIT_CODE)${NC}" + UNITTEST_OK=false + fi +} + echo echo Unit test gpio GPIO${GPIOOUT} and GPIO${GPIOIN} - functions: mode, write, wfi echo ------------------------------------------------------------------ @@ -108,6 +152,60 @@ else fi +echo +echo Unit test gpio GPIO${GPIOOUT} and GPIO${GPIOIN} - functions: mode, write, wfis +echo ------------------------------------------------------------------ +echo + +#prepare trigger out +gpio -g mode $GPIOOUT out + +gpio -g write $GPIOOUT 1 +wfis_test "rising" 0 0 2 + +gpio -g write $GPIOOUT 0 +wfis_test "falling" 0 0 1 + +gpio -g write $GPIOOUT 1 +wfis_test "both" 0 0 1 + +gpio -g write $GPIOOUT 0 +wfis_test "both" 0 0 2 + + +#wfi iteration test + +gpio -g write $GPIOOUT 0 +wfis_test "rising" 4 7 2 + +gpio -g write $GPIOOUT 1 +wfis_test "falling" 4 8 1 + +gpio -g write $GPIOOUT 0 +wfis_test "both" 3 3 2 + +gpio -g write $GPIOOUT 1 +wfis_test "both" 3 4 1 + +gpio -g write $GPIOOUT 0 +gpio -g mode $GPIOOUT in + +### #wfi timeout test +gpio -g wfis $GPIOIN rising $DEBOUNCE 2 4 & +GPIO_PID=$! +wait_with_timeout "$GPIO_PID" "$TIMEOUT" +EXIT_CODE=$? +echo +if [ $EXIT_CODE -eq 255 ]; then + echo -e "${GREEN}wfi timeout passed (exit code $EXIT_CODE)${NC}" +else + echo -e "${RED}wfi timeout failed (code $EXIT_CODE)${NC}" + UNITTEST_OK=false +fi + + + + if [ ${UNITTEST_OK} = true ]; then echo -e "\n\n${GREEN}Unit test result OK.${NC}" exit 0