diff --git a/Cargo.toml b/Cargo.toml index 03ea8bf..1c91e81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/example/rp_pico_dht22.rs b/example/rp_pico_dht22.rs index 84ff60f..8e25f48 100644 --- a/example/rp_pico_dht22.rs +++ b/example/rp_pico_dht22.rs @@ -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; } }; diff --git a/src/dht.rs b/src/dht.rs index 00747ed..1c50490 100644 --- a/src/dht.rs +++ b/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 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((