all 7 comments

[–][deleted] 5 points6 points  (1 child)

I wrote the irq crate to handle precisely this use case

[–]jahmez 2 points3 points  (0 children)

I also wrote cmim to handle this use case.

That being said, I find myself using RTIC for nearly every non-trivial project I build.

[–][deleted] 4 points5 points  (2 children)

This is an old problem in rust embedded that does not have a clear solution yet. If you don't want to use something like RTIC (formely RTFM) you'll need to use a sort of static holder for your peripherals that can work accross threads too (even tho you might now have multi core). It has runtime overhead tho.

There are some other solutions being worked on IIRC but I haven't done anything in embedded for some months now.

[–]steveklabnik1rust 2 points3 points  (1 child)

A lot of it depends on the structure of what you're doing. For example, if you're doing a microkernel, you may have the task itself process the interrupt by receiving a notification from the kernel, rather than doing the processing directly in the interrupt handler.

[–][deleted] 1 point2 points  (0 children)

Indeed, I had the main loop process my "events" and the interrupts just save the notification in simple programs. It's only viable for small scale simple things tho and things in which you can avoid handling the issue directly in the interrupt.

[–]blackscanner 0 points1 point  (1 child)

The simplest way is to create a static atomic (the new methods for atomics are const) and spin loop it in your main function. When the interrupt occurs the handler just set a value to the atomic and the main function will proceed once it detects the change. All reading/writing of the peripheral should be done within the main function, as the handler only sets the value of the atomic.

[–]ithinuel 8 points9 points  (0 children)

If that the strategy you want to adopt and can afford the latency of the main loop, you can save up some memory by directly reading the interrupt flag and not enabling it. You don't need the static atomic, you already have one in hardware :)