Skip to content

Commit 957f240

Browse files
author
Deepika
committed
Remove deep sleep lock from wait_ms
API's updated as: 1. wait(float) calls wait_ms for >=0.01s and not in interrupt, else wait_us. 2. wait_ms() is just the thread sleep and doesn't lock hardware sleep. In order to have backward compatibility, if used in ISR `wait_us` is called if MBED_TRAP_ERRORS_ENABLED is false 3. wait_us() is a ticker-based wait, always spinning.
1 parent a9f4323 commit 957f240

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

platform/mbed_wait_api.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ extern "C" {
5555
* @param s number of seconds to wait
5656
*
5757
* @note
58-
* If the RTOS is present, this function always spins to get the exact number of microseconds,
59-
* which potentially affects power (such as preventing deep sleep) and multithread performance.
60-
* You can avoid it by using ThisThread::sleep_for().
58+
* If the RTOS is present, this function spins to get the exact number of microseconds for
59+
* usec precision upto 10msec. If delay is larger then 10msec and not in ISR, it is same as
60+
* `wait_ms`. `wait_us` and `wait_ms` are recommended over `wait`
6161
*/
6262
void wait(float s);
6363

@@ -66,9 +66,8 @@ void wait(float s);
6666
* @param ms the whole number of milliseconds to wait
6767
*
6868
* @note
69-
* If the RTOS is present, this function always spins to get the exact number of microseconds,
70-
* which potentially affects power (such as preventing deep sleep) and multithread performance.
71-
* You can avoid it by using ThisThread::sleep_for().
69+
* If the RTOS is present, it is same as CMSIS osDelay()
70+
* Not callable from interrupts, doesn't lock hardware sleep.
7271
*/
7372
void wait_ms(int ms);
7473

@@ -77,8 +76,9 @@ void wait_ms(int ms);
7776
* @param us the whole number of microseconds to wait
7877
*
7978
* @note
80-
* If the RTOS is present, this function always spins to get the exact number of microseconds,
81-
* which potentially affects power (such as preventing deep sleep) and multithread performance.
79+
* This function always spins to get the exact number of microseconds.
80+
* If RTOS is present, this will affect power (prevents deep sleep) and
81+
* multithread performance, hence spinning for milli-sec wait is not recommended.
8282
*/
8383
void wait_us(int us);
8484

platform/mbed_wait_api_rtos.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,18 @@
2323
#include "rtos/rtos.h"
2424
#include "platform/mbed_critical.h"
2525
#include "platform/mbed_power_mgmt.h"
26+
#include "platform/mbed_error.h"
27+
#include "rtos/ThisThread.h"
2628

2729
void wait(float s)
2830
{
29-
wait_us(s * 1000000.0f);
30-
}
31-
32-
void wait_ms(int ms)
33-
{
34-
wait_us(ms * 1000);
35-
}
31+
if ((s >= 0.01f) && core_util_are_interrupts_enabled()) {
32+
wait_ms(s * 1000.0f);
33+
return;
34+
}
3635

37-
void wait_us(int us)
38-
{
36+
uint32_t us = (s * 1000000.0f);
3937
const ticker_data_t *const ticker = get_us_ticker_data();
40-
4138
uint32_t start = ticker_read(ticker);
4239
if ((us >= 1000) && core_util_are_interrupts_enabled()) {
4340
// Use the RTOS to wait for millisecond delays if possible
@@ -50,5 +47,32 @@ void wait_us(int us)
5047
while ((ticker_read(ticker) - start) < (uint32_t)us);
5148
}
5249

50+
/* The actual time delay may be up to one timer tick less - 1 msec */
51+
void wait_ms(int ms)
52+
{
53+
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
54+
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
55+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_INVALID_OPERATION),
56+
"Deprecated behavior: milli-sec delay should not be used in interrupt.\n");
57+
#else
58+
wait_us(ms * 1000);
59+
#endif
60+
} else {
61+
rtos::ThisThread::sleep_for(ms);
62+
}
63+
}
64+
65+
/* The actual time delay may be 1 less usec */
66+
void wait_us(int us)
67+
{
68+
if (us > 10000) {
69+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_UNKNOWN),
70+
"wait_us blocks deep sleep, wait_ms recommended for long delays\n");
71+
}
72+
const ticker_data_t *const ticker = get_us_ticker_data();
73+
uint32_t start = ticker_read(ticker);
74+
while ((ticker_read(ticker) - start) < (uint32_t)us);
75+
}
76+
5377
#endif // #if MBED_CONF_RTOS_PRESENT
5478

0 commit comments

Comments
 (0)