Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/periph_timer_periodic: spice up test #17388

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions tests/periph_timer_periodic/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
* @}
*/

#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "board.h"
#include "macros/units.h"
Expand Down Expand Up @@ -57,6 +58,10 @@
#define MAX_CHANNELS (10)
#endif

#ifndef ITERATIONS
#define ITERATIONS 3
#endif

static unsigned count[MAX_CHANNELS];

static void cb(void *arg, int chan)
Expand All @@ -75,11 +80,12 @@ static void cb(void *arg, int chan)

static const char* _print_ok(int chan, bool *succeeded)
{
if (chan == 0 && count[chan] > 0) {
return "OK";
if (chan == 0) {
if (count[chan] > 0) {
return "OK";
}
}

if (chan > 0 && count[chan] == 0) {
else if (count[chan] == 0) {
return "OK";
}
Comment on lines 82 to 90
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is btw. cppcheck approved (I locally still run it in my editor - with appropriate include paths). The chan > 0 looked suspiciously like a bound check to cppcheck and cppecheck pointed out that chan < MAX_CHANNELS would also be needed for full bounds checking.


Expand All @@ -89,7 +95,7 @@ static const char* _print_ok(int chan, bool *succeeded)

int main(void)
{
mutex_t lock = MUTEX_INIT_LOCKED;
mutex_t lock = MUTEX_INIT;
const unsigned long timer_hz = XTIMER_HZ;
const unsigned steps = (CYCLE_MS * timer_hz) / 1000;

Expand All @@ -100,25 +106,42 @@ int main(void)
expect(timer_init(TIMER_CYCL, timer_hz, cb, &lock) == 0);

puts("TEST START");

/* Only the first channel should trigger and reset the counter */
/* If subsequent channels trigger this is an error. */
bool succeeded = true;
unsigned channel_numof = 1;
for(unsigned i = 1; i < MAX_CHANNELS; i++) {
if(!timer_set_periodic(TIMER_CYCL, i, (1 + i) * steps, TIM_FLAG_RESET_ON_SET)) {
channel_numof = i;
break;

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 */
/* If subsequent channels trigger this is an error. */
for (unsigned chan = 1; chan < MAX_CHANNELS; chan++) {
if (!timer_set_periodic(TIMER_CYCL, chan, (1 + chan) * steps,
TIM_FLAG_RESET_ON_SET))
{
channel_numof = chan;
break;
}
}

if (iter == 0) {
/* configure timer on first iterations */
timer_set_periodic(TIMER_CYCL, 0, steps, TIM_FLAG_RESET_ON_MATCH);
}
else {
/* resume timer on subsequent iterations */
timer_start(TIMER_CYCL);
}
}
timer_set_periodic(TIMER_CYCL, 0, steps, TIM_FLAG_RESET_ON_MATCH);

mutex_lock(&lock);
mutex_lock(&lock);

puts("\nCycles:");
puts("\nCycles:");

bool succeeded = true;
for (unsigned i = 0; i < channel_numof; ++i) {
printf("channel %u = %02u\t[%s]\n", i, count[i], _print_ok(i, &succeeded));
for (unsigned i = 0; i < channel_numof; ++i) {
printf("channel %u = %02u\t[%s]\n",
i, count[i], _print_ok(i, &succeeded));
}
}

if (succeeded) {
Expand Down
23 changes: 17 additions & 6 deletions tests/periph_timer_periodic/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@

def testfunc(child):
child.expect_exact('TEST START')
start = time.time()
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()
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()
# test should run 10 cycles with 25ms each
elapsed = end - start
assert elapsed > 0.25 * (1 - PRECISION), "=< 0.25s ({})".format(elapsed)
assert elapsed < 0.40 * (1 + PRECISION), "=> 0.40s ({})".format(elapsed)
if iteration == last_iteration:
break
child.expect_exact('TEST SUCCEEDED')
end = time.time()
# test should run 10 cycles with 25ms each
elapsed = end - start
assert elapsed > 0.25 * (1 - PRECISION), "=< 0.25s ({})".format(elapsed)
assert elapsed < 0.40 * (1 + PRECISION), "=> 0.40s ({})".format(elapsed)


if __name__ == "__main__":
Expand Down