Skip to content

Commit 7203be9

Browse files
committed
Add tests for LowPowerTimeout and lp_ticker
1 parent 584f997 commit 7203be9

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if !DEVICE_LOWPOWERTIMER
18+
#error [NOT_SUPPORTED] Low power timer not supported for this target
19+
#endif
20+
21+
#include "utest/utest.h"
22+
#include "unity/unity.h"
23+
#include "greentea-client/test_env.h"
24+
25+
#include "mbed.h"
26+
#include "us_ticker_api.h"
27+
28+
using namespace utest::v1;
29+
30+
volatile static bool complete;
31+
static LowPowerTimeout lpt;
32+
33+
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
34+
#define LONG_TIMEOUT (100000)
35+
#define SHORT_TIMEOUT (600)
36+
37+
void cb_done() {
38+
complete = true;
39+
}
40+
41+
#if DEVICE_SLEEP
42+
void lp_timeout_1s_deepsleep(void)
43+
{
44+
complete = false;
45+
46+
timestamp_t start = us_ticker_read();
47+
lpt.attach(&cb_done, 1);
48+
deepsleep();
49+
while (!complete);
50+
timestamp_t end = us_ticker_read();
51+
52+
/* It takes longer to wake up from deep sleep */
53+
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
54+
TEST_ASSERT_TRUE(complete);
55+
}
56+
57+
void lp_timeout_1s_sleep(void)
58+
{
59+
complete = false;
60+
61+
timestamp_t start = us_ticker_read();
62+
lpt.attach(&cb_done, 1);
63+
sleep();
64+
while (!complete);
65+
timestamp_t end = us_ticker_read();
66+
67+
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
68+
TEST_ASSERT_TRUE(complete);
69+
}
70+
#endif /* DEVICE_SLEEP */
71+
72+
void lp_timeout_us(uint32_t delay_us, uint32_t tolerance)
73+
{
74+
complete = false;
75+
76+
timestamp_t start = us_ticker_read();
77+
lpt.attach_us(&cb_done, delay_us);
78+
while (!complete);
79+
timestamp_t end = us_ticker_read();
80+
81+
/* Using RTC which is less accurate */
82+
TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
83+
TEST_ASSERT_TRUE(complete);
84+
}
85+
86+
void lp_timeout_5s(void)
87+
{
88+
lp_timeout_us(5000000, LONG_TIMEOUT);
89+
}
90+
91+
void lp_timeout_1s(void)
92+
{
93+
lp_timeout_us(1000000, LONG_TIMEOUT);
94+
}
95+
96+
void lp_timeout_1ms(void)
97+
{
98+
lp_timeout_us(1000, SHORT_TIMEOUT);
99+
}
100+
101+
void lp_timeout_500us(void)
102+
{
103+
lp_timeout_us(500, SHORT_TIMEOUT);
104+
105+
}
106+
107+
status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
108+
greentea_case_failure_abort_handler(source, reason);
109+
return STATUS_CONTINUE;
110+
}
111+
112+
Case cases[] = {
113+
Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler),
114+
Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler),
115+
Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler),
116+
Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler),
117+
#if DEVICE_SLEEP
118+
Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler),
119+
Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler),
120+
#endif /* DEVICE_SLEEP */
121+
};
122+
123+
status_t greentea_test_setup(const size_t number_of_cases) {
124+
GREENTEA_SETUP(20, "default_auto");
125+
return greentea_test_setup_handler(number_of_cases);
126+
}
127+
128+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
129+
130+
int main() {
131+
Harness::run(specification);
132+
}

