Skip to content

Commit 23be7a8

Browse files
Merge pull request #5074 from mprse/lp_timer_test
Add Low Power Timer test.
2 parents 493e378 + 34a0326 commit 23be7a8

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed

TESTS/mbed_drivers/lp_timer/main.cpp

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
/*
2+
* Copyright (c) 2017, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* 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.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "rtos.h"
23+
#include "hal/us_ticker_api.h"
24+
25+
#if !DEVICE_LOWPOWERTIMER
26+
#error [NOT_SUPPORTED] test not supported
27+
#endif
28+
29+
using namespace utest::v1;
30+
31+
extern uint32_t SystemCoreClock;
32+
33+
/* This test is created based on the test for Timer class.
34+
* Since low power timer is less accurate than regular
35+
* timer we need to adjust delta.
36+
*/
37+
38+
/* Macro to define delta based on CPU clock frequency.
39+
*
40+
* Note that some extra time is counted by the timer.
41+
* Additional time is caused by the function calls and
42+
* additional operations performed by wait and
43+
* stop functions before in fact timer is stopped. This may
44+
* add additional time to the counted result.
45+
*
46+
* To take in to account this extra time we introduce DELTA
47+
* value based on CPU clock (speed):
48+
* DELTA = TOLERANCE_FACTOR / SystemCoreClock * US_FACTOR
49+
*
50+
* e.g.
51+
* For K64F DELTA = (40000 / 120000000) * 1000000 = 333[us]
52+
* For NUCLEO_F070RB DELTA = (40000 / 48000000) * 1000000 = 833[us]
53+
* For NRF51_DK DELTA = (40000 / 16000000) * 1000000 = 2500[us]
54+
*/
55+
#define US_PER_SEC 1000000
56+
#define TOLERANCE_FACTOR 40000.0f
57+
#define US_FACTOR 1000000.0f
58+
59+
static const int delta_sys_clk_us = ((int) (TOLERANCE_FACTOR / (float) SystemCoreClock * US_FACTOR));
60+
61+
#define DELTA_US delta_sys_clk_us
62+
#define DELTA_S ((float)delta_sys_clk_us/US_PER_SEC)
63+
#define DELTA_MS 1
64+
65+
/* This test verifies if low power timer is stopped after
66+
* creation.
67+
*
68+
* Given Timer has been successfully created.
69+
* When read of timer elapsed time is requested.
70+
* Then result is always 0.
71+
*/
72+
void test_lptimer_creation()
73+
{
74+
LowPowerTimer lp_timer;
75+
76+
/* Check results. */
77+
TEST_ASSERT_EQUAL_FLOAT(0, lp_timer.read());
78+
TEST_ASSERT_EQUAL(0, lp_timer.read_ms());
79+
TEST_ASSERT_EQUAL(0, lp_timer.read_us());
80+
TEST_ASSERT_EQUAL(0, lp_timer.read_high_resolution_us());
81+
82+
/* Wait 10 ms.
83+
* After that operation timer read routines should still return 0. */
84+
wait_ms(10);
85+
86+
/* Check results. */
87+
TEST_ASSERT_EQUAL_FLOAT(0, lp_timer.read());
88+
TEST_ASSERT_EQUAL(0, lp_timer.read_ms());
89+
TEST_ASSERT_EQUAL(0, lp_timer.read_us());
90+
TEST_ASSERT_EQUAL(0, lp_timer.read_high_resolution_us());
91+
}
92+
93+
/* This test verifies if read(), read_us(), read_ms(),
94+
* read_high_resolution_us()
95+
* functions return time accumulated between
96+
* low power timer starts and stops.
97+
*
98+
* Given Timer has been successfully created and
99+
* few times started and stopped after a specified period of time.
100+
* When timer read request is performed.
101+
* Then read functions return accumulated time elapsed between starts
102+
* and stops.
103+
*/
104+
void test_lptimer_time_accumulation()
105+
{
106+
LowPowerTimer lp_timer;
107+
108+
/* Start the timer. */
109+
lp_timer.start();
110+
111+
/* Wait 10 ms. */
112+
wait_ms(10);
113+
114+
/* Stop the timer. */
115+
lp_timer.stop();
116+
117+
/* Check results - totally 10 ms have elapsed. */
118+
TEST_ASSERT_FLOAT_WITHIN(DELTA_S, 0.010f, lp_timer.read());
119+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 10, lp_timer.read_ms());
120+
TEST_ASSERT_INT32_WITHIN(DELTA_US, 10000, lp_timer.read_us());
121+
TEST_ASSERT_UINT64_WITHIN(DELTA_US, 10000, lp_timer.read_high_resolution_us());
122+
123+
/* Wait 50 ms - this is done to show that time elapsed when
124+
* the timer is stopped does not have influence on the
125+
* timer counted time. */
126+
wait_ms(50);
127+
128+
/* ------ */
129+
130+
/* Start the timer. */
131+
lp_timer.start();
132+
133+
/* Wait 20 ms. */
134+
wait_ms(20);
135+
136+
/* Stop the timer. */
137+
lp_timer.stop();
138+
139+
/* Check results - totally 30 ms have elapsed. */
140+
TEST_ASSERT_FLOAT_WITHIN(2 * DELTA_S, 0.030f, lp_timer.read());
141+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 30, lp_timer.read_ms());
142+
TEST_ASSERT_INT32_WITHIN(2 * DELTA_US, 30000, lp_timer.read_us());
143+
TEST_ASSERT_UINT64_WITHIN(2 * DELTA_US, 30000, lp_timer.read_high_resolution_us());
144+
145+
/* Wait 50 ms - this is done to show that time elapsed when
146+
* the timer is stopped does not have influence on the
147+
* timer counted time. */
148+
149+
/* ------ */
150+
151+
/* Start the timer. */
152+
lp_timer.start();
153+
154+
/* Wait 30 ms. */
155+
wait_ms(30);
156+
157+
/* Stop the timer. */
158+
lp_timer.stop();
159+
160+
/* Check results - totally 60 ms have elapsed. */
161+
TEST_ASSERT_FLOAT_WITHIN(3 * DELTA_S, 0.060f, lp_timer.read());
162+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 60, lp_timer.read_ms());
163+
TEST_ASSERT_INT32_WITHIN(3 * DELTA_US, 60000, lp_timer.read_us());
164+
TEST_ASSERT_UINT64_WITHIN(3 * DELTA_US, 60000, lp_timer.read_high_resolution_us());
165+
166+
/* Wait 50 ms - this is done to show that time elapsed when
167+
* the timer is stopped does not have influence on the
168+
* timer time. */
169+
wait_ms(50);
170+
171+
/* ------ */
172+
173+
/* Start the timer. */
174+
lp_timer.start();
175+
176+
/* Wait 1 sec. */
177+
wait_ms(1000);
178+
179+
/* Stop the timer. */
180+
lp_timer.stop();
181+
182+
/* Check results - totally 5060 ms have elapsed. */
183+
TEST_ASSERT_FLOAT_WITHIN(4 * DELTA_S, 1.060f, lp_timer.read());
184+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 1060, lp_timer.read_ms());
185+
TEST_ASSERT_INT32_WITHIN(4 * DELTA_US, 1060000, lp_timer.read_us());
186+
TEST_ASSERT_UINT64_WITHIN(4 * DELTA_US, 1060000, lp_timer.read_high_resolution_us());
187+
}
188+
189+
/* This test verifies if reset() function resets the
190+
* low power timer counted time.
191+
*
192+
* Given timer has been started and stopped once, then reset
193+
* operation was performed.
194+
* When timer is started and stopped next time.
195+
* Then timer read functions returns only the the second
196+
* measured time.
197+
*/
198+
void test_lptimer_reset()
199+
{
200+
LowPowerTimer lp_timer;
201+
202+
/* First measure 10 ms delay. */
203+
lp_timer.start();
204+
205+
/* Wait 10 ms. */
206+
wait_ms(10);
207+
208+
/* Stop the timer. */
209+
lp_timer.stop();
210+
211+
/* Check results - totally 10 ms elapsed. */
212+
TEST_ASSERT_FLOAT_WITHIN(DELTA_S, 0.010f, lp_timer.read());
213+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 10, lp_timer.read_ms());
214+
TEST_ASSERT_INT32_WITHIN(DELTA_US, 10000, lp_timer.read_us());
215+
TEST_ASSERT_UINT64_WITHIN(DELTA_US, 10000, lp_timer.read_high_resolution_us());
216+
217+
/* Reset the timer - previous measured time should be lost now. */
218+
lp_timer.reset();
219+
220+
/* Now measure 20 ms delay. */
221+
lp_timer.start();
222+
223+
/* Wait 20 ms. */
224+
wait_ms(20);
225+
226+
/* Stop the timer. */
227+
lp_timer.stop();
228+
229+
/* Check results - 20 ms elapsed since the reset. */
230+
TEST_ASSERT_FLOAT_WITHIN(DELTA_S, 0.020f, lp_timer.read());
231+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 20, lp_timer.read_ms());
232+
TEST_ASSERT_INT32_WITHIN(DELTA_US, 20000, lp_timer.read_us());
233+
TEST_ASSERT_UINT64_WITHIN(DELTA_US, 20000, lp_timer.read_high_resolution_us());
234+
}
235+
236+
/* This test verifies if calling start() for already
237+
* started low power timer does nothing.
238+
*
239+
* Given timer is already started.
240+
* When timer is started again.
241+
* Then second start operation is ignored.
242+
*/
243+
void test_lptimer_start_started_timer()
244+
{
245+
LowPowerTimer lp_timer;
246+
247+
/* Start the timer. */
248+
lp_timer.start();
249+
250+
/* Wait 10 ms. */
251+
wait_ms(10);
252+
253+
/* Now start timer again. */
254+
lp_timer.start();
255+
256+
/* Wait 20 ms. */
257+
wait_ms(20);
258+
259+
/* Stop the timer. */
260+
lp_timer.stop();
261+
262+
/* Check results - 30 ms have elapsed since the first start. */
263+
TEST_ASSERT_FLOAT_WITHIN(2 * DELTA_S, 0.030f, lp_timer.read());
264+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, 30, lp_timer.read_ms());
265+
TEST_ASSERT_INT32_WITHIN(2 * DELTA_US, 30000, lp_timer.read_us());
266+
TEST_ASSERT_UINT64_WITHIN(2 * DELTA_US, 30000, lp_timer.read_high_resolution_us());
267+
}
268+
269+
/* This test verifies low power timer float operator.
270+
*
271+
* Given timer is created and a time period time is counted.
272+
* When timer object is casted on float type.
273+
* Then counted type in seconds is returned by means of
274+
* read() function.
275+
*/
276+
void test_lptimer_float_operator()
277+
{
278+
LowPowerTimer lp_timer;
279+
280+
/* Start the timer. */
281+
lp_timer.start();
282+
283+
/* Wait 10 ms. */
284+
wait_ms(10);
285+
286+
/* Stop the timer. */
287+
lp_timer.stop();
288+
289+
/* Check result - 10 ms elapsed. */
290+
TEST_ASSERT_FLOAT_WITHIN(DELTA_S, 0.010f, (float )(lp_timer));
291+
}
292+
293+
/* This test verifies if time counted by the low power timer is
294+
* valid.
295+
*
296+
* Given timer is created.
297+
* When timer is used to measure 1ms/10ms/100ms/1s
298+
* delays.
299+
* Then the results are valid (within acceptable range).
300+
*/
301+
template<int wait_val_us>
302+
void test_lptimer_time_measurement()
303+
{
304+
LowPowerTimer lp_timer;
305+
306+
/* Start the timer. */
307+
lp_timer.start();
308+
309+
/* Wait <wait_val_us> us. */
310+
wait_us(wait_val_us);
311+
312+
/* Stop the timer. */
313+
lp_timer.stop();
314+
315+
/* Check results - wait_val_us us have elapsed. */
316+
TEST_ASSERT_FLOAT_WITHIN(DELTA_S, (float )wait_val_us / 1000000, lp_timer.read());
317+
TEST_ASSERT_INT32_WITHIN(DELTA_MS, wait_val_us / 1000, lp_timer.read_ms());
318+
TEST_ASSERT_INT32_WITHIN(DELTA_US, wait_val_us, lp_timer.read_us());
319+
TEST_ASSERT_UINT64_WITHIN(DELTA_US, wait_val_us, lp_timer.read_high_resolution_us());
320+
}
321+
322+
utest::v1::status_t test_setup(const size_t number_of_cases)
323+
{
324+
GREENTEA_SETUP(15, "default_auto");
325+
return verbose_test_setup_handler(number_of_cases);
326+
}
327+
328+
Case cases[] = {
329+
Case("Test: LowPowerTimer - stopped after creation.", test_lptimer_creation),
330+
Case("Test: LowPowerTimer - measure time accumulation.", test_lptimer_time_accumulation),
331+
Case("Test: LowPowerTimer - reset.", test_lptimer_reset),
332+
Case("Test: LowPowerTimer - start started timer.", test_lptimer_start_started_timer),
333+
Case("Test: LowPowerTimer - float operator.", test_lptimer_float_operator),
334+
Case("Test: LowPowerTimer - time measurement 1 ms.", test_lptimer_time_measurement<1000>),
335+
Case("Test: LowPowerTimer - time measurement 10 ms.", test_lptimer_time_measurement<10000>),
336+
Case("Test: LowPowerTimer - time measurement 100 ms.", test_lptimer_time_measurement<100000>),
337+
Case("Test: LowPowerTimer - time measurement 1 s.", test_lptimer_time_measurement<1000000>)
338+
};
339+
340+
Specification specification(test_setup, cases);
341+
342+
int main()
343+
{
344+
return !Harness::run(specification);
345+
}

0 commit comments

Comments
 (0)