Skip to content

Add tests for Sleep HAL API #5509

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions TESTS/mbed_drivers/ticker/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "utest/utest.h"
#include "unity/unity.h"

#if !DEVICE_USTICKER
#error [NOT_SUPPORTED] test not supported
#endif

using utest::v1::Case;

Expand Down
4 changes: 4 additions & 0 deletions TESTS/mbed_drivers/timer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include "rtos.h"
#include "hal/us_ticker_api.h"

#if !DEVICE_USTICKER
#error [NOT_SUPPORTED] test not supported
#endif

using namespace utest::v1;

extern uint32_t SystemCoreClock;
Expand Down
203 changes: 203 additions & 0 deletions TESTS/mbed_hal/sleep/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/* mbed Microcontroller Library
* 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.
* 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.
*/

#if !DEVICE_SLEEP
#error [NOT_SUPPORTED] sleep not supported for this target
#endif

#include "mbed.h"

#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"

#include "sleep_api_tests.h"

#define US_PER_S 1000000

unsigned int ticks_to_us(unsigned int ticks, unsigned int freq)
{
return (unsigned int)((unsigned long long)ticks * US_PER_S / freq);
}

unsigned int us_to_ticks(unsigned int us, unsigned int freq)
{
return (unsigned int)((unsigned long long)us * freq / US_PER_S);
}

using namespace utest::v1;

/* The following ticker frequencies are possible:
* high frequency ticker: 250 KHz (1 tick per 4 us) - 8 Mhz (1 tick per 1/8 us)
* low power ticker: 8 KHz (1 tick per 125 us) - 64 KHz (1 tick per ~15.6 us)
*/

/* Used for regular sleep mode, a target should be awake within 10 us. Define us delta value as follows:
* delta = default 10 us + worst ticker resolution + extra time for code execution */
static const uint32_t sleep_mode_delta_us = (10 + 4 + 5);

/* Used for deep-sleep mode, a target should be awake within 10 ms. Define us delta value as follows:
* delta = default 10 ms + worst ticker resolution + extra time for code execution */
static const uint32_t deepsleep_mode_delta_us = (10000 + 125 + 5);


void us_ticker_isr(const ticker_data_t *const ticker_data)
{
us_ticker_clear_interrupt();
}

#ifdef DEVICE_LOWPOWERTIMER
void lp_ticker_isr(const ticker_data_t *const ticker_data)
{
lp_ticker_clear_interrupt();
}
#endif

/* Test that wake-up time from sleep should be less than 10 us and
* high frequency ticker interrupt can wake-up target from sleep. */
void sleep_usticker_test()
{
Timeout timeout;
const ticker_data_t * ticker = get_us_ticker_data();
const unsigned int ticker_freq = ticker->interface->get_info()->frequency;

const ticker_irq_handler_type us_ticker_irq_handler_org = set_us_ticker_irq_handler(us_ticker_isr);

/* Test only sleep functionality. */
sleep_manager_lock_deep_sleep();
TEST_ASSERT_FALSE_MESSAGE(sleep_manager_can_deep_sleep(), "deep sleep should be locked");

/* Testing wake-up time 10 us. */
for (timestamp_t i = 100; i < 1000; i += 100) {
/* note: us_ticker_read() operates on ticks. */

const timestamp_t next_match_timestamp = us_ticker_read() + us_to_ticks(i, ticker_freq);
us_ticker_set_interrupt(next_match_timestamp);

sleep();

TEST_ASSERT_UINT32_WITHIN(us_to_ticks(sleep_mode_delta_us, ticker_freq), next_match_timestamp,
us_ticker_read());
}

set_us_ticker_irq_handler(us_ticker_irq_handler_org);

sleep_manager_unlock_deep_sleep();
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep());
}

#ifdef DEVICE_LOWPOWERTIMER

/* Test that wake-up time from sleep should be less than 10 ms and
* low power ticker interrupt can wake-up target from sleep. */
void deepsleep_lpticker_test()
{
const ticker_data_t * ticker = get_us_ticker_data();
const unsigned int ticker_freq = ticker->interface->get_info()->frequency;

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(10);

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

/* Testing wake-up time 10 ms. */
for (timestamp_t i = 20000; i < 200000; i += 20000) {
/* note: lp_ticker_read() operates on ticks. */
const timestamp_t next_match_timestamp = lp_ticker_read() + us_to_ticks(i, ticker_freq);
lp_ticker_set_interrupt(next_match_timestamp);

sleep();

TEST_ASSERT_UINT32_WITHIN(us_to_ticks(deepsleep_mode_delta_us, ticker_freq), next_match_timestamp,
lp_ticker_read());
}

set_lp_ticker_irq_handler(lp_ticker_irq_handler_org);

}

