Skip to content

Commit 9244f70

Browse files
committed
Add documentation and test the HAL RTC API
Add requirements, tests, an example implementation and additional function documentation to the HAL RTC API.
1 parent 63cccd9 commit 9244f70

File tree

6 files changed

+563
-8
lines changed

6 files changed

+563
-8
lines changed

TESTS/host_tests/rtc_reset.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2017-2017 ARM Limited
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may 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,
13+
WITHOUT 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+
from __future__ import print_function
18+
19+
from mbed_host_tests import BaseHostTest
20+
from time import sleep
21+
22+
23+
class RtcResetTest(BaseHostTest):
24+
"""This test checks that a device's RTC keeps count through a reset
25+
26+
It does this by setting the RTC's time, triggering a reset,
27+
delaying and then reading the RTC's time again to ensure
28+
that the RTC is still counting.
29+
"""
30+
31+
"""Start of the RTC"""
32+
START_TIME = 50000
33+
START_TIME_TOLERANCE = 10
34+
"""Time to delay after sending reset"""
35+
DELAY_TIME = 4.0
36+
DELAY_TOLERANCE = 1.0
37+
VALUE_PLACEHOLDER = "0"
38+
39+
def setup(self):
40+
"""Register callbacks required for the test"""
41+
self._error = False
42+
generator = self.rtc_reset_test()
43+
generator.next()
44+
45+
def run_gen(key, value, time):
46+
"""Run the generator, and fail testing if the iterator stops"""
47+
if self._error:
48+
return
49+
try:
50+
print("Calling generator")
51+
generator.send((key, value, time))
52+
except StopIteration:
53+
self._error = True
54+
55+
for resp in ("start", "read"):
56+
self.register_callback(resp, run_gen)
57+
58+
def teardown(self):
59+
"""No work to do here"""
60+
pass
61+
62+
def rtc_reset_test(self):
63+
"""Generator for running the reset test
64+
65+
This function calls yield to wait for the next event from
66+
the device. If the device gives the wrong response, then the
67+
generator terminates by returing which raises a StopIteration
68+
exception and fails the test.
69+
"""
70+
71+
# Wait for start token
72+
key, value, time = yield
73+
if key != "start":
74+
return
75+
76+
# Initialize, and set the time
77+
self.send_kv("init", self.VALUE_PLACEHOLDER)
78+
self.send_kv("write", str(self.START_TIME))
79+
self.send_kv("read", self.VALUE_PLACEHOLDER)
80+
key, value, time = yield
81+
if key != "read":
82+
return
83+
dev_time_start = int(value)
84+
85+
# Unitialize, and reset
86+
self.send_kv("free", self.VALUE_PLACEHOLDER)
87+
self.send_kv("reset", self.VALUE_PLACEHOLDER)
88+
sleep(self.DELAY_TIME)
89+
90+
# Restart the test, and send the sync token
91+
self.send_kv("__sync", "00000000-0000-000000000-000000000000")
92+
key, value, time = yield
93+
if key != "start":
94+
return
95+
96+
# Initialize, and read the time
97+
self.send_kv("init", self.VALUE_PLACEHOLDER)
98+
self.send_kv("read", self.VALUE_PLACEHOLDER)
99+
key, value, time = yield
100+
if key != "read":
101+
return
102+
dev_time_end = int(value)
103+
104+
# Check result
105+
elapsed = dev_time_end - dev_time_start
106+
start_time_valid = (self.START_TIME <= dev_time_start <
107+
self.START_TIME + self.START_TIME_TOLERANCE)
108+
elapsed_time_valid = elapsed >= self.DELAY_TIME - self.DELAY_TOLERANCE
109+
passed = start_time_valid and elapsed_time_valid
110+
if not start_time_valid:
111+
self.log("FAIL: Expected start time of %i got %i" %
112+
(self.START_TIME, dev_time_start))
113+
elif not passed:
114+
self.log("FAIL: Delayed for %fs but device "
115+
"reported elapsed time of %fs" %
116+
(self.DELAY_TIME, elapsed))
117+
self.send_kv("exit", "pass" if passed else "fail")
118+
yield # No more events expected
119+

