Skip to content

Commit b18c88e

Browse files
authored
Merge pull request #5106 from fkjagodzinski/timeout_tests
Timeout & LowPowerTimeout tests
2 parents 9cac3b2 + 82f781c commit b18c88e

File tree

3 files changed

+513
-165
lines changed

3 files changed

+513
-165
lines changed

TESTS/mbed_drivers/lp_timeout/main.cpp

Lines changed: 48 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -15,138 +15,79 @@
1515
*/
1616

1717
#if !DEVICE_LOWPOWERTIMER
18-
#error [NOT_SUPPORTED] Low power timer not supported for this target
18+
#error [NOT_SUPPORTED] Low power timer not supported for this target
1919
#endif
2020

21+
#include "mbed.h"
22+
#include "greentea-client/test_env.h"
2123
#include "utest/utest.h"
2224
#include "unity/unity.h"
23-
#include "greentea-client/test_env.h"
24-
25-
#include "mbed.h"
25+
#include "../timeout/timeout_tests.h"
2626

2727
using namespace utest::v1;
2828

29-
volatile static bool complete;
30-
static LowPowerTimeout lpt;
31-
32-
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
33-
#define LONG_TIMEOUT (100000)
34-
#define SHORT_TIMEOUT (600)
35-
36-
void cb_done() {
37-
complete = true;
38-
}
39-
40-
#if DEVICE_SLEEP
41-
void lp_timeout_1s_deepsleep(void)
29+
utest::v1::status_t greentea_failure_handler(const Case * const source, const failure_t reason)
4230
{
43-
complete = false;
44-
LowPowerTimer timer;
45-
46-
/*
47-
* Since deepsleep() may shut down the UART peripheral, we wait for 10ms
48-
* to allow for hardware serial buffers to completely flush.
49-
50-
* This should be replaced with a better function that checks if the
51-
* hardware buffers are empty. However, such an API does not exist now,
52-
* so we'll use the wait_ms() function for now.
53-
*/
54-
wait_ms(10);
55-
56-
/*
57-
* We use here the low power timer instead of microsecond timer for start and
58-
* end because the microseconds timer might be disable during deepsleep.
59-
*/
60-
timer.start();
61-
lpt.attach(&cb_done, 1);
62-
/* Make sure deepsleep is allowed, to go to deepsleep */
63-
bool deep_sleep_allowed = sleep_manager_can_deep_sleep();
64-
TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
65-
sleep();
66-
while (!complete);
67-
68-
/* It takes longer to wake up from deep sleep */
69-
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, timer.read_us());
70-
TEST_ASSERT_TRUE(complete);
31+
greentea_case_failure_abort_handler(source, reason);
32+
return STATUS_CONTINUE;
7133
}
7234

73-
void lp_timeout_1s_sleep(void)
74-
{
75-
complete = false;
76-
Timer timer;
77-
timer.start();
78-
79-
sleep_manager_lock_deep_sleep();
80-
lpt.attach(&cb_done, 1);
81-
bool deep_sleep_allowed = sleep_manager_can_deep_sleep();
82-
TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed");
83-
sleep();
84-
while (!complete);
85-
sleep_manager_unlock_deep_sleep();
86-
87-
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, timer.read_us());
88-
TEST_ASSERT_TRUE(complete);
89-
}
90-
#endif /* DEVICE_SLEEP */
35+
Case cases[] = {
36+
Case("Callback called once (attach)", test_single_call<AttachTester<LowPowerTimeout> >),
37+
Case("Callback called once (attach_us)", test_single_call<AttachUSTester<LowPowerTimeout> >),
9138

92-
void lp_timeout_us(uint32_t delay_us, uint32_t tolerance)
93-
{
94-
complete = false;
95-
Timer timer;
96-
timer.start();
39+
Case("Callback not called when cancelled (attach)", test_cancel<AttachTester<LowPowerTimeout> >),
40+
Case("Callback not called when cancelled (attach_us)", test_cancel<AttachUSTester<LowPowerTimeout> >),
9741

98-
lpt.attach_us(&cb_done, delay_us);
99-
while (!complete);
42+
Case("Callback override (attach)", test_override<AttachTester<LowPowerTimeout> >),
43+
Case("Callback override (attach_us)", test_override<AttachUSTester<LowPowerTimeout> >),
10044

101-
/* Using RTC which is less accurate */
102-
TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, timer.read_us());
103-
TEST_ASSERT_TRUE(complete);
104-
}
45+
Case("Multiple timeouts running in parallel (attach)", test_multiple<AttachTester<LowPowerTimeout> >),
46+
Case("Multiple timeouts running in parallel (attach_us)", test_multiple<AttachUSTester<LowPowerTimeout> >),
10547

