From 5190ec8048950535501d2cc1d6ba496aba83f22f Mon Sep 17 00:00:00 2001 From: ccli8 Date: Mon, 23 Jul 2018 11:42:20 +0800 Subject: [PATCH] Enlarge wait time for flushing serial buffer in Greentea test code Original wait time is not enough for UART to flush out before deepsleep/reset on some targets. --- TESTS/mbed_hal/reset_reason/main.cpp | 20 ++++++++++++++++++-- TESTS/mbed_hal/watchdog/main.cpp | 18 +++++++++++++++++- TESTS/mbed_hal/watchdog_reset/main.cpp | 18 +++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/TESTS/mbed_hal/reset_reason/main.cpp b/TESTS/mbed_hal/reset_reason/main.cpp index 20f21a9409..c2759a387f 100644 --- a/TESTS/mbed_hal/reset_reason/main.cpp +++ b/TESTS/mbed_hal/reset_reason/main.cpp @@ -49,6 +49,22 @@ #define MSG_KEY_RESET_REASON "reason" #define MSG_KEY_DEVICE_RESET "reset" +/* Flush serial buffer before deep sleep/reset + * + * Since deepsleep()/reset would shut down the UART peripheral, we wait for some time + * to allow for hardware serial buffers to completely flush. + * + * Take NUMAKER_PFM_NUC472 as an example: + * Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush + * Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to + * 20ms here for safe. + * + * This should be replaced with a better function that checks if the + * hardware buffers are empty. However, such an API does not exist now, + * so we'll use the wait_ms() function for now. + */ +#define SERIAL_FLUSH_TIME_MS 20 + typedef enum { CMD_STATUS_CONTINUE, CMD_STATUS_ERROR @@ -85,7 +101,7 @@ static cmd_status_t handle_command(const char *key, const char *value) if (strcmp(key, MSG_KEY_DEVICE_RESET) == 0 && strcmp(value, MSG_VALUE_DEVICE_RESET_NVIC) == 0) { greentea_send_kv(MSG_KEY_DEVICE_RESET, MSG_VALUE_DEVICE_RESET_ACK); - wait_ms(10); // Wait for the serial buffers to flush. + wait_ms(SERIAL_FLUSH_TIME_MS); // Wait for the serial buffers to flush. NVIC_SystemReset(); TEST_ASSERT_MESSAGE(0, "NVIC_SystemReset did not reset the device as expected."); return CMD_STATUS_ERROR; @@ -94,7 +110,7 @@ static cmd_status_t handle_command(const char *key, const char *value) #if DEVICE_WATCHDOG if (strcmp(key, MSG_KEY_DEVICE_RESET) == 0 && strcmp(value, MSG_VALUE_DEVICE_RESET_WATCHDOG) == 0) { greentea_send_kv(MSG_KEY_DEVICE_RESET, MSG_VALUE_DEVICE_RESET_ACK); - wait_ms(10); // Wait for the serial buffers to flush. + wait_ms(SERIAL_FLUSH_TIME_MS); // Wait for the serial buffers to flush. watchdog_config_t config = { .timeout_ms = WDG_TIMEOUT_MS }; if (hal_watchdog_init(&config) != WATCHDOG_STATUS_OK) { TEST_ASSERT_MESSAGE(0, "hal_watchdog_init() error."); diff --git a/TESTS/mbed_hal/watchdog/main.cpp b/TESTS/mbed_hal/watchdog/main.cpp index bbb2dd7fbb..a4434ab223 100644 --- a/TESTS/mbed_hal/watchdog/main.cpp +++ b/TESTS/mbed_hal/watchdog/main.cpp @@ -41,6 +41,22 @@ #define MSG_KEY_START_CASE "start_case" #define MSG_KEY_DEVICE_RESET "reset_on_case_teardown" +/* Flush serial buffer before deep sleep/reset + * + * Since deepsleep()/reset would shut down the UART peripheral, we wait for some time + * to allow for hardware serial buffers to completely flush. + * + * Take NUMAKER_PFM_NUC472 as an example: + * Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush + * Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to + * 20ms here for safe. + * + * This should be replaced with a better function that checks if the + * hardware buffers are empty. However, such an API does not exist now, + * so we'll use the wait_ms() function for now. + */ +#define SERIAL_FLUSH_TIME_MS 20 + int CASE_INDEX_START; int CASE_INDEX_CURRENT; @@ -128,7 +144,7 @@ utest::v1::status_t case_teardown_sync_on_reset(const Case * const source, const } greentea_send_kv(MSG_KEY_DEVICE_RESET, CASE_INDEX_START + CASE_INDEX_CURRENT); utest_printf("The device will now restart.\n"); - wait_ms(10); // Wait for the serial buffers to flush. + wait_ms(SERIAL_FLUSH_TIME_MS); // Wait for the serial buffers to flush. NVIC_SystemReset(); return status; // Reset is instant so this line won't be reached. } diff --git a/TESTS/mbed_hal/watchdog_reset/main.cpp b/TESTS/mbed_hal/watchdog_reset/main.cpp index edaf66dea2..5336ca57c3 100644 --- a/TESTS/mbed_hal/watchdog_reset/main.cpp +++ b/TESTS/mbed_hal/watchdog_reset/main.cpp @@ -38,6 +38,22 @@ #define MSG_KEY_START_CASE "start_case" #define MSG_KEY_DEVICE_RESET "dev_reset" +/* Flush serial buffer before deep sleep/reset + * + * Since deepsleep()/reset would shut down the UART peripheral, we wait for some time + * to allow for hardware serial buffers to completely flush. + * + * Take NUMAKER_PFM_NUC472 as an example: + * Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush + * Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to + * 20ms here for safe. + * + * This should be replaced with a better function that checks if the + * hardware buffers are empty. However, such an API does not exist now, + * so we'll use the wait_ms() function for now. + */ +#define SERIAL_FLUSH_TIME_MS 20 + using utest::v1::Case; using utest::v1::Specification; using utest::v1::Harness; @@ -149,7 +165,7 @@ void test_deepsleep_reset() } TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config)); lp_timeout.attach_us(mbed::callback(release_sem, &sem), 1000ULL * (TIMEOUT_MS + TIMEOUT_DELTA_MS)); - wait_ms(10); // Wait for the serial buffers to flush. + 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."); }