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