diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 15f6f6ca1c..0f8c6d8e88 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -102,6 +102,7 @@ set(MICROPY_SOURCE_LIB ${MICROPY_DIR}/shared/runtime/mpirq.c ${MICROPY_DIR}/shared/runtime/pyexec.c ${MICROPY_DIR}/shared/runtime/stdout_helpers.c + ${MICROPY_DIR}/shared/runtime/softtimer.c ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c ${MICROPY_DIR}/shared/timeutils/timeutils.c ${MICROPY_DIR}/shared/tinyusb/mp_cdc_common.c diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 682ea14476..43680a46cd 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -37,6 +37,7 @@ #include "shared/readline/readline.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" +#include "shared/runtime/softtimer.h" #include "tusb.h" #include "uart.h" #include "modmachine.h" @@ -212,6 +213,7 @@ int main(int argc, char **argv) { #if MICROPY_PY_THREAD mp_thread_deinit(); #endif + soft_timer_deinit(); gc_sweep_all(); mp_deinit(); } diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c index 4b1c1fa1f5..c567a560b1 100644 --- a/ports/rp2/mphalport.c +++ b/ports/rp2/mphalport.c @@ -29,8 +29,10 @@ #include "py/mphal.h" #include "extmod/misc.h" #include "shared/runtime/interrupt_char.h" +#include "shared/runtime/softtimer.h" #include "shared/timeutils/timeutils.h" #include "shared/tinyusb/mp_usbd.h" +#include "pendsv.h" #include "tusb.h" #include "uart.h" #include "hardware/rtc.h" @@ -44,6 +46,8 @@ // microseconds since the Epoch. STATIC uint64_t time_us_64_offset_from_epoch; +static alarm_id_t soft_timer_alarm_id = 0; + #if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC #ifndef MICROPY_HW_STDIN_BUFFER_LEN @@ -260,3 +264,22 @@ void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) { uint32_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { panic_unsupported(); } + +static int64_t soft_timer_callback(alarm_id_t id, void *user_data) { + soft_timer_alarm_id = 0; + pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER, soft_timer_handler); + return 0; // don't reschedule this alarm +} + +uint32_t soft_timer_get_ms(void) { + return mp_hal_ticks_ms(); +} + +void soft_timer_schedule_at_ms(uint32_t ticks_ms) { + if (soft_timer_alarm_id != 0) { + cancel_alarm(soft_timer_alarm_id); + } + int32_t ms = soft_timer_ticks_diff(ticks_ms, mp_hal_ticks_ms()); + ms = MAX(0, ms); + soft_timer_alarm_id = add_alarm_in_ms(ms, soft_timer_callback, NULL, true); +} diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h index 95e7cba2c5..b8133c783e 100644 --- a/ports/rp2/mphalport.h +++ b/ports/rp2/mphalport.h @@ -31,10 +31,14 @@ #include "hardware/clocks.h" #include "hardware/structs/systick.h" #include "RP2040.h" // cmsis, for __WFI +#include "pendsv.h" #define SYSTICK_MAX (0xffffff) #define MICROPY_HW_USB_CDC_TX_TIMEOUT (500) +#define MICROPY_PY_PENDSV_ENTER pendsv_suspend() +#define MICROPY_PY_PENDSV_EXIT pendsv_resume() + extern int mp_interrupt_char; extern ringbuf_t stdin_ringbuf; diff --git a/ports/rp2/pendsv.h b/ports/rp2/pendsv.h index 294cef3c70..fd21ad9ef6 100644 --- a/ports/rp2/pendsv.h +++ b/ports/rp2/pendsv.h @@ -29,6 +29,7 @@ #include enum { + PENDSV_DISPATCH_SOFT_TIMER, #if MICROPY_PY_LWIP PENDSV_DISPATCH_LWIP, #endif