stm32f1xx-hal: Some i2c devices do not work correctly
Hi, I’d like to use a HTU21D humidity sensor with a blue pill. Unfortunately, this sensor doesn’t have an “official” driver on this page: https://github.com/rust-embedded/awesome-embedded-rust/blob/master/README.md#driver-crates.
But I found one repository implementing the driver: https://github.com/etrombly/htu21d. But I can’t get it to work. Here is the code I’m using:
// #![deny(unsafe_code)]
// #![deny(warnings)]
#![no_main]
#![no_std]
extern crate cortex_m as cm;
extern crate cortex_m_rt as rt;
extern crate embedded_hal;
extern crate nb;
extern crate panic_itm;
extern crate stm32f1xx_hal as hal;
extern crate htu21d;
// extern crate itoa;
use nb::block;
use rt::entry;
// use embedded_hal::digital::v2::OutputPin;
use hal::prelude::*;
use hal::{
i2c, pac,
serial::{Config, Serial},
delay::Delay,
};
use htu21d::Htu21df;
#[entry]
fn main() -> ! {
// Initialize peripherals
let device_peripherals = pac::Peripherals::take().unwrap();
let cortex_peripherals = cortex_m::Peripherals::take().unwrap();
let mut rcc = device_peripherals.RCC.constrain();
let mut flash = device_peripherals.FLASH.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
// Initialize the GPIO A and B
// let mut gpioa = device_peripherals.GPIOA.split(&mut rcc.apb2);
let mut gpiob = device_peripherals.GPIOB.split(&mut rcc.apb2);
// Prepare the alternate function I/O registers
let mut afio = device_peripherals.AFIO.constrain(&mut rcc.apb2);
let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl);
let sda = gpiob.pb7.into_alternate_open_drain(&mut gpiob.crl);
let i2c = i2c::BlockingI2c::i2c1(
device_peripherals.I2C1,
(scl, sda),
&mut afio.mapr,
i2c::Mode::Standard { frequency: 400 },
clocks,
&mut rcc.apb1,
50000,
3,
10000,
50000,
);
let mut htu21d = Htu21df::new(i2c).unwrap();
let humidity = htu21d.get_humidity().unwrap();
loop {}
}
The sensor works (tested on an Arduino Uno), the blue pill + I2C works (I can read a bmp085 I2C sensor with more or less the same code).
But I can’t seem to get anywhere with the htu21d on the blue pill, with the code above. While debugging, it seems the instruction let mut htu21d = Htu21df::new(i2c).unwrap() never completes.
From what I can tell from the debugger, I thinks the reset instruction triggered in the driver doesn’t complete. It’s stuck here:
https://github.com/etrombly/htu21d/blob/693bcd73d7b618425ff767ae425f0b25011182c0/src/lib.rs#L38
Also, I tried disabling the reset to try another function from the driver (eg: get_humidity), but the code hangs as well.
For everything I tried, I never got an error, the code just seems to hang.
Do you have any idea where to start to find the problem?
Cheers
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (11 by maintainers)
@eupn I did, I’ll see if I can still reproduce the issue, and then open an issue