Skip to content

Commit b99f424

Browse files
committed
Add overridable wait hook
Useful for external yield functions, currently there's no other way to hook a yield function into a driver
1 parent cc49181 commit b99f424

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

platform/mbed_power_mgmt.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
#define MBED_POWER_MGMT_H
2626

2727
#include "hal/sleep_api.h"
28-
#include "mbed_toolchain.h"
2928
#include "hal/ticker_api.h"
29+
#include "platform/mbed_toolchain.h"
30+
#include "platform/mbed_critical.h"
3031
#include <stdbool.h>
3132

3233
#ifdef __cplusplus
@@ -170,6 +171,33 @@ bool sleep_manager_can_deep_sleep_test_check(void);
170171
*/
171172
void sleep_manager_sleep_auto(void);
172173

174+
/** Applications may implement this to hook any calls to sleep or deepsleep
175+
*
176+
* This hook gives the application a chance to run additional code when the
177+
* system would normally waste cycles waiting for an external event.
178+
*
179+
* @note This function is explicitly _not_ called when we are in an interrupt
180+
* or critical section, as this would violate the expectations of both critical
181+
* sections and sleep hooks. In normal usage interrupts should not be calling
182+
* sleep.
183+
*
184+
* Example blinks an LED when the application sleeps:
185+
* @code
186+
* DigitalOut led1(LED1);
187+
*
188+
* void mbed_override_sleep_hook(void) {
189+
* led1 = !led1;
190+
* }
191+
*
192+
* int main() {
193+
* while (true) {
194+
* ThisThread::sleep_for(1000);
195+
* }
196+
* }
197+
* @endcode
198+
*/
199+
void mbed_override_sleep_hook(void);
200+
173201
/** Send the microcontroller to sleep
174202
*
175203
* @note This function can be a noop if not implemented by the platform.
@@ -193,6 +221,9 @@ void sleep_manager_sleep_auto(void);
193221
*/
194222
static inline void sleep(void)
195223
{
224+
if (core_util_are_interrupts_enabled()) {
225+
mbed_override_sleep_hook();
226+
}
196227
#if DEVICE_SLEEP
197228
#if (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_SYSTICK_CLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS)
198229
sleep_manager_sleep_auto();
@@ -223,6 +254,9 @@ static inline void sleep(void)
223254
MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()")
224255
static inline void deepsleep(void)
225256
{
257+
if (core_util_are_interrupts_enabled()) {
258+
mbed_override_sleep_hook();
259+
}
226260
#if DEVICE_SLEEP
227261
sleep_manager_sleep_auto();
228262
#endif /* DEVICE_SLEEP */

platform/mbed_sleep_manager.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,8 @@ bool sleep_manager_can_deep_sleep(void)
255255
}
256256

257257
#endif
258+
259+
// default hook for sleep calls
260+
MBED_WEAK void mbed_override_sleep_hook(void)
261+
{
262+
}

platform/mbed_wait_api_no_rtos.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "cmsis.h"
1919
#include "platform/mbed_toolchain.h"
2020
#include "platform/mbed_wait_api.h"
21+
#include "platform/mbed_critical.h"
2122

2223
// This implementation of the wait functions will be compiled only
2324
// if the RTOS is not present.
@@ -36,7 +37,19 @@ void wait_ms(int ms)
3637
#if DEVICE_LPTICKER
3738
const ticker_data_t *const ticker = get_lp_ticker_data();
3839
uint32_t start = ticker_read(ticker);
39-
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000));
40+
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000)) {
41+
if (core_util_are_interrupts_enabled()) {
42+
mbed_override_sleep_hook();
43+
}
44+
}
45+
#elif DEVICE_USTICKER
46+
const ticker_data_t *const ticker = get_us_ticker_data();
47+
uint32_t start = ticker_read(ticker);
48+
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000)) {
49+
if (core_util_are_interrupts_enabled()) {
50+
mbed_override_sleep_hook();
51+
}
52+
}
4053
#else
4154
wait_us(ms * 1000);
4255
#endif

0 commit comments

Comments
 (0)