Skip to content

Commit 9cf1c47

Browse files
committed
tests/sys/busy_wait: add test for busy wait
1 parent fff9ff1 commit 9cf1c47

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

tests/sys/busy_wait/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include ../Makefile.sys_common
2+
3+
USEMODULE += ztimer_usec
4+
5+
include $(RIOTBASE)/Makefile.include

tests/sys/busy_wait/Makefile.ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_INSUFFICIENT_MEMORY := \
2+
atmega8 \
3+
#

tests/sys/busy_wait/main.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2024 ML!PA Consulting GmbH
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup tests
11+
* @{
12+
*
13+
* @file
14+
* @brief Busy Wait loop Test Application
15+
*
16+
* This can be used to determine `CPU_CYCLES_PER_LOOP` by
17+
* comparing the time the busy wait loop took with the
18+
* actual µsec timer.
19+
*
20+
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
21+
* @}
22+
*/
23+
24+
#include <stdio.h>
25+
#include "busy_wait.h"
26+
#include "ztimer/stopwatch.h"
27+
28+
static inline void _measure_interval(ztimer_stopwatch_t *clock, unsigned usec)
29+
{
30+
unsigned usec_real;
31+
32+
printf("waiting for %u µs…\n", usec);
33+
ztimer_stopwatch_start(clock);
34+
busy_wait_us(usec);
35+
usec_real = ztimer_stopwatch_measure(clock);
36+
printf("took %u µs (diff: %d µs)\n", usec_real, (int)usec_real - usec);
37+
}
38+
39+
int main(void)
40+
{
41+
ztimer_stopwatch_t clock;
42+
ztimer_stopwatch_init(ZTIMER_USEC, &clock);
43+
44+
_measure_interval(&clock, 10);
45+
_measure_interval(&clock, 100);
46+
_measure_interval(&clock, 1000);
47+
_measure_interval(&clock, 10000);
48+
49+
return 0;
50+
}

0 commit comments

Comments
 (0)