summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-11-14 13:21:13 +1100
committerDamien George <damien@micropython.org>2023-11-29 16:23:49 +1100
commitc9a9b2e682f040d2c5cfce97bb4858d3e871bf4e (patch)
tree6fe4bae886ac50c48857db11bd00752a02592ad0
parent516cc280e03b35469096db5f7de141a1018a185f (diff)
rp2: Integrate soft_timer using the alarm pool.
The alarm pool is used to schedule the callback to soft_timer_handler(). Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/rp2/CMakeLists.txt1
-rw-r--r--ports/rp2/main.c2
-rw-r--r--ports/rp2/mphalport.c23
-rw-r--r--ports/rp2/mphalport.h4
-rw-r--r--ports/rp2/pendsv.h1
5 files changed, 31 insertions, 0 deletions
diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt
index 15f6f6ca1..0f8c6d8e8 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 682ea1447..43680a46c 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 4b1c1fa1f..c567a560b 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 95e7cba2c..b8133c783 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 294cef3c7..fd21ad9ef 100644
--- a/ports/rp2/pendsv.h
+++ b/ports/rp2/pendsv.h
@@ -29,6 +29,7 @@
#include <stddef.h>
enum {
+ PENDSV_DISPATCH_SOFT_TIMER,
#if MICROPY_PY_LWIP
PENDSV_DISPATCH_LWIP,
#endif