8000 extmod/uasyncio Interface to ISR is problematic · Issue #6415 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

extmod/uasyncio Interface to ISR is problematic #6415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
peterhinch opened this issue Sep 6, 2020 · 4 comments
Closed

extmod/uasyncio Interface to ISR is problematic #6415

peterhinch opened this issue Sep 6, 2020 · 4 comments
Labels
extmod Relates to extmod/ directory in source

Comments

@peterhinch
Copy link
Contributor

This arose from this forum post. The following code sample only runs the task scheduled by the ISR when the one running task wakes up. Tested on a Pyboard 1.1 running firmware V1.13.

import uasyncio
from machine import Pin
import micropython

async def example_task():
    print('example task running')

def task_launcher(_):
    print('task_launcher running')
    uasyncio.create_task(example_task())

def irq_handler(pin):
    print('IRQ!', pin.value())
    micropython.schedule(task_launcher, None)

trigger = Pin(Pin.board.X6, Pin.IN, Pin.PULL_UP)
trigger.irq(irq_handler, Pin.IRQ_FALLING | Pin.IRQ_RISING)

async def endless_loop():
    while True:
        print('Tick.')
        await uasyncio.sleep(5)
        print('Tock.')
        await uasyncio.sleep(5)

uasyncio.run(endless_loop())

Given that #6106 is not merged, the problem can't be fixed with an Event.

Is there a way to interface uasyncio with an ISR other than a busy-wait loop?

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Sep 8, 2020
@dpgeorge
Copy link
Member
dpgeorge commented Sep 8, 2020

This is a nice example/use-case, of interfacing IRQs with a cooperative scheduler (ie uasyncio). But it has the same problems as other attempts to do this, namely that uasyncio doesn't know that the IRQ made something runnable, and also there are possible race conditions (adding the new task may occur in the middle of adding another task).

In CPython I think you want to use loop.call_soon_threadsafe(lambda: loop.create_task(example_task())) but it's exactly the implementation of call_soon_threadsafe() that is difficult.

@dpgeorge
Copy link
Member
dpgeorge commented Sep 8, 2020

Is there a way to interface uasyncio with an ISR other than a busy-wait loop?

On bare-metal it's possible to use uio.IOBase to make an object which can be added to uasyncio's poller. That's really the only way to do it at the moment.

@peterhinch
Copy link
Contributor Author

Closing as fixed by ThreadSafeFlag.

@scy
Copy link
Contributor
scy commented Mar 16, 2021

Closing as fixed by ThreadSafeFlag.

For reference, that’s #6886.

tannewt pushed a commit to tannewt/circuitpython that referenced this issue May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

No branches or pull requests

3 participants
0