25
25
#include " watchdog_reset_tests.h"
26
26
#include " mbed.h"
27
27
28
- #define TIMEOUT_MS 100UL
28
+ #include < cinttypes>
29
+
30
+ #define TIMEOUT_MS 100ms
29
31
30
32
/* This value is used to calculate the time to kick the watchdog.
31
33
* Given the watchdog timeout is set to TIMEOUT_MS, the kick will be performed
40
42
* and as short as 66 ms.
41
43
* The value of 35 ms is used to cover the worst case scenario (66 ms).
42
44
*/
43
- #define KICK_ADVANCE_MS 35UL
45
+ #define KICK_ADVANCE_MS 35ms
44
46
45
47
#define MSG_VALUE_DUMMY " 0"
46
48
#define CASE_DATA_INVALID 0xffffffffUL
66
68
* (1 start_bit + 8 data_bits + 1 stop_bit) * 128 * 1000 / 9600 = 133.3 ms.
67
69
* To be on the safe side, set the wait time to 150 ms.
68
70
*/
69
- #define SERIAL_FLUSH_TIME_MS 150
71
+ #define SERIAL_FLUSH_TIME_MS 150ms
70
72
71
- #define TIMEOUT_US (1000 * (TIMEOUT_MS))
72
- #define KICK_ADVANCE_US (1000 * (KICK_ADVANCE_MS))
73
- #define SERIAL_FLUSH_TIME_US (1000 * (SERIAL_FLUSH_TIME_MS))
74
73
75
74
using utest::v1::Case;
76
75
using utest::v1::Specification;
@@ -86,10 +85,10 @@ testcase_data current_case;
86
85
87
86
Ticker wdg_kicking_ticker;
88
87
89
- bool send_reset_notification (testcase_data *tcdata, uint32_t delay_ms)
88
+ bool send_reset_notification (testcase_data *tcdata, std::chrono::milliseconds delay_ms)
90
89
{
91
90
char msg_value[12 ];
92
- int str_len = snprintf (msg_value, sizeof msg_value, " %02x,%08lx " , tcdata->start_index + tcdata->index , delay_ms);
91
+ int str_len = snprintf (msg_value, sizeof msg_value, " %02x,%08 " PRIx64 , tcdata->start_index + tcdata->index , delay_ms. count () );
93
92
if (str_len < 0 ) {
94
93
utest_printf (" Failed to compose a value string to be sent to host." );
95
94
return false ;
@@ -110,15 +109,15 @@ void test_simple_reset()
110
109
111
110
// Phase 1. -- run the test code.
112
111
// Init the watchdog and wait for a device reset.
113
- watchdog_config_t config = { TIMEOUT_MS };
112
+ watchdog_config_t config = { TIMEOUT_MS. count () };
114
113
if (send_reset_notification (¤t_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false ) {
115
114
TEST_ASSERT_MESSAGE (0 , " Dev-host communication error." );
116
115
return ;
117
116
}
118
- wait_us (SERIAL_FLUSH_TIME_US ); // Wait for the serial buffers to flush.
117
+ wait_us (std::chrono::duration_cast<std::chrono::microseconds>(SERIAL_FLUSH_TIME_MS). count () ); // Wait for the serial buffers to flush.
119
118
TEST_ASSERT_EQUAL (WATCHDOG_STATUS_OK, hal_watchdog_init (&config));
120
119
// Watchdog should fire before twice the timeout value.
121
- wait_us (2 * TIMEOUT_US ); // Device reset expected.
120
+ wait_us (2 * std::chrono::duration_cast<std::chrono::microseconds>(TIMEOUT_MS). count () ); // Device reset expected.
122
121
123
122
// Watchdog reset should have occurred during a wait above.
124
123
@@ -215,28 +214,29 @@ void test_restart_reset()
215
214
}
216
215
217
216
// Phase 1. -- run the test code.
218
- watchdog_config_t config = { TIMEOUT_MS };
217
+ watchdog_config_t config = { TIMEOUT_MS. count () };
219
218
TEST_ASSERT_EQUAL (WATCHDOG_STATUS_OK, hal_watchdog_init (&config));
220
- wait_us (TIMEOUT_US / 2 );
219
+ wait_us (std::chrono::duration_cast<std::chrono::microseconds>(TIMEOUT_MS / 2 ). count () );
221
220
TEST_ASSERT_EQUAL (WATCHDOG_STATUS_OK, hal_watchdog_stop ());
222
221
// Check that stopping the Watchdog prevents a device reset.
223
222
// The watchdog should trigger at, or after the timeout value.
224
223
// The watchdog should trigger before twice the timeout value.
225
- wait_us (TIMEOUT_US / 2 + TIMEOUT_US );
224
+ wait_us (std::chrono::duration_cast<std::chrono::microseconds>(TIMEOUT_MS + TIMEOUT_MS / 2 ). count () );
226
225
227
- if (send_reset_notification (¤t_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false ) {
226
+
227
+ if (!send_reset_notification (¤t_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS)) {
228
228
TEST_ASSERT_MESSAGE (0 , " Dev-host communication error." );
229
229
return ;
230
230
}
231
- wait_us (SERIAL_FLUSH_TIME_US ); // Wait for the serial buffers to flush.
231
+ wait_us (std::chrono::duration_cast<std::chrono::microseconds>(SERIAL_FLUSH_TIME_MS). count () ); // Wait for the serial buffers to flush.
232
232
TEST_ASSERT_EQUAL (WATCHDOG_STATUS_OK, hal_watchdog_init (&config));
233
233
// Watchdog should fire before twice the timeout value.
234
- wait_us (2 * TIMEOUT_US ); // Device reset expected.
234
+ wait_us (2 * std::chrono::duration_cast<std::chrono::microseconds>(TIMEOUT_MS). count () ); // Device reset expected.
235
235
236
236
// Watchdog reset should have occurred during a wait above.
237
237
238
238
hal_watchdog_kick ();
239
- wdg_kicking_ticker.attach_us (mbed::callback (hal_watchdog_kick), 20000 ); // For testsuite failure handling.
239
+ wdg_kicking_ticker.attach (mbed::callback (hal_watchdog_kick), 20ms ); // For testsuite failure handling.
240
240
TEST_ASSERT_MESSAGE (0 , " Watchdog did not reset the device as expected." );
241
241
}
242
242
@@ -250,21 +250,21 @@ void test_kick_reset()
250
250
}
251
251
252
252
// Phase 1. -- run the test code.
253
- watchdog_config_t config = { TIMEOUT_MS };
253
+ watchdog_config_t config = { TIMEOUT_MS. count () };
254
254
TEST_ASSERT_EQUAL (WATCHDOG_STATUS_OK, hal_watchdog_init (&config));
255
255
for (int i = 3 ; i; i--) {
256
256
// The reset is prevented as long as the watchdog is kicked
257
257
// anytime before the timeout.
258
- wait_us (TIMEOUT_US - KICK_ADVANCE_US );
258
+ wait_us (2 * std::chrono::duration_cast<std::chrono::microseconds>(TIMEOUT_MS - KICK_ADVANCE_MS). count () );
259
259
hal_watchdog_kick ();
260
260
}
261
- if (send_reset_notification (¤t_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false ) {
261
+ if (! send_reset_notification (¤t_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS)) {
262
262
TEST_ASSERT_MESSAGE (0 , " Dev-host communication error." );
263
263
return ;
264
264
}
265
- wait_us (SERIAL_FLUSH_TIME_US ); // Wait for the serial buffers to flush.
265
+ wait_us (std::chrono::duration_cast<std::chrono::microseconds>(SERIAL_FLUSH_TIME_MS). count () ); // Wait for the serial buffers to flush.
266
266
// Watchdog should fire before twice the timeout value.
267
- wait_us (2 * TIMEOUT_US ); // Device reset expected.
267
+ wait_us (2 * std::chrono::duration_cast<std::chrono::microseconds>(TIMEOUT_MS). count () ); // Device reset expected.
268
268
269
269
// Watchdog reset should have occurred during a wait above.
270
270
@@ -279,7 +279,7 @@ utest::v1::status_t case_setup(const Case *const source, const size_t index_of_c
279
279
return utest::v1::greentea_case_setup_handler (source, index_of_case);
280
280
}
281
281
282
- int testsuite_setup (const size_t number_of_cases)
282
+ utest::v1:: status_t testsuite_setup (const size_t number_of_cases)
283
283
{
284
284
GREENTEA_SETUP (90 , " watchdog_reset" );
285
285
utest::v1::status_t status = utest::v1::greentea_test_setup_handler (number_of_cases);
@@ -306,7 +306,7 @@ int testsuite_setup(const size_t number_of_cases)
306
306
307
307
utest_printf (" This test suite is composed of %i test cases. Starting at index %i.\n " , number_of_cases,
308
308
current_case.start_index );
309
- return current_case.start_index ;
309
+ return static_cast <utest::v1:: status_t >( current_case.start_index ) ;
310
310
}
311
311
312
312
Case cases[] = {
@@ -321,7 +321,7 @@ Case cases[] = {
321
321
Case (" Kicking the Watchdog prevents reset" , case_setup, test_kick_reset),
322
322
};
323
323
324
- Specification specification ((utest::v1:: test_setup_handler_t ) testsuite_setup, cases);
324
+ Specification specification (testsuite_setup, cases);
325
325
326
326
int main ()
327
327
{
0 commit comments