rust-embedded / embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Apache License 2.0
1.95k stars 197 forks source link

unable to return error with embedded hal i2c example #580

Closed tamba91 closed 6 months ago

tamba91 commented 6 months ago

Hi, when i try to replicate the example code for i2c sensor in i2c.rs i have a warning from the compiler: unused import: Error I can communicate with my sensor (an accelerometer), but when i try to generate an error, for example by detatching the sensor from the board, the code crashes an i'm unable to return an error. I'm using an stm32f401

use embedded_hal::i2c::{I2c, Error};

const ADDR: u8 = 0x15; pub struct TemperatureSensorDriver { i2c: I2C, }

impl TemperatureSensorDriver { pub fn new(i2c: I2C) -> Self { Self { i2c } }

pub fn read_temperature(&mut self) -> Result<u8, I2C::Error> {
    let mut temp = [0];
    self.i2c.write_read(ADDR, &[TEMP_REGISTER], &mut temp)?;
    Ok(temp[0])
}

}

tamba91 commented 6 months ago

it seems it's a problem of stm32f4xxhal