Skip to content

Commit cc6f4c5

Browse files
add kernel ticker test
1 parent f3424da commit cc6f4c5

File tree

1 file changed

+119
-0
lines changed
  • TESTS/mbedmicro-rtos-mbed/kernel_tick_count

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018-2018 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+
#include "greentea-client/test_env.h"
17+
#include "utest/utest.h"
18+
#include "unity/unity.h"
19+
20+
#include "rtos/Kernel.h"
21+
#include "mbed.h"
22+
23+
24+
using utest::v1::Case;
25+
26+
#define TEST_REPEAT_COUNT 1000
27+
#define NUM_WAIT_TICKS 1000
28+
29+
// all in [us]
30+
#define ONE_SECOND 1000000
31+
#define SMALL_DELTA 1500 // 0.15%
32+
#define BIG_DELTA 15000 // 1.5%
33+
34+
/** Test if kernel ticker frequency is 1kHz
35+
36+
Given a RTOS kernel ticker
37+
When check it frequency
38+
Then the the frequency is 1kHz
39+
*/
40+
void test_frequency()
41+
{
42+
uint32_t freq = osKernelGetTickFreq();
43+
TEST_ASSERT_EQUAL_UINT32_MESSAGE(1000, freq, "Expected SysTick frequency is 1kHz");
44+
}
45+
46+
/** Test if kernel ticker increments by one
47+
48+
Given a RTOS kernel ticker
49+
When perform subsequent calls of @a rtos::Kernel::get_ms_count
50+
Then subsequent reads should not differ by more than one
51+
*/
52+
void test_increment(void)
53+
{
54+
for (uint32_t i = 0; i < TEST_REPEAT_COUNT; i++) {
55+
const uint64_t start = rtos::Kernel::get_ms_count();
56+
while (true) {
57+
uint64_t diff = rtos::Kernel::get_ms_count() - start;
58+
if (diff != 0) {
59+
TEST_ASSERT_EQUAL_UINT64(1, diff);
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
66+
/** Test if kernel ticker interval is 1ms
67+
68+
Given a RTOS kernel ticker
69+
When perform subsequent calls of @a rtos::Kernel::get_ms_count
70+
Then the ticker interval should be 1ms
71+
*/
72+
void test_interval()
73+
{
74+
uint64_t start, stop;
75+
Timer timer;
76+
77+
start = rtos::Kernel::get_ms_count();
78+
// wait for tick
79+
do {
80+
stop = rtos::Kernel::get_ms_count();
81+
} while ((stop - start) == 0);
82+
timer.start();
83+
start = stop;
84+
85+
// wait for NUM_WAIT_TICKS ticks
86+
do {
87+
stop = rtos::Kernel::get_ms_count();
88+
} while ((stop - start) != NUM_WAIT_TICKS);
89+
timer.stop();
90+
TEST_ASSERT_EQUAL_UINT64(NUM_WAIT_TICKS, (stop - start));
91+
92+
#if defined(NO_SYSTICK) || defined(MBED_TICKLESS)
93+
// On targets with NO_SYSTICK/MBED_TICKLESS enabled, systick is emulated by lp_ticker what makes it less accurate
94+
// for more details https://os.mbed.com/docs/latest/reference/tickless.html
95+
TEST_ASSERT_UINT64_WITHIN(BIG_DELTA, ONE_SECOND, timer.read_high_resolution_us());
96+
#else
97+
TEST_ASSERT_UINT64_WITHIN(SMALL_DELTA, ONE_SECOND, timer.read_high_resolution_us());
98+
#endif
99+
}
100+
101+
// Test cases
102+
Case cases[] = {
103+
Case("Test kernel ticker frequency", test_frequency),
104+
Case("Test if kernel ticker increments by one", test_increment),
105+
Case("Test if kernel ticker interval is 1ms", test_interval)
106+
};
107+
108+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
109+
{
110+
GREENTEA_SETUP(10, "timing_drift_auto");
111+
return utest::v1::greentea_test_setup_handler(number_of_cases);
112+
}
113+
114+
utest::v1::Specification specification(greentea_test_setup, cases);
115+
116+
int main()
117+
{
118+
return !utest::v1::Harness::run(specification);
119+
}

0 commit comments

Comments
 (0)