Skip to content

Commit

Permalink
i2c: add max_retry to i2c read and fix i2c-demo (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Junxing Zhu <zjx1319@hust.edu.cn>
  • Loading branch information
jakezhu9 authored Dec 24, 2024
1 parent 69b39c5 commit c167e09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 7 additions & 1 deletion bouffalo-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,15 @@ impl<I2C: Deref<Target = RegisterBlock>, PADS> embedded_hal::i2c::I2c for I2c<I2
};

let mut i = 0;
let max_retry = len * 100;
let mut retry = 0;
while i < len {
while self.i2c.fifo_config_1.read().receive_available_bytes() == 0 {
core::hint::spin_loop();
retry += 1;
if retry >= max_retry {
unsafe { self.i2c.config.modify(|config| config.disable_master()) };
return Err(Error::Other);
}
}
let word = self.i2c.fifo_read.read();
let bytes_to_read = core::cmp::min(len - i, 4);
Expand Down
14 changes: 9 additions & 5 deletions examples/peripherals/i2c-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ fn main(p: Peripherals, c: Clocks) -> ! {
let mut serial = p.uart0.freerun(config, pads, &c).unwrap();
let mut led = p.gpio.io8.into_floating_output();

let scl = p.gpio.io6.into_i2c::<2>();
let sda = p.gpio.io7.into_i2c::<2>();
let scl = p.gpio.io6.into_i2c::<0>();
let sda = p.gpio.io7.into_i2c::<0>();
let mut i2c = I2c::new(p.i2c0, (scl, sda), &p.glb);
i2c.enable_sub_address(SCREEN_TOUCH_SUB_ADDRESS);

writeln!(serial, "Hello Rust🦀!").ok();
writeln!(
serial,
"Welcome to I2C demo, touch the screen to turn on the LED"
)
.ok();
led.set_high().ok();
let mut buf = [0u8; 6];
loop {
riscv::asm::delay(100_000);
Expand All @@ -39,9 +45,7 @@ fn main(p: Peripherals, c: Clocks) -> ! {
}
writeln!(serial, "pos: ({}, {})[{}]", buf[3], buf[5], buf[2] >> 4).ok();
}
Err(_) => {
led.set_high().ok();
}
Err(_) => {}
}
}
}

0 comments on commit c167e09

Please sign in to comment.