Skip to content

Commit 08a8f16

Browse files
committed
Add wait_ns timing test
1 parent 7215515 commit 08a8f16

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

TESTS/mbed_platform/wait_ns/main.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2018, 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 "platform/mbed_wait_api.h"
23+
#include "hal/us_ticker_api.h"
24+
#include "hal/lp_ticker_api.h"
25+
26+
using namespace utest::v1;
27+
28+
/* This test is created based on the test for Timer class.
29+
* Since low power timer is less accurate than regular
30+
* timer we need to adjust delta.
31+
*/
32+
33+
/*
34+
* Define tolerance as follows:
35+
* Timer might be +/-5% out; wait_ns is permitted 20% slow, but not fast.
36+
* Therefore minimum measured time should be 95% of requested, maximum should
37+
* be 125%. Unity doesn't let us specify an asymmetric error though.
38+
*/
39+
#define TOLERANCE_MIN 0.95f
40+
#define TOLERANCE_MAX 1.25f
41+
#define MIDPOINT ((TOLERANCE_MIN+TOLERANCE_MAX)/2)
42+
#define DELTA (MIDPOINT-TOLERANCE_MIN)
43+
44+
/* This test verifies if wait_ns's wait time
45+
* is accurate, according to a timer.
46+
*
47+
* Given timer is created.
48+
* When timer is used to measure delay.
49+
* Then the results are valid (within acceptable range).
50+
*/
51+
template<int wait_val_ms, class CompareTimer>
52+
void test_wait_ns_time_measurement()
53+
{
54+
CompareTimer timer;
55+
56+
float wait_val_s = (float)wait_val_ms / 1000;
57+
58+
/* Start the timer. */
59+
timer.start();
60+
61+
/* Wait <wait_val_ms> ms - arithmetic inside wait_ns will overflow if
62+
* asked for too large a delay, so break it up.
63+
*/
64+
for (int i = 0; i < wait_val_ms; i++) {
65+
wait_ns(1000000);
66+
}
67+
68+
/* Stop the timer. */
69+
timer.stop();
70+
71+
/* Check results - wait_val_us us have elapsed. */
72+
TEST_ASSERT_FLOAT_WITHIN(DELTA * wait_val_s, MIDPOINT * wait_val_s, timer.read());
73+
}
74+
75+
utest::v1::status_t test_setup(const size_t number_of_cases)
76+
{
77+
GREENTEA_SETUP(15, "default_auto");
78+
return verbose_test_setup_handler(number_of_cases);
79+
}
80+
81+
Case cases[] = {
82+
#if DEVICE_LPTICKER
83+
Case("Test: wait_ns - compare with lp_timer 1s", test_wait_ns_time_measurement<1000, LowPowerTimer>),
84+
#endif
85+
Case("Test: wait_ns - compare with us_timer 1s", test_wait_ns_time_measurement<1000, Timer>)
86+
};
87+
88+
Specification specification(test_setup, cases);
89+
90+
int main()
91+
{
92+
return !Harness::run(specification);
93+
}

0 commit comments

Comments
 (0)