tests/ztimer_periodic: initial commit
This commit is contained in:
parent
548c4f02fc
commit
7b30777429
7
tests/ztimer_periodic/Makefile
Normal file
7
tests/ztimer_periodic/Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
DEVELHELP ?= 0
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += fmt
|
||||
USEMODULE += ztimer_usec ztimer_msec
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
3
tests/ztimer_periodic/README.md
Normal file
3
tests/ztimer_periodic/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Introduction
|
||||
|
||||
This test application does some basic functionality testing of ztimer_periodic.
|
||||
94
tests/ztimer_periodic/main.c
Normal file
94
tests/ztimer_periodic/main.c
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
* 2020 Freie Universität Berlin
|
||||
* 2020 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 test
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief ztimer periodic test application
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "ztimer.h"
|
||||
#include "ztimer/periodic.h"
|
||||
#include "fmt.h"
|
||||
#include "mutex.h"
|
||||
|
||||
static mutex_t _mutex = MUTEX_INIT_LOCKED;
|
||||
|
||||
#define MAX_OFFSET 2
|
||||
#define N 5
|
||||
#define INTERVAL 100LU
|
||||
|
||||
static uint32_t _times[N];
|
||||
|
||||
static int callback(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
static int count = 0;
|
||||
|
||||
_times[count] = ztimer_now(ZTIMER_MSEC);
|
||||
|
||||
count += 1;
|
||||
|
||||
/* enable this to test underflow behavior */
|
||||
#if 0
|
||||
if (count == 2) {
|
||||
ztimer_spin(ZTIMER_MSEC, INTERVAL*2);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (count == N) {
|
||||
mutex_unlock(&_mutex);
|
||||
}
|
||||
|
||||
return (count == N);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ztimer_periodic_t t;
|
||||
|
||||
ztimer_periodic_init(ZTIMER_MSEC, &t, callback, NULL, INTERVAL);
|
||||
uint32_t last = ztimer_now(ZTIMER_MSEC);
|
||||
|
||||
ztimer_periodic_start(&t);
|
||||
|
||||
/* wait for periodic to trigger N times */
|
||||
mutex_lock(&_mutex);
|
||||
|
||||
int failed = 0;
|
||||
|
||||
for (unsigned i = 0; i < N; i++) {
|
||||
uint32_t offset = labs((int32_t)(_times[i] - INTERVAL - last));
|
||||
printf("i: %u time: %" PRIu32 " offset: %" PRIu32 "\n",
|
||||
i, _times[i], offset);
|
||||
if (offset > MAX_OFFSET) {
|
||||
failed = 1;
|
||||
}
|
||||
last = _times[i];
|
||||
}
|
||||
|
||||
if (!failed) {
|
||||
print_str("Test successful.\n");
|
||||
}
|
||||
else {
|
||||
print_str("Test failed!\n");
|
||||
}
|
||||
}
|
||||
18
tests/ztimer_periodic/tests/01-run.py
Executable file
18
tests/ztimer_periodic/tests/01-run.py
Executable file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Copyright (C) 2019 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
#
|
||||
# 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("Test successful.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run(testfunc))
|
||||
Loading…
x
Reference in New Issue
Block a user