8000 py: Add port-agnostic inline functions for event handling. · micropython/micropython@f5be012 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5be012

Browse files
projectgusdpgeorge
authored andcommitted
py: Add port-agnostic inline functions for event handling.
These are intended to replace MICROPY_EVENT_POLL_HOOK and MICROPY_EVENT_POLL_HOOK_FAST, which are insufficient for tickless ports. This implementation is along the lines suggested here: #12925 (comment) Currently any usage of these functions expands to use the existing hook macros, but this can be switched over port by port. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 66be82d commit f5be012

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

py/mphal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,17 @@ uint64_t mp_hal_time_ns(void);
9898
#include "extmod/virtpin.h"
9999
#endif
100100

101+
// Event handling and wait-for-event functions.
102+
103+
#ifndef MICROPY_INTERNAL_WFE
104+
// Fallback definition for ports that don't need to suspend the CPU.
105+
#define MICROPY_INTERNAL_WFE(TIMEOUT_MS) (void)0
106+
#endif
107+
108+
#ifndef MICROPY_INTERNAL_EVENT_HOOK
109+
// Fallback definition for ports that don't need any port-specific
110+
// non-blocking event processing.
111+
#define MICROPY_INTERNAL_EVENT_HOOK (void)0
112+
#endif
113+
101114
#endif // MICROPY_INCLUDED_PY_MPHAL_H

py/runtime.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
109109
bool mp_sched_schedule_node(mp_sched_node_t *node, mp_sched_callback_t callback);
110110
#endif
111111

112+
// Handles any pending MicroPython events without waiting for an interrupt or event.
113+
void mp_event_handle_nowait(void);
114+
115+
// Handles any pending MicroPython events and then suspends execution until the
116+
// next interrupt or event.
117+
//
118+
// Note: on "tickless" ports this can suspend execution for a long time,
119+
// don't call unless you know an interrupt is coming to continue execution.
120+
// On "ticked" ports it may return early due to the tick interrupt.
121+
void mp_event_wait_indefinite(void);
122+
123+
// Handle any pending MicroPython events and then suspends execution until the
124+
// next interrupt or event, or until timeout_ms milliseconds have elapsed.
125+
//
126+
// On "ticked" ports it may return early due to the tick interrupt.
127+
void mp_event_wait_ms(mp_uint_t timeout_ms);
128+
112129
// extra printing method specifically for mp_obj_t's which are integral type
113130
int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec);
114131

py/scheduler.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,39 @@ void mp_handle_pending(bool raise_exc) {
241241
}
242242
#endif
243243
}
244+
245+
// Handles any pending MicroPython events without waiting for an interrupt or event.
246+
void mp_event_handle_nowait(void) {
247+
#if defined(MICROPY_EVENT_POLL_HOOK_FAST) && !MICROPY_PREVIEW_VERSION_2
248+
// For ports still using the old macros.
249+
MICROPY_EVENT_POLL_HOOK_FAST
250+
#else
251+
// Process any port layer (non-blocking) events.
252+
MICROPY_INTERNAL_EVENT_HOOK;
253+
mp_handle_pending(true);
254+
#endif
255+
}
256+
257+
// Handles any pending MicroPython events and then suspends execution until the
258+
// next interrupt or event.
259+
void mp_event_wait_indefinite(void) {
260+
#if defined(MICROPY_EVENT_POLL_HOOK) && !MICROPY_PREVIEW_VERSION_2
261+
// For ports still using the old macros.
262+
MICROPY_EVENT_POLL_HOOK
263+
#else
264+
mp_event_handle_nowait();
265+
MICROPY_INTERNAL_WFE(-1);
266+
#endif
267+
}
268+
269+
// Handle any pending MicroPython events and then suspends execution until the
270+
// next interrupt or event, or until timeout_ms milliseconds have elapsed.
271+
void mp_event_wait_ms(mp_uint_t timeout_ms) {
272+
#if defined(MICROPY_EVENT_POLL_HOOK) && !MICROPY_PREVIEW_VERSION_2
273+
// For ports still using the old macros.
274+
MICROPY_EVENT_POLL_HOOK
275+
#else
276+
mp_event_handle_nowait();
277+
MICROPY_INTERNAL_WFE(timeout_ms);
278+
#endif
279+
}

0 commit comments

Comments
 (0)
0