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.
pull/8189/head
Deepika 2018-10-15 14:07:42 -05:00 committed by deepikabhavnani
parent a9f43239be
commit 957f240450
2 changed files with 42 additions and 18 deletions

View File

@ -55,9 +55,9 @@ extern "C" {
* @param s number of seconds to wait
*
* @note
* If the RTOS is present, this function always spins to get the exact number of microseconds,
* which potentially affects power (such as preventing deep sleep) and multithread performance.
* You can avoid it by using ThisThread::sleep_for().
* If the RTOS is present, this function spins to get the exact number of microseconds for
* usec precision upto 10msec. If delay is larger then 10msec and not in ISR, it is same as
* `wait_ms`. `wait_us` and `wait_ms` are recommended over `wait`
*/
void wait(float s);
@ -66,9 +66,8 @@ void wait(float s);
* @param ms the whole number of milliseconds to wait
*
* @note
* If the RTOS is present, this function always spins to get the exact number of microseconds,
* which potentially affects power (such as preventing deep sleep) and multithread performance.
* You can avoid it by using ThisThread::sleep_for().
* If the RTOS is present, it is same as CMSIS osDelay()
* Not callable from interrupts, doesn't lock hardware sleep.
*/
void wait_ms(int ms);
@ -77,8 +76,9 @@ void wait_ms(int ms);
* @param us the whole number of microseconds to wait
*
* @note
* If the RTOS is present, this function always spins to get the exact number of microseconds,
* which potentially affects power (such as preventing deep sleep) and multithread performance.
* This function always spins to get the exact number of microseconds.
* If RTOS is present, this will affect power (prevents deep sleep) and
* multithread performance, hence spinning for milli-sec wait is not recommended.
*/
void wait_us(int us);

View File

@ -23,21 +23,18 @@
#include "rtos/rtos.h"
#include "platform/mbed_critical.h"
#include "platform/mbed_power_mgmt.h"
#include "platform/mbed_error.h"
#include "rtos/ThisThread.h"
void wait(float s)
{
wait_us(s * 1000000.0f);
}
if ((s >= 0.01f) && core_util_are_interrupts_enabled()) {
wait_ms(s * 1000.0f);
return;
}
void wait_ms(int ms)
{
wait_us(ms * 1000);
}
void wait_us(int us)
{
uint32_t us = (s * 1000000.0f);
const ticker_data_t *const ticker = get_us_ticker_data();
uint32_t start = ticker_read(ticker);
if ((us >= 1000) && core_util_are_interrupts_enabled()) {
// Use the RTOS to wait for millisecond delays if possible
@ -50,5 +47,32 @@ void wait_us(int us)
while ((ticker_read(ticker) - start) < (uint32_t)us);
}
/* The actual time delay may be up to one timer tick less - 1 msec */
void wait_ms(int ms)
{
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_INVALID_OPERATION),
"Deprecated behavior: milli-sec delay should not be used in interrupt.\n");
#else
wait_us(ms * 1000);
#endif
} else {
rtos::ThisThread::sleep_for(ms);
}
}
/* The actual time delay may be 1 less usec */
void wait_us(int us)
{
if (us > 10000) {
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_UNKNOWN),
"wait_us blocks deep sleep, wait_ms recommended for long delays\n");
}
const ticker_data_t *const ticker = get_us_ticker_data();
uint32_t start = ticker_read(ticker);
while ((ticker_read(ticker) - start) < (uint32_t)us);
}
#endif // #if MBED_CONF_RTOS_PRESENT