shared/runtime/softtimer: Add support for hard callbacks.

Add a flag SOFT_TIMER_HARD_CALLBACK to request that a soft timer's python
callback is run directly from the IRQ handler with the scheduler and heap
locked, instead of being scheduled via mp_sched_schedule().

Signed-off-by: Chris Webb <chris@arachsys.com>
This commit is contained in:
Chris Webb
2025-08-25 13:59:53 +01:00
committed by Damien George
parent 706cc8d833
commit ad11df7f7a
2 changed files with 7 additions and 1 deletions

View File

@@ -28,6 +28,7 @@
#include "py/gc.h" #include "py/gc.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared/runtime/mpirq.h"
#include "softtimer.h" #include "softtimer.h"
#ifdef MICROPY_SOFT_TIMER_TICKS_MS #ifdef MICROPY_SOFT_TIMER_TICKS_MS
@@ -88,7 +89,11 @@ void soft_timer_handler(void) {
soft_timer_entry_t *entry = heap; soft_timer_entry_t *entry = heap;
heap = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap->pairheap); heap = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap->pairheap);
if (entry->flags & SOFT_TIMER_FLAG_PY_CALLBACK) { if (entry->flags & SOFT_TIMER_FLAG_PY_CALLBACK) {
mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry)); if (mp_irq_dispatch(entry->py_callback, MP_OBJ_FROM_PTR(entry),
entry->flags & SOFT_TIMER_FLAG_HARD_CALLBACK)) {
// Uncaught exception; disable the callback so it doesn't run again.
entry->mode = SOFT_TIMER_MODE_ONE_SHOT;
}
} else if (entry->c_callback) { } else if (entry->c_callback) {
entry->c_callback(entry); entry->c_callback(entry);
} }

View File

@@ -30,6 +30,7 @@
#define SOFT_TIMER_FLAG_PY_CALLBACK (1) #define SOFT_TIMER_FLAG_PY_CALLBACK (1)
#define SOFT_TIMER_FLAG_GC_ALLOCATED (2) #define SOFT_TIMER_FLAG_GC_ALLOCATED (2)
#define SOFT_TIMER_FLAG_HARD_CALLBACK (4)
#define SOFT_TIMER_MODE_ONE_SHOT (1) #define SOFT_TIMER_MODE_ONE_SHOT (1)
#define SOFT_TIMER_MODE_PERIODIC (2) #define SOFT_TIMER_MODE_PERIODIC (2)