TESTS/mbed_hal/lp_ticker/main.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if !DEVICE_LOWPOWERTIMER
18+
#error [NOT_SUPPORTED] Low power timer not supported for this target
19+
#endif
20+
21+
#include "utest/utest.h"
22+
#include "unity/unity.h"
23+
#include "greentea-client/test_env.h"
24+
25+
#include "mbed.h"
26+
#include "us_ticker_api.h"
27+
#include "lp_ticker_api.h"
28+
29+
using namespace utest::v1;
30+
31+
volatile static bool complete;
32+
static ticker_event_t delay_event;
33+
static const ticker_data_t *lp_ticker_data = get_lp_ticker_data();
34+
35+
36+
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
37+
#define LONG_TIMEOUT (100000)
38+
#define SHORT_TIMEOUT (600)
39+
40+
void cb_done(uint32_t id) {
41+
complete = true;
42+
}
43+
44+
void lp_ticker_delay_us(uint32_t delay_us, uint32_t tolerance)
45+
{
46+
complete = false;
47+
uint32_t delay_ts;
48+
49+
ticker_set_handler(lp_ticker_data, cb_done);
50+
ticker_remove_event(lp_ticker_data, &delay_event);
51+
delay_ts = lp_ticker_read() + delay_us;
52+
53+
timestamp_t start = us_ticker_read();
54+
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
55+
while (!complete);
56+
timestamp_t end = us_ticker_read();
57+
58+
TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
59+
TEST_ASSERT_TRUE(complete);
60+
}
61+
62+
#if DEVICE_SLEEP
63+
void lp_ticker_1s_deepsleep()
64+
{
65+
complete = false;
66+
uint32_t delay_ts;
67+
68+
ticker_set_handler(lp_ticker_data, cb_done);
69+
ticker_remove_event(lp_ticker_data, &delay_event);
70+
delay_ts = lp_ticker_read() + 1000000;
71+
72+
timestamp_t start = us_ticker_read();
73+
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
74+
deepsleep();
75+
while (!complete);
76+
timestamp_t end = us_ticker_read();
77+
78+
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
79+
TEST_ASSERT_TRUE(complete);
80+
}
81+
82+
void lp_ticker_1s_sleep()
83+
{
84+
complete = false;
85+
uint32_t delay_ts;
86+
87+
ticker_set_handler(lp_ticker_data, cb_done);
88+
ticker_remove_event(lp_ticker_data, &delay_event);
89+
delay_ts = lp_ticker_read() + 1000000;
90+
91+
timestamp_t start = us_ticker_read();
92+
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
93+
sleep();
94+
while (!complete);
95+
timestamp_t end = us_ticker_read();
96+
97+
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
98+
TEST_ASSERT_TRUE(complete);
99+
}
100+
#endif /* DEVICE_SLEEP */
101+
102+
void lp_ticker_500us(void)
103+
{
104+
lp_ticker_delay_us(500, SHORT_TIMEOUT);
105+
}
106+
107+
void lp_ticker_1ms(void)
108+
{
109+
lp_ticker_delay_us(1000, SHORT_TIMEOUT);
110+
}
111+
112+
void lp_ticker_1s(void)
113+
{
114+
lp_ticker_delay_us(1000000, LONG_TIMEOUT);
115+
}
116+
117+
void lp_ticker_5s(void)
118+
{
119+
lp_ticker_delay_us(5000000, LONG_TIMEOUT);
120+
}
121+
122+
status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
123+
greentea_case_failure_abort_handler(source, reason);
124+
return STATUS_CONTINUE;
125+
}
126+
127+
Case cases[] = {
128+
Case("500us lp_ticker", lp_ticker_500us, greentea_failure_handler),
129+
Case("1ms lp_ticker", lp_ticker_1ms, greentea_failure_handler),
130+
Case("1s lp_ticker", lp_ticker_1s, greentea_failure_handler),
131+
Case("5s lp_ticker", lp_ticker_5s, greentea_failure_handler),
132+
#if DEVICE_SLEEP
133+
Case("1s lp_ticker sleep", lp_ticker_1s_sleep, greentea_failure_handler),
134+
Case("1s lp_ticker deepsleep", lp_ticker_1s_deepsleep, greentea_failure_handler),
135+
#endif /* DEVICE_SLEEP */
136+
};
137+
138+
status_t greentea_test_setup(const size_t number_of_cases) {
139+
GREENTEA_SETUP(20, "default_auto");
140+
lp_ticker_data->interface->init();
141+
return greentea_test_setup_handler(number_of_cases);
142+
}
143+
144+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
145+
146+
int main() {
147+
Harness::run(specification);
148+
}

0 commit comments

Comments
 (0)