Skip to content

Commit 42e0780

Browse files
Fix watchdog test warnings, fix broken hal_watchdog_get_reload_value(), fix missing frequency field in watchdog features
1 parent 789bd95 commit 42e0780

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

hal/tests/TESTS/mbed_hal/watchdog/main.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
#include <stdlib.h>
3030

3131
/* The shortest timeout value, this test suite is able to handle correctly. */
32-
#define WDG_MIN_TIMEOUT_MS 50UL
32+
#define WDG_MIN_TIMEOUT_MS 50ms
3333

3434
// Do not set watchdog timeout shorter than WDG_MIN_TIMEOUT_MS, as it may
3535
// cause the host-test-runner return 'TIMEOUT' instead of 'FAIL' / 'PASS'
3636
// if watchdog performs reset during test suite teardown.
37-
#define WDG_TIMEOUT_MS 100UL
37+
#define WDG_TIMEOUT_MS 100ms
3838

3939
#define MSG_VALUE_DUMMY "0"
4040
#define MSG_VALUE_LEN 24
@@ -57,7 +57,7 @@
5757
* (1 start_bit + 8 data_bits + 1 stop_bit) * 128 * 1000 / 9600 = 133.3 ms.
5858
* To be on the safe side, set the wait time to 150 ms.
5959
*/
60-
#define SERIAL_FLUSH_TIME_MS 150
60+
#define SERIAL_FLUSH_TIME_MS 150ms
6161

6262
int CASE_INDEX_START;
6363
int CASE_INDEX_CURRENT;
@@ -67,7 +67,7 @@ using utest::v1::Case;
6767
using utest::v1::Specification;
6868
using utest::v1::Harness;
6969

70-
const watchdog_config_t WDG_CONFIG_DEFAULT = { .timeout_ms = WDG_TIMEOUT_MS };
70+
const watchdog_config_t WDG_CONFIG_DEFAULT = { .timeout_ms = WDG_TIMEOUT_MS.count() };
7171

7272
void test_max_timeout_is_valid()
7373
{
@@ -115,12 +115,12 @@ void test_update_config()
115115
}
116116

