首页 > 解决方案 > `PE2 没有实现特征`_embedded_hal_digital_InputPin`>`

问题描述

我正在尝试为我的 STM32F303VC使用DHT11库

我收到错误:

错误[E0277] : 特征绑定 `PE2<Output<OpenDrain>>: _embedded_hal_digital_InputPin` 不满足
  --> src/DHT11/auxiliary/src/lib.rs:51:32
    | 
51  |     让 mut dht11 = Dht11::new(pin);
   |                                ^^^ 特性 `_embedded_hal_digital_InputPin` 没有为 `PE2<Output<OpenDrain>>` 实现
   | 
   =注意:由于对 `PE2<Output<OpenDrain>>` 的 `embedded_hal::digital::InputPin` 的 impl 的要求而
   需要=注意:`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);
    }
}

标签: rustcompiler-errorsembeddedstm32

解决方案


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。请参阅参考文档(出于某种原因,您需要通过单击“+”来扩展实现块以查看方法;这也使我无法直接链接到它们)。

使用其中之一应该可以解决此错误。


推荐阅读