Skip to content

Commit 9875338

Browse files
author
Seppo Takalo
authored
Merge pull request #10104 from kjbracey-arm/sleep_api
Sleep rework, RTOS API for bare metal, wait deprecations
2 parents 8efd123 + 163fb19 commit 9875338

File tree

99 files changed

+2074
-883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+2074
-883
lines changed

TESTS/mbed_drivers/timeout/timeout_tests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ void test_deepsleep(void)
313313
314314
* This should be replaced with a better function that checks if the
315315
* hardware buffers are empty. However, such an API does not exist now,
316-
* so we'll use the wait_ms() function for now.
316+
* so we'll use the ThisThread::sleep_for() function for now.
317317
*/
318-
wait_ms(20);
318+
ThisThread::sleep_for(20);
319319

320320
timer.start();
321321
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);

TESTS/mbed_hal/rtc/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void rtc_persist_test()
125125
rtc_write(start);
126126
rtc_free();
127127

128-
wait(WAIT_TIME);
128+
ThisThread::sleep_for(WAIT_TIME * 1000);
129129

130130
rtc_init();
131131
const uint32_t stop = rtc_read();
@@ -167,7 +167,7 @@ void rtc_range_test()
167167
for (uint32_t i = 0; i < sizeof(starts) / sizeof(starts[0]); i++) {
168168
const uint32_t start = starts[i];
169169
rtc_write(start);
170-
wait(WAIT_TIME);
170+
ThisThread::sleep_for(WAIT_TIME * 1000);
171171
const uint32_t stop = rtc_read();
172172
TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start);
173173
}

TESTS/mbed_platform/error_handling/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void test_error_logging_multithread()
228228
errThread[i] = new Thread(osPriorityNormal1, THREAD_STACK_SIZE, NULL, NULL);
229229
errThread[i]->start(callback(err_thread_func, &error_status[i]));
230230
}
231-
wait(2.0);
231+
ThisThread::sleep_for(2000);
232232
for (i = 0; i < NUM_TEST_THREADS; i++) {
233233
errThread[i]->join();
234234
}

TESTS/mbedmicro-rtos-mbed/systimer/main.cpp

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#ifndef MBED_TICKLESS
17-
#error [NOT_SUPPORTED] Tickless mode not supported for this target.
18-
#endif
1916

2017
#include "mbed.h"
2118
#include "greentea-client/test_env.h"
@@ -26,9 +23,10 @@
2623
extern "C" {
2724
#include "rtx_lib.h"
2825
}
29-
#include "rtos/TARGET_CORTEX/SysTimer.h"
26+
#include "platform/SysTimer.h"
3027

31-
#define TEST_TICKS 42UL
28+
#define TEST_TICKS 42
29+
#define TEST_TICK_US (TEST_TICKS * 1000)
3230
#define DELAY_DELTA_US 2500ULL
3331

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

4240
using namespace utest::v1;
41+
using mbed::internal::SysTimer;
4342

44-
const us_timestamp_t DELAY_US = 1000000ULL * TEST_TICKS / OS_TICK_FREQ;
43+
const us_timestamp_t DELAY_US = TEST_TICK_US;
4544

