This commit is contained in:
parent
3ec3d2cfbb
commit
cf98999e96
1 changed files with 16 additions and 27 deletions
39
src/dht.rs
39
src/dht.rs
|
@ -1,7 +1,7 @@
|
||||||
use embassy_rp::pio::{
|
use embassy_rp::pio::{
|
||||||
Common, Config, Instance, PioPin, ShiftConfig, ShiftDirection, StateMachine,
|
Common, Config, Instance, PioPin, ShiftConfig, ShiftDirection, StateMachine,
|
||||||
};
|
};
|
||||||
use embassy_time::Timer;
|
use embassy_time::{with_timeout, Duration};
|
||||||
use fixed::traits::ToFixed;
|
use fixed::traits::ToFixed;
|
||||||
use fixed_macro::types::U56F8;
|
use fixed_macro::types::U56F8;
|
||||||
|
|
||||||
|
@ -45,35 +45,17 @@ impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read_data(&mut self) -> Result<(u16, u16), DhtError> {
|
pub async fn read_data(&mut self) -> Result<(u16, u16), DhtError> {
|
||||||
let mut timeout = 2000;
|
|
||||||
let mut raw: [Option<u32>; 2] = [None; 2];
|
|
||||||
|
|
||||||
self.sm.tx().wait_push(1).await;
|
self.sm.tx().wait_push(1).await;
|
||||||
|
|
||||||
while timeout > 0 && raw[1].is_none() {
|
let (data, crc) = match with_timeout(Duration::from_secs(2), self.read_rx()).await {
|
||||||
match self.sm.rx().try_pull() {
|
Ok(r) => r,
|
||||||
Some(d) => {
|
Err(_) => {
|
||||||
if raw[0].is_none() {
|
|
||||||
raw[0] = Some(d);
|
|
||||||
} else {
|
|
||||||
raw[1] = Some(d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
Timer::after_millis(1).await;
|
|
||||||
timeout -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout <= 0 {
|
|
||||||
self.sm.restart();
|
|
||||||
self.sm.clear_fifos();
|
self.sm.clear_fifos();
|
||||||
|
self.sm.restart();
|
||||||
|
|
||||||
return Err(DhtError::Timeout);
|
return Err(DhtError::Timeout);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
let data = raw[0].unwrap();
|
|
||||||
let crc = raw[1].unwrap();
|
|
||||||
|
|
||||||
if Self::compute_crc(data) != crc {
|
if Self::compute_crc(data) != crc {
|
||||||
return Err(DhtError::CrcMismatch(data, crc));
|
return Err(DhtError::CrcMismatch(data, crc));
|
||||||
|
@ -85,6 +67,13 @@ impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn read_rx(&mut self) -> (u32, u32) {
|
||||||
|
(
|
||||||
|
self.sm.rx().wait_pull().await,
|
||||||
|
self.sm.rx().wait_pull().await,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn compute_crc(data: u32) -> u32 {
|
fn compute_crc(data: u32) -> u32 {
|
||||||
let mut crc: u32 = 0;
|
let mut crc: u32 = 0;
|
||||||
crc += data & 0x000000FF;
|
crc += data & 0x000000FF;
|
||||||
|
|
Loading…
Reference in a new issue