-
Notifications
You must be signed in to change notification settings - Fork 3k
Add tests for ticker HAL API. #5233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a607483
b3cbea7
336a03d
7ec02a0
4728677
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2017-2017 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** \addtogroup hal_lp_ticker_tests | ||
* @{ | ||
*/ | ||
|
||
#ifndef LP_TICKER_API_TESTS_H | ||
#define LP_TICKER_API_TESTS_H | ||
|
||
#include "device.h" | ||
|
||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** Test that the ticker has the correct frequency and number of bits. | ||
* | ||
* Given ticker is available. | ||
* When ticker information data is obtained. | ||
* Then collected data indicates that ticker frequency is between 8KHz and 64KHz and the counter is at least 12 bits wide. | ||
*/ | ||
void lp_ticker_info_test(void); | ||
|
||
/** Test that the ticker continues operating in deep sleep mode. | ||
* | ||
* Given ticker is available. | ||
* When ticker has interrupt set and board enters deep-sleep mode. | ||
* Then ticker continues operating. | ||
*/ | ||
void lp_ticker_deepsleep_test(void); | ||
|
||
|
||
/**@}*/ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif | ||
|
||
/**@}*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2016 ARM Limited | ||
* Copyright (c) 2017 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -13,177 +13,93 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "mbed.h" | ||
#include "greentea-client/test_env.h" | ||
#include "unity.h" | ||
#include "utest.h" | ||
#include "rtos.h" | ||
#include "lp_ticker_api_tests.h" | ||
#include "hal/lp_ticker_api.h" | ||
|
||
#if !DEVICE_LOWPOWERTIMER | ||
#error [NOT_SUPPORTED] Low power timer not supported for this target | ||
#error [NOT_SUPPORTED] test not supported | ||
#endif | ||
|
||
#include "utest/utest.h" | ||
#include "unity/unity.h" | ||
#include "greentea-client/test_env.h" | ||
|
||
#include "mbed.h" | ||
#include "lp_ticker_api.h" | ||
|
||
using namespace utest::v1; | ||
|
||
static volatile bool complete; | ||
static volatile timestamp_t complete_time; | ||
static ticker_event_t delay_event; | ||
static const ticker_data_t *lp_ticker_data = get_lp_ticker_data(); | ||
static Timer timer; | ||
static LowPowerTimer lp_timer; | ||
|
||
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */ | ||
#define LONG_TIMEOUT (100000) | ||
#define SHORT_TIMEOUT (600) | ||
|
||
void cb_done(uint32_t id) { | ||
if ((uint32_t)&delay_event == id) { | ||
complete_time = timer.read_us(); | ||
complete = true; | ||
} else { | ||
// Normal ticker handling | ||
TimerEvent::irq(id); | ||
} | ||
} | ||
volatile int intFlag = 0; | ||
|
||
void cb_done_deepsleep(uint32_t id) { | ||
if ((uint32_t)&delay_event == id) { | ||
complete_time = lp_timer.read_us(); | ||
complete = true; | ||
} else { | ||
// Normal ticker handling | ||
TimerEvent::irq(id); | ||
} | ||
} | ||
#define TICKER_INT_VAL 5000 | ||
#define TICKER_DELTA 50 | ||
|
||
void lp_ticker_delay_us(uint32_t delay_us, uint32_t tolerance) | ||
void ticker_event_handler_stub(const ticker_data_t * const ticker) | ||
{ | ||
complete = false; | ||
uint32_t delay_ts; | ||
|
||
ticker_set_handler(lp_ticker_data, cb_done); | ||
ticker_remove_event(lp_ticker_data, &delay_event); | ||
delay_ts = ticker_read(lp_ticker_data) + delay_us; | ||
|
||
timer.reset(); | ||
timer.start(); | ||
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); | ||
while (!complete); | ||
timer.stop(); | ||
/* Indicate that ISR has been executed in interrupt context. */ | ||
if (IS_IRQ_MODE()) { | ||
intFlag++; | ||
} | ||
|
||
TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, complete_time); | ||
TEST_ASSERT_TRUE(complete); | ||
/* Clear and disable ticker interrupt. */ | ||
lp_ticker_clear_interrupt(); | ||
lp_ticker_disable_interrupt(); | ||
} | ||
|
||
#if DEVICE_SLEEP | ||
void lp_ticker_1s_deepsleep() | ||
void wait_cycles(volatile unsigned int cycles) | ||
{ | ||
complete = false; | ||
uint32_t delay_ts; | ||
|
||
/* | ||
* Since deepsleep() may shut down the UART peripheral, we wait for 10ms | ||
* to allow for hardware serial buffers to completely flush. | ||
|
||
* 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. | ||
*/ | ||
wait_ms(10); | ||
|
||
ticker_set_handler(lp_ticker_data, cb_done_deepsleep); | ||
ticker_remove_event(lp_ticker_data, &delay_event); | ||
delay_ts = ticker_read(lp_ticker_data) + 1000000; | ||
|
||
/* | ||
* We use here the low power timer instead of microsecond timer for start and | ||
* end because the microseconds timer might be disable during deepsleep. | ||
*/ | ||
lp_timer.reset(); | ||
lp_timer.start(); | ||
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); | ||
/* Make sure deepsleep is allowed, to go to deepsleep */ | ||
bool deep_sleep_allowed = sleep_manager_can_deep_sleep(); | ||
TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed"); | ||
sleep(); | ||
while (!complete); | ||
lp_timer.stop(); | ||
|
||
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, complete_time); | ||
TEST_ASSERT_TRUE(complete); | ||
while (cycles--); | ||
} | ||
|
||
void lp_ticker_1s_sleep() | ||
/* Test that the ticker has the correct frequency and number of bits. */ | ||
void lp_ticker_info_test() | ||
{ | ||
complete = false; | ||
uint32_t delay_ts; | ||
|
||
ticker_set_handler(lp_ticker_data, cb_done); | ||
ticker_remove_event(lp_ticker_data, &delay_event); | ||
delay_ts = ticker_read(lp_ticker_data) + 1000000; | ||
|
||
sleep_manager_lock_deep_sleep(); | ||
timer.reset(); | ||
timer.start(); | ||
bool deep_sleep_allowed = sleep_manager_can_deep_sleep(); | ||
TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed"); | ||
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); | ||
sleep(); | ||
while (!complete); | ||
timer.stop(); | ||
sleep_manager_unlock_deep_sleep(); | ||
|
||
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, complete_time); | ||
TEST_ASSERT_TRUE(complete); | ||
} | ||
#endif /* DEVICE_SLEEP */ | ||
const ticker_info_t* p_ticker_info = lp_ticker_get_info(); | ||
|
||
void lp_ticker_500us(void) | ||
{ | ||
lp_ticker_delay_us(500, SHORT_TIMEOUT); | ||
TEST_ASSERT(p_ticker_info->frequency >= 8000); | ||
TEST_ASSERT(p_ticker_info->frequency <= 64000); | ||
TEST_ASSERT(p_ticker_info->bits >= 12); | ||
} | ||
|
||
void lp_ticker_1ms(void) | ||
/* Test that the ticker continues operating in deep sleep mode. */ | ||
void lp_ticker_deepsleep_test() | ||
{ | ||
lp_ticker_delay_us(1000, SHORT_TIMEOUT); | ||
} | ||
intFlag = 0; | ||
|
||
void lp_ticker_1s(void) | ||
{ | ||
lp_ticker_delay_us(1000000, LONG_TIMEOUT); | ||
} | ||
set_lp_ticker_irq_handler(ticker_event_handler_stub); | ||
|
||
void lp_ticker_5s(void) | ||
{ | ||
lp_ticker_delay_us(5000000, LONG_TIMEOUT); | ||
lp_ticker_init(); | ||
|
||
/* Wait for green tea UART transmission before entering deep-sleep mode. */ | ||
wait_cycles(40000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, |
||
|
||
const uint32_t tick_count = lp_ticker_read(); | ||
|
||
/* Set interrupt. Interrupt should be fired when tick count is equal to: | ||
* tick_count + TICKER_INT_VAL. */ | ||
lp_ticker_set_interrupt(tick_count + TICKER_INT_VAL); | ||
|
||
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep()); | ||
|
||
while (!intFlag) { | ||
sleep(); | ||
} | ||
|
||
TEST_ASSERT_EQUAL(1, intFlag); | ||
} | ||
|
||
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { | ||
greentea_case_failure_abort_handler(source, reason); | ||
return STATUS_CONTINUE; | ||
utest::v1::status_t test_setup(const size_t number_of_cases) | ||
{ | ||
GREENTEA_SETUP(20, "default_auto"); | ||
return verbose_test_setup_handler(number_of_cases); | ||
} | ||
|
||
Case cases[] = { | ||
Case("500us lp_ticker", lp_ticker_500us, greentea_failure_handler), | ||
Case("1ms lp_ticker", lp_ticker_1ms, greentea_failure_handler), | ||
Case("1s lp_ticker", lp_ticker_1s, greentea_failure_handler), | ||
Case("5s lp_ticker", lp_ticker_5s, greentea_failure_handler), | ||
#if DEVICE_SLEEP | ||
Case("1s lp_ticker sleep", lp_ticker_1s_sleep, greentea_failure_handler), | ||
Case("1s lp_ticker deepsleep", lp_ticker_1s_deepsleep, greentea_failure_handler), | ||
#endif /* DEVICE_SLEEP */ | ||
Case("lp ticker info test", lp_ticker_info_test), | ||
Case("lp ticker sleep test", lp_ticker_deepsleep_test), | ||
}; | ||
|
||
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { | ||
GREENTEA_SETUP(20, "default_auto"); | ||
lp_ticker_data->interface->init(); | ||
return greentea_test_setup_handler(number_of_cases); | ||
} | ||
Specification specification(test_setup, cases); | ||
|
||
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); | ||
|
||
int main() { | ||
Harness::run(specification); | ||
int main() | ||
{ | ||
return !Harness::run(specification); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why setting a high limit to the ticker's frequency ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should ask Russ since he is author of the ticker requirements.
@c1728p9 @ithinuel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the note in the header file
low power ticker: 8 KHz (1 tick per 125 us) - 64 KHz (1 tick per ~15.6 us)
My assumption is that with the upper limit, we have a guarantee that at least within this period of time, we have a tick (having tick match).