Reading multiple bytes

This commit is contained in:
2025-04-08 19:30:38 -04:00
parent 04195a2122
commit bcfc041f50

View File

@@ -36,10 +36,19 @@ fn toggle_mcu_led(gpioa: &mut stm32f103::GPIOA) {
#[interrupt] #[interrupt]
fn I2C1_EV() { fn I2C1_EV() {
cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|cs| {
static mut BYTES_SENT: u8 = 0;
if let Some(ref mut i2c1) = *I2C1.borrow(cs).borrow_mut() { if let Some(ref mut i2c1) = *I2C1.borrow(cs).borrow_mut() {
if i2c1.sr1.read().addr().bit_is_set() { if i2c1.sr1.read().addr().bit_is_set() {
let _ = i2c1.sr2.read(); 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.cr2.modify(|_,w| w.itbufen().enabled());
*I2C1.borrow(cs).borrow_mut() = Some(i2c1); *I2C1.borrow(cs).borrow_mut() = Some(i2c1);
// I don't understand why this is unsafe or how I could make it safe. // 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); } unsafe { cortex_m::peripheral::NVIC::unmask(stm32f1::stm32f103::interrupt::I2C1_EV); }
}); });