Skip to content

Commit

Permalink
Fix tests to work with LPTICKER_DELAY_TICKS
Browse files Browse the repository at this point in the history
Fix the HAL common_tickers and sleep tests so they work correctly when
the define LPTICKER_DELAY_TICKS is set.
  • Loading branch information
c1728p9 committed Aug 17, 2018
1 parent adc64cc commit f68958d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 37 deletions.
25 changes: 21 additions & 4 deletions TESTS/mbed_hal/common_tickers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ticker_api_tests.h"
#include "hal/us_ticker_api.h"
#include "hal/lp_ticker_api.h"
#include "hal/mbed_lp_ticker_wrapper.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -496,7 +497,15 @@ utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index
{
intf = get_us_ticker_data()->interface;

OS_Tick_Disable();
/* OS, common ticker and low power ticker wrapper
* may make use of us ticker so suspend them for this test */
osKernelSuspend();
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
/* Suspend the lp ticker wrapper since it makes use of the us ticker */
ticker_suspend(get_lp_ticker_data());
lp_ticker_wrapper_suspend();
#endif
ticker_suspend(get_us_ticker_data());

intf->init();

Expand All @@ -515,7 +524,12 @@ utest::v1::status_t us_ticker_teardown(const Case *const source, const size_t pa

prev_irq_handler = NULL;

OS_Tick_Enable();
ticker_resume(get_us_ticker_data());
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_resume();
ticker_resume(get_lp_ticker_data());
#endif
osKernelResume(0);

return greentea_case_teardown_handler(source, passed, failed, reason);
}
Expand All @@ -525,7 +539,9 @@ utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index
{
intf = get_lp_ticker_data()->interface;

OS_Tick_Disable();
/* OS and common ticker may make use of lp ticker so suspend them for this test */
osKernelSuspend();
ticker_suspend(get_lp_ticker_data());

intf->init();

Expand All @@ -544,7 +560,8 @@ utest::v1::status_t lp_ticker_teardown(const Case *const source, const size_t pa

prev_irq_handler = NULL;

OS_Tick_Enable();
ticker_resume(get_lp_ticker_data());
osKernelResume(0);

return greentea_case_teardown_handler(source, passed, failed, reason);
}
Expand Down
49 changes: 41 additions & 8 deletions TESTS/mbed_hal/common_tickers_freq/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ticker_api_test_freq.h"
#include "hal/us_ticker_api.h"
#include "hal/lp_ticker_api.h"
#include "hal/mbed_lp_ticker_wrapper.h"

#if !DEVICE_USTICKER
#error [NOT_SUPPORTED] test not supported
Expand All @@ -38,6 +39,7 @@
using namespace utest::v1;

const ticker_interface_t *intf;
ticker_irq_handler_type prev_handler;

static volatile unsigned int overflowCounter;

Expand Down Expand Up @@ -106,43 +108,74 @@ void ticker_frequency_test()

utest::v1::status_t us_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case)
{
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
/* Suspend the lp ticker wrapper since it makes use of the us ticker */
ticker_suspend(get_lp_ticker_data());
lp_ticker_wrapper_suspend();
#endif
ticker_suspend(get_us_ticker_data());
intf = get_us_ticker_data()->interface;
set_us_ticker_irq_handler(ticker_event_handler_stub);
prev_handler = set_us_ticker_irq_handler(ticker_event_handler_stub);
return greentea_case_setup_handler(source, index_of_case);
}

utest::v1::status_t us_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed,
const failure_t reason)
{
set_us_ticker_irq_handler(prev_handler);
ticker_resume(get_us_ticker_data());
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_resume();
ticker_resume(get_lp_ticker_data());
#endif
return greentea_case_teardown_handler(source, passed, failed, reason);
}

#if DEVICE_LPTICKER
utest::v1::status_t lp_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case)
{
ticker_suspend(get_lp_ticker_data());
intf = get_lp_ticker_data()->interface;
set_lp_ticker_irq_handler(ticker_event_handler_stub);
prev_handler = set_lp_ticker_irq_handler(ticker_event_handler_stub);
return greentea_case_setup_handler(source, index_of_case);
}
#endif

