diff --git a/src/main.rs b/src/main.rs index 56c0197..f8ec219 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,10 +36,19 @@ fn toggle_mcu_led(gpioa: &mut stm32f103::GPIOA) { #[interrupt] fn I2C1_EV() { cortex_m::interrupt::free(|cs| { + static mut BYTES_SENT: u8 = 0; if let Some(ref mut i2c1) = *I2C1.borrow(cs).borrow_mut() { if i2c1.sr1.read().addr().bit_is_set() { let _ = i2c1.sr2.read(); - i2c1.dr.write(|w| w.dr().bits(0xAA)); + i2c1.dr.write(|w| w.dr().bits(0xDE)); + + + // This should be safe because this this variable + // is only written or accessed within this interrupt. + unsafe { BYTES_SENT += 1} + } else if unsafe {read_volatile(&raw const BYTES_SENT) == 1 } { + i2c1.dr.write(|w| w.dr().bits(0xAD)); + unsafe { BYTES_SENT = 0;} } } }); @@ -94,6 +103,7 @@ fn main() -> ! { i2c1.cr2.modify(|_,w| w.itbufen().enabled()); *I2C1.borrow(cs).borrow_mut() = Some(i2c1); + // I don't understand why this is unsafe or how I could make it safe. unsafe { cortex_m::peripheral::NVIC::unmask(stm32f1::stm32f103::interrupt::I2C1_EV); } });