diff --git a/tests/ztimer_msg/Makefile b/tests/ztimer_msg/Makefile new file mode 100644 index 0000000000..e198e2c38e --- /dev/null +++ b/tests/ztimer_msg/Makefile @@ -0,0 +1,8 @@ +include ../Makefile.tests_common + +USEMODULE += ztimer_usec + +# uncomment this to test using ztimer msec on rtt +#USEMODULE += ztimer_msec ztimer_periph_rtt + +include $(RIOTBASE)/Makefile.include diff --git a/tests/ztimer_msg/Makefile.ci b/tests/ztimer_msg/Makefile.ci new file mode 100644 index 0000000000..8a0aef2b9e --- /dev/null +++ b/tests/ztimer_msg/Makefile.ci @@ -0,0 +1,8 @@ +BOARD_INSUFFICIENT_MEMORY := \ + arduino-duemilanove \ + arduino-nano \ + arduino-uno \ + atmega328p \ + nucleo-f031k6 \ + stm32f030f4-demo \ + # diff --git a/tests/ztimer_msg/README.md b/tests/ztimer_msg/README.md new file mode 100644 index 0000000000..b9d1ab991a --- /dev/null +++ b/tests/ztimer_msg/README.md @@ -0,0 +1,9 @@ +# Overview + +This test application is a direct translation of xtimer_msg to the ztimer API. +It is meant mostly as a means to do size comparisons, thus tries to be as close +as possible to the original. + +One notable change is the option to choose a different ztimer clock. +By default, the test will use ZTIMER_USEC, unless ZTIMER_MSEC is compiled in, +which will be used in that case. diff --git a/tests/ztimer_msg/main.c b/tests/ztimer_msg/main.c new file mode 100644 index 0000000000..b40422e9ed --- /dev/null +++ b/tests/ztimer_msg/main.c @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2015-19 Kaspar Schleiser + * 2013 INRIA + * 2017 HAW Hamburg + * + * 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 + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief xtimer_msg test application + * + * @author Kaspar Schleiser + * @author Oliver Hahm + * @author Christian Mehlis + * @author Sebastian Meiling + * @} + */ + +#include + +#include "ztimer.h" +#include "thread.h" +#include "msg.h" +#include "timex.h" + +#ifdef MODULE_ZTIMER_MSEC +#define ZTIMER ZTIMER_MSEC +#define TICKS_PER_SEC MS_PER_SEC +#else +#define ZTIMER ZTIMER_USEC +#define TICKS_PER_SEC US_PER_SEC +#endif + +char timer_stack[THREAD_STACKSIZE_DEFAULT]; +char timer_stack_local[THREAD_STACKSIZE_DEFAULT]; + +struct timer_msg { + ztimer_t timer; + uint32_t interval; + char *text; + msg_t msg; +}; + +struct timer_msg msg_a = { .interval = (2 * TICKS_PER_SEC), + .text = "Hello World" }; +struct timer_msg msg_b = { .interval = (5 * TICKS_PER_SEC), + .text = "This is a Test" }; + +void *timer_thread(void *arg) +{ + (void) arg; + + printf("This is thread %" PRIkernel_pid "\n", thread_getpid()); + + /* The queue is required to avoid loss of a 2nd message, when the 1st is + * still processed. The timing ensures that at most 1 message is queued. + */ + msg_t msgq[1]; + msg_init_queue(msgq, 1); + + while (1) { + msg_t m; + msg_receive(&m); + struct timer_msg *tmsg = m.content.ptr; + uint32_t now = ztimer_now(ZTIMER); + printf("now=%" PRIu32 ":%" PRIu32 " -> every %" PRIu32 ".%" PRIu32 "s: %s\n", + (now / TICKS_PER_SEC), + (now % TICKS_PER_SEC), + tmsg->interval / TICKS_PER_SEC, + tmsg->interval % TICKS_PER_SEC, + tmsg->text); + + tmsg->msg.type = 12345; + tmsg->msg.content.ptr = tmsg; + ztimer_set_msg(ZTIMER, &tmsg->timer, tmsg->interval, &tmsg->msg, thread_getpid()); + } +} + +void *timer_thread_local(void *arg) +{ + (void) arg; + + printf("This is thread %" PRIkernel_pid "\n", thread_getpid()); + + while (1) { + msg_t m; + msg_receive(&m); + + uint32_t now = ztimer_now(ZTIMER); + int sec = now / TICKS_PER_SEC; + int min = sec / 60; + int hr = sec / 3600; + printf("sec=%d min=%d hour=%d\n", sec, min, hr); + } +} + +int main(void) +{ + msg_t m; + kernel_pid_t pid = thread_create( + timer_stack, + sizeof(timer_stack), + THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_STACKTEST, + timer_thread, + NULL, + "timer"); + + puts("sending 1st msg"); + m.content.ptr = &msg_a; + msg_try_send(&m, pid); + + puts("sending 2nd msg"); + m.content.ptr = &msg_b; + msg_try_send(&m, pid); + + kernel_pid_t pid2 = thread_create( + timer_stack_local, + sizeof(timer_stack_local), + THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_STACKTEST, + timer_thread_local, + NULL, + "timer local"); + + while (1) { + ztimer_sleep(ZTIMER, 1 * TICKS_PER_SEC); + msg_try_send(&m, pid2); + } +} diff --git a/tests/ztimer_msg/tests/01-run.py b/tests/ztimer_msg/tests/01-run.py new file mode 100755 index 0000000000..136b6443a4 --- /dev/null +++ b/tests/ztimer_msg/tests/01-run.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2017 HAW Hamburg +# +# 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 +# directory for more details. + +import sys +from testrunner import run + + +def testfunc(child): + # 1st check for periodic 2s Hello World message, i.e., 2 output + 1 msg + for _ in range(7): + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"now=\d+:\d+ -> every 2.0s: Hello World") + # 2nd check for periodic 5s test message, i.e., 5 output + 1 msg + for _ in range(3): + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"sec=\d+ min=\d+ hour=\d+") + child.expect(r"now=\d+:\d+ -> every 5.0s: This is a Test") + + +if __name__ == "__main__": + sys.exit(run(testfunc)) diff --git a/tests/ztimer_overhead/Makefile b/tests/ztimer_overhead/Makefile new file mode 100644 index 0000000000..9c105c8470 --- /dev/null +++ b/tests/ztimer_overhead/Makefile @@ -0,0 +1,6 @@ +DEVELHELP ?= 0 +include ../Makefile.tests_common + +USEMODULE += ztimer_overhead ztimer_usec + +include $(RIOTBASE)/Makefile.include diff --git a/tests/ztimer_overhead/README.md b/tests/ztimer_overhead/README.md new file mode 100644 index 0000000000..7724edf39d --- /dev/null +++ b/tests/ztimer_overhead/README.md @@ -0,0 +1,7 @@ +# Introduction + +This test application sets up a ztimer_periph at 1MHz, then measures 1024 +times how much overhead ztimer adds. + +It uses the "ztimer_overhead()" function. See it's documentation for more +information. diff --git a/tests/ztimer_overhead/main.c b/tests/ztimer_overhead/main.c new file mode 100644 index 0000000000..9e2723e2a8 --- /dev/null +++ b/tests/ztimer_overhead/main.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2019 Kaspar Schleiser + * + * 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 + * directory for more details. + */ + +/** + * @ingroup test + * @{ + * + * @file + * @brief ztimer overhead test application + * + * @author Kaspar Schleiser + * + * @} + */ + +#include +#include +#include + +#include "ztimer.h" +#include "ztimer/overhead.h" + +#define BASE 1000 +#define SAMPLES 1024 + +int main(void) +{ + uint32_t total = 0; + + uint16_t min = 0xFFFF; + uint16_t max = 0; + + /* unset configured adjustment */ + /* ZTIMER_USEC->adjust = 0; */ + + unsigned n = SAMPLES; + while (n--) { + unsigned overhead = ztimer_overhead(ZTIMER_USEC, BASE); + total += overhead; + if (overhead < min) { + min = overhead; + } + else if (overhead > max) { + max = overhead; + } + } + + printf("min=%u max=%u avg=%" PRIu32 "\n", min, max, (total / SAMPLES)); + + return 0; +} diff --git a/tests/ztimer_overhead/tests/01-run.py b/tests/ztimer_overhead/tests/01-run.py new file mode 100755 index 0000000000..8430a94fea --- /dev/null +++ b/tests/ztimer_overhead/tests/01-run.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 Kaspar Schleiser +# +# 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 +# directory for more details. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect(r"min=\d+ max=\d+ avg=\d+\r\n") + + +if __name__ == "__main__": + sys.exit(run(testfunc))