esp-rs / book

The Rust on ESP Book
https://esp-rs.github.io/book/
Apache License 2.0
161 stars 54 forks source link

RTOS Support #32

Open vpochapuis opened 1 year ago

vpochapuis commented 1 year ago

Hello! Is there any current support for a RTOS with esp-rs (for example a safe API to work with the ESP-IDF FreeRTOS) ? And if yes could it be documented in the Book? (And if not maybe add a part or note about the status of RTOS with esp-rs). (I am new to this environment, sorry if the question isn't relevant.)

Thank you for your help

MabezDev commented 1 year ago

Most RTOS APIs are already integrated into the standard library, for things like threads and mutexes. Other APIs that are not expressible via the standard library we recommend using the freertos C functions from esp-idf-sys. If you have ideas for safe wrappers for those, PRs are most welcome :).

vpochapuis commented 1 year ago

Most RTOS APIs are already integrated into the standard library, for things like threads and mutexes. Other APIs that are not expressible via the standard library we recommend using the freertos C functions from esp-idf-sys. If you have ideas for safe wrappers for those, PRs are most welcome :).

Thank you. My main concern is about the timings, I need to run task at a specific frequency that is not dependent on the Task execution time (using DelayUntil in FreeRTOS). And I am worried that using the esp-idf-sys function wrapper would not be a good solution inside a Thread as I don't understand well yet the relationship between the Standard Library and the way FreeRTOS is integrated under. (For example, is a Thread considered a FreeRTOS Task? etc.. And what about Task Priorities).

I use Rust since March, so I am sorry if my low level understanding is still limited in this language.

And I wish I could do a PR XD I still need more experience before doing so! (But would be pleased to do so once I get the knowledge and the confidence)

Thanks for your help

MabezDev commented 1 year ago

(For example, is a Thread considered a FreeRTOS Task? etc.. And what about Task Priorities).

Yes, but sort of no. A thread is a FreeRTOS task, but instead of the Rust standard library calling FreeRTOS APIs, it actually calls libc APIs, specifically pthread APIs. esp-idf has the glue between the two so that pthread can create and manage FreeRTOS tasks.

With all that said, they are still FreeRTOS tasks underneath, so you can get the handle to the task with https://github.com/esp-rs/esp-idf-hal/blob/75b4e4f406c91d8b5e444343b42518b3a176dbdb/src/interrupt.rs#L108 and use whatever raw C FreeRTOS API you like :)

vpochapuis commented 1 year ago

(For example, is a Thread considered a FreeRTOS Task? etc.. And what about Task Priorities).

Yes, but sort of no. A thread is a FreeRTOS task, but instead of the Rust standard library calling FreeRTOS APIs, it actually calls libc APIs, specifically pthread APIs. esp-idf has the glue between the two so that pthread can create and manage FreeRTOS tasks.

With all that said, they are still FreeRTOS tasks underneath, so you can get the handle to the task with https://github.com/esp-rs/esp-idf-hal/blob/75b4e4f406c91d8b5e444343b42518b3a176dbdb/src/interrupt.rs#L108 and use whatever raw C FreeRTOS API you like :)

Ok great! Thanks for the explanations, it helps me better understand the ecosystem and guide me for future developments. I will contribute if i have any chance in the future!

For the Book, I don't know if it really would be interesting for others, but when coming from Real Time Embedded Systems, I think having a Section about RTOS, even just with your previous explanation, could be useful for anyone concerned by this topic!

Thanks for everything, I am glad we can have such quick and good support, this is a great community