Skip to content

Commit

Permalink
rp2: Integrate soft_timer using the alarm pool.
Browse files Browse the repository at this point in the history
The alarm pool is used to schedule the callback to soft_timer_handler().

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Nov 29, 2023
1 parent 516cc28 commit c9a9b2e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions ports/rp2/CMakeLists.txt
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ports/rp2/main.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
}
Expand Down
23 changes: 23 additions & 0 deletions ports/rp2/mphalport.c
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions ports/rp2/mphalport.h
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions ports/rp2/pendsv.h
Expand Up @@ -29,6 +29,7 @@
#include <stddef.h>

enum {
PENDSV_DISPATCH_SOFT_TIMER,
#if MICROPY_PY_LWIP
PENDSV_DISPATCH_LWIP,
#endif
Expand Down

0 comments on commit c9a9b2e

Please sign in to comment.