diff --git a/tests/xtimer_usleep/Makefile b/tests/xtimer_usleep/Makefile index c148cf411904e..8ec9c6fc345eb 100644 --- a/tests/xtimer_usleep/Makefile +++ b/tests/xtimer_usleep/Makefile @@ -2,6 +2,11 @@ include ../Makefile.tests_common USEMODULE += xtimer +# Port and pin configuration for probing with oscilloscope +# Port number should be found in port enum e.g in cpu/include/periph_cpu.h +#CFLAGS += -DSLEEP_PIN=7 +#CFLAGS += -DSLEEP_PORT=5 + include $(RIOTBASE)/Makefile.include test: diff --git a/tests/xtimer_usleep/README.md b/tests/xtimer_usleep/README.md index 2d66938673e4b..9a2102deb5db2 100644 --- a/tests/xtimer_usleep/README.md +++ b/tests/xtimer_usleep/README.md @@ -1,12 +1,76 @@ # xtimer_usleep test application -This test tests `xtimer_usleep()` both against the timings of `xtimer_now_usec()` - and by providing capabilities to compare against an external timer. +This test tests `xtimer_usleep()` against the timings of `xtimer_now_usec()` + and by comparing the incoming strings with the test hosts time. -The sleep times can be probed at a pin if SLEEP_PIN` is set to 1 and the respective -gpio pin is define as `SLEEP_GPIO_PIN`. +The sleep times can be probed with a oscilloscope at a pin if `SLEEP_PIN` is set +to and the respective gpio `SLEEP_PORT` is defined in the makefile. + +CFLAGS += SLEEP_PIN=7 +CFLAGS += SLEEP_PORT=PORTF ## Usage +Executed from the project directory +``` +make BOARD= flash test test +``` + +### Expected result running +``` +018-04-03 18:00:06,825 - INFO # Connect to serial port /dev/ttyACM0 +Welcome to pyterm! +Type '/exit' to exit. +2018-04-03 18:00:07,828 - INFO # n: XXX +2018-04-03 18:00:07,829 - INFO # Running test 5 times with + +2018-04-03 18:00:07,830 - INFO # main(): This is RIOT! (Version: XXX ) +2018-04-03 18:00:07,830 - INFO # Running test 5 times with 3 distinct sleep times +2018-04-03 18:00:07,831 - INFO # Please hit any key and then ENTER to continue +a +a +2018-04-03 18:00:09,429 - INFO # Slept for 10224 us (expected: 10000 us) +2018-04-03 18:00:09,489 - INFO # Slept for 50224 us (expected: 50000 us) +2018-04-03 18:00:09,600 - INFO # Slept for 100224 us (expected: 100000 us) +2018-04-03 18:00:09,620 - INFO # Slept for 10224 us (expected: 10000 us) +2018-04-03 18:00:09,680 - INFO # Slept for 50224 us (expected: 50000 us) +2018-04-03 18:00:09,791 - INFO # Slept for 100224 us (expected: 100000 us) +2018-04-03 18:00:09,811 - INFO # Slept for 10224 us (expected: 10000 us) +2018-04-03 18:00:09,872 - INFO # Slept for 50224 us (expected: 50000 us) +2018-04-03 18:00:09,982 - INFO # Slept for 100224 us (expected: 100000 us) +2018-04-03 18:00:10,003 - INFO # Slept for 10224 us (expected: 10000 us) +2018-04-03 18:00:10,063 - INFO # Slept for 50224 us (expected: 50000 us) +2018-04-03 18:00:10,173 - INFO # Slept for 100224 us (expected: 100000 us) +2018-04-03 18:00:10,194 - INFO # Slept for 10224 us (expected: 10000 us) +2018-04-03 18:00:10,254 - INFO # Slept for 50224 us (expected: 50000 us) +2018-04-03 18:00:10,365 - INFO # Slept for 100224 us (expected: 100000 us) +2018-04-03 18:00:10,371 - INFO # Test ran for 955136 us + +``` + +### On Error with pyterm ``` -make flash test +2018-04-03 18:11:48,952 - INFO # Connect to serial port /dev/ttyACM0 +Welcome to pyterm! +Type '/exit' to exit. +2018-04-03 18:11:49,957 - INFO # (Version: XXX ) +2018-04-03 19:12:20,747 - INFO # Running test 5 times with 7 distinct sleep times +2018-04-03 19:12:20,747 - INFO # Please hit any key and then ENTER to continue +2018-04-03 19:12:20,747 - INFO # +a +a +2018-04-03 19:12:22,411 - INFO # +2018-04-03 19:12:22,421 - INFO # Slept for 10224 us (expected: 10000 us) +2018-04-03 19:12:22,427 - INFO # MCU max error 70 +2018-04-03 19:12:22,480 - INFO # error 224 +2018-04-03 19:12:22,490 - INFO # Slept for 50232 us (expected: 50000 us) +2018-04-03 19:12:22,495 - INFO # MCU max error 70 +2018-04-03 19:12:22,500 - INFO # error 232 +2018-04-03 19:12:22,510 - INFO # Slept for 1456 us (expected: 1234 us) +Invalid timeout 1456 ,expected 1172 < 1234 < 1295 +HOST max error 61 +error 283 +``` +### On Error with terminal +``` + ``` diff --git a/tests/xtimer_usleep/main.c b/tests/xtimer_usleep/main.c index 4de873d5a1982..2b54113ab7252 100644 --- a/tests/xtimer_usleep/main.c +++ b/tests/xtimer_usleep/main.c @@ -33,60 +33,57 @@ static const uint32_t sleep_times[] = { 10000, 50000, 1234, 5678, 121212, 98765, 75000}; -#define ERROR_US 500 +#define ERROR_US 70 /* * To use a pin to probe the sleep times enable the gpio module and define * respective SLEEP_GPIO_PIN and set SLEEP_PIN to (1) * */ -#define SLEEP_PIN (0) -#if SLEEP_PIN +#ifdef SLEEP_PIN #include "board.h" #include "periph/gpio.h" -#define SLEEP_GPIO_PIN GPIO_PIN(PORT_F, 7) +#define SLEEP_GPIO_PIN GPIO_PIN(SLEEP_PORT, SLEEP_PIN) #endif int main(void) { uint32_t start_test, testtime; - uint32_t start_sleep; - uint32_t diff; - int32_t err; -#if SLEEP_PIN - gpio_init( SLEEP_GPIO_PIN, GPIO_OUT ); +#ifdef SLEEP_PIN + gpio_init(SLEEP_GPIO_PIN, GPIO_OUT); #endif printf("Running test %u times with %u distinct sleep times\n", RUNS, (unsigned)SLEEP_TIMES_NUMOF); - puts("Please hit any key and then ENTER to continue\n"); - // getchar(); + puts("Please hit any key and then ENTER to continue"); + getchar(); start_test = xtimer_now_usec(); for (unsigned m = 0; m < RUNS; m++) { for (unsigned n = 0; n < sizeof(sleep_times) / sizeof(sleep_times[0]); n++) { + uint32_t start_sleep, diff; + start_sleep = xtimer_now_usec(); -#if SLEEP_PIN +#ifdef SLEEP_PIN gpio_set(SLEEP_GPIO_PIN); #endif xtimer_usleep(sleep_times[n]); -#if SLEEP_PIN +#ifdef SLEEP_PIN gpio_clear(SLEEP_GPIO_PIN); #endif diff = xtimer_now_usec() - start_sleep; - err = (diff - sleep_times[n]); - - if( err < 0 ){ - err *= -1; - } - if( err > ERROR_US ){ - printf("\n\t\tSlept for %" PRIu32 " us (expected: %" PRIu32 " us)" - "error: %" PRIi32 " us\n",diff, sleep_times[n], err); + if(sleep_times[n] < diff - ERROR_US + ||sleep_times[n] > diff + ERROR_US ){ + int32_t err = (diff - sleep_times[n]); + printf("\nSlept for %" PRIu32 " us (expected: %" PRIu32 " us)", + diff, sleep_times[n]); + printf("\nMCU max error\t%5" PRIi32 "\nerror\t\t%5" PRIi32 "", + (uint32_t)ERROR_US, err); }else{ printf("Slept for %" PRIu32 " us (expected: %" PRIu32 " us)\n", diff, sleep_times[n]); @@ -94,9 +91,7 @@ int main(void) } } testtime = xtimer_now_usec() - start_test; - printf("\nTest ran for %" PRIu32 " us\n", testtime); + printf("Test ran for %" PRIu32 " us\n", testtime); - /* atmega mcu will hang if main ends */ - while(1){} return 0; } diff --git a/tests/xtimer_usleep/tests/01-run.py b/tests/xtimer_usleep/tests/01-run.py index 1cc3f1757756e..564f3adf12316 100755 --- a/tests/xtimer_usleep/tests/01-run.py +++ b/tests/xtimer_usleep/tests/01-run.py @@ -38,7 +38,10 @@ def testfunc(child): lower_bound = exp - (exp * INTERNAL_JITTER) upper_bound = exp + (exp * INTERNAL_JITTER) if not (lower_bound < sleep_time < upper_bound): - raise InvalidTimeout("Invalid timeout %d (expected %d)" % (sleep_time, exp)) + delta = (upper_bound-lower_bound)/2 + raise InvalidTimeout("Invalid timeout %d ,expected %d < %d < %d" + "\nHOST max error\t%d\nerror\t\t%d"%(sleep_time,lower_bound, exp + ,upper_bound, delta, sleep_time-lower_bound)) testtime = (time.time() - start_test) * US_PER_SEC child.expect(u"Test ran for (\\d+) us") exp = int(child.match.group(1)) @@ -51,7 +54,6 @@ def testfunc(child): print(e) sys.exit(1) - if __name__ == "__main__": sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner')) from testrunner import run