2525#include " rtc_test.h"
2626
2727#include " mbed.h"
28+ #include " drivers/RealTimeClock.h"
2829#include " rtc_api.h"
2930
3031using namespace utest ::v1;
32+ using namespace std ::chrono;
3133
32- static const uint32_t WAIT_TIME = 4 ;
33- static const uint32_t WAIT_TOLERANCE = 1 ;
34-
35- #define US_PER_SEC 1000000
36- #define ACCURACY_FACTOR 10
37-
38- static const uint32_t DELAY_4S = 4 ;
39- static const uint32_t DELAY_10S = 10 ;
40- static const uint32_t RTC_TOLERANCE = 1 ;
41- static const uint32_t TOLERANCE_ACCURACY_US = (DELAY_10S *US_PER_SEC / ACCURACY_FACTOR);
34+ static constexpr auto WAIT_TIME = 4s;
35+ static constexpr auto WAIT_TOLERANCE = 1s;
4236
4337#if DEVICE_LPTICKER
44- volatile bool expired;
38+ mstd::atomic_bool expired;
39+
40+ #define TEST_ASSERT_DURATION_WITHIN (delta, expected, actual ) \
41+ TEST_ASSERT_INT64_WITHIN (microseconds(delta).count(), microseconds(expected).count(), microseconds(actual).count())
4542
4643void set_flag_true(void )
4744{
@@ -53,7 +50,7 @@ void set_flag_true(void)
5350void rtc_sleep_test_support (bool deepsleep_mode)
5451{
5552 LowPowerTimeout timeout;
56- const uint32_t start = 100 ;
53+ const auto start = RealTimeClock::time_point (100s) ;
5754 expired = false ;
5855
5956 /*
@@ -63,46 +60,46 @@ void rtc_sleep_test_support(bool deepsleep_mode)
6360 * hardware buffers are empty. However, such an API does not exist now,
6461 * so we'll use the wait_ms() function for now.
6562 */
66- wait_ms ( 10 );
63+ ThisThread::sleep_for (10ms );
6764
68- rtc_init ();
65+ RealTimeClock::init ();
6966
7067 if (deepsleep_mode == false ) {
7168 sleep_manager_lock_deep_sleep ();
7269 }
7370
74- rtc_write (start);
71+ RealTimeClock::write (start);
7572
76- timeout.attach (set_flag_true, DELAY_4S );
73+ timeout.attach (set_flag_true, 4s );
7774
7875 TEST_ASSERT (sleep_manager_can_deep_sleep_test_check () == deepsleep_mode);
7976
8077 while (!expired) {
8178 sleep ();
8279 }
8380
84- const uint32_t stop = rtc_read ();
81+ const auto stop = RealTimeClock::now ();
8582
86- TEST_ASSERT_UINT32_WITHIN (RTC_TOLERANCE, DELAY_4S , stop - start);
83+ TEST_ASSERT_DURATION_WITHIN (1s, 4s , stop - start);
8784
8885 timeout.detach ();
8986
9087 if (deepsleep_mode == false ) {
9188 sleep_manager_unlock_deep_sleep ();
9289 }
9390
94- rtc_free ();
91+ RealTimeClock::free ();
9592}
9693#endif
9794
9895/* Test that ::rtc_init can be called multiple times. */
9996void rtc_init_test ()
10097{
10198 for (int i = 0 ; i < 10 ; i++) {
102- rtc_init ();
99+ RealTimeClock::init ();
103100 }
104101
105- rtc_free ();
102+ RealTimeClock::free ();
106103}
107104
108105#if DEVICE_LPTICKER
@@ -121,103 +118,98 @@ void rtc_sleep_test()
121118/* Test that the RTC keeps counting even after ::rtc_free has been called. */
122119void rtc_persist_test ()
123120{
124- const uint32_t start = 100 ;
125- rtc_init ();
126- rtc_write (start);
127- rtc_free ();
121+ const auto start = RealTimeClock::time_point (100s) ;
122+ RealTimeClock::init ();
123+ RealTimeClock::write (start);
124+ RealTimeClock::free ();
128125
129- ThisThread::sleep_for (WAIT_TIME * 1000 );
126+ ThisThread::sleep_for (WAIT_TIME);
130127
131- rtc_init ();
132- const uint32_t stop = rtc_read ();
133- const int enabled = rtc_isenabled ();
134- rtc_free ();
128+ RealTimeClock::init ();
129+ const auto stop = RealTimeClock::now ();
130+ const bool enabled = RealTimeClock::isenabled ();
131+ RealTimeClock::free ();
135132
136133 TEST_ASSERT_TRUE (enabled);
137- TEST_ASSERT_UINT32_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
134+ TEST_ASSERT_DURATION_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
138135}
139136
140137/* Test time does not glitch backwards due to an incorrectly implemented ripple counter driver. */
141138void rtc_glitch_test ()
142139{
143- const uint32_t start = 0xffffe ;
144- rtc_init ();
140+ const auto start = RealTimeClock::time_point (0xffffes) ;
141+ RealTimeClock::init ();
145142
146- rtc_write (start);
147- uint32_t last = start;
148- while (last < start + 4 ) {
149- const uint32_t cur = rtc_read ();
143+ RealTimeClock::write (start);
144+ auto last = start;
145+ while (last < start + 4s ) {
146+ const auto cur = RealTimeClock::now ();
150147 TEST_ASSERT (cur >= last);
151148 last = cur;
152149 }
153150
154- rtc_free ();
151+ RealTimeClock::free ();
155152}
156153
157154/* Test that the RTC correctly handles different time values. */
158155void rtc_range_test ()
159156{
160- static const uint32_t starts[] = {
161- 0x00000000 ,
162- 0xEFFFFFFF ,
163- 0x00001000 ,
164- 0x00010000 ,
157+ static const RealTimeClock::time_point starts[] {
158+ 0x00000000s ,
159+ 0xEFFFFFFFs ,
160+ 0x00001000s ,
161+ 0x00010000s ,
165162 };
166163
167- rtc_init ();
168- for (uint32_t i = 0 ; i < sizeof (starts) / sizeof (starts[0 ]); i++) {
169- const uint32_t start = starts[i];
170- rtc_write (start);
171- ThisThread::sleep_for (WAIT_TIME * 1000 );
172- const uint32_t stop = rtc_read ();
173- TEST_ASSERT_UINT32_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
164+ RealTimeClock::init ();
165+ for (const auto &start : starts) {
166+ RealTimeClock::write (start);
167+ ThisThread::sleep_for (WAIT_TIME);
168+ const auto stop = RealTimeClock::now ();
169+ TEST_ASSERT_DURATION_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
174170 }
175- rtc_free ();
171+ RealTimeClock::free ();
176172}
177173
178174/* Test that the RTC accuracy is at least 10%. */
179175void rtc_accuracy_test ()
180176{
181177 Timer timer1;
182178
183- const uint32_t start = 100 ;
184- rtc_init ();
185- rtc_write (start);
179+ const auto start = RealTimeClock::time_point (100s) ;
180+ RealTimeClock::init ();
181+ RealTimeClock::write (start);
186182
187183 timer1.start ();
188- while (rtc_read () < (start + DELAY_10S )) {
184+ while (RealTimeClock::now () < (start + 10s )) {
189185 /* Just wait. */
190186 }
191187 timer1.stop ();
192188
193189 /* RTC accuracy is at least 10%. */
194- TEST_ASSERT_INT32_WITHIN (TOLERANCE_ACCURACY_US, DELAY_10S * US_PER_SEC , timer1.read_us ());
190+ TEST_ASSERT_DURATION_WITHIN (1s, 10s , timer1.read_duration ());
195191}
196192
197193/* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */
198194void rtc_write_read_test ()
199195{
200- static const uint32_t rtc_init_val = 100 ;
201-
202- rtc_init ();
203-
204- for (int i = 0 ; i < 3 ; i++) {
205- const uint32_t init_val = (rtc_init_val + i * rtc_init_val);
196+ RealTimeClock::init ();
206197
198+ for (auto init_val = RealTimeClock::time_point (100s); init_val < RealTimeClock::time_point (400s); init_val += 100s) {
207199 core_util_critical_section_enter ();
208200
209- rtc_write (init_val);
210- const uint32_t read_val = rtc_read ();
201+ RealTimeClock::write (init_val);
202+ const auto read_val = RealTimeClock::now ();
211203
212204 core_util_critical_section_exit ();
213205
214206 /* No tolerance is provided since we should have 1 second to
215207 * execute this case after the RTC time is set.
216208 */
217- TEST_ASSERT_EQUAL_UINT32 (init_val, read_val);
209+ TEST_ASSERT (init_val == read_val);
218210 }
219211
220- rtc_free ();
212+ RealTimeClock::free ();
221213}
222214
223215/* Test that ::is_enabled function returns 1 if the RTC is counting and the time has been set. */
@@ -228,10 +220,10 @@ void rtc_enabled_test()
228220 * and RTC time is set.
229221 */
230222
231- rtc_init ();
232- rtc_write ( 0 );
233- TEST_ASSERT_EQUAL_INT ( 1 , rtc_isenabled ());
234- rtc_free ();
223+ RealTimeClock::init ();
224+ RealTimeClock::write ( RealTimeClock::time_point ( 0 ) );
225+ TEST_ASSERT_TRUE ( RealTimeClock::isenabled ());
226+ RealTimeClock::free ();
235227}
236228
237229Case cases[] = {
0 commit comments