#264 gpio wfis & unit test

This commit is contained in:
mstroh76
2025-05-16 18:31:28 +02:00
parent b1b896582e
commit 2f52fa27e5
2 changed files with 126 additions and 13 deletions

View 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) {
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");
exit(0);
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

View File

@@ -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