Skip to content

Commit e678325

Browse files
committed
Modifying timing tests to use timing drift host test
1 parent f6c60d0 commit e678325

File tree

5 files changed

+106
-56
lines changed

5 files changed

+106
-56
lines changed

TESTS/mbed_drivers/ticker/main.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,31 @@
2121
using namespace utest::v1;
2222

2323
static const int ONE_SECOND_MS = 1000;
24+
static const int total_ticks = 10;
2425

2526
DigitalOut led1(LED1);
2627
DigitalOut led2(LED2);
2728

2829
Ticker *ticker1;
2930
Ticker *ticker2;
3031

32+
volatile int ticker_count = 0;
33+
volatile bool print_tick = false;
34+
3135
void send_kv_tick() {
32-
static int count = 0;
33-
if (count < 10) {
34-
greentea_send_kv("tick", count);
35-
} else if (count == 10) {
36-
count = 0;
37-
Harness::validate_callback();
36+
if (ticker_count <= total_ticks) {
37+
print_tick = true;
3838
}
39-
count++;
4039
}
4140

4241
void ticker_callback_0(void) {
43-
static int ticker_count = 0;
44-
if (ticker_count >= ONE_SECOND_MS) {
42+
static int fast_ticker_count = 0;
43+
if (fast_ticker_count >= ONE_SECOND_MS) {
4544
send_kv_tick();
46-
ticker_count = 0;
45+
fast_ticker_count = 0;
4746
led1 = !led1;
4847
}
49-
ticker_count++;
48+
fast_ticker_count++;
5049
}
5150

5251
void ticker_callback_1(void) {
@@ -78,26 +77,38 @@ void ticker_callback_2_switch_to_1(void) {
7877
ticker_callback_2();
7978
}
8079

81-
utest::v1::control_t test_case_1x_ticker() {
80+
void wait_and_print() {
81+
while(ticker_count <= total_ticks) {
82+
if (print_tick) {
83+
print_tick = false;
84+
greentea_send_kv("tick", ticker_count++);
85+
}
86+
}
87+
}
88+
89+
void test_case_1x_ticker() {
8290
led1 = 0;
8391
led2 = 0;
92+
ticker_count = 0;
8493
ticker1->attach_us(ticker_callback_0, ONE_SECOND_MS);
85-
return CaseTimeout(15 * ONE_SECOND_MS);
94+
wait_and_print();
8695
}
8796

88-
control_t test_case_2x_ticker() {
97+
void test_case_2x_ticker() {
8998
led1 = 0;
9099
led2 = 0;
100+
ticker_count = 0;
91101
ticker1->attach(&ticker_callback_1, 1.0);
92102
ticker2->attach(&ticker_callback_2_led, 2.0);
93-
return CaseTimeout(15 * ONE_SECOND_MS);
103+
wait_and_print();
94104
}
95105

96-
utest::v1::control_t test_case_2x_callbacks() {
106+
void test_case_2x_callbacks() {
97107
led1 = 0;
98108
led2 = 0;
109+
ticker_count = 0;
99110
ticker1->attach(ticker_callback_1_switch_to_2, 1.0);
100-
return CaseTimeout(15 * ONE_SECOND_MS);
111+
wait_and_print();
101112
}
102113

103114
utest::v1::status_t one_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) {
@@ -130,7 +141,7 @@ Case cases[] = {
130141
};
131142

132143
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
133-
GREENTEA_SETUP(60, "wait_us_auto");
144+
GREENTEA_SETUP((total_ticks + 5) * 3, "timing_drift_auto");
134145
return greentea_test_setup_handler(number_of_cases);
135146
}
136147

TESTS/mbed_drivers/timeout/main.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,51 @@ using namespace utest::v1;
2222

2323
Timeout timer;
2424
DigitalOut led(LED1);
25+
volatile int ticker_count = 0;
26+
volatile bool print_tick = false;
27+
static const int total_ticks = 10;
2528

2629
namespace {
2730
const int MS_INTERVALS = 1000;
2831
}
2932

3033
void send_kv_tick() {
31-
static int count = 0;
32-
if (count < 10) {
33-
greentea_send_kv("tick", count);
34-
} else if (count == 10) {
35-
Harness::validate_callback();
34+
if (ticker_count <= total_ticks) {
35+
print_tick = true;
3636
}
37-
count++;
3837
}
3938

4039
void toggleOff(void);
4140

4241
void toggleOn(void) {
4342
static int toggle_counter = 0;
43+
timer.attach_us(toggleOff, 500);
44+
4445
if (toggle_counter == MS_INTERVALS) {
45-
led = !led;
4646
send_kv_tick();
4747
toggle_counter = 0;
48+
} else {
49+
toggle_counter++;
4850
}
49-
toggle_counter++;
50-
timer.attach_us(toggleOff, 500);
5151
}
5252

5353
void toggleOff(void) {
5454
timer.attach_us(toggleOn, 500);
5555
}
5656

57-
control_t test_case_ticker() {
57+
void wait_and_print() {
58+
while(ticker_count <= total_ticks) {
59+
if (print_tick) {
60+
print_tick = false;
61+
greentea_send_kv("tick", ticker_count++);
62+
led = !led;
63+
}
64+
}
65+
}
66+
67+
void test_case_ticker() {
5868
toggleOn();
59-
return CaseTimeout(15 * 1000);
69+
wait_and_print();
6070
}
6171

6272
// Test cases
@@ -65,7 +75,7 @@ Case cases[] = {
6575
};
6676

6777
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
68-
GREENTEA_SETUP(20, "wait_us_auto");
78+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
6979
return greentea_test_setup_handler(number_of_cases);
7080
}
7181

TESTS/mbed_drivers/wait_us/main.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,27 @@
2121
using namespace utest::v1;
2222

2323
DigitalOut led(LED1);
24+
Timer timer;
25+
volatile bool print_tick = false;
26+
const int ONE_SECOND_US = 1000000;
27+
const int total_ticks = 10;
2428

2529
void test_case_ticker() {
26-
for (int i=0; i < 10; ++i) {
27-
// 10 secs...
28-
for (int j = 0; j < 1000; ++j) {
29-
// 1000 * 1000us = 1 sec
30-
wait_us(1000);
31-
}
32-
led = !led; // Blink
30+
int before_print_us;
31+
int after_print_us;
32+
int wait_time_us = ONE_SECOND_US;
33+
34+
timer.start();
35+
for (int i = 0; i <= total_ticks; ++i) {
36+
wait_us(wait_time_us);
37+
before_print_us = timer.read();
3338
greentea_send_kv("tick", i);
39+
after_print_us = timer.read();
40+
41+
// This won't be 100% exact, but it should be pretty close
42+
wait_time_us = ONE_SECOND_US - (after_print_us - before_print_us);
3443
}
44+
timer.stop();
3545
}
3646

3747
// Test cases
@@ -40,7 +50,7 @@ Case cases[] = {
4050
};
4151

4252
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
43-
GREENTEA_SETUP(20, "wait_us_auto");
53+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
4454
return greentea_test_setup_handler(number_of_cases);
4555
}
4656

TESTS/mbedmicro-rtos-mbed/basic/main.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,31 @@
2929
#define STACK_SIZE DEFAULT_STACK_SIZE
3030
#endif
3131

32+
#define SIGNAL_PRINT_TICK 0x01
33+
3234
DigitalOut led1(LED1);
33-
DigitalOut led2(LED2);
3435

35-
void led2_thread(void const *argument) {
36-
static int count = 0;
37-
while (true) {
38-
led2 = !led2;
39-
Thread::wait(1000);
40-
greentea_send_kv("tick", count++);
36+
const int total_ticks = 10;
37+
38+
void print_tick_thread() {
39+
for (int i = 0; i <= total_ticks; i++) {
40+
Thread::signal_wait(SIGNAL_PRINT_TICK);
41+
greentea_send_kv("tick", i);
42+
led1 = !led1;
4143
}
4244
}
4345

4446
int main() {
45-
GREENTEA_SETUP(15, "wait_us_auto");
46-
47-
Thread thread(led2_thread, NULL, osPriorityNormal, STACK_SIZE);
48-
49-
while (true) {
50-
led1 = !led1;
51-
Thread::wait(500);
47+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
48+
49+
Thread tick_thread(osPriorityNormal, STACK_SIZE);
50+
tick_thread.start(print_tick_thread);
51+
52+
for (int i = 0; i <= total_ticks; i++) {
53+
Thread::wait(1000);
54+
tick_thread.signal_set(SIGNAL_PRINT_TICK);
5255
}
56+
57+
tick_thread.join();
58+
GREENTEA_TESTSUITE_RESULT(1);
5359
}

TESTS/mbedmicro-rtos-mbed/timer/main.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
#error [NOT_SUPPORTED] test not supported
77
#endif
88

9+
int total_ticks = 10;
10+
volatile int current_tick = 0;
11+
912
DigitalOut LEDs[4] = {
1013
DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
1114
};
1215

1316
void blink(void const *n) {
1417
static int blink_counter = 0;
15-
static int count = 0;
1618
const int led_id = int(n);
1719
LEDs[led_id] = !LEDs[led_id];
18-
if (++blink_counter == 75) {
19-
greentea_send_kv("tick", count++);
20+
if (++blink_counter == 75 && current_tick <= total_ticks) {
21+
greentea_send_kv("tick", current_tick++);
2022
blink_counter = 0;
2123
}
2224
}
2325

2426
int main(void) {
25-
GREENTEA_SETUP(15, "wait_us_auto");
27+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
2628

2729
RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
2830
RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
@@ -33,6 +35,17 @@ int main(void) {
3335
led_2_timer.start(100);
3436
led_3_timer.start(50);
3537
led_4_timer.start(25);
38+
39+
while(current_tick <= total_ticks) {
40+
Thread::wait(10);
41+
}
42+
43+
led_4_timer.stop();
44+
led_3_timer.stop();
45+
led_2_timer.stop();
46+
led_1_timer.stop();
47+
48+
GREENTEA_TESTSUITE_RESULT(1);
3649

3750
Thread::wait(osWaitForever);
3851
}

0 commit comments

Comments
 (0)