stm32-rs / stm32h7xx-hal

Peripheral access API for STM32H7 series microcontrollers
BSD Zero Clause License
215 stars 101 forks source link

i2c.clear_irq should also clear rxie interrupts #436

Open ost-ing opened 1 year ago

ost-ing commented 1 year ago

I've noticed that when attempting to clear i2c receive interrupts utilising the clear_irq function, that this is essentially a no op. Clearing the rxie interrupt involves reading data from the rxdr register, and the clear_irq does not do this.

I would suggest having the clear_irq function return a Option, something like this:

                /// Clears interrupt flag for `event`
                pub fn clear_irq(&mut self, event: Event) -> Option<u8> {
                    self.i2c.icr.write(|w| {
                        match event {
                            Event::Stop => w.stopcf().set_bit(),
                            Event::Errors => w
                                .berrcf().set_bit()
                                .arlocf().set_bit()
                                .ovrcf().set_bit(),
                            Event::NotAcknowledge => w.nackcf().set_bit(),
                            _ => w
                        }
                    });
                    let _ = self.i2c.isr.read();
                    let _ = self.i2c.isr.read(); // Delay 2 peripheral clocks

                    if event == Event::Receive {
                        return Some(self.i2c.rxdr.read().rxdata().bits());
                    }

                    None
                }