diff --git a/TESTS/mbed_hal/common_tickers/main.cpp b/TESTS/mbed_hal/common_tickers/main.cpp index 2ea54637a8..60349f8e87 100644 --- a/TESTS/mbed_hal/common_tickers/main.cpp +++ b/TESTS/mbed_hal/common_tickers/main.cpp @@ -43,6 +43,7 @@ extern "C" { #define US_TICKER_OVERFLOW_DELTA2 60 #define TICKER_100_TICKS 100 +#define TICKER_500_TICKS 500 #define MAX_FUNC_EXEC_TIME_US 20 #define DELTA_FUNC_EXEC_TIME_US 5 @@ -420,8 +421,19 @@ void ticker_increment_test(void) void ticker_speed_test(void) { Timer timer; +#if DEVICE_LPTICKER + LowPowerTimer lptimer; +#endif int counter = NUM_OF_CALLS; + /* Free function will disable the ticker. For time measurement + * we need to use other one. + */ +#if DEVICE_LPTICKER + bool us_ticker_test = (intf == get_us_ticker_data()->interface); + Timer * free_timer = us_ticker_test ? &lptimer : &timer; +#endif + /* ---- Test ticker_read function. ---- */ timer.reset(); timer.start(); @@ -475,8 +487,70 @@ void ticker_speed_test(void) timer.stop(); TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US))); + + /* ---- Test free function. ---- */ +#if DEVICE_LPTICKER + counter = NUM_OF_CALLS; + free_timer->reset(); + free_timer->start(); + while (counter--) { + intf->free(); + } + free_timer->stop(); + + TEST_ASSERT(free_timer->read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US))); +#endif } +/* Test that ticker_free disables ticker interrupt. */ +void ticker_free_interrupt_test(void) +{ + overflow_protect(); + + uint32_t cycles_500_ticks = 50; + uint32_t reference_ticks_count = 0; + + while(reference_ticks_count < TICKER_500_TICKS) { + cycles_500_ticks *= 2; + const uint32_t start = intf->read(); + wait_cycles(cycles_500_ticks); + reference_ticks_count = intf->read() - start; + } + + intFlag = 0; + + intf->set_interrupt(intf->read() + (TICKER_500_TICKS / 2)); + intf->free(); + wait_cycles(cycles_500_ticks); + intf->init(); + TEST_ASSERT_EQUAL(0, intFlag); +} + +/* Test that ticker can be successfully re-initialized after free(). */ +void ticker_init_free_test(void) +{ + intf->free(); + intf->init(); + + overflow_protect(); + + intFlag = 0; + + const uint32_t tick_count = intf->read(); + + intf->set_interrupt(intf->read() + TICKER_INT_VAL); + + while (intf->read() < (tick_count + TICKER_INT_VAL - TICKER_DELTA)) { + TEST_ASSERT_EQUAL_INT_MESSAGE(0, intFlag, "Interrupt fired too early"); + } + + while (intf->read() < (tick_count + TICKER_INT_VAL + TICKER_DELTA)) { + } + + TEST_ASSERT_EQUAL(1, intFlag); +} + + utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index_of_case) { intf = get_us_ticker_data()->interface; @@ -551,6 +625,8 @@ Case cases[] = { Case("Microsecond ticker overflow test", us_ticker_setup, ticker_overflow_test, us_ticker_teardown), Case("Microsecond ticker increment test", us_ticker_setup, ticker_increment_test, us_ticker_teardown), Case("Microsecond ticker speed test", us_ticker_setup, ticker_speed_test, us_ticker_teardown), + Case("Microsecond ticker free interrupt test", us_ticker_setup, ticker_free_interrupt_test, us_ticker_teardown), + Case("Microsecond re-init after free test", us_ticker_setup, ticker_init_free_test, us_ticker_teardown), #if DEVICE_LPTICKER Case("lp ticker init is safe to call repeatedly", lp_ticker_setup, ticker_init_test, lp_ticker_teardown), Case("lp ticker info test", lp_ticker_setup, ticker_info_test, lp_ticker_teardown), @@ -561,6 +637,8 @@ Case cases[] = { Case("lp ticker overflow test", lp_ticker_setup, ticker_overflow_test, lp_ticker_teardown), Case("lp ticker increment test", lp_ticker_setup, ticker_increment_test, lp_ticker_teardown), Case("lp ticker speed test", lp_ticker_setup, ticker_speed_test, lp_ticker_teardown), + Case("lp ticker free interrupt test", lp_ticker_setup, ticker_free_interrupt_test, lp_ticker_teardown), + Case("lp ticker re-init after free test", lp_ticker_setup, ticker_init_free_test, lp_ticker_teardown), #endif }; diff --git a/TESTS/mbed_hal/common_tickers/ticker_api_tests.h b/TESTS/mbed_hal/common_tickers/ticker_api_tests.h index 84b064de45..8df506f5eb 100644 --- a/TESTS/mbed_hal/common_tickers/ticker_api_tests.h +++ b/TESTS/mbed_hal/common_tickers/ticker_api_tests.h @@ -118,6 +118,21 @@ void ticker_speed_test(void); */ void ticker_overflow_test(void); +/** Test ticker_free disables ticker interrupt. + * + * Given ticker is available. + * When ticker interrupt is set and then ticker_free is called. + * Then ticker interrupt is not triggered. + */ +void ticker_free_interrupt_test(void); + +/** Test that ticker can be successfully re-initialized after free(). + * + * Given ticker is available. + * When ticker has been re-initialized after free(). + * Then ticker counts and generates interrupts. + */ +void ticker_init_free_test(void); /**@}*/ #ifdef __cplusplus