Skip to content

Fix watchdog reset test #11773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 48 additions & 34 deletions TESTS/mbed_drivers/watchdog_reset/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
*/
#define SERIAL_FLUSH_TIME_MS 20

#define TIMEOUT_US (1000 * (TIMEOUT_MS))
#define KICK_ADVANCE_US (1000 * (KICK_ADVANCE_MS))
#define SERIAL_FLUSH_TIME_US (1000 * (SERIAL_FLUSH_TIME_MS))

using utest::v1::Case;
using utest::v1::Specification;
using utest::v1::Harness;
Expand All @@ -80,13 +84,21 @@ struct testcase_data {
uint32_t received_data;
};

void release_sem(Semaphore *sem)
testcase_data current_case;

Thread wdg_kicking_thread(osPriorityNormal, 768);
Semaphore kick_wdg_during_test_teardown(0, 1);

void wdg_kicking_thread_fun()
{
sem->release();
kick_wdg_during_test_teardown.acquire();
Watchdog &watchdog = Watchdog::get_instance();
while (true) {
watchdog.kick();
wait_us(20000);
}
}

testcase_data current_case;

bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms)
{
char msg_value[12];
Expand Down Expand Up @@ -120,11 +132,11 @@ void test_simple_reset()
TEST_ASSERT_TRUE(watchdog.start(TIMEOUT_MS));
TEST_ASSERT_TRUE(watchdog.is_running());
// Watchdog should fire before twice the timeout value.
wait_ms(2 * TIMEOUT_MS); // Device reset expected.
wait_us(2 * TIMEOUT_US); // Device reset expected.

// Watchdog reset should have occurred during wait_ms() above;
// Watchdog reset should have occurred during a wait above.

watchdog.kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

Expand All @@ -139,8 +151,6 @@ void test_sleep_reset()
}

// Phase 1. -- run the test code.
Semaphore sem(0, 1);
Timeout timeout;
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
Expand All @@ -150,22 +160,21 @@ void test_sleep_reset()
TEST_ASSERT_TRUE(watchdog.start(TIMEOUT_MS));
TEST_ASSERT_TRUE(watchdog.is_running());
sleep_manager_lock_deep_sleep();
// Watchdog should fire before twice the timeout value.
timeout.attach_us(mbed::callback(release_sem, &sem), 1000ULL * (2 * TIMEOUT_MS));
if (sleep_manager_can_deep_sleep()) {
TEST_ASSERT_MESSAGE(0, "Deepsleep should be disallowed.");
return;
}
sem.wait(); // Device reset expected.
// Watchdog should fire before twice the timeout value.
ThisThread::sleep_for(2 * TIMEOUT_MS); // Device reset expected.
sleep_manager_unlock_deep_sleep();

// Watchdog reset should have occurred during sem.wait() (sleep) above;
// Watchdog reset should have occurred during the sleep above.

watchdog.kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

#if DEVICE_LOWPOWERTIMER
#if DEVICE_LPTICKER
void test_deepsleep_reset()
{
// Phase 2. -- verify the test results.
Expand All @@ -176,27 +185,28 @@ void test_deepsleep_reset()
}

// Phase 1. -- run the test code.
Semaphore sem(0, 1);
LowPowerTimeout lp_timeout;
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
}
wait_us(SERIAL_FLUSH_TIME_US); // Wait for the serial buffers to flush.
Watchdog &watchdog = Watchdog::get_instance();
TEST_ASSERT_FALSE(watchdog.is_running());
TEST_ASSERT_TRUE(watchdog.start(TIMEOUT_MS));
TEST_ASSERT_TRUE(watchdog.is_running());
// Watchdog should fire before twice the timeout value.
lp_timeout.attach_us(mbed::callback(release_sem, &sem), 1000ULL * (2 * TIMEOUT_MS));
wait_ms(SERIAL_FLUSH_TIME_MS); // Wait for the serial buffers to flush.
if (!sleep_manager_can_deep_sleep()) {
TEST_ASSERT_MESSAGE(0, "Deepsleep should be allowed.");
}
sem.wait(); // Device reset expected.

// Watchdog reset should have occurred during sem.wait() (deepsleep) above;
// The Watchdog reset is allowed to be delayed up to twice the timeout
// value when the deepsleep mode is active.
// To make the test less sensitive to clock/wait accuracy, add 20% extra
// (making tha whole deepsleep wait equal to 2.2 * timeout).
ThisThread::sleep_for(220 * TIMEOUT_MS / 100); // Device reset expected.

// Watchdog reset should have occurred during the deepsleep above.

