summaryrefslogtreecommitdiffstatshomepage
path: root/shared/runtime/softtimer.h
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-11-14 13:19:31 +1100
committerDamien George <damien@micropython.org>2023-11-29 16:17:12 +1100
commit516cc280e03b35469096db5f7de141a1018a185f (patch)
treed6d401975d5070dc55d0e9673757fca5ed26c620 /shared/runtime/softtimer.h
parent9c7067d9ad91a303389ec0add306bedac55e18f9 (diff)
downloadmicropython-516cc280e03b35469096db5f7de141a1018a185f.tar.gz
micropython-516cc280e03b35469096db5f7de141a1018a185f.zip
shared/runtime/softtimer: Generalise soft_timer to work without SysTick.
If a port defines MICROPY_SOFT_TIMER_TICKS_MS then soft_timer assumes a SysTick back end, and provides a soft_timer_next variable that sets when the next call to soft_timer_handler() should occur. Otherwise, a port should provide soft_timer_get_ms() and soft_timer_schedule_at_ms() with appropriate semantics (see comments). Existing users of soft_timer should continue to work as they did. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'shared/runtime/softtimer.h')
-rw-r--r--shared/runtime/softtimer.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/shared/runtime/softtimer.h b/shared/runtime/softtimer.h
index 8603ea13f0..fe5d02b907 100644
--- a/shared/runtime/softtimer.h
+++ b/shared/runtime/softtimer.h
@@ -48,6 +48,13 @@ typedef struct _soft_timer_entry_t {
extern volatile uint32_t soft_timer_next;
+static inline int32_t soft_timer_ticks_diff(uint32_t t1, uint32_t t0) {
+ // t1 is after t0 (i.e. positive result) if there exists a uint32_t X <= INT_MAX
+ // such that t0 + X = t1. Otherwise t1 is interpreted to be earlier than
+ // t0 (negative result).
+ return t1 - t0;
+}
+
void soft_timer_deinit(void);
void soft_timer_handler(void);
void soft_timer_gc_mark_all(void);
@@ -63,4 +70,17 @@ static inline void soft_timer_reinsert(soft_timer_entry_t *entry, uint32_t initi
soft_timer_insert(entry, initial_delta_ms);
}
+#if !defined(MICROPY_SOFT_TIMER_TICKS_MS)
+// IF MICROPY_SOFT_TIMER_TICKS_MS is not defined then the port must provide the
+// following timer functions:
+// - soft_timer_get_ms() must return a 32-bit millisecond counter that wraps around.
+// - soft_timer_schedule_at_ms(ticks_ms) must schedule a callback of soft_timer_handler()
+// when the above millisecond counter reaches the given ticks_ms value. If ticks_ms
+// is behind the current counter (using int32_t arithmetic) then the callback should
+// be scheduled immediately. The callback of soft_timer_handler() should be made at
+// pend-SV IRQ level, or equivalent.
+uint32_t soft_timer_get_ms(void);
+void soft_timer_schedule_at_ms(uint32_t ticks_ms);
+#endif
+
#endif // MICROPY_INCLUDED_SHARED_RUNTIME_SOFTTIMER_H