Skip to content

Commit

Permalink
tests/xtimer_usleep: improved test, added pin toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Josar committed Apr 2, 2018
1 parent c23d85a commit ec463fc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
8 changes: 5 additions & 3 deletions tests/xtimer_usleep/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# 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()` both against the timings of `xtimer_now_usec()`
and by providing capabilities to compare against an external timer.

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`.

## Usage
```
Expand Down
50 changes: 44 additions & 6 deletions tests/xtimer_usleep/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,29 @@
#define RUNS (5U)
#define SLEEP_TIMES_NUMOF (sizeof(sleep_times) / sizeof(sleep_times[0]))

static const uint32_t sleep_times[] = { 10000, 50000, 100000 };
static const uint32_t sleep_times[] = { 10000, 50000, 1234, 5678, 121212, 98765, 75000};

#define ERROR_US 500

/*
* 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
#include "board.h"
#include "periph/gpio.h"
#define SLEEP_GPIO_PIN GPIO_PIN(PORT_F, 7)
#endif
int main(void)
{
uint32_t start_test, testtime;

#if 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");
Expand All @@ -43,16 +60,37 @@ int main(void)
for (unsigned n = 0;
n < sizeof(sleep_times) / sizeof(sleep_times[0]);
n++) {
uint32_t diff, start;
start = xtimer_now_usec();

start_sleep = xtimer_now_usec();

#ifdef SLEEP_PIN
gpio_set(SLEEP_GPIO_PIN);
#endif
xtimer_usleep(sleep_times[n]);
diff = xtimer_now_usec() - start;
printf("Slept for %" PRIu32 " us (expected: %" PRIu32 " us)\n",
diff, sleep_times[n]);
#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);
}else{
printf("Slept for %" PRIu32 " us (expected: %" PRIu32 " us)\n",
diff, sleep_times[n]);
}
}
}
testtime = xtimer_now_usec() - start_test;
printf("Test ran for %" PRIu32 " us\n", testtime);

/* atmega mcu will hang if main ends */
while(1){}
return 0;
}

0 comments on commit ec463fc

Please sign in to comment.