Open
Description
Hi, I'm writing a driver using embedded-hal 1.0.0 for I2C communication. I can successfully communicate with the sensor, and the code compiles and works correctly. However, when I detach the sensor from the F401 board, I'm unable to obtain an I2C error (such as NoAcknowledge). It seems that the code gets stuck in an infinite loop at around line 260 of stm32f4xx-hal i2c.rs.
Here is the example code. As I mentioned, the code works properly with the sensor attached and returns the correct result.
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use cortex_m_semihosting::hprint;
use embedded_hal::i2c::{I2c, Error};
use panic_halt as _;
use stm32f4xx_hal::{
i2c::*,
pac::{self},
prelude::*,
};
const ADDR: u8 = 0x6B;
const WHO_AM_I_REGISTER: u8 = 0xF;
pub struct TestDriver<I2C> {
i2c: I2C,
}
impl<I2C: I2c> TestDriver<I2C> {
pub fn new(i2c: I2C) -> Self {
Self { i2c }
}
pub fn read_who_am_i(&mut self) -> Result<u8, I2C::Error> {
let mut buf = [0];
self.i2c.write_read(ADDR, &[WHO_AM_I_REGISTER], &mut buf)?;
Ok(buf[0])
}
}
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
//let cp = cortex_m::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.use_hse(8.MHz()).freeze();
//let mut delay = cp.SYST.delay(&clocks);
let gpiob = dp.GPIOB.split();
let scl = gpiob.pb8;
let sda = gpiob.pb9;
let i2c: stm32f4xx_hal::i2c::I2c<pac::I2C1> = I2c1::new(
dp.I2C1,
(scl, sda),
Mode::Standard {
frequency: 100.kHz(),
},
&clocks,
);
let mut sensor = TestDriver::new(i2c);
let result: u8 = match sensor.read_who_am_i() {
Ok(n) => n,
Err(e) => {
hprint!("error: {:?}\n", e.kind());
0
},
};
hprint!("result: {}\n", result);
loop {
}
}
Metadata
Metadata
Assignees
Labels
No labels