Skip to content

Commit 70dfbbf

Browse files
authored
Merge pull request #8189 from deepikabhavnani/wait_updated
Wait API updated to remove deepsleep lock
2 parents f1f6426 + c58c80e commit 70dfbbf

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+
* microsecond precision up to 10 milliseconds. If delay is larger than 10 milliseconds and not in ISR, it is the same as
60+
* `wait_ms`. We recommend `wait_us` and `wait_ms` 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 calls ThisThread::sleep_for(), which is same as CMSIS osDelay().
70+
* You can't call this from interrupts, and it 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 (by preventing deep sleep) and
81+
* multithread performance. Therefore, spinning for millisecond 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/ThisThread.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)