Hi,
I've browsed through the internet a bit and looked at some options, but there does not seem to be a clear-cut or ideal way to solve this issue (yet). I was hoping to get some more ideas here because I am still new to embedded rust. What is the best way to share data like arrays or buffers in general between the main thread and an interrupt handler?
My goal was to write an I2C Slave interrupt handler. There are the following requirements
- The slave interrupt handler takes care of loading received data from a FIFO into a receive buffer which can then be used by the main thread
- The slave interrupt handlers takes care of loading the transmit FIFO from a shared buffer. This shared buffer can be filled by the main thread as well
My initial skeleton code can be found here: va108xx-hal-rs/i2c-slave.rs at mueller/i2c-slave · robamu-org/va108xx-hal-rs · GitHub
My plan is to make the IRQ handler part of the I2cSlave struct because this struct also consumes the I2C peripheral. Also, it's the higher level abstraction I can use to tweak how the IRQ handler operates.
My first though was to have a member function like this:
pub fn slave_irq_handler(&mut self, buffer: &mut [u8]) {
let irq_enb = self.i2c_base.i2c.irq_enb.read();
}
However, this assumes some buffer is passed to the IRQ handler, and the IRQ handler is now part of the HAL which makes using components like RTIC .. more difficult or impossible?
I though this was also a good use case for channels, but the standard channels provided by rust only are available for std applications if I understand correctly.
I think I will have the same issue when writing something like an interrupt handler for a UART which permanently empties the reception FIFO, ensuring that no data is missed. Doing something like this without interrupts seems to be difficult to impossible to me in any non-trivial application, and being reliant on an UART IRQ handler to make sure all data is read into some data structure is something I have already implemented multiple times for MCUs and is a common use-case for embedded applications in my opinion. Therefore, I think it might also be a good idea to maybe add a chapter or section to the embedded rust book specifying how to solve issues like this.
What would be some methods and ways to solve this issue? Especially if parts of the IRQ code are part of a HAL.
Kind Regards
Robin