peterhinch / micropython-async

Application of uasyncio to hardware interfaces. Tutorial and code.
MIT License
726 stars 166 forks source link

Recommended way to yield an awaitable or set the wait flag in a native MPY module? #84

Closed wuyuanyi135 closed 2 years ago

wuyuanyi135 commented 2 years ago

Hello,

I want to implement a driver in micropython with the async interface. There are some time delay (100us to 1ms, varying) required within the communication slots therefore employing the sleep_ms may not be accurate enough. The desired way is to set a flag in the C code that was triggered by the ISR to notify the scheduler that the waiting ends. In the doc I found the way to use ThreadSafeFlag in a MPY ISR. I wonder what should I use to set up such a flag in the C ISR?

Regards, YW

peterhinch commented 2 years ago

My understanding is that a ThreadSafeFlag may be triggered from any process. It is designed to work with truly asynchronous sources and does not rely on Python interpreter magic. For example on RP2 I have had a process running on core 1 trigger a ThreadSafeFlag created on core 0. uasyncio was running on core 0 with a task waiting on it. So I believe triggering it from a C ISR is valid.

The mechanics of how you pass the flag to a C ISR are beyond me - I have little experience of writing MicroPython C modules.

wuyuanyi135 commented 2 years ago

Thank you for your explanation!