46-
// Override the handler() -- the SysTick interrupt must not be set as pending by the test code.
47-
class SysTimerTest: public rtos::internal::SysTimer {
45+
// The SysTick interrupt must not be set as pending by the test code.
46+
template <uint32_t US_IN_TICK>
47+
class SysTimerTest: public SysTimer<US_IN_TICK, false> {
4848
private:
4949
Semaphore _sem;
5050
virtual void handler()
5151
{
52-
core_util_critical_section_enter();
53-
_increment_tick();
54-
core_util_critical_section_exit();
5552
_sem.release();
53+
SysTimer<US_IN_TICK, false>::handler();
5654
}
5755

5856
public:
5957
SysTimerTest() :
60-
SysTimer(), _sem(0, 1)
58+
SysTimer<US_IN_TICK, false>(), _sem(0, 1)
6159
{
6260
}
6361

6462
SysTimerTest(const ticker_data_t *data) :
65-
SysTimer(data), _sem(0, 1)
63+
SysTimer<US_IN_TICK, false>(data), _sem(0, 1)
6664
{
6765
}
6866

@@ -153,7 +151,7 @@ void mock_ticker_reset()
153151
*/
154152
void test_created_with_zero_tick_count(void)
155153
{
156-
SysTimerTest st;
154+
SysTimerTest<1000> st;
157155
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
158156
}
159157

@@ -164,26 +162,27 @@ void test_created_with_zero_tick_count(void)
164162
* Then the tick count is not updated
165163
* When @a suspend and @a resume methods are called again after a delay
166164
* Then the tick count is updated
167-
* and the number of ticks incremented is equal TEST_TICKS - 1
165+
* and the number of ticks incremented is equal TEST_TICKS
168166
* When @a suspend and @a resume methods are called again without a delay
169167
* Then the tick count is not updated
170168
*/
171169
void test_update_tick(void)
172170
{
173171
mock_ticker_reset();
174-
SysTimerTest st(&mock_ticker_data);
175-
st.suspend(TEST_TICKS * 2);
176-
TEST_ASSERT_EQUAL_UINT32(0, st.resume());
172+
SysTimerTest<1000> st(&mock_ticker_data);
173+
st.set_wake_time(st.get_tick() + TEST_TICKS * 2);
174+
st.cancel_wake();
177175
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
178176

179-
st.suspend(TEST_TICKS * 2);
177+
st.set_wake_time(st.get_tick() + TEST_TICKS * 2);
180178
mock_ticker_timestamp = DELAY_US;
181-
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS - 1, st.resume());
182-
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS - 1, st.get_tick());
179+
st.cancel_wake();
180+
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS, st.update_and_get_tick());
181+
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS, st.get_tick());
183182

184-
st.suspend(TEST_TICKS * 2);
185-
TEST_ASSERT_EQUAL_UINT32(0, st.resume());
186-
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS - 1, st.get_tick());
183+
st.set_wake_time(st.get_tick() + TEST_TICKS * 2);
184+
st.cancel_wake();
185+
TEST_ASSERT_EQUAL_UINT32(TEST_TICKS, st.get_tick());
187186
}
188187

189188
/** Test get_time returns correct time
@@ -195,7 +194,7 @@ void test_update_tick(void)
195194
void test_get_time(void)
196195
{
197196
mock_ticker_reset();
198-
SysTimerTest st(&mock_ticker_data);
197+
SysTimerTest<1000> st(&mock_ticker_data);
199198
us_timestamp_t t1 = st.get_time();
200199

201200
mock_ticker_timestamp = DELAY_US;
@@ -212,60 +211,51 @@ void test_get_time(void)
212211
*/
213212
void test_cancel_tick(void)
214213
{
215-
SysTimerTest st;
214+
SysTimerTest<TEST_TICK_US> st;
216215
st.cancel_tick();
217-
st.schedule_tick(TEST_TICKS);
216+
st.start_tick();
218217

219218
st.cancel_tick();
220219
bool acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
221220
TEST_ASSERT_FALSE(acquired);
222221
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
223222
}
224223

