Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to obtain an error from i2c #749

Open
tamba91 opened this issue Mar 16, 2024 · 0 comments
Open

unable to obtain an error from i2c #749

tamba91 opened this issue Mar 16, 2024 · 0 comments

Comments

@tamba91
Copy link

tamba91 commented Mar 16, 2024

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 {
        
    } 
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant