Repeating timer with negative delay

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
torpi
Posts: 8
Joined: Thu Oct 28, 2021 7:12 pm

Repeating timer with negative delay

Post by torpi » Thu Oct 28, 2021 7:45 pm

Hello everyone,

I have been looking as much as I can in the documentation and tried to find anything in the source but could not figured out if Micropython offers the equivalent of the SDK negative delay in repeating timer using a Raspberry Pico (RP2)

Here in the SDK Documentation we can read:

Code: Select all

// Negative delay so means we will call repeating_timer_callback, and call it again
// 500ms later regardless of how long the callback took to execute
add_repeating_timer_ms(-500, repeating_timer_callback, NULL, &timer);
This is what I would like to do but with Micropython.
The RP2 Documentation does not say anything about that.

In the Micropython source code, I found that (line 85) negative delay are changed to a delay of 1:

Code: Select all

} else {
        // Period specified
        self->delta_us = (uint64_t)args[ARG_period].u_int * 1000000 / args[ARG_tick_hz].u_int;
    }
    if (self->delta_us < 1) {
        self->delta_us = 1;
    }
It lead me to alarm_pool_add_alarm_in_us where again, I do not read anything about negative delay.

What I am not sure to understand is why this difference between alarm_pool_add_repeating_timer_us() that takes negative delays and alarm_pool_add_alarm_in_us which doesn't apart that one is an alarm and the other a repeating timer?

So my question is, how to create a hardware timer that ticks independently of the delay the callback method might add?

Thank you so much,
Torpi

Post Reply