Skip to content

Commit e4b78b3

Browse files
Merge pull request #5306 from mprse/feature-hal-spec-rtc_tests-update
Update of RTC HAL API specification and tests.
2 parents 5e6c46e + df26cab commit e4b78b3

File tree

5 files changed

+234
-40
lines changed

5 files changed

+234
-40
lines changed

TESTS/mbed_hal/rtc/main.cpp

Lines changed: 140 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,96 @@
2626
#include "mbed.h"
2727
#include "rtc_api.h"
2828

29-
3029
using namespace utest::v1;
3130

3231
static const uint32_t WAIT_TIME = 4;
3332
static const uint32_t WAIT_TOLERANCE = 1;
3433

34+
#define US_PER_SEC 1000000
35+
#define ACCURACY_FACTOR 10
3536

36-
void rtc_init_test()
37+
static const uint32_t DELAY_4S = 4;
38+
static const uint32_t DELAY_10S = 10;
39+
static const uint32_t RTC_TOLERANCE = 1;
40+
static const uint32_t TOLERANCE_ACCURACY_US = (DELAY_10S * US_PER_SEC / ACCURACY_FACTOR);
41+
42+
#if DEVICE_LOWPOWERTIMER
43+
volatile bool expired;
44+
45+
void callback(void)
3746
{
38-
for (int i = 0; i < 10; i++) {
39-
rtc_init();
40-
}
47+
expired = true;
4148
}
4249

43-
void rtc_sleep_test()
50+
/* Auxiliary function to test if RTC continue counting in
51+
* sleep and deep-sleep modes. */
52+
void rtc_sleep_test_support (bool deepsleep_mode)
4453
{
54+
LowPowerTimeout timeout;
4555
const uint32_t start = 100;
56+
expired = false;
57+
58+
/*
59+
* Since deepsleep() may shut down the UART peripheral, we wait for 10ms
60+
* to allow for hardware serial buffers to completely flush.
61+
* This should be replaced with a better function that checks if the
62+
* hardware buffers are empty. However, such an API does not exist now,
63+
* so we'll use the wait_ms() function for now.
64+
*/
65+
wait_ms(10);
66+
4667
rtc_init();
4768

69+
if(deepsleep_mode == false) {
70+
sleep_manager_lock_deep_sleep();
71+
}
72+
4873
rtc_write(start);
49-
wait(WAIT_TIME);
74+
75+
timeout.attach(callback, DELAY_4S);
76+
77+
TEST_ASSERT(sleep_manager_can_deep_sleep() == deepsleep_mode);
78+
79+
while(!expired) sleep();
80+
5081
const uint32_t stop = rtc_read();
5182

83+
TEST_ASSERT_UINT32_WITHIN(RTC_TOLERANCE, DELAY_4S, stop - start);
84+
85+
timeout.detach();
86+
87+
if(deepsleep_mode == false) {
88+
sleep_manager_unlock_deep_sleep();
89+
}
90+
5291
rtc_free();
92+
}
93+
#endif
5394

54-
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
95+
/* Test that ::rtc_init can be called multiple times. */
96+
void rtc_init_test()
97+
{
98+
for (int i = 0; i < 10; i++) {
99+
rtc_init();
100+
}
101+
102+
rtc_free();
55103
}
56104

105+
#if DEVICE_LOWPOWERTIMER
106+
/** Test that the RTC keeps counting in the various sleep modes. */
107+
108+
void rtc_sleep_test()
109+
{
110+
/* Test sleep mode. */
111+
rtc_sleep_test_support(false);
112+
113+
/* Test deep-sleep mode. */
114+
rtc_sleep_test_support(true);
115+
}
116+
#endif
117+
118+
/* Test that the RTC keeps counting even after ::rtc_free has been called. */
57119
void rtc_persist_test()
58120
{
59121
const uint32_t start = 100;
@@ -72,6 +134,7 @@ void rtc_persist_test()
72134
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
73135
}
74136

137+
/* Test time does not glitch backwards due to an incorrectly implemented ripple counter driver. */
75138
void rtc_glitch_test()
76139
{
77140
const uint32_t start = 0xffffe;
@@ -88,12 +151,14 @@ void rtc_glitch_test()
88151
rtc_free();
89152
}
90153

