Skip to content

Commit d1ae0d5

Browse files
authored
Merge pull request #12425 from kjbracey-arm/chrono
C++ Chrono support
2 parents a9cb876 + b614bd3 commit d1ae0d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3221
-1056
lines changed

TESTS/mbed_drivers/timer/main.cpp

Lines changed: 91 additions & 187 deletions
Large diffs are not rendered by default.

TESTS/mbed_drivers/timerevent/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TestTimerEvent: public TimerEvent {
4141

4242
public:
4343
TestTimerEvent() :
44-
TimerEvent(), sem(0, 1)
44+
TimerEvent(get_us_ticker_data()), sem(0, 1)
4545
{
4646

4747
sleep_manager_lock_deep_sleep();

TESTS/mbed_hal/rtc/main.cpp

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@
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

3033
using 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

4648
void set_flag_true(void)
4749
{
@@ -53,7 +55,7 @@ void set_flag_true(void)
5355
void 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. */
99101
void 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. */
122124
void 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. */
141143
void 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. */
158160
void 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%. */
179180
void 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. */
198199
void 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

237234
Case cases[] = {

TESTS/mbedmicro-rtos-mbed/basic/main.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@
2828
#else
2929

3030
using utest::v1::Case;
31+
using std::milli;
32+
using std::micro;
33+
using namespace std::chrono;
3134

3235
#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
3336
#define TEST_STACK_SIZE 512
3437
#else
3538
#define TEST_STACK_SIZE 256
3639
#endif
37-
#define ONE_MILLI_SEC 1000
3840

39-
volatile uint32_t elapsed_time_ms = 0;
41+
static duration<uint32_t, milli> elapsed_time_ms;
4042
static const int test_timeout = 40;
4143

4244

4345
void update_tick_thread(Mutex *mutex)
4446
{
4547
while (true) {
46-
ThisThread::sleep_for(1);
48+
ThisThread::sleep_for(1ms);
4749
mutex->lock();
4850
++elapsed_time_ms;
4951
mutex->unlock();
@@ -69,7 +71,7 @@ void test(void)
6971
char _value[128] = { };
7072
int expected_key = 1;
7173
Mutex mutex;
72-
uint32_t elapsed_time;
74+
duration<uint32_t, micro> elapsed_time;
7375

7476
Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
7577
tick_thread.start(callback(update_tick_thread, &mutex));
@@ -86,7 +88,7 @@ void test(void)
8688
elapsed_time = elapsed_time_ms;
8789
mutex.unlock();
8890
// send base_time
89-
greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
91+
greentea_send_kv(_key, elapsed_time.count());
9092

9193
// wait for 2nd signal from host
9294
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
@@ -95,7 +97,7 @@ void test(void)
9597
elapsed_time = elapsed_time_ms;
9698
mutex.unlock();
9799
// send final_time
98-
greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
100+
greentea_send_kv(_key, elapsed_time.count());
99101

100102
//get the results from host
101103
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));

TESTS/mbedmicro-rtos-mbed/condition_variable/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
#include "rtos.h"
3030

3131
using namespace utest::v1;
32+
using namespace std::chrono_literals;
3233

3334
#define TEST_STACK_SIZE 512
34-
#define TEST_DELAY 10
35+
#define TEST_DELAY 10ms
3536

3637
static int change_counter = 0;
3738
static Mutex mutex;

0 commit comments

Comments
 (0)