Skip to content

Conversation

dpgeorge
Copy link
Member

This PR adds a very simple but useful implementation of machine.Timer to the stm32 port, providing a "software timer" with a 1ms resolution, allowing unlimited number of concurrent timers. They can be one-shot or periodic, have a Python callback, and can be cancelled.

It uses SysTick to implement the soft timer, so doesn't impact an existing hardware timer. There is a very small overhead added to the SysTick IRQ, which could be further optimised (eg by patching systick_irq code dynamically).

The Python-level API is intended to match esp8266 and esp32, like this:

from machine import Timer
t = Timer(-1, mode=Timer.PERIODIC, callback=lambda t:print(t), period=2000)
<wait a bit>
t.deinit()

Ultimately the Timer API will be refined further, see discussion at #2971, but for now this at least provides a starting point and something useful and portable.

@dpgeorge
Copy link
Member Author

Would address #2693

@dpgeorge dpgeorge force-pushed the stm32-machine-soft-timer branch 3 times, most recently from d2041a7 to af16be5 Compare October 31, 2019 09:23
This commit adds an implementation of a "software timer" with a 1ms
resolution, using SysTick.  It allows unlimited number of concurrent
timers (limited only by memory needed for each timer entry).  They can be
one-shot or periodic, and associated with a Python callback.

There is a very small overhead added to the SysTick IRQ, which could be
further optimised in the future, eg by patching SysTick_Handler code
dynamically.
This commit adds an implementation of machine.Timer backed by the soft
timer mechanism.  It allows an arbitrary number of timers with 1ms
resolution, with an associated Python callback.  The Python-level API
matches existing ports that have a soft timer, and is used as:

    from machine import Timer
    t = Timer(freq=10, callback=lambda t:print(t))
    ...
    t = Timer(mode=Timer.ONE_SHOT, period=2000, callback=lambda t:print(t))
    ...
    t.deinit()
@dpgeorge dpgeorge force-pushed the stm32-machine-soft-timer branch from af16be5 to 48b25a8 Compare October 31, 2019 11:13
@dpgeorge dpgeorge merged commit 48b25a8 into micropython:master Oct 31, 2019
@dpgeorge dpgeorge deleted the stm32-machine-soft-timer branch October 31, 2019 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant