Merge pull request #8189 from deepikabhavnani/wait_updated

Wait API updated to remove deepsleep lock
pull/8836/head
Martin Kojtal 2018-11-22 11:01:27 +01:00 committed by GitHub
commit 70dfbbfee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 18 deletions

View File

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

View File

@ -23,21 +23,18 @@
#include "rtos/ThisThread.h" #include "rtos/ThisThread.h"
#include "platform/mbed_critical.h" #include "platform/mbed_critical.h"
#include "platform/mbed_power_mgmt.h" #include "platform/mbed_power_mgmt.h"
#include "platform/mbed_error.h"
#include "rtos/ThisThread.h"
void wait(float s) 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) uint32_t us = (s * 1000000.0f);
{
wait_us(ms * 1000);
}
void wait_us(int us)
{
const ticker_data_t *const ticker = get_us_ticker_data(); const ticker_data_t *const ticker = get_us_ticker_data();
uint32_t start = ticker_read(ticker); uint32_t start = ticker_read(ticker);
if ((us >= 1000) && core_util_are_interrupts_enabled()) { if ((us >= 1000) && core_util_are_interrupts_enabled()) {
// Use the RTOS to wait for millisecond delays if possible // 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); 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 #endif // #if MBED_CONF_RTOS_PRESENT