225-
/** Test schedule zero
226-
*
227-
* Given a SysTimer
228-
* When a tick is scheduled with delta = 0 ticks
229-
* Then the handler is called instantly
230-
*/
231-
void test_schedule_zero(void)
232-
{
233-
SysTimerTest st;
234-
235-
st.schedule_tick(0UL);
236-
bool acquired = st.sem_try_acquire(0);
237-
TEST_ASSERT_TRUE(acquired);
238-
}
239-
240-
/** Test handler called once
224+
/** Test handler called twice
241225
*
242226
* Given a SysTimer with a tick scheduled with delta = TEST_TICKS
243227
* When the handler is called
244228
* Then the tick count is incremented by 1
245229
* and elapsed time is equal 1000000ULL * TEST_TICKS / OS_TICK_FREQ;
246230
* When more time elapses
247-
* Then the handler is not called again
231+
* Repeat a second time.
248232
*/
249-
void test_handler_called_once(void)
233+
void test_handler_called_twice(void)
250234
{
251-
SysTimerTest st;
252-
st.schedule_tick(TEST_TICKS);
235+
SysTimerTest<TEST_TICK_US> st;
253236
us_timestamp_t t1 = st.get_time();
254237
bool acquired = st.sem_try_acquire(0);
255238
TEST_ASSERT_FALSE(acquired);
256239

240+
st.start_tick();
257241
// Wait in a busy loop to prevent entering sleep or deepsleep modes.
258-
while (!acquired) {
242+
do {
259243
acquired = st.sem_try_acquire(0);
260-
}
244+
} while (!acquired);
261245
us_timestamp_t t2 = st.get_time();
262246
TEST_ASSERT_TRUE(acquired);
263247
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
264248
TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, t2 - t1);
265249

266-
acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
267-
TEST_ASSERT_FALSE(acquired);
268-
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
250+
// Wait in a busy loop to prevent entering sleep or deepsleep modes.
251+
do {
252+
acquired = st.sem_try_acquire(0);
253+
} while (!acquired);
254+
t2 = st.get_time();
255+
TEST_ASSERT_TRUE(acquired);
256+
TEST_ASSERT_EQUAL_UINT32(2, st.get_tick());
257+
TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US * 2, t2 - t1);
258+
st.cancel_tick();
269259
}
270260

271261
#if DEVICE_SLEEP
@@ -281,16 +271,17 @@ void test_handler_called_once(void)
281271
void test_sleep(void)
282272
{
283273
Timer timer;
284-
SysTimerTest st;
274+
SysTimerTest<TEST_TICK_US> st;
285275

286276
sleep_manager_lock_deep_sleep();
287277
timer.start();
288-
st.schedule_tick(TEST_TICKS);
278+
st.start_tick();
289279

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

293283
timer.stop();
284+
st.cancel_tick();
294285
sleep_manager_unlock_deep_sleep();
295286

296287
TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, timer.read_high_resolution_us());
@@ -319,13 +310,14 @@ void test_deepsleep(void)
319310
wait_ms(10);
320311
// Regular Timer might be disabled during deepsleep.
321312
LowPowerTimer lptimer;
322-
SysTimerTest st;
313+
SysTimerTest<TEST_TICK_US> st;
323314

324315
lptimer.start();
325-
st.schedule_tick(TEST_TICKS);
316+
st.start_tick();
326317
TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep_test_check(), "Deep sleep should be allowed");
327318
st.sem_acquire();
328319
lptimer.stop();
320+
st.cancel_tick();
329321

330322
TEST_ASSERT_UINT64_WITHIN(DEEP_SLEEP_DELAY_DELTA_US, DELAY_US, lptimer.read_high_resolution_us());
331323
}
@@ -334,7 +326,7 @@ void test_deepsleep(void)
334326

335327
utest::v1::status_t test_setup(const size_t number_of_cases)
336328
{
337-
GREENTEA_SETUP(5, "default_auto");
329+
GREENTEA_SETUP(15, "default_auto");
338330
return verbose_test_setup_handler(number_of_cases);
339331
}
340332

@@ -343,8 +335,7 @@ Case cases[] = {
343335
Case("Tick count is updated correctly", test_update_tick),
344336
Case("Time is updated correctly", test_get_time),
345337
Case("Tick can be cancelled", test_cancel_tick),
346-
Case("Schedule zero ticks", test_schedule_zero),
347-
Case("Handler called once", test_handler_called_once),
338+
Case("Handler called twice", test_handler_called_twice),
348339
#if DEVICE_SLEEP
349340
Case("Wake up from sleep", test_sleep),
350341
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_TICKLESS_FROM_US_TICKER

TESTS/netsocket/dns/asynchronous_dns_cancel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ void ASYNCHRONOUS_DNS_CANCEL()
8181

8282
delete[] data;
8383

84-
wait(5.0);
84+
ThisThread::sleep_for(5000);
8585
}

