diff --git a/TESTS/mbed_hal/lp_us_tickers/main.cpp b/TESTS/mbed_hal/lp_us_tickers/main.cpp index 8090df6a67..5d3e691a73 100644 --- a/TESTS/mbed_hal/lp_us_tickers/main.cpp +++ b/TESTS/mbed_hal/lp_us_tickers/main.cpp @@ -26,7 +26,7 @@ #endif #define FORCE_OVERFLOW_TEST (false) -#define TICKER_INT_VAL 2000 +#define TICKER_INT_VAL 500 #define TICKER_DELTA 50 #define LP_TICKER_OVERFLOW_DELTA 0 // this will allow to detect that ticker counter rollovers to 0 @@ -51,9 +51,6 @@ unsigned int ticker_overflow_delta; * Parameter is used to disable compiler optimisation. */ uint32_t count_ticks(volatile uint32_t cycles, uint32_t step) { - /* Init the ticker. */ - intf->init(); - core_util_critical_section_enter(); const uint32_t start = intf->read(); @@ -133,9 +130,7 @@ void ticker_info_test(void) /* Test that ticker interrupt fires only when the ticker counter increments to the value set by ticker_set_interrupt. */ void ticker_interrupt_test(void) { - uint32_t ticker_timeout[] = { 100, 500, 1000, 2000 }; - - intf->init(); + uint32_t ticker_timeout[] = { 100, 200, 300, 500 }; for (uint32_t i = 0; i < (sizeof(ticker_timeout) / sizeof(uint32_t)); i++) { intFlag = 0; @@ -175,8 +170,6 @@ void ticker_past_test(void) { intFlag = 0; - intf->init(); - const uint32_t tick_count = intf->read(); /* Set interrupt tick count to value in the past. @@ -193,8 +186,6 @@ void ticker_repeat_reschedule_test(void) { intFlag = 0; - intf->init(); - const uint32_t tick_count = intf->read(); /* Set interrupt. Interrupt should be fired when tick count is equal to: @@ -227,8 +218,6 @@ void ticker_fire_now_test(void) { intFlag = 0; - intf->init(); - intf->fire_interrupt(); /* On some platforms set_interrupt function sets interrupt in the nearest feature. */ @@ -256,8 +245,6 @@ void ticker_overflow_test(void) intFlag = 0; - intf->init(); - /* Wait for max count. */ while (intf->read() != (max_count - ticker_overflow_delta)) { /* Just wait. */ @@ -296,8 +283,6 @@ void ticker_overflow_test(void) /* Test that the ticker increments by one on each tick. */ void ticker_increment_test(void) { - intf->init(); - const ticker_info_t* p_ticker_info = intf->get_info(); /* Perform test based on ticker speed. */ @@ -334,8 +319,6 @@ void ticker_speed_test(void) Timer timer; int counter = NUM_OF_CALLS; - intf->init(); - /* ---- Test ticker_read function. ---- */ timer.reset(); timer.start(); @@ -391,15 +374,38 @@ void ticker_speed_test(void) TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US))); } +/* Since according to the ticker requirements min acceptable counter size is + * 12 bits (low power timer) for which max count is + * 4095, then all test cases must be executed in this time window. + * HAL ticker layer handles overflow and it is not handled in the target + * ticker drivers. + */ +void overflow_protect() +{ + const uint32_t ticks_now = intf->read(); + const ticker_info_t* p_ticker_info = intf->get_info(); + + const uint32_t max_count = (1 << p_ticker_info->bits - 1); + + if ((max_count - ticks_now) > 4000) { + return; + } + + while (intf->read() > ticks_now); +} utest::v1::status_t hf_ticker_setup(const Case *const source, const size_t index_of_case) { intf = get_us_ticker_data()->interface; + intf->init(); + set_us_ticker_irq_handler(ticker_event_handler_stub); ticker_overflow_delta = HF_TICKER_OVERFLOW_DELTA; + overflow_protect(); + return greentea_case_setup_handler(source, index_of_case); } @@ -408,10 +414,14 @@ utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index { intf = get_lp_ticker_data()->interface; + intf->init(); + set_lp_ticker_irq_handler(ticker_event_handler_stub); ticker_overflow_delta = LP_TICKER_OVERFLOW_DELTA; + overflow_protect(); + return greentea_case_setup_handler(source, index_of_case); } #endif