]> git.feebdaed.xyz Git - 0xmirror/WiringPi.git/commitdiff
#264 gpio wfis & unit test
authormstroh76 <m.stroh76@gmail.com>
Fri, 16 May 2025 16:31:28 +0000 (18:31 +0200)
committermstroh76 <m.stroh76@gmail.com>
Fri, 16 May 2025 16:31:28 +0000 (18:31 +0200)
gpio/gpio.c
gpio/test/gpio_test6_wfi.sh [new file with mode: 0755]
gpio/test/gpio_test7_wfi.sh [deleted file]

index 9fa5bf6a7bd5fea56de48222651d73e226a2405b..2f6b683977a1193db6cb9c6b4b95782bd3e82a52 100644 (file)
@@ -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<timeoutSec; ++Sec) {
     printgpioflush(".");
-    delay (999);
+    delay(999);
   }
   printgpio("\nstopping wait for interrupt\n");
   wiringPiISRStop(pin);
@@ -250,11 +260,11 @@ void doWfi(int argc, char *argv [])
 
   if (argc != 4 && argc != 5 && argc != 6) {
     fprintf (stderr, "Usage: %s wfi pin mode [interations] [timeout sec.]\n", argv [0]) ;
-    exit (1) ;
+    exit(1);
   }
 
   pin  = atoi (argv[2]) ;