void deepsleep_high_speed_clocks_turned_off_test()
{
const ticker_data_t * us_ticker = get_us_ticker_data();
const ticker_data_t * lp_ticker = get_lp_ticker_data();
const unsigned int us_ticker_freq = us_ticker->interface->get_info()->frequency;
const unsigned int lp_ticker_freq = lp_ticker->interface->get_info()->frequency;

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

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

const unsigned int us_ticks_before_sleep = us_ticker_read();

const timestamp_t wakeup_time = lp_ticker_read() + us_to_ticks(200000, lp_ticker_freq);
lp_ticker_set_interrupt(wakeup_time);

sleep();

const unsigned int us_ticks_after_sleep = us_ticker_read();
const unsigned int lp_ticks_after_sleep = lp_ticker_read();

/* High freqency ticker should be disabled in deep-sleep mode. We expect that time difference between
* ticker reads before and after the sleep represents only code execution time between calls.
* Since we went to sleep for about 200 ms check if time counted by high frequency timer does not
* exceed 1 ms.
*/
TEST_ASSERT_UINT32_WITHIN(1000, 0, ticks_to_us(us_ticks_after_sleep - us_ticks_before_sleep, us_ticker_freq));

/* Check if we have woken-up after expected time. */
TEST_ASSERT_UINT32_WITHIN(us_to_ticks(deepsleep_mode_delta_us, lp_ticker_freq), wakeup_time, lp_ticks_after_sleep);
}

#endif

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 greentea_test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(60, "default_auto");
us_ticker_init();
#if DEVICE_LOWPOWERTIMER
lp_ticker_init();
#endif
/* Suspend RTOS Kernel to enable sleep modes. */
osKernelSuspend();
return greentea_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("sleep - source of wake-up - us ticker", sleep_usticker_test, greentea_failure_handler),
#if DEVICE_LOWPOWERTIMER
Case("deep-sleep - source of wake-up - lp ticker",deepsleep_lpticker_test, greentea_failure_handler),
Case("deep-sleep - high-speed clocks are turned off",deepsleep_high_speed_clocks_turned_off_test, greentea_failure_handler),
#endif
};

Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);

int main() {
Harness::run(specification);
}
69 changes: 69 additions & 0 deletions TESTS/mbed_hal/sleep/sleep_api_tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* mbed Microcontroller Library
* 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.
* 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_sleep_tests */
/** @{*/

#ifndef MBED_SLEEP_API_TESTS_H
#define MBED_SLEEP_API_TESTS_H

#include "device.h"

#if DEVICE_SLEEP

#ifdef __cplusplus
extern "C" {
#endif

/** High frequency ticker interrupt can wake up from sleep (locked deep-sleep).
*
* Given is an environment with high frequency ticker.
* When the board enters sleep mode.
* Then the board can be wake up from the sleep by high frequency ticker interrupt and
* wake-up time should be less than 10 us.
*/
void sleep_usticker_test();

/** Low power ticker interrupt to wake up from deep-sleep (unlocked deep-sleep).
*
* Given is an environment with low power ticker.
* When the board enters deep-sleep mode.
* Then the board can be wake up from the sleep by low power ticker interrupt and
* wake-up time should be less than 10 ms.
*
*/
void deepsleep_lpticker_test();

/** High speed clocks are turned off in deep-sleep (unlocked deep-sleep)
*
* Given is an environment with high frequency ticker.
* When the board enters deep-sleep mode.
* Then high frequency ticker does not count while the board is in the deep-sleep mode.
*
*/
void deepsleep_high_speed_clocks_turned_off_test();

/**@}*/

#ifdef __cplusplus
}
#endif

#endif

#endif

/**@}*/
40 changes: 39 additions & 1 deletion hal/sleep_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@
extern "C" {
#endif

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is that? That should be on the branch already?

Copy link
Contributor Author

@mprse mprse Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added requirements section since it wasn't available on this branch. Requirements have been created based on function descriptions which are precise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That explains it, thanks @mprse

* \defgroup hal_sleep sleep hal requirements
* Low level interface to the sleep mode of a target.
*
* # Defined behaviour
*
* * Sleep mode
* * wake-up time should be less than 10 us - Verified by sleep_usticker_test().
* * the processor can be woken up by any internal peripheral interrupt - Verified by sleep_usticker_test().
* * all peripherals operate as in run mode - not verified.
* * the processor can be woken up by external pin interrupt - not verified.
* * Deep sleep
* * the wake-up time should be less than 10 ms - Verified by deepsleep_lpticker_test().
* * lp ticker should wake up a target from this mode - Verified by deepsleep_lpticker_test().
* * RTC should wake up a target from this mode - not verified.
* * an external interrupt on a pin should wake up a target from this mode - not verified.
* * a watchdog timer should wake up a target from this mode - not verified.
* * High-speed clocks are turned off - Verified by deepsleep_high_speed_clocks_turned_off_test().
* * RTC keeps time - Verified by rtc_sleep_test().
*
* # Undefined behaviour
*
* * peripherals aside from RTC, GPIO and lp ticker result in undefined behaviour in deep sleep.
* @{
*/

/**
* \defgroup hal_sleep_tests sleep hal tests
* The sleep HAL tests ensure driver conformance to defined behaviour.
*
* To run the sleep hal tests use the command:
*
* mbed test -t <toolchain> -m <target> -n tests-mbed_hal-sleep*
*
*/

/** Send the microcontroller to sleep
*
* The processor is setup ready for sleep, and sent to sleep. In this mode, the
Expand All @@ -53,6 +89,8 @@ void hal_sleep(void);
*/
void hal_deepsleep(void);

/**@}*/

#ifdef __cplusplus
}
#endif
Expand All @@ -61,4 +99,4 @@ void hal_deepsleep(void);

#endif

/** @}*/
/**@}*/
Loading