Convert to embassy_rp

This commit is contained in:
hodasemi 2024-03-13 16:46:52 +01:00
parent e31d0cf40b
commit 31d222e0b0
4 changed files with 68 additions and 80 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"rust-analyzer.check.allTargets": false
}

View file

@ -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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
embedded-hal = { version = "0.2.5" } embassy-rp = { version = "0.1.0", features = ["unstable-pac", "time-driver", "critical-section-impl"] }
cortex-m = { version = "0.7.6"} embassy-time = { version = "0.3.0" }
cortex-m-rt = "0.7.3" 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-proc = "0.2.2"
pio = "0.2.1" pio = "0.2.1"
[dev-dependencies] fixed = "1.23.1"
rp-pico = { version = "0.8.0", features = ["rp2040-e5"] } fixed-macro = "1.2"
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"
[[example]]
name = "rp-pico-dht22"
path = "example/rp_pico_dht22.rs"

View file

@ -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 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; use crate::DhtError;
pub struct DhtPio<P: PIOExt, STI: StateMachineIndex> { pub struct DhtPio<'d, PIO: Instance, const SM: usize> {
// sm: PicoStateMachine<P, STI>, sm: StateMachine<'d, PIO, SM>,
sm: StateMachine<(P, STI), Running>,
rx_fifo: Rx<(P, STI)>,
tx_fifo: Tx<(P, STI)>,
} }
impl<P: PIOExt, STI: StateMachineIndex> DhtPio<P, STI> { impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> {
pub fn new<I: AnyPin<Function = P::PinFunction>>( pub fn new(
mut pio: rp2040_hal::pio::PIO<P>, pio: &mut Common<'d, PIO>,
sm: UninitStateMachine<(P, STI)>, mut sm: StateMachine<'d, PIO, SM>,
dht_pin: I, dht_pin: impl PioPin,
) -> Self { ) -> Self {
let program = pio_file!("./src/dht.pio"); 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); sm.set_pin_dirs(embassy_rp::pio::Direction::Out, &[&pin]);
let (mut sm, rx, tx) = rp2040_hal::pio::PIOBuilder::from_program(installed) sm.set_config(&config);
.out_pins(pin.id().num, 1) sm.set_enable(true);
.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)]);
Self { Self { sm }
sm: sm.start(),
rx_fifo: rx,
tx_fifo: tx,
}
} }
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 timeout = 2000;
let mut raw: [Option<u32>; 2] = [None; 2]; let mut raw: [Option<u32>; 2] = [None; 2];
self.tx_fifo.write(1); self.sm.tx().wait_push(1).await;
while timeout > 0 && raw[1].is_none() { while timeout > 0 && raw[1].is_none() {
match self.rx_fifo.read() { match self.sm.rx().try_pull() {
Some(d) => { Some(d) => {
if raw[0].is_none() { if raw[0].is_none() {
raw[0] = Some(d); raw[0] = Some(d);
@ -60,7 +52,7 @@ impl<P: PIOExt, STI: StateMachineIndex> DhtPio<P, STI> {
None => (), None => (),
} }
delay.delay_ms(1); Timer::after_millis(1).await;
timeout -= 1; timeout -= 1;
} }
@ -95,3 +87,9 @@ impl<P: PIOExt, STI: StateMachineIndex> DhtPio<P, STI> {
crc % 256 crc % 256
} }
} }
impl<'d, PIO: Instance, const SM: usize> Drop for DhtPio<'d, PIO, SM> {
fn drop(&mut self) {
self.sm.set_enable(false);
}
}

View file

@ -1,9 +1,6 @@
#![no_std] #![no_std]
use cortex_m::delay::Delay; use embassy_rp::pio::{Common, Instance, PioPin, StateMachine};
use rp2040_hal::gpio::AnyPin;
use rp2040_hal::pio::UninitStateMachine;
use rp2040_hal::pio::{PIOExt, StateMachineIndex};
mod dht; mod dht;
use dht::DhtPio; use dht::DhtPio;
@ -24,23 +21,23 @@ pub struct DhtResult {
pub humidity: f32, pub humidity: f32,
} }
pub struct Dht22<P: PIOExt, STI: StateMachineIndex> { pub struct Dht22<'d, PIO: Instance, const SM: usize> {
dht: DhtPio<P, STI>, dht: DhtPio<'d, PIO, SM>,
} }
impl<P: PIOExt, STI: StateMachineIndex> Dht22<P, STI> { impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> {
pub fn new<I: AnyPin<Function = P::PinFunction>>( pub fn new(
pio: rp2040_hal::pio::PIO<P>, pio: &mut Common<'d, PIO>,
sm: UninitStateMachine<(P, STI)>, sm: StateMachine<'d, PIO, SM>,
dht_pin: I, dht_pin: impl PioPin,
) -> Self { ) -> Self {
Self { Self {
dht: DhtPio::new(pio, sm, dht_pin), dht: DhtPio::new(pio, sm, dht_pin),
} }
} }
pub fn read(&mut self, delay: &mut Delay) -> Result<DhtResult, DhtError> { pub async fn read(&mut self) -> Result<DhtResult, DhtError> {
let (raw_temp, raw_hum) = self.dht.read_data(delay)?; let (raw_temp, raw_hum) = self.dht.read_data().await?;
let mut final_t: f32 = (raw_temp & 0x7FFF) as f32; let mut final_t: f32 = (raw_temp & 0x7FFF) as f32;
if (raw_temp & 0x8000) > 0 { if (raw_temp & 0x8000) > 0 {
@ -54,23 +51,23 @@ impl<P: PIOExt, STI: StateMachineIndex> Dht22<P, STI> {
} }
} }
pub struct Dht22Type2<P: PIOExt, STI: StateMachineIndex> { pub struct Dht22Type2<'d, PIO: Instance, const SM: usize> {
dht: DhtPio<P, STI>, dht: DhtPio<'d, PIO, SM>,
} }
impl<P: PIOExt, STI: StateMachineIndex> Dht22Type2<P, STI> { impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> {
pub fn new<I: AnyPin<Function = P::PinFunction>>( pub fn new(
pio: rp2040_hal::pio::PIO<P>, pio: &mut Common<'d, PIO>,
sm: UninitStateMachine<(P, STI)>, sm: StateMachine<'d, PIO, SM>,
dht_pin: I, dht_pin: impl PioPin,
) -> Self { ) -> Self {
Self { Self {
dht: DhtPio::new(pio, sm, dht_pin), dht: DhtPio::new(pio, sm, dht_pin),
} }
} }
pub fn read(&mut self, delay: &mut Delay) -> Result<DhtResult, DhtError> { pub async fn read(&mut self) -> Result<DhtResult, DhtError> {
let (raw_temp, raw_hum) = self.dht.read_data(delay)?; let (raw_temp, raw_hum) = self.dht.read_data().await?;
let tmp: i16 = raw_temp as i16; let tmp: i16 = raw_temp as i16;
@ -81,23 +78,23 @@ impl<P: PIOExt, STI: StateMachineIndex> Dht22Type2<P, STI> {
} }
} }
pub struct Dht11<P: PIOExt, STI: StateMachineIndex> { pub struct Dht11<'d, PIO: Instance, const SM: usize> {
dht: DhtPio<P, STI>, dht: DhtPio<'d, PIO, SM>,
} }
impl<P: PIOExt, STI: StateMachineIndex> Dht11<P, STI> { impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> {
pub fn new<I: AnyPin<Function = P::PinFunction>>( pub fn new(
pio: rp2040_hal::pio::PIO<P>, pio: &mut Common<'d, PIO>,
sm: UninitStateMachine<(P, STI)>, sm: StateMachine<'d, PIO, SM>,
dht_pin: I, dht_pin: impl PioPin,
) -> Self { ) -> Self {
Self { Self {
dht: DhtPio::new(pio, sm, dht_pin), dht: DhtPio::new(pio, sm, dht_pin),
} }
} }
pub fn read(&mut self, delay: &mut Delay) -> Result<DhtResult, DhtError> { pub async fn read(&mut self) -> Result<DhtResult, DhtError> {
let (t, h) = self.dht.read_data(delay)?; let (t, h) = self.dht.read_data().await?;
let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32; let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32;
if (t & 0x8000) > 0 { if (t & 0x8000) > 0 {