106-
void lp_timeout_5s(void)
107-
{
108-
lp_timeout_us(5000000, LONG_TIMEOUT);
109-
}
48+
Case("Zero delay (attach)", test_no_wait<AttachTester<LowPowerTimeout> >),
49+
Case("Zero delay (attach_us)", test_no_wait<AttachUSTester<LowPowerTimeout> >),
11050

111-
void lp_timeout_1s(void)
112-
{
113-
lp_timeout_us(1000000, LONG_TIMEOUT);
114-
}
51+
Case("10 ms delay accuracy (attach)", test_delay_accuracy<AttachTester<LowPowerTimeout>, 10000, SHORT_DELTA_US>,
52+
greentea_failure_handler),
53+
Case("10 ms delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<LowPowerTimeout>, 10000, SHORT_DELTA_US>,
54+
greentea_failure_handler),
11555

116-
void lp_timeout_1ms(void)
117-
{
118-
lp_timeout_us(1000, SHORT_TIMEOUT);
119-
}
56+
Case("1 s delay accuracy (attach)", test_delay_accuracy<AttachTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
57+
greentea_failure_handler),
58+
Case("1 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
59+
greentea_failure_handler),
12060

121-
void lp_timeout_500us(void)
122-
{
123-
lp_timeout_us(500, SHORT_TIMEOUT);
124-
125-
}
61+
Case("5 s delay accuracy (attach)", test_delay_accuracy<AttachTester<LowPowerTimeout>, 5000000, LONG_DELTA_US>,
62+
greentea_failure_handler),
63+
Case("5 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<LowPowerTimeout>, 5000000, LONG_DELTA_US>,
64+
greentea_failure_handler),
12665

127-
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
128-
greentea_case_failure_abort_handler(source, reason);
129-
return STATUS_CONTINUE;
130-
}
131-
132-
Case cases[] = {
133-
Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler),
134-
Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler),
135-
Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler),
136-
Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler),
13766
#if DEVICE_SLEEP
138-
Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler),
139-
Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler),
140-
#endif /* DEVICE_SLEEP */
67+
Case("1 s delay during sleep (attach)", test_sleep<AttachTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
68+
greentea_failure_handler),
69+
Case("1 s delay during sleep (attach_us)", test_sleep<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
70+
greentea_failure_handler),
71+
72+
Case("1 s delay during deepsleep (attach)", test_deepsleep<AttachTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
73+
greentea_failure_handler),
74+
Case("1 s delay during deepsleep (attach_us)", test_deepsleep<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
75+
greentea_failure_handler),
76+
#endif
77+
78+
Case("Timing drift (attach)", test_drift<AttachTester<LowPowerTimeout> >),
79+
Case("Timing drift (attach_us)", test_drift<AttachUSTester<LowPowerTimeout> >),
14180
};
14281

143-
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
144-
GREENTEA_SETUP(20, "default_auto");
82+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
83+
{
84+
GREENTEA_SETUP(240, "timing_drift_auto");
14585
return greentea_test_setup_handler(number_of_cases);
14686
}
14787

14888
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
14989