TESTS/netsocket/dns/asynchronous_dns_external_event_queue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE()
7979
TEST_ASSERT_EQUAL(0, result_exp_timeout);
8080

8181
// Give event queue time to finalise before destructors
82-
wait(2.0);
82+
ThisThread::sleep_for(2000);
8383

8484
nsapi_dns_call_in_set(0);
8585
}

TESTS/netsocket/dns/asynchronous_dns_timeouts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void ASYNCHRONOUS_DNS_TIMEOUTS()
7474
TEST_ASSERT(result_exp_timeout > 0);
7575

7676
// Give event queue time to finalise before destructors
77-
wait(2.0);
77+
ThisThread::sleep_for(2000);
7878

7979
nsapi_dns_call_in_set(0);
8080
}

TESTS/netsocket/udp/udpsocket_echotest_burst.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void UDPSOCKET_ECHOTEST_BURST()
110110
} else if (recvd < 0) {
111111
pkg_fail += BURST_PKTS - j; // Assume all the following packets of the burst to be lost
112112
printf("[%02d] network error %d\n", i, recvd);
113-
wait(recv_timeout);
113+
ThisThread::sleep_for(recv_timeout * 1000);
114114
recv_timeout *= 2; // Back off,
115115
break;
116116
} else if (temp_addr != udp_addr) {

TESTS/netsocket/udp/udpsocket_sendto_repeat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void UDPSOCKET_SENDTO_REPEAT()
4545
break;
4646
}
4747
oom_earlier = true;
48-
wait(1);
48+
ThisThread::sleep_for(1000);
4949
continue;
5050
}
5151
oom_earlier = false;

TESTS/network/interface/networkinterface_status.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void NETWORKINTERFACE_STATUS_GET()
153153
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
154154

155155
while (net->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
156-
wait(0.5);
156+
ThisThread::sleep_for(500);
157157
}
158158

159159
err = net->disconnect();

TEST_APPS/device/socket_app/cmd_socket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ static void bg_traffic_thread(SInfo *info)
944944
tr_err("Background sent: \"%s\"", sbuffer);
945945
tr_err("Background received: \"%s\"", rbuffer);
946946
}
947-
wait_ms(10);
947+
ThisThread::sleep_for(10);
948948
}
949949
}
950950

UNITTESTS/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ endif(COVERAGE)
8686
# UNIT TESTS
8787
####################
8888

89+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNITTEST")
90+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNITTEST")
91+
8992
# Set include dirs.
9093
set(unittest-includes-base
9194
"${PROJECT_SOURCE_DIR}/target_h"

UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ set(unittest-test-sources
2626
stubs/CellularUtil_stub.cpp
2727
stubs/us_ticker_stub.cpp
2828
stubs/mbed_assert_stub.c
29-
stubs/mbed_wait_api_stub.cpp
29+
stubs/ThisThread_stub.cpp
3030
)

UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ set(unittest-test-sources
2323
stubs/EventQueue_stub.cpp
2424
stubs/FileHandle_stub.cpp
2525
stubs/us_ticker_stub.cpp
26-
stubs/mbed_wait_api_stub.cpp
2726
stubs/mbed_assert_stub.c
2827
stubs/mbed_poll_stub.cpp
2928
stubs/Timer_stub.cpp

UNITTESTS/stubs/UARTSerial_stub.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ int UARTSerial::enable_output(bool enabled)
130130
return 0;
131131
}
132132

133-
void UARTSerial::wait_ms(uint32_t millisec)
134-
{
135-
136-
}
137-
138133
void UARTSerial::set_flow_control(mbed::SerialBase::Flow, PinName, PinName)
139134
{
140135

UNITTESTS/target_h/rtos/Mutex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#define __MUTEX_H__
1919

2020
#include <inttypes.h>
21-
#include "cmsis_os2.h"
21+
#include "mbed_rtos_types.h"
22+
#include "mbed_rtos1_types.h"
2223

2324
namespace rtos {
2425

File renamed without changes.

0 commit comments

Comments
 (0)