117117
watchdog_config_t config = WDG_CONFIG_DEFAULT;
118-
uint32_t timeouts[] = {
119-
features.max_timeout / 4,
120-
features.max_timeout / 8,
121-
features.max_timeout / 16
118+
std::chrono::milliseconds timeouts[] = {
119+
std::chrono::milliseconds(features.max_timeout / 4),
120+
std::chrono::milliseconds(features.max_timeout / 8),
121+
std::chrono::milliseconds(features.max_timeout / 16)
122122
};
123-
int num_timeouts = sizeof timeouts / sizeof timeouts[0];
123+
size_t num_timeouts = sizeof timeouts / sizeof timeouts[0];
124124

125125
for (size_t i = 0; i < num_timeouts; i++) {
126126
if (timeouts[i] < WDG_MIN_TIMEOUT_MS) {
@@ -129,9 +129,10 @@ void test_update_config()
129129
return;
130130
}
131131

132-
config.timeout_ms = timeouts[i];
132+
config.timeout_ms = timeouts[i].count();
133133
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
134-
uint32_t reload_value = hal_watchdog_get_reload_value();
134+
135+
auto reload_value = std::chrono::milliseconds(hal_watchdog_get_reload_value());
135136
// The watchdog should trigger at, or after the timeout value.
136137
TEST_ASSERT(reload_value >= timeouts[i]);
137138
// The watchdog should trigger before twice the timeout value.
@@ -155,7 +156,7 @@ utest::v1::status_t case_teardown_sync_on_reset(const Case *const source, const
155156
// Start kicking the watchdog during teardown.
156157
hal_watchdog_kick();
157158
Ticker wdg_kicking_ticker;
158-
wdg_kicking_ticker.attach_us(mbed::callback(hal_watchdog_kick), 20000);
159+
wdg_kicking_ticker.attach(mbed::callback(hal_watchdog_kick), 20ms);
159160
utest::v1::status_t status = utest::v1::greentea_case_teardown_handler(source, passed, failed, failure);
160161
if (failed) {
161162
/* Return immediately and skip the device reset, if the test case failed.
@@ -193,7 +194,7 @@ utest::v1::status_t case_teardown_wdg_stop_or_reset(const Case *const source, co
193194
template<uint32_t timeout_ms>
194195
void test_init()
195196
{
196-
if (timeout_ms < WDG_MIN_TIMEOUT_MS) {
197+
if (std::chrono::milliseconds(timeout_ms) < WDG_MIN_TIMEOUT_MS) {
197198
CASE_IGNORED = true;
198199
TEST_IGNORE_MESSAGE("Requested timeout value is too short -- ignoring test case.");
199200
return;
@@ -216,7 +217,7 @@ void test_init_max_timeout()
216217
TEST_ASSERT(hal_watchdog_get_reload_value() >= features.max_timeout);
217218
}
218219

219-
int testsuite_setup_sync_on_reset(const size_t number_of_cases)
220+
utest::v1::status_t testsuite_setup_sync_on_reset(const size_t number_of_cases)
220221
{
221222
GREENTEA_SETUP(45, "sync_on_reset");
222223
utest::v1::status_t status = utest::v1::greentea_test_setup_handler(number_of_cases);
@@ -243,7 +244,7 @@ int testsuite_setup_sync_on_reset(const size_t number_of_cases)
243244
}
244245

245246
utest_printf("Starting with test case index %i of all %i defined test cases.\n", CASE_INDEX_START, number_of_cases);
246-
return CASE_INDEX_START;
247+
return static_cast<utest::v1::status_t>(CASE_INDEX_START);
247248
}
248249

249250
Case cases[] = {
@@ -262,7 +263,7 @@ Case cases[] = {
262263
test_init_max_timeout, (utest::v1::case_teardown_handler_t) case_teardown_sync_on_reset),
263264
};
264265

265-
Specification specification((utest::v1::test_setup_handler_t) testsuite_setup_sync_on_reset, cases);
266+
Specification specification(testsuite_setup_sync_on_reset, cases);
266267

267268
int main()
268269
{

hal/tests/TESTS/mbed_hal/watchdog_reset/main.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
#include "watchdog_reset_tests.h"
2626
#include "mbed.h"
2727

28-
#define TIMEOUT_MS 100UL
28+
#include <cinttypes>
29+
30+
#define TIMEOUT_MS 100ms
2931

3032
/* This value is used to calculate the time to kick the watchdog.
3133
* Given the watchdog timeout is set to TIMEOUT_MS, the kick will be performed
@@ -40,7 +42,7 @@
4042
* and as short as 66 ms.
4143
* The value of 35 ms is used to cover the worst case scenario (66 ms).
4244
*/
43-
#define KICK_ADVANCE_MS 35UL
45+
#define KICK_ADVANCE_MS 35ms
4446

4547
#define MSG_VALUE_DUMMY "0"
4648
#define CASE_DATA_INVALID 0xffffffffUL
@@ -66,11 +68,8 @@
6668
* (1 start_bit + 8 data_bits + 1 stop_bit) * 128 * 1000 / 9600 = 133.3 ms.
6769
* To be on the safe side, set the wait time to 150 ms.
6870
*/
69-
#define SERIAL_FLUSH_TIME_MS 150
71+
#define SERIAL_FLUSH_TIME_MS 150ms
7072

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))
7473

7574
using utest::v1::Case;
7675
using utest::v1::Specification;
@@ -86,10 +85,10 @@ testcase_data current_case;
8685

8786
Ticker wdg_kicking_ticker;
8887

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)
9089
{
9190
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());
9392
if (str_len < 0) {
9493
utest_printf("Failed to compose a value string to be sent to host.");
9594
return false;
@@ -110,15 +109,15 @@ void test_simple_reset()
110109

111110
// Phase 1. -- run the test code.
112111
// Init the watchdog and wait for a device reset.
113-
watchdog_config_t config = { TIMEOUT_MS };
112+
watchdog_config_t config = { TIMEOUT_MS.count() };
114113
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false) {
115114
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
116115
return;
117116
}
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.
119118
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
120119
// 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.
122121

123122
// Watchdog reset should have occurred during a wait above.
124123

@@ -215,28 +214,29 @@ void test_restart_reset()
215214
}
216215

217216
// Phase 1. -- run the test code.
218-
watchdog_config_t config = { TIMEOUT_MS };
217+
watchdog_config_t config = { TIMEOUT_MS.count() };
219218
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());
221220
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_stop());
222221
// Check that stopping the Watchdog prevents a device reset.
223222
// The watchdog should trigger at, or after the timeout value.
224223
// 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());
226225

