Skip to content
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

Sleep rework, RTOS API for bare metal, wait deprecations #10104

Merged
merged 17 commits into from Jul 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions TESTS/mbed_drivers/timeout/timeout_tests.h
Expand Up @@ -313,9 +313,9 @@ void test_deepsleep(void)

* 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 ThisThread::sleep_for() function for now.
*/
wait_ms(20);
ThisThread::sleep_for(20);

timer.start();
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
Expand Down
4 changes: 2 additions & 2 deletions TESTS/mbed_hal/rtc/main.cpp
Expand Up @@ -125,7 +125,7 @@ void rtc_persist_test()
rtc_write(start);
rtc_free();

wait(WAIT_TIME);
ThisThread::sleep_for(WAIT_TIME * 1000);

rtc_init();
const uint32_t stop = rtc_read();
Expand Down Expand Up @@ -167,7 +167,7 @@ void rtc_range_test()
for (uint32_t i = 0; i < sizeof(starts) / sizeof(starts[0]); i++) {
const uint32_t start = starts[i];
rtc_write(start);
wait(WAIT_TIME);
ThisThread::sleep_for(WAIT_TIME * 1000);
const uint32_t stop = rtc_read();
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
}
Expand Down
2 changes: 1 addition & 1 deletion TESTS/mbed_platform/error_handling/main.cpp
Expand Up @@ -228,7 +228,7 @@ void test_error_logging_multithread()
errThread[i] = new Thread(osPriorityNormal1, THREAD_STACK_SIZE, NULL, NULL);
errThread[i]->start(callback(err_thread_func, &error_status[i]));
}
wait(2.0);
ThisThread::sleep_for(2000);
for (i = 0; i < NUM_TEST_THREADS; i++) {
errThread[i]->join();
}
Expand Down
109 changes: 50 additions & 59 deletions TESTS/mbedmicro-rtos-mbed/systimer/main.cpp
Expand Up @@ -13,9 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_TICKLESS
#error [NOT_SUPPORTED] Tickless mode not supported for this target.
#endif

#include "mbed.h"
#include "greentea-client/test_env.h"
Expand All @@ -26,9 +23,10 @@
extern "C" {
#include "rtx_lib.h"
}
#include "rtos/TARGET_CORTEX/SysTimer.h"
#include "platform/SysTimer.h"

#define TEST_TICKS 42UL
#define TEST_TICKS 42
#define TEST_TICK_US (TEST_TICKS * 1000)
#define DELAY_DELTA_US 2500ULL

/* Use a specific delta value for deep sleep, as entry/exit adds up extra latency.
Expand All @@ -40,29 +38,29 @@ extern "C" {
#endif

using namespace utest::v1;
using mbed::internal::SysTimer;

const us_timestamp_t DELAY_US = 1000000ULL * TEST_TICKS / OS_TICK_FREQ;
const us_timestamp_t DELAY_US = TEST_TICK_US;

// Override the handler() -- the SysTick interrupt must not be set as pending by the test code.
class SysTimerTest: public rtos::internal::SysTimer {
// The SysTick interrupt must not be set as pending by the test code.
template <uint32_t US_IN_TICK>
class SysTimerTest: public SysTimer<US_IN_TICK, false> {
private:
Semaphore _sem;
virtual void handler()
{
core_util_critical_section_enter();
_increment_tick();
core_util_critical_section_exit();
_sem.release();
SysTimer<US_IN_TICK, false>::handler();
}

public:
SysTimerTest() :
SysTimer(), _sem(0, 1)
SysTimer<US_IN_TICK, false>(), _sem(0, 1)
{
}

SysTimerTest(const ticker_data_t *data) :
SysTimer(data), _sem(0, 1)
SysTimer<US_IN_TICK, false>(data), _sem(0, 1)
{
}

Expand Down Expand Up @@ -153,7 +151,7 @@ void mock_ticker_reset()
*/
void test_created_with_zero_tick_count(void)
{
SysTimerTest st;
SysTimerTest<1000> st;
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
}

Expand All @@ -164,26 +162,27 @@ void test_created_with_zero_tick_count(void)
* Then the tick count is not updated
* When @a suspend and @a resume methods are called again after a delay
* Then the tick count is updated
* and the number of ticks incremented is equal TEST_TICKS - 1
* and the number of ticks incremented is equal TEST_TICKS
* When @a suspend and @a resume methods are called again without a delay
* Then the tick count is not updated
*/
void test_update_tick(void)
{
mock_ticker_reset();
SysTimerTest st(&mock_ticker_data);
st.suspend(TEST_TICKS * 2);
TEST_ASSERT_EQUAL_UINT32(0, st.resume());
SysTimerTest<1000> st(&mock_ticker_data);
st.set_wake_time(st.get_tick() + TEST_TICKS * 2);
st.cancel_wake();
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());