154+
/* Test that the RTC correctly handles different time values. */
91155
void rtc_range_test()
92156
{
93157
static const uint32_t starts[] = {
94-
0x00000000,
95-
0xEFFFFFFF,
96-
0x00001000,
158+
0x00000000,
159+
0xEFFFFFFF,
160+
0x00001000,
161+
0x00010000,
97162
};
98163

99164
rtc_init();
@@ -107,21 +172,83 @@ void rtc_range_test()
107172
rtc_free();
108173
}
109174

175+
/* Test that the RTC accuracy is at least 10%. */
176+
void rtc_accuracy_test()
177+
{
178+
Timer timer1;
179+
180+
const uint32_t start = 100;
181+
rtc_init();
182+
rtc_write(start);
183+
184+
timer1.start();
185+
while(rtc_read() < (start + DELAY_10S)) {
186+
/* Just wait. */
187+
}
188+
timer1.stop();
189+
190+
/* RTC accuracy is at least 10%. */
191+
TEST_ASSERT_INT32_WITHIN(TOLERANCE_ACCURACY_US, DELAY_10S * US_PER_SEC, timer1.read_us());
192+
}
193+
194+
/* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */
195+
void rtc_write_read_test()
196+
{
197+
static const uint32_t rtc_init_val = 100;
198+
199+
rtc_init();
200+
201+
for (int i = 0; i < 3; i++) {
202+
const uint32_t init_val = (rtc_init_val + i * rtc_init_val);
203+
204+
core_util_critical_section_enter();
205+
206+
rtc_write(init_val);
207+
const uint32_t read_val = rtc_read();
208+
209+
core_util_critical_section_exit();
210+
211+
/* No tolerance is provided since we should have 1 second to
212+
* execute this case after the RTC time is set.
213+
*/
214+
TEST_ASSERT_EQUAL_UINT32(init_val, read_val);
215+
}
216+
217+
rtc_free();
218+
}
219+
220+
/* Test that ::is_enabled function returns 1 if the RTC is counting and the time has been set, 0 otherwise. */
221+
void rtc_enabled_test()
222+
{
223+
rtc_init();
224+
TEST_ASSERT_EQUAL_INT(0, rtc_isenabled());
225+
rtc_write(0);
226+
TEST_ASSERT_EQUAL_INT(1, rtc_isenabled());
227+
rtc_free();
228+
}
229+
110230
Case cases[] = {
111231
Case("RTC - init", rtc_init_test),
232+
#if DEVICE_LOWPOWERTIMER
112233
Case("RTC - sleep", rtc_sleep_test),
234+
#endif
113235
Case("RTC - persist", rtc_persist_test),
114236
Case("RTC - glitch", rtc_glitch_test),
115237
Case("RTC - range", rtc_range_test),
238+
Case("RTC - accuracy", rtc_accuracy_test),
239+
Case("RTC - write/read", rtc_write_read_test),
240+
Case("RTC - enabled", rtc_enabled_test),
116241
};
117242

118-
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
243+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
244+
{
119245
GREENTEA_SETUP(30, "default_auto");
120246
return greentea_test_setup_handler(number_of_cases);
121247
}
122248

123249
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
124250

