Skip to content

Commit 49fe946

Browse files
committed
Fix some targets fail to pass ticker overflow test
In mbed-os-tests-mbed_hal-common_tickers/Microsecond ticker overflow test, some targets would fail to catch specified ticker value near overflow in time and so fail. This commit alleviates the issue by checking ticker value range rather than one exact ticker value near overflow.
1 parent dd6482b commit 49fe946

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

TESTS/mbed_hal/common_tickers/main.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ extern "C" {
3737
#define TICKER_INT_VAL 500
3838
#define TICKER_DELTA 10
3939

40-
#define LP_TICKER_OVERFLOW_DELTA 0 // this will allow to detect that ticker counter rollovers to 0
41-
#define US_TICKER_OVERFLOW_DELTA 50
40+
#define LP_TICKER_OVERFLOW_DELTA1 0 // this will allow to detect that ticker counter rollovers to 0
41+
#define LP_TICKER_OVERFLOW_DELTA2 0
42+
#define US_TICKER_OVERFLOW_DELTA1 50
43+
#define US_TICKER_OVERFLOW_DELTA2 60
4244

4345
#define TICKER_100_TICKS 100
4446

@@ -56,7 +58,18 @@ using namespace utest::v1;
5658
volatile int intFlag = 0;
5759
const ticker_interface_t* intf;
5860
ticker_irq_handler_type prev_irq_handler;
59-
unsigned int ticker_overflow_delta;
61+
/* Some targets might fail overflow test uncertainly due to getting trapped in busy
62+
* intf->read() loop. In the loop, some ticker values wouldn't get caught in time
63+
* because of:
64+
* 1. Lower CPU clock
65+
* 2. Compiled code with worse performance
66+
* 3. Interrupt at that time
67+
*
68+
* We fix it by checking small ticker value range rather than one exact ticker point
69+
* in near overflow check.
70+
*/
71+
unsigned int ticker_overflow_delta1;
72+
unsigned int ticker_overflow_delta2;
6073

6174
/* Auxiliary function to count ticker ticks elapsed during execution of N cycles of empty while loop.
6275
* Parameter <step> is used to disable compiler optimisation. */
@@ -301,12 +314,13 @@ void ticker_overflow_test(void)
301314
intFlag = 0;
302315

303316
/* Wait for max count. */
304-
while (intf->read() != (max_count - ticker_overflow_delta)) {
317+
while (intf->read() >= (max_count - ticker_overflow_delta2) &&
318+
intf->read() <= (max_count - ticker_overflow_delta1)) {
305319
/* Just wait. */
306320
}
307321

308322
/* Now we are near/at the overflow point. Detect rollover. */
309-
while (intf->read() > ticker_overflow_delta);
323+
while (intf->read() > ticker_overflow_delta1);
310324

311325
const uint32_t after_overflow = intf->read();
312326

@@ -318,7 +332,7 @@ void ticker_overflow_test(void)
318332
const uint32_t next_after_overflow = intf->read();
319333

320334
/* Check that after the overflow ticker continue count. */
321-
TEST_ASSERT(after_overflow <= ticker_overflow_delta);
335+
TEST_ASSERT(after_overflow <= ticker_overflow_delta1);
322336
TEST_ASSERT(next_after_overflow >= TICKER_100_TICKS);
323337
TEST_ASSERT_EQUAL(0, intFlag);
324338

@@ -473,7 +487,8 @@ utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index
473487

474488
prev_irq_handler = set_us_ticker_irq_handler(ticker_event_handler_stub);
475489

476-
ticker_overflow_delta = US_TICKER_OVERFLOW_DELTA;
490+
ticker_overflow_delta1 = US_TICKER_OVERFLOW_DELTA1;
491+
ticker_overflow_delta2 = US_TICKER_OVERFLOW_DELTA2;
477492

478493
return greentea_case_setup_handler(source, index_of_case);
479494
}
@@ -501,7 +516,8 @@ utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index
501516

502517
prev_irq_handler = set_lp_ticker_irq_handler(ticker_event_handler_stub);
503518

504-
ticker_overflow_delta = LP_TICKER_OVERFLOW_DELTA;
519+
ticker_overflow_delta1 = LP_TICKER_OVERFLOW_DELTA1;
520+
ticker_overflow_delta2 = LP_TICKER_OVERFLOW_DELTA2;
505521

506522
return greentea_case_setup_handler(source, index_of_case);
507523
}

0 commit comments

Comments
 (0)