utest::v1::status_t ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed,
const failure_t reason)
utest::v1::status_t lp_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed,
const failure_t reason)
{
set_lp_ticker_irq_handler(prev_handler);
ticker_resume(get_lp_ticker_data());
return greentea_case_teardown_handler(source, passed, failed, reason);
}
#endif

// Test cases
Case cases[] = {
Case("Microsecond ticker frequency test", us_ticker_case_setup_handler_t, ticker_frequency_test,
ticker_case_teardown_handler_t),
us_ticker_case_teardown_handler_t),
#if DEVICE_LPTICKER
Case("Low power ticker frequency test", lp_ticker_case_setup_handler_t, ticker_frequency_test,
ticker_case_teardown_handler_t),
lp_ticker_case_teardown_handler_t),
#endif
};

utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
{
/* Suspend RTOS Kernel so the timers are not in use. */
osKernelSuspend();

GREENTEA_SETUP(120, "timing_drift_auto");
return greentea_test_setup_handler(number_of_cases);
}

Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
void greentea_test_teardown(const size_t passed, const size_t failed, const failure_t failure)
{
osKernelResume(0);

greentea_test_teardown_handler(passed, failed, failure);
}

Specification specification(greentea_test_setup, cases, greentea_test_teardown);

int main()
{
Expand Down
33 changes: 30 additions & 3 deletions TESTS/mbed_hal/lp_ticker/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "rtos.h"
#include "lp_ticker_api_tests.h"
#include "hal/lp_ticker_api.h"
#include "hal/mbed_lp_ticker_wrapper.h"

#if !DEVICE_LPTICKER
#error [NOT_SUPPORTED] Low power timer not supported for this target
Expand All @@ -29,6 +30,8 @@ using namespace utest::v1;

volatile int intFlag = 0;

ticker_irq_handler_type prev_handler;

#define US_PER_MS 1000

#define TICKER_GLITCH_TEST_TICKS 1000
Expand Down Expand Up @@ -113,8 +116,6 @@ void lp_ticker_deepsleep_test()
{
intFlag = 0;

set_lp_ticker_irq_handler(ticker_event_handler_stub);

lp_ticker_init();

/* Give some time Green Tea to finish UART transmission before entering
Expand Down Expand Up @@ -157,6 +158,32 @@ void lp_ticker_glitch_test()
}
}

#if DEVICE_LPTICKER
utest::v1::status_t lp_ticker_deepsleep_test_setup_handler(const Case *const source, const size_t index_of_case)
{
/* disable everything using the lp ticker for this test */
osKernelSuspend();
ticker_suspend(get_lp_ticker_data());
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_suspend();
#endif
prev_handler = set_lp_ticker_irq_handler(ticker_event_handler_stub);
return greentea_case_setup_handler(source, index_of_case);
}

utest::v1::status_t lp_ticker_deepsleep_test_teardown_handler(const Case *const source, const size_t passed, const size_t failed,
const failure_t reason)
{
set_lp_ticker_irq_handler(prev_handler);
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_resume();
#endif
ticker_resume(get_lp_ticker_data());
osKernelResume(0);
return greentea_case_teardown_handler(source, passed, failed, reason);
}
#endif

utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(20, "default_auto");
Expand All @@ -166,7 +193,7 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
Case cases[] = {
Case("lp ticker info test", lp_ticker_info_test),
#if DEVICE_SLEEP
Case("lp ticker sleep test", lp_ticker_deepsleep_test),
Case("lp ticker sleep test", lp_ticker_deepsleep_test_setup_handler, lp_ticker_deepsleep_test, lp_ticker_deepsleep_test_teardown_handler),
#endif
Case("lp ticker glitch test", lp_ticker_glitch_test)
};
Expand Down
66 changes: 44 additions & 22 deletions TESTS/mbed_hal/sleep/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"
#include "mbed_lp_ticker_wrapper.h"

#include "sleep_api_tests.h"

Expand All @@ -40,7 +41,7 @@
*
* 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.
* so we'll use the busy_wait_ms() function for now.
*/
#define SERIAL_FLUSH_TIME_MS 20

Expand Down Expand Up @@ -103,6 +104,20 @@ bool compare_timestamps(unsigned int delta_ticks, unsigned int ticker_width, uns
}
}

