我正在尝试为我的 STM32F303VC 使用 my Visual Studio configuration 库
我收到错误:
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);
}
}
答案 0 :(得分:0)
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_input
和 into_pull_up_input
。请参阅reference documentation(出于某种原因,您需要通过单击“+”来展开实现块以查看方法;这也阻止了我直接链接到它们)。
使用其中之一应该可以解决此错误。