watchdog.kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}
#endif
Expand All @@ -222,13 +232,13 @@ void test_restart_reset()
TEST_ASSERT_FALSE(watchdog.is_running());
TEST_ASSERT_TRUE(watchdog.start(TIMEOUT_MS));
TEST_ASSERT_TRUE(watchdog.is_running());
wait_ms(TIMEOUT_MS / 2UL);
wait_us(TIMEOUT_US / 2);
TEST_ASSERT_TRUE(watchdog.stop());
TEST_ASSERT_FALSE(watchdog.is_running());
// Check that stopping the Watchdog prevents a device reset.
// The watchdog should trigger at, or after the timeout value.
// The watchdog should trigger before twice the timeout value.
wait_ms(TIMEOUT_MS / 2UL + TIMEOUT_MS);
wait_us(TIMEOUT_US / 2 + TIMEOUT_US);

if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
Expand All @@ -237,11 +247,11 @@ void test_restart_reset()
TEST_ASSERT_TRUE(watchdog.start(TIMEOUT_MS));
TEST_ASSERT_TRUE(watchdog.is_running());
// Watchdog should fire before twice the timeout value.
wait_ms(2 * TIMEOUT_MS); // Device reset expected.
wait_us(2 * TIMEOUT_US); // Device reset expected.

// Watchdog reset should have occurred during that wait() above;
// Watchdog reset should have occurred during a wait above.

watchdog.kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

Expand All @@ -262,19 +272,19 @@ void test_kick_reset()
for (int i = 3; i; i--) {
// The reset is prevented as long as the watchdog is kicked
// anytime before the timeout.
wait_ms(TIMEOUT_MS - KICK_ADVANCE_MS);
wait_us(TIMEOUT_US - KICK_ADVANCE_US);
watchdog.kick();
}
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
}
// Watchdog should fire before twice the timeout value.
wait_ms(2 * TIMEOUT_MS); // Device reset expected.
wait_us(2 * TIMEOUT_US); // Device reset expected.

// Watchdog reset should have occurred during that wait() above;
// Watchdog reset should have occurred during a wait above.

watchdog.kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

Expand Down Expand Up @@ -309,6 +319,10 @@ int testsuite_setup(const size_t number_of_cases)
return utest::v1::STATUS_ABORT;
}

// The thread is started here, but feeding the watchdog will start
// when the semaphore is released during a test case teardown.
wdg_kicking_thread.start(mbed::callback(wdg_kicking_thread_fun));

utest_printf("This test suite is composed of %i test cases. Starting at index %i.\n", number_of_cases,
current_case.start_index);
return current_case.start_index;
Expand All @@ -318,7 +332,7 @@ Case cases[] = {
Case("Watchdog reset", case_setup, test_simple_reset),
#if DEVICE_SLEEP
Case("Watchdog reset in sleep mode", case_setup, test_sleep_reset),
#if DEVICE_LOWPOWERTIMER
#if DEVICE_LPTICKER
Case("Watchdog reset in deepsleep mode", case_setup, test_deepsleep_reset),
#endif
#endif
Expand Down
83 changes: 48 additions & 35 deletions TESTS/mbed_hal/watchdog_reset/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
* flushing the Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 ms.
* To be on the safe side, set the wait time to 20 ms.
*/
#define SERIAL_FLUSH_TIME_MS 20
#define SERIAL_FLUSH_TIME_MS 20

#define TIMEOUT_US (1000 * (TIMEOUT_MS))
#define KICK_ADVANCE_US (1000 * (KICK_ADVANCE_MS))
#define SERIAL_FLUSH_TIME_US (1000 * (SERIAL_FLUSH_TIME_MS))

using utest::v1::Case;
using utest::v1::Specification;
Expand All @@ -78,13 +82,20 @@ struct testcase_data {
uint32_t received_data;
};

void release_sem(Semaphore *sem)
testcase_data current_case;

Thread wdg_kicking_thread(osPriorityNormal, 768);
Semaphore kick_wdg_during_test_teardown(0, 1);

void wdg_kicking_thread_fun()
{
sem->release();
kick_wdg_during_test_teardown.acquire();
while (true) {
hal_watchdog_kick();
wait_us(20000);
}
}

testcase_data current_case;

bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms)
{
char msg_value[12];
Expand Down Expand Up @@ -116,11 +127,11 @@ void test_simple_reset()
}
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
// Watchdog should fire before twice the timeout value.
wait_ms(2 * TIMEOUT_MS); // Device reset expected.
wait_us(2 * TIMEOUT_US); // Device reset expected.

// Watchdog reset should have occurred during wait_ms() above;
// Watchdog reset should have occurred during a wait above.

hal_watchdog_kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

Expand All @@ -136,30 +147,27 @@ void test_sleep_reset()

// Phase 1. -- run the test code.
watchdog_config_t config = { TIMEOUT_MS };
Semaphore sem(0, 1);
Timeout timeout;
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
}
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
sleep_manager_lock_deep_sleep();
// Watchdog should fire before twice the timeout value.
timeout.attach_us(mbed::callback(release_sem, &sem), 1000ULL * (2 * TIMEOUT_MS));
if (sleep_manager_can_deep_sleep()) {
TEST_ASSERT_MESSAGE(0, "Deepsleep should be disallowed.");
return;
}
sem.wait(); // Device reset expected.
// Watchdog should fire before twice the timeout value.
ThisThread::sleep_for(2 * TIMEOUT_MS); // Device reset expected.
sleep_manager_unlock_deep_sleep();

