1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/ztimer/overhead.c
Jue bf5dd34deb ztimer/overhead: acquire and release clocks
The start/stop overhead that might by introduced by ztimer_acquire() and ztimer_release() called during ztimer_set() resp. ztimer_handler() should not be mesured here. It has its own adjustment field.

Furthermore, the overhead mesaurement uses ztimer_now(). It is allowed to called it only after the clock has been acquired.
2022-11-22 00:25:34 +01:00

64 lines
1.4 KiB
C

/*
* 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 sys_ztimer_overhead
* @{
*
* @file
* @brief ztimer overhead measurement functions
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "ztimer.h"
#include "ztimer/overhead.h"
typedef struct {
ztimer_clock_t *clock;
volatile uint32_t *val;
} callback_arg_t;
static void _callback(void *arg)
{
callback_arg_t *callback_arg = (callback_arg_t *)arg;
*callback_arg->val = ztimer_now(callback_arg->clock);
}
int32_t ztimer_overhead_set(ztimer_clock_t *clock, uint32_t base)
{
volatile uint32_t after = 0;
uint32_t pre;
callback_arg_t arg = { .clock = clock, .val = &after };
ztimer_t t = { .callback = _callback, .arg = &arg };
ztimer_acquire(clock);
pre = ztimer_now(clock);
ztimer_set(clock, &t, base);
while (!after) {}
ztimer_release(clock);
return after - pre - base;
}
int32_t ztimer_overhead_sleep(ztimer_clock_t *clock, uint32_t base)
{
ztimer_acquire(clock);
uint32_t pre = ztimer_now(clock);
ztimer_sleep(clock, base);
uint32_t after = ztimer_now(clock);
ztimer_release(clock);
return after - pre - base;
}