Skip to content
Closed
50 changes: 45 additions & 5 deletions tests/xtimer_usleep_short/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017 HAW-Hamburg
* 2018 Josua Arndt
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand All @@ -14,6 +15,7 @@
* @brief xtimer_usleep_short test application
*
* @author Michel Rottleuthner <michel.rottleuthner@haw-hamburg.de>
* @author Josua Arndt <jarndt@ias.rwth-aachen.de>
* @}
*/
#include <stdio.h>
Expand All @@ -22,18 +24,56 @@
#define TEST_USLEEP_MIN (0)
#define TEST_USLEEP_MAX (500)

#ifdef BOARD_NATIVE
/* native can sometime take more time to respond as it is not real time */
#define TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US (1000)
#else
#define TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US (20)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The margin may not be enough as mentioned in #8990.
I need to do the xtimer_configure to check if it helps getting in the margin.

#endif /* BOARD_NATIVE */

int main(void)
{
xtimer_sleep(3);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This xtimer_sleep(3) break my tests when doing make flash test on some boards as the test could match the child.expect(u"This test will call xtimer_usleep for values from (\d+) down to (\d+)") line before make reset is done.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions on how to fix this? Increase the sleep?

printf("This test will call xtimer_usleep for values from %d down to %d\n",
TEST_USLEEP_MAX, TEST_USLEEP_MIN);
printf("This test will call xtimer_usleep for values from %d us down to %d us.\n",
TEST_USLEEP_MAX, TEST_USLEEP_MIN);

printf("Expected delay margin is %d us.\n",
TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US);

uint32_t test_time = 0, sleeping_time = 0, margin_fault = 0;

for (int i = TEST_USLEEP_MAX; i >= TEST_USLEEP_MIN; i--) {
printf("going to sleep %d usecs...\n", i);

uint32_t start = xtimer_now_usec();
xtimer_usleep(i);
uint32_t slept = xtimer_now_usec() - start;

if (slept < (unsigned int)i ||
slept > (unsigned int)i + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US) {
printf("ERROR: Slept for %" PRIu32 " us not in range [%d, %d] us.\n",
slept, i, i + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US);
++margin_fault;
}
else {
printf("OK: Slept for %" PRIu32 " us not in range [%d, %d] us.\n",
slept, i, i + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US);
}

sleeping_time += slept;
test_time += i;
}

puts("[SUCCESS]");
printf("Slept for %" PRIu32 " us expected %" PRIu32 " us.\n",
sleeping_time, test_time);

return 0;
if (margin_fault != 0) {
printf("Sleep delay margin was not kept for %" PRIu32 " times.\n",
margin_fault);
puts("[FAILED]");
return 1;
}
else {
puts("[SUCCESS]");
return 0;
}
}
29 changes: 19 additions & 10 deletions tests/xtimer_usleep_short/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@


def testfunc(child):
child.expect(u"This test will call xtimer_usleep for values from \\d+ down to \\d+\r\n")
child.expect("This test will call xtimer_usleep for values from (\d+) us down to (\d+) us.")
sleep_max = int(child.match.group(1))
sleep_min = int(child.match.group(2))
delay = sleep_max - sleep_min

i = 500
child.expect("Expected delay margin is (\d+) us.")
sleep_margin = int(child.match.group(1))

while (i >= 0):
for delay in range(sleep_max, sleep_min, -1):
try:
child.expect(u"going to sleep \\d+ usecs...\r\n", timeout=3)
child.expect("\w+: Slept for \d+ us not in range \[\d+, \d+\] us.",
timeout=20)

except pexpect.TIMEOUT:
print("xtimer stuck when trying to sleep %d usecs" % (i+1))
print("xtimer stuck when trying to sleep %d us or failed.".format(delay))
print("[FAILED]")
break
i = i - 1

child.expect(u"[SUCCESS]", timeout=3)

exit(1)

child.expect("Slept for \d+ us expected \d+ us.")
child.expect("((Sleep delay margin was not kept for \d+ times.)|(\[SUCCESS\]))")
msg = child.match.group(1)
if msg != "[SUCCESS]" :
child.expect("[FAILED]")
exit(1)

if __name__ == "__main__":
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
Expand Down