150-
int main() {
90+
int main()
91+
{
15192
Harness::run(specification);
15293
}

TESTS/mbed_drivers/timeout/main.cpp

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,83 @@
1-
/*
2-
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3-
* SPDX-License-Identifier: Apache-2.0
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
43
*
5-
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6-
* not use this file except in compliance with the License.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
76
* You may obtain a copy of the License at
87
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
109
*
1110
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1413
* See the License for the specific language governing permissions and
1514
* limitations under the License.
1615
*/
17-
18-
19-
/*
20-
* Tests is to measure the accuracy of Timeout over a period of time
21-
*
22-
*
23-
* 1) DUT would start to update callback_trigger_count every milli sec
24-
* 2) Host would query what is current count base_time, Device responds by the callback_trigger_count
25-
* 3) Host after waiting for measurement stretch. It will query for device time again final_time.
26-
* 4) Host computes the drift considering base_time, final_time, transport delay and measurement stretch
27-
* 5) Finally host send the results back to device pass/fail based on tolerance.
28-
* 6) More details on tests can be found in timing_drift_auto.py
29-
*
30-
*/
31-
3216
#include "mbed.h"
3317
#include "greentea-client/test_env.h"
3418
#include "utest/utest.h"
3519
#include "unity/unity.h"
20+
#include "timeout_tests.h"
3621

3722
using namespace utest::v1;
3823

39-
#define PERIOD_US 10000
40-
41-
volatile int ticker_count = 0;
42-
volatile uint32_t callback_trigger_count = 0;
43-
static const int test_timeout = 240;
44-
Timeout timeout;
45-
46-
void set_incremeant_count() {
47-
timeout.attach_us(set_incremeant_count, PERIOD_US);
48-
++callback_trigger_count;
24+
utest::v1::status_t greentea_failure_handler(const Case * const source, const failure_t reason)
25+
{
26+
greentea_case_failure_abort_handler(source, reason);
27+
return STATUS_CONTINUE;
4928
}
5029

51-
void test_case_timeout() {
30+
Case cases[] = {
31+
Case("Callback called once (attach)", test_single_call<AttachTester<Timeout> >),
32+
Case("Callback called once (attach_us)", test_single_call<AttachUSTester<Timeout> >),
5233

53-
char _key[11] = { };
54-
char _value[128] = { };
55-
int expected_key = 1;
56-
uint8_t results_size = 0;
34+
Case("Callback not called when cancelled (attach)", test_cancel<AttachTester<Timeout> >),
35+
Case("Callback not called when cancelled (attach_us)", test_cancel<AttachUSTester<Timeout> >),
5736

58-
greentea_send_kv("timing_drift_check_start", 0);
59-
timeout.attach_us(set_incremeant_count, PERIOD_US);
37+
Case("Callback override (attach)", test_override<AttachTester<Timeout> >),
38+
Case("Callback override (attach_us)", test_override<AttachUSTester<Timeout> >),
6039

61-
// wait for 1st signal from host
62-
do {
63-
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
64-
expected_key = strcmp(_key, "base_time");
65-
} while (expected_key);
40+
Case("Multiple timeouts running in parallel (attach)", test_multiple<AttachTester<Timeout> >),
41+
Case("Multiple timeouts running in parallel (attach_us)", test_multiple<AttachUSTester<Timeout> >),
6642

67-
greentea_send_kv(_key, callback_trigger_count * PERIOD_US);
43+
Case("Zero delay (attach)", test_no_wait<AttachTester<Timeout> >),
44+
Case("Zero delay (attach_us)", test_no_wait<AttachUSTester<Timeout> >),
6845

69-
// wait for 2nd signal from host
70-
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
71-
greentea_send_kv(_key, callback_trigger_count * PERIOD_US);
46+
Case("10 ms delay accuracy (attach)", test_delay_accuracy<AttachTester<Timeout>, 10000, SHORT_DELTA_US>,
47+
greentea_failure_handler),
48+
Case("10 ms delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<Timeout>, 10000, SHORT_DELTA_US>,
49+
greentea_failure_handler),
7250

73-
//get the results from host
74-
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
51+
Case("1 s delay accuracy (attach)", test_delay_accuracy<AttachTester<Timeout>, 1000000, LONG_DELTA_US>,
52+
greentea_failure_handler),
53+
Case("1 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<Timeout>, 1000000, LONG_DELTA_US>,
54+
greentea_failure_handler),
7555

76-
TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key,"Host side script reported a fail...");
77-
}
56+
Case("5 s delay accuracy (attach)", test_delay_accuracy<AttachTester<Timeout>, 5000000, LONG_DELTA_US>,
57+
greentea_failure_handler),
58+
Case("5 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<Timeout>, 5000000, LONG_DELTA_US>,
59+
greentea_failure_handler),
60+
61+
#if DEVICE_SLEEP
62+
Case("1 s delay during sleep (attach)", test_sleep<AttachTester<Timeout>, 1000000, LONG_DELTA_US>,
63+
greentea_failure_handler),
64+
Case("1 s delay during sleep (attach_us)", test_sleep<AttachUSTester<Timeout>, 1000000, LONG_DELTA_US>,
65+
greentea_failure_handler),
66+
#endif
7867

79-
// Test casess
80-
Case cases[] = { Case("Timers: toggle on/off", test_case_timeout), };
68+
Case("Timing drift (attach)", test_drift<AttachTester<Timeout> >),
69+
Case("Timing drift (attach_us)", test_drift<AttachUSTester<Timeout> >),
70+
};
8171

82-
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
83-
GREENTEA_SETUP(test_timeout, "timing_drift_auto");
72+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
73+
{
74+
GREENTEA_SETUP(240, "timing_drift_auto");
8475
return greentea_test_setup_handler(number_of_cases);
8576
}
8677

8778
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
8879

89-
int main() {
80+
int main()
81+
{
9082
Harness::run(specification);
9183
}

0 commit comments

Comments
 (0)