25
25
#include " rtc_test.h"
26
26
27
27
#include " mbed.h"
28
+ #include " drivers/RealTimeClock.h"
28
29
#include " rtc_api.h"
30
+ #include < type_traits>
31
+ #include < mstd_atomic>
29
32
30
33
using namespace utest ::v1;
34
+ using namespace std ::chrono;
31
35
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 ;
34
38
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 )
42
44
43
45
#if DEVICE_LPTICKER
44
- volatile bool expired;
46
+ mstd::atomic_bool expired;
45
47
46
48
void set_flag_true (void )
47
49
{
@@ -53,7 +55,7 @@ void set_flag_true(void)
53
55
void rtc_sleep_test_support (bool deepsleep_mode)
54
56
{
55
57
LowPowerTimeout timeout;
56
- const uint32_t start = 100 ;
58
+ const auto start = RealTimeClock::time_point (100s) ;
57
59
expired = false ;
58
60
59
61
/*
@@ -63,46 +65,46 @@ void rtc_sleep_test_support(bool deepsleep_mode)
63
65
* hardware buffers are empty. However, such an API does not exist now,
64
66
* so we'll use the ThisThread::sleep_for() function for now.
65
67
*/
66
- ThisThread::sleep_for (10 );
68
+ ThisThread::sleep_for (10ms );
67
69
68
- rtc_init ();
70
+ RealTimeClock::init ();
69
71
70
72
if (deepsleep_mode == false ) {
71
73
sleep_manager_lock_deep_sleep ();
72
74
}
73
75
74
- rtc_write (start);
76
+ RealTimeClock::write (start);
75
77
76
- timeout.attach (set_flag_true, DELAY_4S );
78
+ timeout.attach (set_flag_true, 4s );
77
79
78
80
TEST_ASSERT (sleep_manager_can_deep_sleep_test_check () == deepsleep_mode);
79
81
80
82
while (!expired) {
81
83
sleep ();
82
84
}
83
85
84
- const uint32_t stop = rtc_read ();
86
+ const auto stop = RealTimeClock::now ();
85
87
86
- TEST_ASSERT_UINT32_WITHIN (RTC_TOLERANCE, DELAY_4S , stop - start);
88
+ TEST_ASSERT_DURATION_WITHIN (1s, 4s , stop - start);
87
89
88
90
timeout.detach ();
89
91
90
92
if (deepsleep_mode == false ) {
91
93
sleep_manager_unlock_deep_sleep ();
92
94
}
93
95
94
- rtc_free ();
96
+ RealTimeClock::free ();
95
97
}
96
98
#endif
97
99
98
100
/* Test that ::rtc_init can be called multiple times. */
99
101
void rtc_init_test ()
100
102
{
101
103
for (int i = 0 ; i < 10 ; i++) {
102
- rtc_init ();
104
+ RealTimeClock::init ();
103
105
}
104
106
105
- rtc_free ();
107
+ RealTimeClock::free ();
106
108
}
107
109
108
110
#if DEVICE_LPTICKER
@@ -121,103 +123,98 @@ void rtc_sleep_test()
121
123
/* Test that the RTC keeps counting even after ::rtc_free has been called. */
122
124
void rtc_persist_test ()
123
125
{
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 ();
128
130
129
- ThisThread::sleep_for (WAIT_TIME * 1000 );
131
+ ThisThread::sleep_for (WAIT_TIME);
130
132
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 ();
135
137
136
138
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);
138
140
}
139
141
140
142
/* Test time does not glitch backwards due to an incorrectly implemented ripple counter driver. */
141
143
void rtc_glitch_test ()
142
144
{
143
- const uint32_t start = 0xffffe ;
144
- rtc_init ();
145
+ const auto start = RealTimeClock::time_point (0xffffes) ;
146
+ RealTimeClock::init ();
145
147
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 ();
150
152
TEST_ASSERT (cur >= last);
151
153
last = cur;
152
154
}
153
155
154
- rtc_free ();
156
+ RealTimeClock::free ();
155
157
}
156
158
157
159
/* Test that the RTC correctly handles different time values. */
158
160
void rtc_range_test ()
159
161
{
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} ,
165
167
};
166
168
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);
174
175
}
175
- rtc_free ();
176
+ RealTimeClock::free ();
176
177
}
177
178
178
179
/* Test that the RTC accuracy is at least 10%. */
179
180
void rtc_accuracy_test ()
180
181
{
181
182
Timer timer1;
182
183
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);
186
187
187
188
timer1.start ();
188
- while (rtc_read () < (start + DELAY_10S )) {
189
+ while (RealTimeClock::now () < (start + 10s )) {
189
190
/* Just wait. */
190
191
}
191
192
timer1.stop ();
192
193
193
194
/* 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 ());
195
196
}
196
197
197
198
/* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */
198
199
void rtc_write_read_test ()
199
200
{
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 ();
206
202
203
+ for (auto init_val = RealTimeClock::time_point (100s); init_val < RealTimeClock::time_point (400s); init_val += 100s) {
207
204
core_util_critical_section_enter ();
208
205
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 ();
211
208
212
209
core_util_critical_section_exit ();
213
210
214
211
/* No tolerance is provided since we should have 1 second to
215
212
* execute this case after the RTC time is set.
216
213
*/
217
- TEST_ASSERT_EQUAL_UINT32 (init_val, read_val);
214
+ TEST_ASSERT (init_val == read_val);
218
215
}
219
216
220
- rtc_free ();
217
+ RealTimeClock::free ();
221
218
}
222
219
223
220
/* 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()
228
225
* and RTC time is set.
229
226
*/
230
227
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 ();
235
232
}
236
233
237
234
Case cases[] = {
0 commit comments