diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e671987 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.check.allTargets": false +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 14620d0..b27e7b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,23 +14,13 @@ categories = ["embedded", "hardware-support", "no-std"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -embedded-hal = { version = "0.2.5" } -cortex-m = { version = "0.7.6"} +embassy-rp = { version = "0.1.0", features = ["unstable-pac", "time-driver", "critical-section-impl"] } +embassy-time = { version = "0.3.0" } cortex-m-rt = "0.7.3" -rp2040-boot2 = "0.2.1" -rp2040-hal = { version = "0.9.0", features = ["rp2040-e5"] } + pio-proc = "0.2.2" pio = "0.2.1" -[dev-dependencies] -rp-pico = { version = "0.8.0", features = ["rp2040-e5"] } -embedded-hal = { version = "0.2.5", features = ["unproven"] } -embedded-alloc = "0.5.0" -cortex-m = { version = "0.7.6"} -cortex-m-rt = "0.7.3" -usb-device = "0.2.9" -usbd-serial = "0.1.1" +fixed = "1.23.1" +fixed-macro = "1.2" -[[example]] -name = "rp-pico-dht22" -path = "example/rp_pico_dht22.rs" \ No newline at end of file diff --git a/src/dht.rs b/src/dht.rs index 984c0c0..e8aa460 100644 --- a/src/dht.rs +++ b/src/dht.rs @@ -1,55 +1,47 @@ -use cortex_m::delay::Delay; +use embassy_rp::pio::{Common, Config, Instance, PioPin, StateMachine}; +use embassy_time::Timer; +use fixed::traits::ToFixed; use pio_proc::pio_file; -use rp2040_hal::gpio::AnyPin; -use rp2040_hal::pio::{PIOExt, Running, StateMachine, StateMachineIndex, Tx}; -use rp2040_hal::pio::{Rx, UninitStateMachine}; use crate::DhtError; -pub struct DhtPio { - // sm: PicoStateMachine, - sm: StateMachine<(P, STI), Running>, - rx_fifo: Rx<(P, STI)>, - tx_fifo: Tx<(P, STI)>, +pub struct DhtPio<'d, PIO: Instance, const SM: usize> { + sm: StateMachine<'d, PIO, SM>, } -impl DhtPio { - pub fn new>( - mut pio: rp2040_hal::pio::PIO

