diff --git a/tests/event_ztimer/Makefile b/tests/event_ztimer/Makefile new file mode 100644 index 0000000000..bce9ef00d5 --- /dev/null +++ b/tests/event_ztimer/Makefile @@ -0,0 +1,9 @@ +include ../Makefile.tests_common + +FORCE_ASSERTS = 1 +USEMODULE += event_thread +USEMODULE += event_callback +USEMODULE += event_timeout_ztimer +USEMODULE += event_periodic + +include $(RIOTBASE)/Makefile.include diff --git a/tests/event_ztimer/Makefile.ci b/tests/event_ztimer/Makefile.ci new file mode 100644 index 0000000000..b5de876337 --- /dev/null +++ b/tests/event_ztimer/Makefile.ci @@ -0,0 +1,9 @@ +BOARD_INSUFFICIENT_MEMORY := \ + arduino-duemilanove \ + arduino-leonardo \ + arduino-nano \ + arduino-uno \ + atmega328p \ + atmega328p-xplained-mini \ + nucleo-l011k4 \ + # diff --git a/tests/event_ztimer/main.c b/tests/event_ztimer/main.c new file mode 100644 index 0000000000..da1119f62c --- /dev/null +++ b/tests/event_ztimer/main.c @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2021 Inria + * + * 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 event_timeout application using ztimer + * + * @author Kaspar Schleiser + * @author Francisco Molina + * + * @} + */ + +#include + +#include "atomic_utils.h" +#include "test_utils/expect.h" +#include "timex.h" +#include "thread.h" +#include "mutex.h" +#include "event.h" +#include "event/timeout.h" +#include "event/periodic.h" +#include "event/thread.h" +#include "event/callback.h" +#include "ztimer.h" +#include "ztimer/periodic.h" + +#define EVENT_TIMEOUT_TIME (1 * US_PER_SEC) + +static void callback_never(void *arg); +static void callback_timed(void *arg); +static void callback_4times(void *arg); + +static uint8_t iter; +static event_timeout_t event_timeout; +static event_timeout_t event_timeout_cleared; +static event_periodic_t event_periodic; +static event_callback_t event_4times = EVENT_CALLBACK_INIT(callback_4times, &iter); +static event_callback_t event_timed = EVENT_CALLBACK_INIT(callback_timed, &iter); +static event_callback_t event_never = EVENT_CALLBACK_INIT(callback_never, 0); +static uint32_t before; +static mutex_t lock = MUTEX_INIT_LOCKED; + +static void callback_timed(void *arg) +{ + expect(arg == event_timed.arg); + uint32_t now = ztimer_now(ZTIMER_USEC); + expect((now - before) >= 1 * US_PER_SEC); + printf("triggered timed callback after %" PRIu32 "us\n", now - before); + mutex_unlock(&lock); +} + +static void callback_4times(void *arg) +{ + uint8_t *count = (uint8_t *)arg; + + *count = *count + 1; + uint32_t now = event_periodic.timer.last; + uint32_t elapsed = now - before; + before = now; + expect((elapsed) >= 1 * US_PER_SEC); + if (*count <= 4) { + printf("trigger %d of periodic timeout, elapsed time: %" PRIu32 " us\n", + *count, elapsed); + } + if (*count == 4) { + event_periodic_stop(&event_periodic); + mutex_unlock(&lock); + } + else if (*count > 4) { + /* this callback should never be called */ + puts("this should only be called 4 times"); + puts("[FAILED]"); + while (1) { + expect(false); + } + } +} + +static void callback_never(void *arg) +{ + (void)arg; + /* this callback should never be called */ + puts("this should never happen"); + puts("[FAILED]"); + while (1) { + expect(false); + } +} + +int main(void) +{ + iter = 0; + puts("posting periodic timed callback with timeout 1sec"); + event_periodic_init(&event_periodic, ZTIMER_USEC, EVENT_PRIO_MEDIUM, + &event_4times.super); + event_periodic_start(&event_periodic, EVENT_TIMEOUT_TIME); + before = event_periodic.timer.last; + puts("waiting for periodic callback to be triggered 4 times"); + mutex_lock(&lock); + puts("posting timed callback with timeout 0.5sec, clear right after"); + event_timeout_ztimer_init(&event_timeout_cleared, ZTIMER_USEC, + EVENT_PRIO_MEDIUM, &event_never.super); + event_timeout_set(&event_timeout_cleared, EVENT_TIMEOUT_TIME / 2); + event_timeout_clear(&event_timeout_cleared); + puts("posting timed callback with timeout 1sec"); + event_timeout_ztimer_init(&event_timeout, ZTIMER_USEC, EVENT_PRIO_MEDIUM, + &event_timed.super); + before = ztimer_now(ZTIMER_USEC); + event_timeout_set(&event_timeout, EVENT_TIMEOUT_TIME); + puts("waiting for timed callback to trigger"); + mutex_lock(&lock); + puts("[SUCCESS]"); + return 0; +} diff --git a/tests/event_ztimer/tests/01-run.py b/tests/event_ztimer/tests/01-run.py new file mode 100755 index 0000000000..a8c5d9b211 --- /dev/null +++ b/tests/event_ztimer/tests/01-run.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2016 Kaspar Schleiser +# 2017 Sebastian Meiling +# +# 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_exact(u"[SUCCESS]") + + +if __name__ == "__main__": + sys.exit(run(testfunc))