125-
int main() {
251+
int main()
252+
{
126253
Harness::run(specification);
127254
}

TESTS/mbed_hal/rtc/rtc_test.h

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/** \addtogroup hal_rtc_tests
2-
* @{
3-
*/
41
/* mbed Microcontroller Library
52
* Copyright (c) 2017-2017 ARM Limited
63
*
@@ -16,6 +13,11 @@
1613
* See the License for the specific language governing permissions and
1714
* limitations under the License.
1815
*/
16+
17+
/** \addtogroup hal_rtc_tests
18+
* @{
19+
*/
20+
1921
#ifndef MBED_RTC_TEST_H
2022
#define MBED_RTC_TEST_H
2123

@@ -25,31 +27,81 @@
2527
extern "C" {
2628
#endif
2729

28-
/** Test that ::rtc_init can be called multiple times
30+
/** Test that ::rtc_init can be called multiple times.
31+
*
32+
* Given board provides RTC.
33+
* When ::rtc_init is called multiple times.
34+
* Then ::rtc_init are successfully performed (no exception is generated).
2935
*
3036
*/
3137
void rtc_init_test(void);
3238

33-
/** Test that the RTC keeps counting in the various sleep modes
39+
/** Test that the RTC keeps counting in the various sleep modes.
40+
*
41+
* Given board provides RTC.
42+
* When system enters sleep/deep-sleep mode.
43+
* RTC keeps counting.
3444
*
3545
*/
3646
void rtc_sleep_test(void);
3747

38-
/** Test that the RTC keeps counting even after ::rtc_free has been called
48+
/** Test that the RTC keeps counting even after ::rtc_free has been called.
49+
*
50+
* Given board provides RTC.
51+
* When ::rtc_free has been called.
52+
* RTC keeps counting.
3953
*
4054
*/
4155
void rtc_persist_test(void);
4256

43-
/** Test time does not glitch backwards due to an incorrectly implemented ripple counter driver
57+
/** Test time does not glitch backwards due to an incorrectly implemented ripple counter driver.
58+
*
59+
* Given board provides RTC.
60+
* When RTC is enabled and counts.
61+
* Then time does not glitch backwards due to an incorrectly implemented ripple counter driver.
4462
*
4563
*/
4664
void rtc_glitch_test(void);
4765

48-
/** Test that the RTC correctly handles large time values
66+
/** Test that the RTC correctly handles large time values.
4967
*
68+
* Given board provides RTC.
69+
* When RTC is enabled and counts.
70+
* The RTC correctly handles different time values.
5071
*/
5172
void rtc_range_test(void);
5273

74+
/** Test that the RTC accuracy is at least 10%.
75+
*
76+
* Given platform provides Real Time Clock.
77+
* When delay is performed based on RTC (10 sec delay).
78+
* Then the delay time measured using high frequency timer indicate that RTC accuracy is at least 10%.
79+
*
80+
*/
81+
void rtc_accuracy_test(void);
82+
83+
/** Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time.
84+
*
85+
* Given platform provides Real Time Clock.
86+
* When an example RTC time is set by means of rtc_write function and rtc_read is performed immediately after this operation.
87+
* Then rtc_read function returns time which has been set.
88+
*
89+
*/
90+
void rtc_write_read_test(void);
91+
92+
/** Test that ::rtc_isenabled function returns 1 if the RTC is counting and the time has been set, 0 otherwise
93+
*
94+
* NOTE: RTC is counting when it has been initialised by means of rtc_init().
95+
* RTC time is set by means of rtc_write() function.
96+
* RTC must be initialised before rtc_isenabled() function can be called.
97+
*
98+
* Given platform provides Real Time Clock.
99+
* When rtc_isenabled() function is called.
100+
* Then the result is 1 if RTC time has been set, otherwise the result is 0.
101+
*
102+
*/
103+
void rtc_enabled_test(void);
104+
53105
/**@}*/
54106

55107
#ifdef __cplusplus

TESTS/mbed_hal/rtc_reset/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static cmd_status_t handle_command(const char *key, const char *value)
7171
}
7272
}
7373

74+
/* Test that software reset doesn't stop RTC from counting. */
7475
void rtc_reset_test()
7576
{
7677
GREENTEA_SETUP(60, "rtc_reset");

TESTS/mbed_hal/rtc_reset/rtc_reset_test.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
/** \addtogroup hal_rtc_tests
18+
* @{
19+
*/
20+
1621
#ifndef MBED_RTC_TEST_H
1722
#define MBED_RTC_TEST_H
1823

@@ -22,8 +27,12 @@
2227
extern "C" {
2328
#endif
2429

25-
/** Test that the RTC does not stop counting after a software reset
26-
* \ingroup hal_rtc_tests
30+
/** Test that the RTC does not stop counting after a software reset.
31+
*
32+
* Given board provides RTC.
33+
* When software reset is performed.
34+
* Then the RTC does not stop counting.
35+
*
2736
*/
2837
void rtc_reset_test();
2938

0 commit comments

Comments
 (0)