Skip to content

Add overridable wait hook #10693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion platform/mbed_power_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
#define MBED_POWER_MGMT_H

#include "hal/sleep_api.h"
#include "mbed_toolchain.h"
#include "hal/ticker_api.h"
#include "platform/mbed_toolchain.h"
#include "platform/mbed_critical.h"
#include <stdbool.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -170,6 +171,33 @@ bool sleep_manager_can_deep_sleep_test_check(void);
*/
void sleep_manager_sleep_auto(void);

/** Applications may implement this to hook any calls to sleep or deepsleep
*
* This hook gives the application a chance to run additional code when the
* system would normally waste cycles waiting for an external event.
*
* @note This function is explicitly _not_ called when we are in an interrupt
* or critical section, as this would violate the expectations of both critical
* sections and sleep hooks. In normal usage interrupts should not be calling
* sleep.
*
* Example blinks an LED when the application sleeps:
* @code
* DigitalOut led1(LED1);
*
* void mbed_override_sleep_hook(void) {
* led1 = !led1;
* }
*
* int main() {
* while (true) {
* ThisThread::sleep_for(1000);
* }
* }
* @endcode
*/
void mbed_override_sleep_hook(void);

/** Send the microcontroller to sleep
*
* @note This function can be a noop if not implemented by the platform.
Expand All @@ -193,6 +221,9 @@ void sleep_manager_sleep_auto(void);
*/
static inline void sleep(void)
{
if (core_util_are_interrupts_enabled()) {
mbed_override_sleep_hook();
}
#if DEVICE_SLEEP
#if (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_SYSTICK_CLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS)
sleep_manager_sleep_auto();
Expand Down Expand Up @@ -223,6 +254,9 @@ static inline void sleep(void)
MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()")
static inline void deepsleep(void)
{
if (core_util_are_interrupts_enabled()) {
mbed_override_sleep_hook();
}
#if DEVICE_SLEEP
sleep_manager_sleep_auto();
#endif /* DEVICE_SLEEP */
Expand Down
5 changes: 5 additions & 0 deletions platform/mbed_sleep_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,8 @@ bool sleep_manager_can_deep_sleep(void)
}

#endif

// default hook for sleep calls
MBED_WEAK void mbed_override_sleep_hook(void)
{
}
15 changes: 14 additions & 1 deletion platform/mbed_wait_api_no_rtos.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "cmsis.h"
#include "platform/mbed_toolchain.h"
#include "platform/mbed_wait_api.h"
#include "platform/mbed_critical.h"

// This implementation of the wait functions will be compiled only
// if the RTOS is not present.
Expand All @@ -36,7 +37,19 @@ void wait_ms(int ms)
#if DEVICE_LPTICKER
const ticker_data_t *const ticker = get_lp_ticker_data();
uint32_t start = ticker_read(ticker);
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000));
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000)) {
if (core_util_are_interrupts_enabled()) {
mbed_override_sleep_hook();
}
}
#elif DEVICE_USTICKER
const ticker_data_t *const ticker = get_us_ticker_data();
uint32_t start = ticker_read(ticker);
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000)) {
if (core_util_are_interrupts_enabled()) {
mbed_override_sleep_hook();
}
}
#else
wait_us(ms * 1000);
#endif
Expand Down