From 42a9cc1e20071b6dd2133bfa1c3e79ace08c5a99 Mon Sep 17 00:00:00 2001 From: Przemyslaw Stekiel Date: Wed, 26 Feb 2020 13:07:32 +0100 Subject: [PATCH] tests-mbed_hal-sleep_manager: fix counter wraparound handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is a mismatch while handling counter wraparound inĀ `test_sleep_auto` test case. In the test timestamps in ticks are first converted to us and then while counting the time difference wraparound is handled (us and ticks fields are mismatched). The ticker wraparound case must be handled in the field of ticks, and then the difference converted to us. --- TESTS/mbed_hal/sleep_manager/main.cpp | 29 +++++++++++++++------------ 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/TESTS/mbed_hal/sleep_manager/main.cpp b/TESTS/mbed_hal/sleep_manager/main.cpp index b73b970773..09a0ad1c4d 100644 --- a/TESTS/mbed_hal/sleep_manager/main.cpp +++ b/TESTS/mbed_hal/sleep_manager/main.cpp @@ -141,7 +141,7 @@ void test_sleep_auto() const ticker_info_t *lp_ticker_info = get_lp_ticker_data()->interface->get_info(); const unsigned lp_ticker_mask = ((1 << lp_ticker_info->bits) - 1); const ticker_irq_handler_type lp_ticker_irq_handler_org = set_lp_ticker_irq_handler(lp_ticker_isr); - us_timestamp_t us_ts1, us_ts2, lp_ts1, lp_ts2, us_diff1, us_diff2, lp_diff1, lp_diff2; + uint32_t us_ts1, us_ts2, lp_ts1, lp_ts2, us_diff1, us_diff2, lp_diff1, lp_diff2; /* Let's avoid the Lp ticker wrap-around case */ wraparound_lp_protect(); @@ -155,14 +155,16 @@ void test_sleep_auto() sleep_manager_lock_deep_sleep(); - us_ts1 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency); - lp_ts1 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency); + us_ts1 = us_ticker_read(); + lp_ts1 = lp_ticker_read(); sleep_manager_sleep_auto(); - us_ts2 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency); - us_diff1 = (us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1); - lp_ts2 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency); - lp_diff1 = (lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1); + + us_ts2 = us_ticker_read(); + lp_ts2 = lp_ticker_read(); + + us_diff1 = ticks_to_us((us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1), us_ticker_info->frequency); + lp_diff1 = ticks_to_us((lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1), lp_ticker_info->frequency); // Deep sleep locked -- ordinary sleep mode used: // * us_ticker powered ON, @@ -190,15 +192,16 @@ void test_sleep_auto() * set and forbid deep_sleep during that period. Let this period pass */ TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep_test_check()); - us_ts1 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency); - lp_ts1 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency); + us_ts1 = us_ticker_read(); + lp_ts1 = lp_ticker_read(); sleep_manager_sleep_auto(); - us_ts2 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency); - us_diff2 = (us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1); - lp_ts2 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency); - lp_diff2 = (lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1); + us_ts2 = us_ticker_read(); + lp_ts2 = lp_ticker_read(); + + us_diff2 = ticks_to_us((us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1), us_ticker_info->frequency); + lp_diff2 = ticks_to_us((lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1), lp_ticker_info->frequency); // Deep sleep unlocked -- deep sleep mode used: // * us_ticker powered OFF,