1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys: xtimer: add slow-timer-read compensation + test application

This commit is contained in:
Kaspar Schleiser 2015-09-12 14:31:19 +02:00
parent a0d78cf989
commit bd19a903ab
3 changed files with 118 additions and 1 deletions

View File

@ -426,10 +426,34 @@ void _xtimer_sleep(uint32_t offset, uint32_t long_offset);
static inline void xtimer_spin_until(uint32_t value);
/** @} */
#if XTIMER_MASK
#ifndef XTIMER_SHIFT_ON_COMPARE
/**
* @brief ignore some bits when comparing timer values
*
* (only relevant when XTIMER_MASK != 0, e.g., timers < 16bit.)
*
* When combining _xtimer_now() and _high_cnt, we have to get the same value in
* order to work around a race between overflowing _xtimer_now() and OR'ing the
* resulting values.
* But some platforms are too slow to get the same timer
* value twice, so we use this define to ignore some of the bits.
*
* Use tests/xtimer_shift_on_compare to find the correct value for your MCU.
*/
#define XTIMER_SHIFT_ON_COMPARE 0
#endif
#endif
static inline uint32_t xtimer_now(void)
{
#if XTIMER_MASK
return _xtimer_now() | _high_cnt;
uint32_t a, b;
do {
a = _xtimer_now() | _high_cnt;
b = _xtimer_now() | _high_cnt;
} while ((a >> XTIMER_SHIFT_ON_COMPARE) != (b >> XTIMER_SHIFT_ON_COMPARE));
return b;
#else
return _xtimer_now();
#endif

View File

@ -0,0 +1,12 @@
APPLICATION = xtimer_shift_on_compare
include ../Makefile.tests_common
BOARD_WHITELIST += msb-430h arduino-mega2560
BOARD ?= native
RIOTBASE ?= $(CURDIR)/../..
FEATURES_REQUIRED += periph_timer
USEMODULE += xtimer
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2015 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.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief timer test application
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include <stdio.h>
#include "xtimer.h"
#include "periph_conf.h"
#define ITERATIONS 128
#define MAXSHIFT 8
unsigned min[MAXSHIFT];
unsigned max[MAXSHIFT];
unsigned avg[MAXSHIFT];
unsigned total[MAXSHIFT];
int main(void)
{
puts("xtimer_shift_on_compare test application.\n");
printf("trying %u iterations\n", ITERATIONS);
uint32_t a, b;
unsigned i = 0;
for (int shift = 0; shift < MAXSHIFT; shift++) {
min[shift] = (unsigned)0xffffffff;
max[shift] = 0;
total[shift] = 0;
avg[shift] = 0;
for (int j=0; j < ITERATIONS; j++) {
i = 0;
do {
i++;
if (i >= 0xF) {
break;
}
else {
}
a = _xtimer_now() | _high_cnt;
b = _xtimer_now() | _high_cnt;
} while ((a>>shift) != (b>>shift));
min[shift] = i < min[shift] ? i : min[shift];
max[shift] = i > max[shift] ? i : max[shift];
if (i != 0xF) {
total[shift]++;
avg[shift] += i;
}
}
}
for (int i=0; i < MAXSHIFT; i++) {
if (min[i] >= 0xF) {
printf("shift %i is too little.\n", i);
} else {
printf("shift %i min=%u max=%u n_success=%u avg=%u\n", i, min[i], max[i], total[i], avg[i]/total[i]);
}
}
printf("\nTest complete.\n");
return 0;
}