// Watchdog reset should have occurred during sem.wait() (sleep) above;
// Watchdog reset should have occurred during the sleep above.

hal_watchdog_kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

#if DEVICE_LOWPOWERTIMER
#if DEVICE_LPTICKER
void test_deepsleep_reset()
{
// Phase 2. -- verify the test results.
Expand All @@ -171,24 +179,25 @@ void test_deepsleep_reset()

// Phase 1. -- run the test code.
watchdog_config_t config = { TIMEOUT_MS };
Semaphore sem(0, 1);
LowPowerTimeout lp_timeout;
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS + SERIAL_FLUSH_TIME_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
}
wait_us(SERIAL_FLUSH_TIME_US); // Wait for the serial buffers to flush.
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
// Watchdog should fire before twice the timeout value.
lp_timeout.attach_us(mbed::callback(release_sem, &sem), 1000ULL * (2 * TIMEOUT_MS));
wait_ms(SERIAL_FLUSH_TIME_MS); // Wait for the serial buffers to flush.
if (!sleep_manager_can_deep_sleep()) {
TEST_ASSERT_MESSAGE(0, "Deepsleep should be allowed.");
}
sem.wait(); // Device reset expected.

// Watchdog reset should have occurred during sem.wait() (deepsleep) above;
// The Watchdog reset is allowed to be delayed up to twice the timeout
// value when the deepsleep mode is active.
// To make the test less sensitive to clock/wait accuracy, add 20% extra
// (making tha whole deepsleep wait equal to 2.2 * timeout).
ThisThread::sleep_for(220 * TIMEOUT_MS / 100); // Device reset expected.

// Watchdog reset should have occurred during the deepsleep above.

hal_watchdog_kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}
#endif
Expand All @@ -212,24 +221,24 @@ void test_restart_reset()
// Phase 1. -- run the test code.
watchdog_config_t config = { TIMEOUT_MS };
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
wait_ms(TIMEOUT_MS / 2UL);
wait_us(TIMEOUT_US / 2);
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_stop());
// Check that stopping the Watchdog prevents a device reset.
// The watchdog should trigger at, or after the timeout value.
// The watchdog should trigger before twice the timeout value.
wait_ms(TIMEOUT_MS / 2UL + TIMEOUT_MS);
wait_us(TIMEOUT_US / 2 + TIMEOUT_US);

if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
}
TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config));
// Watchdog should fire before twice the timeout value.
wait_ms(2 * TIMEOUT_MS); // Device reset expected.
wait_us(2 * TIMEOUT_US); // Device reset expected.

// Watchdog reset should have occurred during that wait() above;
// Watchdog reset should have occurred during a wait above.

hal_watchdog_kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

Expand All @@ -248,19 +257,19 @@ void test_kick_reset()
for (int i = 3; i; i--) {
// The reset is prevented as long as the watchdog is kicked
// anytime before the timeout.
wait_ms(TIMEOUT_MS - KICK_ADVANCE_MS);
wait_us(TIMEOUT_US - KICK_ADVANCE_US);
hal_watchdog_kick();
}
if (send_reset_notification(&current_case, 2 * TIMEOUT_MS) == false) {
TEST_ASSERT_MESSAGE(0, "Dev-host communication error.");
return;
}
// Watchdog should fire before twice the timeout value.
wait_ms(2 * TIMEOUT_MS); // Device reset expected.
wait_us(2 * TIMEOUT_US); // Device reset expected.

// Watchdog reset should have occurred during that wait() above;
// Watchdog reset should have occurred during a wait above.

hal_watchdog_kick(); // Just to buy some time for testsuite failure handling.
kick_wdg_during_test_teardown.release(); // For testsuite failure handling.
TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected.");
}

Expand Down Expand Up @@ -295,6 +304,10 @@ int testsuite_setup(const size_t number_of_cases)
return utest::v1::STATUS_ABORT;
}

// The thread is started here, but feeding the watchdog will start
// when the semaphore is released during a test case teardown.
wdg_kicking_thread.start(mbed::callback(wdg_kicking_thread_fun));

utest_printf("This test suite is composed of %i test cases. Starting at index %i.\n", number_of_cases,
current_case.start_index);
return current_case.start_index;
Expand All @@ -304,7 +317,7 @@ Case cases[] = {
Case("Watchdog reset", case_setup, test_simple_reset),
#if DEVICE_SLEEP
Case("Watchdog reset in sleep mode", case_setup, test_sleep_reset),
#if DEVICE_LOWPOWERTIMER
#if DEVICE_LPTICKER
Case("Watchdog reset in deepsleep mode", case_setup, test_deepsleep_reset),
#endif
#endif
Expand Down
Loading