st.suspend(TEST_TICKS * 2);
st.set_wake_time(st.get_tick() + TEST_TICKS * 2);
mock_ticker_timestamp = DELAY_US;
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS - 1, st.resume());
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS - 1, st.get_tick());
st.cancel_wake();
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS, st.update_and_get_tick());
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS, st.get_tick());

st.suspend(TEST_TICKS * 2);
TEST_ASSERT_EQUAL_UINT32(0, st.resume());
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS - 1, st.get_tick());
st.set_wake_time(st.get_tick() + TEST_TICKS * 2);
st.cancel_wake();
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS, st.get_tick());
}

/** Test get_time returns correct time
Expand All @@ -195,7 +194,7 @@ void test_update_tick(void)
void test_get_time(void)
{
mock_ticker_reset();
SysTimerTest st(&mock_ticker_data);
SysTimerTest<1000> st(&mock_ticker_data);
us_timestamp_t t1 = st.get_time();

mock_ticker_timestamp = DELAY_US;
Expand All @@ -212,60 +211,51 @@ void test_get_time(void)
*/
void test_cancel_tick(void)
{
SysTimerTest st;
SysTimerTest<TEST_TICK_US> st;
st.cancel_tick();
st.schedule_tick(TEST_TICKS);
st.start_tick();

st.cancel_tick();
bool acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_FALSE(acquired);
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
}

/** Test schedule zero
*
* Given a SysTimer
* When a tick is scheduled with delta = 0 ticks
* Then the handler is called instantly
*/
void test_schedule_zero(void)
{
SysTimerTest st;

st.schedule_tick(0UL);
bool acquired = st.sem_try_acquire(0);
TEST_ASSERT_TRUE(acquired);
}

/** Test handler called once
/** Test handler called twice
*
* Given a SysTimer with a tick scheduled with delta = TEST_TICKS
* When the handler is called
* Then the tick count is incremented by 1
* and elapsed time is equal 1000000ULL * TEST_TICKS / OS_TICK_FREQ;
* When more time elapses
* Then the handler is not called again
* Repeat a second time.
*/
void test_handler_called_once(void)
void test_handler_called_twice(void)
{
SysTimerTest st;
st.schedule_tick(TEST_TICKS);
SysTimerTest<TEST_TICK_US> st;
us_timestamp_t t1 = st.get_time();
bool acquired = st.sem_try_acquire(0);
TEST_ASSERT_FALSE(acquired);

st.start_tick();
// Wait in a busy loop to prevent entering sleep or deepsleep modes.
while (!acquired) {
do {
acquired = st.sem_try_acquire(0);
}
} while (!acquired);
us_timestamp_t t2 = st.get_time();
TEST_ASSERT_TRUE(acquired);
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, t2 - t1);

acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_FALSE(acquired);
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
// Wait in a busy loop to prevent entering sleep or deepsleep modes.
do {
acquired = st.sem_try_acquire(0);
} while (!acquired);
t2 = st.get_time();
TEST_ASSERT_TRUE(acquired);
TEST_ASSERT_EQUAL_UINT32(2, st.get_tick());
TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US * 2, t2 - t1);
st.cancel_tick();
}

#if DEVICE_SLEEP
Expand All @@ -281,16 +271,17 @@ void test_handler_called_once(void)
void test_sleep(void)
{
Timer timer;
SysTimerTest st;
SysTimerTest<TEST_TICK_US> st;

sleep_manager_lock_deep_sleep();
timer.start();
st.schedule_tick(TEST_TICKS);
st.start_tick();

TEST_ASSERT_FALSE_MESSAGE(sleep_manager_can_deep_sleep(), "Deep sleep should be disallowed");
st.sem_acquire();

timer.stop();
st.cancel_tick();
sleep_manager_unlock_deep_sleep();

TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, timer.read_high_resolution_us());
Expand Down Expand Up @@ -319,13 +310,14 @@ void test_deepsleep(void)
wait_ms(10);
// Regular Timer might be disabled during deepsleep.
LowPowerTimer lptimer;
SysTimerTest st;
SysTimerTest<TEST_TICK_US> st;

lptimer.start();
st.schedule_tick(TEST_TICKS);
st.start_tick();
TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep_test_check(), "Deep sleep should be allowed");
st.sem_acquire();
lptimer.stop();
st.cancel_tick();

TEST_ASSERT_UINT64_WITHIN(DEEP_SLEEP_DELAY_DELTA_US, DELAY_US, lptimer.read_high_resolution_us());
}
Expand All @@ -334,7 +326,7 @@ void test_deepsleep(void)

utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(5, "default_auto");
GREENTEA_SETUP(15, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Expand All @@ -343,8 +335,7 @@ Case cases[] = {
Case("Tick count is updated correctly", test_update_tick),
Case("Time is updated correctly", test_get_time),
Case("Tick can be cancelled", test_cancel_tick),
Case("Schedule zero ticks", test_schedule_zero),
Case("Handler called once", test_handler_called_once),
Case("Handler called twice", test_handler_called_twice),
#if DEVICE_SLEEP
Case("Wake up from sleep", test_sleep),
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_TICKLESS_FROM_US_TICKER
Expand Down
2 changes: 1 addition & 1 deletion TESTS/netsocket/dns/asynchronous_dns_cancel.cpp
Expand Up @@ -81,5 +81,5 @@ void ASYNCHRONOUS_DNS_CANCEL()

delete[] data;

wait(5.0);
ThisThread::sleep_for(5000);
}
Expand Up @@ -79,7 +79,7 @@ void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE()
TEST_ASSERT_EQUAL(0, result_exp_timeout);

// Give event queue time to finalise before destructors
wait(2.0);
ThisThread::sleep_for(2000);

nsapi_dns_call_in_set(0);
}
2 changes: 1 addition & 1 deletion TESTS/netsocket/dns/asynchronous_dns_timeouts.cpp
Expand Up @@ -74,7 +74,7 @@ void ASYNCHRONOUS_DNS_TIMEOUTS()
TEST_ASSERT(result_exp_timeout > 0);

// Give event queue time to finalise before destructors
wait(2.0);
ThisThread::sleep_for(2000);

nsapi_dns_call_in_set(0);
}
2 changes: 1 addition & 1 deletion TESTS/netsocket/udp/udpsocket_echotest_burst.cpp
Expand Up @@ -110,7 +110,7 @@ void UDPSOCKET_ECHOTEST_BURST()
} else if (recvd < 0) {
pkg_fail += BURST_PKTS - j; // Assume all the following packets of the burst to be lost
printf("[%02d] network error %d\n", i, recvd);
wait(recv_timeout);
ThisThread::sleep_for(recv_timeout * 1000);
recv_timeout *= 2; // Back off,
break;
} else if (temp_addr != udp_addr) {
Expand Down
2 changes: 1 addition & 1 deletion TESTS/netsocket/udp/udpsocket_sendto_repeat.cpp
Expand Up @@ -45,7 +45,7 @@ void UDPSOCKET_SENDTO_REPEAT()
break;
}
oom_earlier = true;
wait(1);
ThisThread::sleep_for(1000);
continue;
}
oom_earlier = false;
Expand Down
2 changes: 1 addition & 1 deletion TESTS/network/interface/networkinterface_status.cpp
Expand Up @@ -153,7 +153,7 @@ void NETWORKINTERFACE_STATUS_GET()
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);

while (net->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
wait(0.5);
ThisThread::sleep_for(500);
}

err = net->disconnect();
Expand Down
2 changes: 1 addition & 1 deletion TEST_APPS/device/socket_app/cmd_socket.cpp
Expand Up @@ -944,7 +944,7 @@ static void bg_traffic_thread(SInfo *info)
tr_err("Background sent: \"%s\"", sbuffer);
tr_err("Background received: \"%s\"", rbuffer);
}
wait_ms(10);
ThisThread::sleep_for(10);
}
}

Expand Down
3 changes: 3 additions & 0 deletions UNITTESTS/CMakeLists.txt
Expand Up @@ -86,6 +86,9 @@ endif(COVERAGE)
# UNIT TESTS
####################

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNITTEST")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNITTEST")

# Set include dirs.
set(unittest-includes-base
"${PROJECT_SOURCE_DIR}/target_h"
Expand Down
Expand Up @@ -26,5 +26,5 @@ set(unittest-test-sources
stubs/CellularUtil_stub.cpp
stubs/us_ticker_stub.cpp
stubs/mbed_assert_stub.c
stubs/mbed_wait_api_stub.cpp
stubs/ThisThread_stub.cpp
)
Expand Up @@ -23,7 +23,6 @@ set(unittest-test-sources
stubs/EventQueue_stub.cpp
stubs/FileHandle_stub.cpp
stubs/us_ticker_stub.cpp
stubs/mbed_wait_api_stub.cpp
stubs/mbed_assert_stub.c
stubs/mbed_poll_stub.cpp
stubs/Timer_stub.cpp
Expand Down
5 changes: 0 additions & 5 deletions UNITTESTS/stubs/UARTSerial_stub.cpp
Expand Up @@ -130,11 +130,6 @@ int UARTSerial::enable_output(bool enabled)
return 0;
}

void UARTSerial::wait_ms(uint32_t millisec)
{

}

void UARTSerial::set_flow_control(mbed::SerialBase::Flow, PinName, PinName)
{

Expand Down
3 changes: 2 additions & 1 deletion UNITTESTS/target_h/rtos/Mutex.h
Expand Up @@ -18,7 +18,8 @@
#define __MUTEX_H__

#include <inttypes.h>
#include "cmsis_os2.h"
#include "mbed_rtos_types.h"
#include "mbed_rtos1_types.h"

namespace rtos {

Expand Down
File renamed without changes.