1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

tests/periph_timer_periodic: spice up test

This should detect some bugs regarding incorrect behavior regarding
timer_start() not resuming periodic timers as expected.
This commit is contained in:
Marian Buschsieweke 2021-12-13 13:56:32 +01:00
parent efa24b3f1e
commit 1d57cf9e0f
No known key found for this signature in database
GPG Key ID: CB8E3238CE715A94
2 changed files with 64 additions and 30 deletions

View File

@ -18,8 +18,9 @@
* @} * @}
*/ */
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "board.h" #include "board.h"
#include "macros/units.h" #include "macros/units.h"
@ -57,6 +58,10 @@
#define MAX_CHANNELS (10) #define MAX_CHANNELS (10)
#endif #endif
#ifndef ITERATIONS
#define ITERATIONS 3
#endif
static unsigned count[MAX_CHANNELS]; static unsigned count[MAX_CHANNELS];
static void cb(void *arg, int chan) static void cb(void *arg, int chan)
@ -75,11 +80,12 @@ static void cb(void *arg, int chan)
static const char* _print_ok(int chan, bool *succeeded) static const char* _print_ok(int chan, bool *succeeded)
{ {
if (chan == 0 && count[chan] > 0) { if (chan == 0) {
if (count[chan] > 0) {
return "OK"; return "OK";
} }
}
if (chan > 0 && count[chan] == 0) { else if (count[chan] == 0) {
return "OK"; return "OK";
} }
@ -89,7 +95,7 @@ static const char* _print_ok(int chan, bool *succeeded)
int main(void) int main(void)
{ {
mutex_t lock = MUTEX_INIT_LOCKED; mutex_t lock = MUTEX_INIT;
const unsigned long timer_hz = XTIMER_HZ; const unsigned long timer_hz = XTIMER_HZ;
const unsigned steps = (CYCLE_MS * timer_hz) / 1000; const unsigned steps = (CYCLE_MS * timer_hz) / 1000;
@ -100,25 +106,42 @@ int main(void)
expect(timer_init(TIMER_CYCL, timer_hz, cb, &lock) == 0); expect(timer_init(TIMER_CYCL, timer_hz, cb, &lock) == 0);
puts("TEST START"); puts("TEST START");
bool succeeded = true;
unsigned channel_numof = 1;
for (unsigned iter = 0; iter < ITERATIONS; iter++) {
printf("Running iteration %u of %u\n", iter + 1, (unsigned)ITERATIONS);
lock = (mutex_t)MUTEX_INIT_LOCKED;
memset(count, 0x00, sizeof(count));
/* Only the first channel should trigger and reset the counter */ /* Only the first channel should trigger and reset the counter */
/* If subsequent channels trigger this is an error. */ /* If subsequent channels trigger this is an error. */
unsigned channel_numof = 1; for (unsigned chan = 1; chan < MAX_CHANNELS; chan++) {
for(unsigned i = 1; i < MAX_CHANNELS; i++) { if (!timer_set_periodic(TIMER_CYCL, chan, (1 + chan) * steps,
if(!timer_set_periodic(TIMER_CYCL, i, (1 + i) * steps, TIM_FLAG_RESET_ON_SET)) { TIM_FLAG_RESET_ON_SET))
channel_numof = i; {
channel_numof = chan;
break; break;
} }
} }
if (iter == 0) {
/* configure timer on first iterations */
timer_set_periodic(TIMER_CYCL, 0, steps, TIM_FLAG_RESET_ON_MATCH); timer_set_periodic(TIMER_CYCL, 0, steps, TIM_FLAG_RESET_ON_MATCH);
}
else {
/* resume timer on subsequent iterations */
timer_start(TIMER_CYCL);
}
mutex_lock(&lock); mutex_lock(&lock);
puts("\nCycles:"); puts("\nCycles:");
bool succeeded = true;
for (unsigned i = 0; i < channel_numof; ++i) { for (unsigned i = 0; i < channel_numof; ++i) {
printf("channel %u = %02u\t[%s]\n", i, count[i], _print_ok(i, &succeeded)); printf("channel %u = %02u\t[%s]\n",
i, count[i], _print_ok(i, &succeeded));
}
} }
if (succeeded) { if (succeeded) {

View File

@ -17,13 +17,24 @@ PRECISION = float(os.getenv("TEST_PERIPH_TIMER_PERIODIC_PRECISION", "0"))
def testfunc(child): def testfunc(child):
child.expect_exact('TEST START') child.expect_exact('TEST START')
while True:
child.expect(r"Running iteration (\d+) of (\d+)\r\n")
iteration = int(child.match.group(1))
last_iteration = int(child.match.group(2))
start = time.time() start = time.time()
child.expect_exact('TEST SUCCEEDED') for i in range(12):
child.expect(r"\[(\d+)\] tick\r\n")
chan = int(child.match.group(1))
assert chan == 0, "Only channel 0 should tick"
child.expect("Cycles:\r\n")
end = time.time() end = time.time()
# test should run 10 cycles with 25ms each # test should run 10 cycles with 25ms each
elapsed = end - start elapsed = end - start
assert elapsed > 0.25 * (1 - PRECISION), "=< 0.25s ({})".format(elapsed) assert elapsed > 0.25 * (1 - PRECISION), "=< 0.25s ({})".format(elapsed)
assert elapsed < 0.40 * (1 + PRECISION), "=> 0.40s ({})".format(elapsed) assert elapsed < 0.40 * (1 + PRECISION), "=> 0.40s ({})".format(elapsed)
if iteration == last_iteration:
break
child.expect_exact('TEST SUCCEEDED')
if __name__ == "__main__": if __name__ == "__main__":