2525#include " rtc_test.h"
2626
2727#include " mbed.h"
28+ #include " drivers/RealTimeClock.h"
2829#include " rtc_api.h"
30+ #include < type_traits>
31+ #include < mstd_atomic>
2932
3033using namespace utest ::v1;
34+ using namespace std ::chrono;
3135
32- static const uint32_t WAIT_TIME = 4 ;
33- static const uint32_t WAIT_TOLERANCE = 1 ;
36+ static constexpr auto WAIT_TIME = 4s ;
37+ static constexpr auto WAIT_TOLERANCE = 1s ;
3438
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);
39+ #define TEST_ASSERT_DURATION_WITHIN (delta, expected, actual ) \
40+ do { \
41+ using ct = std::common_type_t <decltype (delta), decltype (expected), decltype (actual)>; \
42+ TEST_ASSERT_INT_WITHIN (ct (delta).count (), ct (expected).count (), ct (actual).count ()); \
43+ } while (0 )
4244
4345#if DEVICE_LPTICKER
44- volatile bool expired;
46+ mstd::atomic_bool expired;
4547
4648void set_flag_true (void )
4749{
@@ -53,7 +55,7 @@ void set_flag_true(void)
5355void rtc_sleep_test_support (bool deepsleep_mode)
5456{
5557 LowPowerTimeout timeout;
56- const uint32_t start = 100 ;
58+ const auto start = RealTimeClock::time_point (100s) ;
5759 expired = false ;
5860
5961 /*
@@ -63,46 +65,46 @@ void rtc_sleep_test_support(bool deepsleep_mode)
6365 * hardware buffers are empty. However, such an API does not exist now,
6466 * so we'll use the ThisThread::sleep_for() function for now.
6567 */
66- ThisThread::sleep_for (10 );
68+ ThisThread::sleep_for (10ms );
6769
68- rtc_init ();
70+ RealTimeClock::init ();
6971
7072 if (deepsleep_mode == false ) {
7173 sleep_manager_lock_deep_sleep ();
7274 }
7375
74- rtc_write (start);
76+ RealTimeClock::write (start);
7577
76- timeout.attach (set_flag_true, DELAY_4S );
78+ timeout.attach (set_flag_true, 4s );
7779
7880 TEST_ASSERT (sleep_manager_can_deep_sleep_test_check () == deepsleep_mode);
7981
8082 while (!expired) {
8183 sleep ();
8284 }
8385
84- const uint32_t stop = rtc_read ();
86+ const auto stop = RealTimeClock::now ();
8587
86- TEST_ASSERT_UINT32_WITHIN (RTC_TOLERANCE, DELAY_4S , stop - start);
88+ TEST_ASSERT_DURATION_WITHIN (1s, 4s , stop - start);
8789
8890 timeout.detach ();
8991
9092 if (deepsleep_mode == false ) {
9193 sleep_manager_unlock_deep_sleep ();
9294 }
9395
94- rtc_free ();
96+ RealTimeClock::free ();
9597}
9698#endif
9799
98100/* Test that ::rtc_init can be called multiple times. */
99101void rtc_init_test ()
100102{
101103 for (int i = 0 ; i < 10 ; i++) {
102- rtc_init ();
104+ RealTimeClock::init ();
103105 }
104106
105- rtc_free ();
107+ RealTimeClock::free ();
106108}
107109
108110#if DEVICE_LPTICKER
@@ -121,103 +123,98 @@ void rtc_sleep_test()
121123/* Test that the RTC keeps counting even after ::rtc_free has been called. */
122124void rtc_persist_test ()
123125{
124- const uint32_t start = 100 ;
125- rtc_init ();
126- rtc_write (start);
127- rtc_free ();
126+ const auto start = RealTimeClock::time_point (100s) ;
127+ RealTimeClock::init ();
128+ RealTimeClock::write (start);
129+ RealTimeClock::free ();
128130
129- ThisThread::sleep_for (WAIT_TIME * 1000 );
131+ ThisThread::sleep_for (WAIT_TIME);
130132
131- rtc_init ();
132- const uint32_t stop = rtc_read ();
133- const int enabled = rtc_isenabled ();
134- rtc_free ();
133+ RealTimeClock::init ();
134+ const auto stop = RealTimeClock::now ();
135+ const bool enabled = RealTimeClock::isenabled ();
136+ RealTimeClock::free ();
135137
136138 TEST_ASSERT_TRUE (enabled);
137- TEST_ASSERT_UINT32_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
139+ TEST_ASSERT_DURATION_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
138140}
139141
140142/* Test time does not glitch backwards due to an incorrectly implemented ripple counter driver. */
141143void rtc_glitch_test ()
142144{
143- const uint32_t start = 0xffffe ;
144- rtc_init ();
145+ const auto start = RealTimeClock::time_point (0xffffes) ;
146+ RealTimeClock::init ();
145147
146- rtc_write (start);
147- uint32_t last = start;
148- while (last < start + 4 ) {
149- const uint32_t cur = rtc_read ();
148+ RealTimeClock::write (start);
149+ auto last = start;
150+ while (last < start + 4s ) {
151+ const auto cur = RealTimeClock::now ();
150152 TEST_ASSERT (cur >= last);
151153 last = cur;
152154 }
153155
154- rtc_free ();
156+ RealTimeClock::free ();
155157}
156158
157159/* Test that the RTC correctly handles different time values. */
158160void rtc_range_test ()
159161{
160- static const uint32_t starts[] = {
161- 0x00000000 ,
162- 0xEFFFFFFF ,
163- 0x00001000 ,
164- 0x00010000 ,
162+ static const RealTimeClock::time_point starts[] = {
163+ RealTimeClock::time_point{0x00000000s} ,
164+ RealTimeClock::time_point{0xEFFFFFFFs} ,
165+ RealTimeClock::time_point{0x00001000s} ,
166+ RealTimeClock::time_point{0x00010000s} ,
165167 };
166168
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);
169+ RealTimeClock::init ();
170+ for (const auto &start : starts) {
171+ RealTimeClock::write (start);
172+ ThisThread::sleep_for (WAIT_TIME);
173+ const auto stop = RealTimeClock::now ();
174+ TEST_ASSERT_DURATION_WITHIN (WAIT_TOLERANCE, WAIT_TIME, stop - start);
174175 }
175- rtc_free ();
176+ RealTimeClock::free ();
176177}
177178
178179/* Test that the RTC accuracy is at least 10%. */
179180void rtc_accuracy_test ()
180181{
181182 Timer timer1;
182183
183- const uint32_t start = 100 ;
184- rtc_init ();
185- rtc_write (start);
184+ const auto start = RealTimeClock::time_point (100s) ;
185+ RealTimeClock::init ();
186+ RealTimeClock::write (start);
186187
187188 timer1.start ();
188- while (rtc_read () < (start + DELAY_10S )) {
189+ while (RealTimeClock::now () < (start + 10s )) {
189190 /* Just wait. */
190191 }
191192 timer1.stop ();
192193
193194 /* RTC accuracy is at least 10%. */
194- TEST_ASSERT_INT32_WITHIN (TOLERANCE_ACCURACY_US, DELAY_10S * US_PER_SEC , timer1.read_us ());
195+ TEST_ASSERT_DURATION_WITHIN (1s, 10s , timer1.elapsed_time ());
195196}
196197
197198/* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */
198199void rtc_write_read_test ()
199200{
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);
201+ RealTimeClock::init ();
206202
203+ for (auto init_val = RealTimeClock::time_point (100s); init_val < RealTimeClock::time_point (400s); init_val += 100s) {
207204 core_util_critical_section_enter ();
208205
209- rtc_write (init_val);
210- const uint32_t read_val = rtc_read ();
206+ RealTimeClock::write (init_val);
207+ const auto read_val = RealTimeClock::now ();
211208
212209 core_util_critical_section_exit ();
213210
214211 /* No tolerance is provided since we should have 1 second to
215212 * execute this case after the RTC time is set.
216213 */
217- TEST_ASSERT_EQUAL_UINT32 (init_val, read_val);
214+ TEST_ASSERT (init_val == read_val);
218215 }
219216
220- rtc_free ();
217+ RealTimeClock::free ();
221218}
222219
223220/* Test that ::is_enabled function returns 1 if the RTC is counting and the time has been set. */
@@ -228,10 +225,10 @@ void rtc_enabled_test()
228225 * and RTC time is set.
229226 */
230227
231- rtc_init ();
232- rtc_write ( 0 );
233- TEST_ASSERT_EQUAL_INT ( 1 , rtc_isenabled ());
234- rtc_free ();
228+ RealTimeClock::init ();
229+ RealTimeClock::write ( RealTimeClock::time_point (0s) );
230+ TEST_ASSERT_TRUE ( RealTimeClock::isenabled ());
231+ RealTimeClock::free ();
235232}
236233
237234Case cases[] = {
0 commit comments