, - sm: UninitStateMachine<(P, STI)>, - dht_pin: I, +impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> { + pub fn new( + pio: &mut Common<'d, PIO>, + mut sm: StateMachine<'d, PIO, SM>, + dht_pin: impl PioPin, ) -> Self { let program = pio_file!("./src/dht.pio"); - let pin = dht_pin.into(); + let pin = pio.make_pio_pin(dht_pin); - let installed = pio.install(&program.program).unwrap(); + let mut config = Config::default(); + config.use_program(&pio.load_program(&program.program), &[]); + config.set_out_pins(&[&pin]); + config.set_set_pins(&[&pin]); + config.set_in_pins(&[&pin]); + config.shift_out.threshold = 32; + config.clock_divider = 125.to_fixed(); - let (int, frac) = (125, 0); - let (mut sm, rx, tx) = rp2040_hal::pio::PIOBuilder::from_program(installed) - .out_pins(pin.id().num, 1) - .set_pins(pin.id().num, 1) - .in_pin_base(pin.id().num) - .clock_divisor_fixed_point(int, frac) - .push_threshold(32) - .build(sm); - sm.set_pindirs([(pin.id().num, rp2040_hal::pio::PinDir::Output)]); + sm.set_pin_dirs(embassy_rp::pio::Direction::Out, &[&pin]); + sm.set_config(&config); + sm.set_enable(true); - Self { - sm: sm.start(), - rx_fifo: rx, - tx_fifo: tx, - } + Self { sm } } - pub fn read_data(&mut self, delay: &mut Delay) -> Result<(u16, u16), DhtError> { + pub async fn read_data(&mut self) -> Result<(u16, u16), DhtError> { let mut timeout = 2000; let mut raw: [Option; 2] = [None; 2]; - self.tx_fifo.write(1); + self.sm.tx().wait_push(1).await; while timeout > 0 && raw[1].is_none() { - match self.rx_fifo.read() { + match self.sm.rx().try_pull() { Some(d) => { if raw[0].is_none() { raw[0] = Some(d); @@ -60,7 +52,7 @@ impl DhtPio { None => (), } - delay.delay_ms(1); + Timer::after_millis(1).await; timeout -= 1; } @@ -95,3 +87,9 @@ impl DhtPio { crc % 256 } } + +impl<'d, PIO: Instance, const SM: usize> Drop for DhtPio<'d, PIO, SM> { + fn drop(&mut self) { + self.sm.set_enable(false); + } +} diff --git a/src/lib.rs b/src/lib.rs index d484a37..d056d66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,6 @@ #![no_std] -use cortex_m::delay::Delay; -use rp2040_hal::gpio::AnyPin; -use rp2040_hal::pio::UninitStateMachine; -use rp2040_hal::pio::{PIOExt, StateMachineIndex}; +use embassy_rp::pio::{Common, Instance, PioPin, StateMachine}; mod dht; use dht::DhtPio; @@ -24,23 +21,23 @@ pub struct DhtResult { pub humidity: f32, } -pub struct Dht22 { - dht: DhtPio, +pub struct Dht22<'d, PIO: Instance, const SM: usize> { + dht: DhtPio<'d, PIO, SM>, } -impl Dht22 { - pub fn new>( - pio: rp2040_hal::pio::PIO

, - sm: UninitStateMachine<(P, STI)>, - dht_pin: I, +impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> { + pub fn new( + pio: &mut Common<'d, PIO>, + sm: StateMachine<'d, PIO, SM>, + dht_pin: impl PioPin, ) -> Self { Self { dht: DhtPio::new(pio, sm, dht_pin), } } - pub fn read(&mut self, delay: &mut Delay) -> Result { - let (raw_temp, raw_hum) = self.dht.read_data(delay)?; + pub async fn read(&mut self) -> Result { + let (raw_temp, raw_hum) = self.dht.read_data().await?; let mut final_t: f32 = (raw_temp & 0x7FFF) as f32; if (raw_temp & 0x8000) > 0 { @@ -54,23 +51,23 @@ impl Dht22 { } } -pub struct Dht22Type2 { - dht: DhtPio, +pub struct Dht22Type2<'d, PIO: Instance, const SM: usize> { + dht: DhtPio<'d, PIO, SM>, } -impl Dht22Type2 { - pub fn new>( - pio: rp2040_hal::pio::PIO

, - sm: UninitStateMachine<(P, STI)>, - dht_pin: I, +impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> { + pub fn new( + pio: &mut Common<'d, PIO>, + sm: StateMachine<'d, PIO, SM>, + dht_pin: impl PioPin, ) -> Self { Self { dht: DhtPio::new(pio, sm, dht_pin), } } - pub fn read(&mut self, delay: &mut Delay) -> Result { - let (raw_temp, raw_hum) = self.dht.read_data(delay)?; + pub async fn read(&mut self) -> Result { + let (raw_temp, raw_hum) = self.dht.read_data().await?; let tmp: i16 = raw_temp as i16; @@ -81,23 +78,23 @@ impl Dht22Type2 { } } -pub struct Dht11 { - dht: DhtPio, +pub struct Dht11<'d, PIO: Instance, const SM: usize> { + dht: DhtPio<'d, PIO, SM>, } -impl Dht11 { - pub fn new>( - pio: rp2040_hal::pio::PIO

, - sm: UninitStateMachine<(P, STI)>, - dht_pin: I, +impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> { + pub fn new( + pio: &mut Common<'d, PIO>, + sm: StateMachine<'d, PIO, SM>, + dht_pin: impl PioPin, ) -> Self { Self { dht: DhtPio::new(pio, sm, dht_pin), } } - pub fn read(&mut self, delay: &mut Delay) -> Result { - let (t, h) = self.dht.read_data(delay)?; + pub async fn read(&mut self) -> Result { + let (t, h) = self.dht.read_data().await?; let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32; if (t & 0x8000) > 0 {