Skip to content

Commit 1f9e239

Browse files
committed
events: Added equeue platform timing tests
Tests the timer/semaphores at a lower level than the event queue, which removes a layer of concerns from issues in the rtos timing.
1 parent 21b91c7 commit 1f9e239

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

TESTS/events/timing/main.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "mbed_events.h"
2+
#include "mbed.h"
3+
#include "rtos.h"
4+
#include "greentea-client/test_env.h"
5+
#include "unity.h"
6+
#include "utest.h"
7+
#include <cstdlib>
8+
#include <cmath>
9+
10+
using namespace utest::v1;
11+
12+
13+
// Test delay
14+
#ifndef TEST_EVENTS_TIMING_TIME
15+
#define TEST_EVENTS_TIMING_TIME 20000
16+
#endif
17+
18+
#ifndef TEST_EVENTS_TIMING_MEAN
19+
#define TEST_EVENTS_TIMING_MEAN 25
20+
#endif
21+
22+
#ifndef M_PI
23+
#define M_PI 3.14159265358979323846264338327950288
24+
#endif
25+
26+
// Random number generation to skew timing values
27+
float gauss(float mu, float sigma) {
28+
float x = (float)rand() / ((float)RAND_MAX+1);
29+
float y = (float)rand() / ((float)RAND_MAX+1);
30+
float x2pi = x*2.0*M_PI;
31+
float g2rad = sqrt(-2.0 * log(1.0-y));
32+
float z = cos(x2pi) * g2rad;
33+
return mu + z*sigma;
34+
}
35+
36+
float chisq(float sigma) {
37+
return pow(gauss(0, sqrt(sigma)), 2);
38+
}
39+
40+
41+
Timer timer;
42+
DigitalOut led(LED1);
43+
44+
equeue_sema_t sema;
45+
46+
// Timer timing test
47+
void timer_timing_test() {
48+
timer.reset();
49+
timer.start();
50+
int prev = timer.read_us();
51+
52+
while (prev < TEST_EVENTS_TIMING_TIME*1000) {
53+
int next = timer.read_us();
54+
if (next < prev) {
55+
printf("backwards drift %d -> %d (%08x -> %08x)\r\n",
56+
prev, next, prev, next);
57+
}
58+
TEST_ASSERT(next >= prev);
59+
prev = next;
60+
}
61+
}
62+
63+
// equeue tick timing test
64+
void tick_timing_test() {
65+
unsigned start = equeue_tick();
66+
int prev = 0;
67+
68+
while (prev < TEST_EVENTS_TIMING_TIME) {
69+
int next = equeue_tick() - start;
70+
if (next < prev) {
71+
printf("backwards drift %d -> %d (%08x -> %08x)\r\n",
72+
prev, next, prev, next);
73+
}
74+
TEST_ASSERT(next >= prev);
75+
prev = next;
76+
}
77+
}
78+
79+
// equeue semaphore timing test
80+
void semaphore_timing_test() {
81+
srand(0);
82+
timer.reset();
83+
timer.start();
84+
85+
int err = equeue_sema_create(&sema);
86+
TEST_ASSERT_EQUAL(0, err);
87+
88+
while (timer.read_ms() < TEST_EVENTS_TIMING_TIME) {
89+
int delay = chisq(TEST_EVENTS_TIMING_MEAN);
90+
91+
int start = timer.read_us();
92+
equeue_sema_wait(&sema, delay);
93+
int taken = timer.read_us() - start;
94+
95+
printf("delay %dms => error %dus\r\n", delay, abs(1000*delay - taken));
96+
TEST_ASSERT_INT_WITHIN(2000, taken, delay * 1000);
97+
98+
led = !led;
99+
}
100+
101+
equeue_sema_destroy(&sema);
102+
}
103+
104+
105+
// Test setup
106+
utest::v1::status_t test_setup(const size_t number_of_cases) {
107+
GREENTEA_SETUP((number_of_cases+1)*TEST_EVENTS_TIMING_TIME, "default_auto");
108+
return verbose_test_setup_handler(number_of_cases);
109+
}
110+
111+
const Case cases[] = {
112+
Case("Testing accuracy of timer", timer_timing_test),
113+
Case("Testing accuracy of equeue tick", tick_timing_test),
114+
Case("Testing accuracy of equeue semaphore", semaphore_timing_test),
115+
};
116+
117+
Specification specification(test_setup, cases);
118+
119+
int main() {
120+
return !Harness::run(specification);
121+
}
122+

0 commit comments

Comments
 (0)