Fix dht reading

This commit is contained in:
hodasemi 2024-03-18 08:43:47 +01:00
parent 2a2fd6b140
commit 38b5077ad6
3 changed files with 19 additions and 9 deletions

View file

@ -22,6 +22,7 @@ pio-proc = "0.2.2"
pio = "0.2.1"
fixed = "1.23.1"
fixed-macro = "1.2"
[dev-dependencies]
embassy-executor = { version = "0.5.0", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "integrated-timers"] }

View file

@ -69,13 +69,13 @@ async fn main(spawner: Spawner) {
Err(err) => match err {
DhtError::Timeout => log::info!("dht timeout error"),
DhtError::CrcMismatch(data, crc) => {
log::info!("dht checksum error:\ndata: {data}\ncrc: {crc}");
log::info!("dht checksum error:\ndata: {data:032b}\ncrc: {crc:032b}");
}
DhtError::ReadError => log::info!("dht read error"),
},
}
Timer::after_secs(3).await;
Timer::after_secs(1).await;
}
};

View file

@ -1,6 +1,9 @@
use embassy_rp::pio::{Common, Config, Instance, PioPin, StateMachine};
use embassy_rp::pio::{
Common, Config, Instance, PioPin, ShiftConfig, ShiftDirection, StateMachine,
};
use embassy_time::Timer;
use fixed::traits::ToFixed;
use fixed_macro::types::U56F8;
use pio_proc::pio_file;
use crate::DhtError;
@ -24,8 +27,17 @@ impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> {
config.set_out_pins(&[&pin]);
config.set_set_pins(&[&pin]);
config.set_in_pins(&[&pin]);
config.shift_in.threshold = 32;
config.clock_divider = 125.to_fixed();
config.shift_in = ShiftConfig {
threshold: 32,
direction: ShiftDirection::Left,
auto_fill: true,
};
config.shift_out = ShiftConfig {
threshold: 32,
direction: ShiftDirection::Left,
auto_fill: false,
};
config.clock_divider = (U56F8!(125_000_000) / 1_000_000).to_fixed();
sm.set_config(&config);
sm.set_pin_dirs(embassy_rp::pio::Direction::Out, &[&pin]);
@ -65,10 +77,7 @@ impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> {
let crc = raw[1].unwrap();
if Self::compute_crc(data) != crc {
return Err(DhtError::CrcMismatch(
raw[0].unwrap_or(0),
raw[1].unwrap_or(0),
));
return Err(DhtError::CrcMismatch(data, crc));
}
Ok((