TESTS/mbed_hal/rtc/main.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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_RTC
18+
#error [NOT_SUPPORTED] RTC API 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+
#include "rtc_test.h"
25+
26+
#include "mbed.h"
27+
#include "rtc_api.h"
28+
29+
30+
using namespace utest::v1;
31+
32+
static const uint32_t WAIT_TIME = 4;
33+
static const uint32_t WAIT_TOLERANCE = 1;
34+
35+
36+
void rtc_init_test()
37+
{
38+
for (int i = 0; i < 10; i++) {
39+
rtc_init();
40+
}
41+
}
42+
43+
void rtc_sleep_test()
44+
{
45+
const uint32_t start = 100;
46+
rtc_init();
47+
48+
rtc_write(start);
49+
wait(WAIT_TIME);
50+
const uint32_t stop = rtc_read();
51+
52+
rtc_free();
53+
54+
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
55+
}
56+
57+
void rtc_persist_test()
58+
{
59+
const uint32_t start = 100;
60+
rtc_init();
61+
rtc_write(start);
62+
rtc_free();
63+
64+
wait(WAIT_TIME);
65+
66+
rtc_init();
67+
const uint32_t stop = rtc_read();
68+
const int enabled = rtc_isenabled();
69+
rtc_free();
70+
71+
TEST_ASSERT_TRUE(enabled);
72+
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
73+
}
74+
75+
void rtc_glitch_test()
76+
{
77+
const uint32_t start = 0xffffe;
78+
rtc_init();
79+
80+
rtc_write(start);
81+
uint32_t last = start;
82+
while (last < start + 4) {
83+
const uint32_t cur = rtc_read();
84+
TEST_ASSERT(cur >= last);
85+
last = cur;
86+
}
87+
88+
rtc_free();
89+
}
90+
91+
void rtc_range_test()
92+
{
93+
static const uint32_t starts[] = {
94+
0x00000000,
95+
0xEFFFFFFF,
96+
0x00001000,
97+
};
98+
99+
rtc_init();
100+
for (uint32_t i = 0; i < sizeof(starts) / sizeof(starts[0]); i++) {
101+
const uint32_t start = starts[i];
102+
rtc_write(start);
103+
wait(WAIT_TIME);
104+
const uint32_t stop = rtc_read();
105+
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
106+
}
107+
rtc_free();
108+
}
109+
110+
Case cases[] = {
111+
Case("RTC - init", rtc_init_test),
112+
Case("RTC - sleep", rtc_sleep_test),
113+
Case("RTC - persist", rtc_persist_test),
114+
Case("RTC - glitch", rtc_glitch_test),
115+
Case("RTC - range", rtc_range_test),
116+
};
117+
118+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
119+
GREENTEA_SETUP(30, "default_auto");
120+
return greentea_test_setup_handler(number_of_cases);
121+
}
122+
123+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
124+
125+
int main() {
126+
Harness::run(specification);
127+
}

TESTS/mbed_hal/rtc/rtc_test.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/** \addtogroup hal_rtc_tests
2+
* @{
3+
*/
4+
/* mbed Microcontroller Library
5+
* Copyright (c) 2017-2017 ARM Limited
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
#ifndef MBED_RTC_TEST_H
20+
#define MBED_RTC_TEST_H
21+
22+
#if DEVICE_RTC
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/** Test that ::rtc_init can be called multiple times
29+
*
30+
*/
31+
void rtc_init_test(void);
32+
33+
/** Test that the RTC keeps counting in the various sleep modes
34+
*
35+
*/
36+
void rtc_sleep_test(void);
37+
38+
/** Test that the RTC keeps counting even after ::rtc_free has been called
39+
*
40+
*/
41+
void rtc_persist_test(void);
42+
43+
/** Test time does not glitch backwards due to an incorrectly implemented ripple counter driver
44+
*
45+
*/
46+
void rtc_glitch_test(void);
47+
48+
/** Test that the RTC correctly handles large time values
49+
*
50+
*/
51+
void rtc_range_test(void);
52+
53+
/**@}*/
54+
55+
#ifdef __cplusplus
56+
}
57+
#endif
58+
59+
#endif
60+
61+
#endif
62+
63+
/** @}*/

0 commit comments

Comments
 (0)