227-
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false) {
226+
227+
if (!send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS)) {
228228
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
229229
return;
230230
}
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.
232232
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
233233
// 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.
235235

236236
// Watchdog reset should have occurred during a wait above.
237237

238238
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.
240240
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
241241
}
242242

@@ -250,21 +250,21 @@ void test_kick_reset()
250250
}
251251

252252
// Phase 1. -- run the test code.
253-
watchdog_config_t config = { TIMEOUT_MS };
253+
watchdog_config_t config = { TIMEOUT_MS.count() };
254254
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
255255
for (int i = 3; i; i--) {
256256
// The reset is prevented as long as the watchdog is kicked
257257
// 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());
259259
hal_watchdog_kick();
260260
}
261-
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false) {
261+
if (!send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS)) {
262262
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
263263
return;
264264
}
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.
266266
// 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.
268268

269269
// Watchdog reset should have occurred during a wait above.
270270

@@ -279,7 +279,7 @@ utest::v1::status_t case_setup(const Case *const source, const size_t index_of_c
279279
return utest::v1::greentea_case_setup_handler(source, index_of_case);
280280
}
281281

282-
int testsuite_setup(const size_t number_of_cases)
282+
utest::v1::status_t testsuite_setup(const size_t number_of_cases)
283283
{
284284
GREENTEA_SETUP(90, "watchdog_reset");
285285
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)
306306

307307
utest_printf("This test suite is composed of %i test cases. Starting at index %i.\n", number_of_cases,
308308
current_case.start_index);
309-
return current_case.start_index;
309+
return static_cast<utest::v1::status_t>(current_case.start_index);
310310
}
311311

312312
Case cases[] = {
@@ -321,7 +321,7 @@ Case cases[] = {
321321
Case("Kicking the Watchdog prevents reset", case_setup, test_kick_reset),
322322
};
323323

324-
Specification specification((utest::v1::test_setup_handler_t) testsuite_setup, cases);
324+
Specification specification(testsuite_setup, cases);
325325

326326
int main()
327327
{

hal/tests/TESTS/mbed_hal/watchdog_timing/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void test_timeout_lower_limit()
157157
wait_us(sleep_time_ms * 1000);
158158
hal_watchdog_kick();
159159

160-
if (send_reset_notification(&current_case, 2 * TIMEOUT_LOWER_LIMIT_MS) == false) {
160+
if (!send_reset_notification(&current_case, 2 * TIMEOUT_LOWER_LIMIT_MS)) {
161161
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
162162
return;
163163
}

targets/TARGET_RASPBERRYPI/TARGET_RP2040/watchdog_api.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ watchdog_status_t hal_watchdog_init(const watchdog_config_t *config)
1010
{
1111
watchdogConfig = *config;
1212
// The pico watchdogs accept a maximum value of 0x7fffff
13-
if ( config->timeout_ms < 0x1 && config->timeout_ms > 0x7FFFFF ) {
13+
if ( config->timeout_ms < 0x1 || config->timeout_ms > 0x7FFFFF ) {
1414
return WATCHDOG_STATUS_INVALID_ARGUMENT;
1515
}
1616

@@ -32,11 +32,7 @@ watchdog_status_t hal_watchdog_stop(void)
3232

3333
uint32_t hal_watchdog_get_reload_value(void)
3434
{
35-
uint32_t load_value = watchdogConfig.timeout_ms * 1000 * 2;
36-
if (load_value > 0xffffffu) {
37-
load_value = 0xffffffu;
38-
}
39-
return load_value;
35+
return watchdogConfig.timeout_ms;
4036
}
4137

4238
watchdog_features_t hal_watchdog_get_platform_features(void)
@@ -46,8 +42,12 @@ watchdog_features_t hal_watchdog_get_platform_features(void)
4642
features.max_timeout = 0x7FFFFF;
4743
features.update_config = true;
4844
features.disable_watchdog = true;
49-
return features;
5045

46+
// SDK configures the watchdog underlying counter to run at 1MHz
47+
features.clock_typical_frequency = 1000000;
48+
features.clock_max_frequency = 1000000;
49+
50+
return features;
5151
}
5252

5353
#endif // DEVICE_WATCHDOG

0 commit comments

Comments
 (0)