1
votes

I am trying to use DHT11 Library for my STM32F303VC

I am getting error:

error[E0277]: the trait bound `PE2<Output<OpenDrain>>: _embedded_hal_digital_InputPin` is not satisfied
  --> src/DHT11/auxiliary/src/lib.rs:51:32
   |
51 |     let mut dht11 = Dht11::new(pin);
   |                                ^^^ the trait `_embedded_hal_digital_InputPin` is not implemented for `PE2<Output<OpenDrain>>`
   |
   = note: required because of the requirements on the impl of `embedded_hal::digital::v2::InputPin` for `PE2<Output<OpenDrain>>`
   = note: required by `Dht11::<GPIO>::new`

My Error Image:

My code is in auxilary module is:

    //! Initialization code

#![no_std]

#[allow(unused_extern_crates)] //  bug rust-lang/rust#53964
extern crate panic_itm; // panic handler

pub use cortex_m::{asm::bkpt, iprint, iprintln};
pub use cortex_m_rt::entry;
pub use f3::hal::{delay::Delay, prelude, stm32f30x::i2c1};
pub use f3::led::{Direction, Leds};
pub use m::Float as _0;
pub use f3::hal::stm32f30x::{gpioc, rcc};
pub use dht11::{self,Measurement,Dht11};
pub use stm32f30x_hal::gpio;
use f3::hal::stm32f30x::{self, GPIOE, RCC};
pub use embedded_hal::digital::v2::OutputPin;
pub use embedded_hal::digital::v2::InputPin;

use cortex_m::peripheral::ITM;
use f3::{
    hal::{
        i2c::I2c,
        prelude::*,
    },
    Lsm303dlhc,
};

pub fn init() -> (Delay, ITM, Leds, Dht11<GPIOE>) {
    (stm32f30x::Peripherals::take().unwrap());
    let cp = cortex_m::Peripherals::take().unwrap();
    let dp = stm32f30x::Peripherals::take().unwrap();

    let mut flash = dp.FLASH.constrain();
    let mut rcc = dp.RCC.constrain();

    let clocks = rcc.cfgr.freeze(&mut flash.acr);
    
    let gpioe = dp.GPIOE.split(&mut rcc.ahb);
    let leds = Leds::new(gpioe);

    let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
    let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
    let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);

    let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 400.khz(), clocks, &mut rcc.apb1);
    let pin = gpioe.pe2.into_open_drain_output(&mut gpioe.moder,&mut gpioe.otyper);

    let delay = Delay::new(cp.SYST, clocks);
    
    let mut dht11 = Dht11::new(pin);

    (delay, cp.ITM, leds, dht11)
}

my main.rs code is:

    #![deny(unsafe_code)]
#![no_main]
#![no_std]

#[allow(unused_imports)]
use aux19::{entry, iprint, iprintln, prelude::*, Direction};
use aux19::{prelude::_embedded_hal_blocking_delay_DelayMs};
use m::Float;
// Slave address
const MAGNETOMETER: u8 = 0b001_1110;


#[entry]
fn main() -> ! {
    let (mut delay, mut itm,mut leds,mut dth11) = aux18::init();

    loop {
        match dht11.perform_measurement(&mut delay) {
            Ok(meas) => iprintln!(&mut itm.stim[0],"Temp: {} Hum: {}", meas.temperature, meas.humidity).unwrap(),
            Err(e) => iprintln!(&mut itm.stim[0],"Error: {:?}", e).unwrap(),
        };

        delay.delay_ms(2_000_u16);
    }
}
1

1 Answers

0
votes

Dht11::new expects the pin to be an input pin, i.e. a type that implements embedded_hal::digital::v2::InputPin. In your auxiliary module, you configure the pin to be an output pin, which is the opposite of what you need to do:

let pin = gpioe.pe2.into_open_drain_output(&mut gpioe.moder,&mut gpioe.otyper);

The HAL library you are using has several methods to put a pin into input mode. into_floating_input might work for your use case. If you need a pull-up or pull-down resistor, there's also into_pull_down_input and into_pull_up_input. See reference documentation (for some reason, you need to expand the implementation blocks by clicking "+" to see the methods; this also prevents me from linking to them directly).

Using one of those should resolve this error.