提问者:小点点

特征` _embedded_hal_digital_InputPin '不是为“PE2<Output<OpenDrain > >”实现的


我正在尝试将DHT11库用于我的STM32F303VC

我得到错误:

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`

我的错误图像:

我的代码在辅助模块中是:

    //! 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)
}

我的main.rs代码是:

    #![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个答案

匿名用户

Dht11::new期望该引脚是一个输入引脚,即实现embedded_hal::digital::v2::InputPin的类型。在辅助模块中,您将引脚配置为输出引脚,这与您需要执行的操作相反:

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

您正在使用的HAL库有几种将引脚置于输入模式的方法。into_floating_input可能适用于您的用例。如果您需要上拉或下拉电阻,还有into_pull_down_inputinto_pull_up_input。请参阅参考留档(出于某种原因,您需要通过单击“”来扩展实现块以查看方法;这也阻止了我直接链接到它们)。

使用其中一个应该可以解决这个错误。