-  mode = get_wfi_edge(argv[1], argv[3]);
+  mode = get_wfi_edge(argv[1], argv[3], 1);
   if (argc>=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_test6_wfi.sh b/gpio/test/gpio_test6_wfi.sh
new file mode 100755 (executable)
index 0000000..89f37e0
--- /dev/null
@@ -0,0 +1,215 @@
+#!/bin/bash
+
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+NC='\033[0m' # Reset color 
+
+UNITTEST_OK=true
+TIMEOUT=8
+iteration=0
+GPIOIN=26
+GPIOOUT=19
+DEBOUNCE=100000
+
+wait_with_timeout() {
+    local pid=$1
+    local timeout=$2
+    iteration=0
+
+    local toggle=0
+    while kill -0 "$pid" 2>/dev/null; do
+        if [ $iteration -ge $timeout ]; then
+            kill -9 "$pid" 2>/dev/null
+            echo -e "${RED}ERROR: timeout ${timeout}s reached ${NC}"
+            return -1
+        fi
+        echo -n .
+        sleep 1
+        toggle=$((1 - toggle))
+        gpio -g write $GPIOOUT $toggle
+        echo -n ${toggle}
+        sleep 0.2
+        iteration=$((iteration + 1))
+    done
+
+    wait "$pid"
+    return $?
+}
+
+wfi_test() {
+    local edge=$1
+    local set_iter=$2
+    local set_iterOK=$3
+
+    #wfi call
+    if [ $iteration -eq 0 ]; then
+        gpio -g wfi $GPIOIN "$edge" &
+    else
+        gpio -g wfi $GPIOIN "$edge" "$set_iter" &
+    fi
+    GPIO_PID=$!
+    wait_with_timeout "$GPIO_PID" "$TIMEOUT"
+    EXIT_CODE=$?
+    echo 
+    if [ $EXIT_CODE -eq 0 ]; then
+        if [ "$set_iter" -gt 0 ]; then
+            if [ "$iteration" -eq "$set_iterOK" ]; then
+                echo -e "${GREEN}wfi "$edge" $set_iter iteration passt (exit code 0)${NC}"
+            else
+                echo -e "${RED}wfi "$edge" failed, $iteration iterations of $set_iter is wrong${NC}"
+                UNITTEST_OK=false
+            fi
+        else
+            echo -e "${GREEN}wfi "$edge" passt (exit code 0)${NC}"
+        fi
+    else
+        echo -e "${RED}wfi "$edge" failed (exit code $EXIT_CODE)${NC}"
+        UNITTEST_OK=false
+    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 ------------------------------------------------------------------
+echo 
+
+#prepare trigger out
+gpio -g mode $GPIOOUT out
+
+gpio -g write $GPIOOUT 0
+wfi_test "rising" 0 0
+
+gpio -g write $GPIOOUT 1
+wfi_test "falling" 0 0
+
+#wfi iteration test
+
+gpio -g write $GPIOOUT 0
+wfi_test "rising" 4 7
+
+gpio -g write $GPIOOUT 1
+wfi_test "falling" 4 8
+
+gpio -g write $GPIOOUT 0
+wfi_test "both" 4 4
+
+gpio -g write $GPIOOUT 0
+gpio -g mode $GPIOOUT in
+
+### #wfi timeout test
+gpio -g wfi $GPIOIN rising 2 4 &
+GPIO_PID=$!
+wait_with_timeout "$GPIO_PID" "$TIMEOUT"
+EXIT_CODE=$?
+echo 
+if [ $EXIT_CODE -eq 0 ]; then
+    echo -e "${GREEN}wfi timeout passed (exit code 0)${NC}"
+else
+    echo -e "${RED}wfi timeout failed (code $EXIT_CODE)${NC}"
+    UNITTEST_OK=false
+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
+else
+    echo -e "\n\n${RED}Unit test result failed.${NC}"
+    exit 1
+fi
\ No newline at end of file
diff --git a/gpio/test/gpio_test7_wfi.sh b/gpio/test/gpio_test7_wfi.sh
deleted file mode 100755 (executable)
index a5d2298..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-#!/bin/bash
-
-RED='\033[0;31m'
-GREEN='\033[0;32m'
-NC='\033[0m' # Reset color 
-
-UNITTEST_OK=true
-TIMEOUT=8
-iteration=0
-GPIOIN=26
-GPIOOUT=19
-
-wait_with_timeout() {
-    local pid=$1
-    local timeout=$2
-    iteration=0
-
-    local toggle=0
-    while kill -0 "$pid" 2>/dev/null; do
-        if [ $iteration -ge $timeout ]; then
-            kill -9 "$pid" 2>/dev/null
-            echo -e "${RED}ERROR: timeout ${timeout}s reached ${NC}"
-            return -1
-        fi
-        echo -n .
-        sleep 1
-        toggle=$((1 - toggle))
-        gpio -g write $GPIOOUT $toggle
-        echo -n ${toggle}
-        iteration=$((iteration + 1))
-    done
-
-    wait "$pid"
-    return $?
-}
-
-wfi_test() {
-    local edge=$1
-    local set_iter=$2
-    local set_iterOK=$3
-
-    #wfi test
-    if [ $iteration -eq 0 ]; then
-        gpio -g wfi $GPIOIN "$edge" &
-    else
-        gpio -g wfi $GPIOIN "$edge" "$set_iter" &
-    fi
-    GPIO_PID=$!
-    wait_with_timeout "$GPIO_PID" "$TIMEOUT"
-    EXIT_CODE=$?
-    echo 
-    if [ $EXIT_CODE -eq 0 ]; then
-        if [ "$set_iter" -gt 0 ]; then
-            if [ "$iteration" -eq "$set_iterOK" ]; then
-                echo -e "${GREEN}wfi "$edge" $set_iter iteration passt (exit code 0)${NC}"
-            else
-                echo -e "${RED}wfi "$edge" failed, $iteration iterations of $set_iter is wrong${NC}"
-                UNITTEST_OK=false
-            fi
-        else
-            echo -e "${GREEN}wfi "$edge" passt (exit code 0)${NC}"
-        fi
-    else
-        echo -e "${RED}wfi "$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 ------------------------------------------------------------------
-echo 
-
-#prepare trigger out
-gpio -g mode $GPIOOUT out
-
-gpio -g write $GPIOOUT 0
-wfi_test "rising" 0 0
-
-gpio -g write $GPIOOUT 1
-wfi_test "falling" 0 0
-
-#wfi iteration test
-
-gpio -g write $GPIOOUT 0
-wfi_test "rising" 4 7
-
-gpio -g write $GPIOOUT 1
-wfi_test "falling" 4 8
-
-gpio -g write $GPIOOUT 0
-wfi_test "both" 4 4
-
-gpio -g write $GPIOOUT 0
-gpio -g mode $GPIOOUT in
-
-### #wfi timeout test
-gpio -g wfi $GPIOIN rising 2 4 &
-GPIO_PID=$!
-wait_with_timeout "$GPIO_PID" "$TIMEOUT"
-EXIT_CODE=$?
-echo 
-if [ $EXIT_CODE -eq 0 ]; then
-    echo -e "${GREEN}wfi timeout passed (exit code 0)${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
-else
-    echo -e "\n\n${RED}Unit test result failed.${NC}"
-    exit 1
-fi
\ No newline at end of file