void busy_wait_ms(int ms)
{
const ticker_info_t *info = us_ticker_get_info();
uint32_t mask = (1 << info->bits) - 1;
int delay = (int)((uint64_t)ms * info->frequency / 1000);

uint32_t prev = us_ticker_read();
while (delay > 0) {
uint32_t next = us_ticker_read();
delay -= (next - prev) & mask;
prev = next;
}
}

void us_ticker_isr(const ticker_data_t *const ticker_data)
{
us_ticker_clear_interrupt();
Expand All @@ -125,17 +140,6 @@ void sleep_usticker_test()

const ticker_irq_handler_type us_ticker_irq_handler_org = set_us_ticker_irq_handler(us_ticker_isr);

// call ticker_read_us to initialize ticker upper layer
// prevents subsequent scheduling of max_delta interrupt during ticker initialization while test execution
// (e.g when ticker_read_us is called)
ticker_read_us(ticker);
#ifdef DEVICE_LPTICKER
// call ticker_read_us to initialize lp_ticker
// prevents scheduling interrupt during ticker initialization (in lp_ticker_init) while test execution
// (e.g when ticker_read_us is called for lp_ticker, see MBED_CPU_STATS_ENABLED)
ticker_read_us(get_lp_ticker_data());
#endif

/* Test only sleep functionality. */
sleep_manager_lock_deep_sleep();
TEST_ASSERT_FALSE_MESSAGE(sleep_manager_can_deep_sleep(), "deep sleep should be locked");
Expand Down Expand Up @@ -173,17 +177,12 @@ void deepsleep_lpticker_test()
const unsigned int ticker_freq = ticker->interface->get_info()->frequency;
const unsigned int ticker_width = ticker->interface->get_info()->bits;

// call ticker_read_us to initialize ticker upper layer
// prevents subsequent scheduling of max_delta interrupt during ticker initialization while test execution
// (e.g when ticker_read_us is called)
ticker_read_us(ticker);

const ticker_irq_handler_type lp_ticker_irq_handler_org = set_lp_ticker_irq_handler(lp_ticker_isr);

/* Give some time Green Tea to finish UART transmission before entering
* deep-sleep mode.
*/
wait_ms(SERIAL_FLUSH_TIME_MS);
busy_wait_ms(SERIAL_FLUSH_TIME_MS);

TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep(), "deep sleep should not be locked");

Expand Down Expand Up @@ -218,7 +217,7 @@ void deepsleep_high_speed_clocks_turned_off_test()
/* Give some time Green Tea to finish UART transmission before entering
* deep-sleep mode.
*/
wait_ms(SERIAL_FLUSH_TIME_MS);
busy_wait_ms(SERIAL_FLUSH_TIME_MS);

TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep(), "deep sleep should not be locked");

Expand Down Expand Up @@ -256,15 +255,38 @@ utest::v1::status_t greentea_failure_handler(const Case *const source, const fai
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(60, "default_auto");
/* Suspend RTOS Kernel to enable sleep modes. */
osKernelSuspend();
#if DEVICE_LPTICKER
ticker_suspend(get_lp_ticker_data());
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_suspend();
#endif
#endif
ticker_suspend(get_us_ticker_data());

us_ticker_init();
#if DEVICE_LPTICKER
lp_ticker_init();
#endif
/* Suspend RTOS Kernel to enable sleep modes. */
osKernelSuspend();

return greentea_test_setup_handler(number_of_cases);
}

void greentea_test_teardown(const size_t passed, const size_t failed, const failure_t failure)
{
ticker_resume(get_us_ticker_data());
#if DEVICE_LPTICKER
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_resume();
#endif
ticker_resume(get_lp_ticker_data());
#endif
osKernelResume(0);

greentea_test_teardown_handler(passed, failed, failure);
}

Case cases[] = {
Case("sleep - source of wake-up - us ticker", sleep_usticker_test, greentea_failure_handler),
#if DEVICE_LPTICKER
Expand All @@ -273,7 +295,7 @@ Case cases[] = {
#endif
};

Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
Specification specification(greentea_test_setup, cases, greentea_test_teardown);

int main()
{
Expand Down

0 comments on commit f68958d

Please sign in to comment.