Fix dht reading
This commit is contained in:
parent
2a2fd6b140
commit
38b5077ad6
3 changed files with 19 additions and 9 deletions
|
@ -22,6 +22,7 @@ pio-proc = "0.2.2"
|
||||||
pio = "0.2.1"
|
pio = "0.2.1"
|
||||||
|
|
||||||
fixed = "1.23.1"
|
fixed = "1.23.1"
|
||||||
|
fixed-macro = "1.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
embassy-executor = { version = "0.5.0", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "integrated-timers"] }
|
embassy-executor = { version = "0.5.0", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "integrated-timers"] }
|
||||||
|
|
|
@ -69,13 +69,13 @@ async fn main(spawner: Spawner) {
|
||||||
Err(err) => match err {
|
Err(err) => match err {
|
||||||
DhtError::Timeout => log::info!("dht timeout error"),
|
DhtError::Timeout => log::info!("dht timeout error"),
|
||||||
DhtError::CrcMismatch(data, crc) => {
|
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"),
|
DhtError::ReadError => log::info!("dht read error"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer::after_secs(3).await;
|
Timer::after_secs(1).await;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
23
src/dht.rs
23
src/dht.rs
|
@ -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 embassy_time::Timer;
|
||||||
use fixed::traits::ToFixed;
|
use fixed::traits::ToFixed;
|
||||||
|
use fixed_macro::types::U56F8;
|
||||||
use pio_proc::pio_file;
|
use pio_proc::pio_file;
|
||||||
|
|
||||||
use crate::DhtError;
|
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_out_pins(&[&pin]);
|
||||||
config.set_set_pins(&[&pin]);
|
config.set_set_pins(&[&pin]);
|
||||||
config.set_in_pins(&[&pin]);
|
config.set_in_pins(&[&pin]);
|
||||||
config.shift_in.threshold = 32;
|
config.shift_in = ShiftConfig {
|
||||||
config.clock_divider = 125.to_fixed();
|
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_config(&config);
|
||||||
|
|
||||||
sm.set_pin_dirs(embassy_rp::pio::Direction::Out, &[&pin]);
|
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();
|
let crc = raw[1].unwrap();
|
||||||
|
|
||||||
if Self::compute_crc(data) != crc {
|
if Self::compute_crc(data) != crc {
|
||||||
return Err(DhtError::CrcMismatch(
|
return Err(DhtError::CrcMismatch(data, crc));
|
||||||
raw[0].unwrap_or(0),
|
|
||||||
raw[1].unwrap_or(0),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
|
|
Loading…
Reference in a new issue