83 lines
2 KiB
Rust
83 lines
2 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
|
|
use core::panic::PanicInfo;
|
|
|
|
use embassy_executor::Spawner;
|
|
use embassy_rp::{
|
|
bind_interrupts,
|
|
gpio::{Level, Output},
|
|
peripherals::{PIN_15, PIO0, USB},
|
|
pio::{InterruptHandler as PioInterruptHandler, Pio},
|
|
usb::{Driver, InterruptHandler as USBInterrupthandler},
|
|
};
|
|
use embassy_time::Timer;
|
|
|
|
use dht_pio::{Dht, Dht22, DhtError};
|
|
|
|
#[panic_handler]
|
|
fn panic(_info: &PanicInfo) -> ! {
|
|
loop {
|
|
unsafe {
|
|
PANIC_LED.as_mut().unwrap().set_high();
|
|
}
|
|
}
|
|
}
|
|
|
|
static mut PANIC_LED: Option<Output<'static, PIN_15>> = None;
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
PIO0_IRQ_0 => PioInterruptHandler<PIO0>;
|
|
USBCTRL_IRQ => USBInterrupthandler<USB>;
|
|
});
|
|
|
|
#[embassy_executor::task]
|
|
async fn logger_task(driver: Driver<'static, USB>) {
|
|
embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver);
|
|
}
|
|
|
|
#[embassy_executor::main]
|
|
async fn main(spawner: Spawner) {
|
|
let p = embassy_rp::init(Default::default());
|
|
|
|
unsafe {
|
|
PANIC_LED = Some(Output::new(p.PIN_15, Level::Low));
|
|
}
|
|
|
|
let driver = Driver::new(p.USB, Irqs);
|
|
spawner.spawn(logger_task(driver)).unwrap();
|
|
|
|
// pio access
|
|
let pio = p.PIO0;
|
|
let Pio {
|
|
mut common,
|
|
|
|
// dht22 state machine
|
|
sm1,
|
|
..
|
|
} = Pio::new(pio, Irqs);
|
|
|
|
// create DHT22
|
|
let mut dht = Dht22::new(&mut common, sm1, p.PIN_0);
|
|
|
|
let dht_reading = async {
|
|
loop {
|
|
match dht.read().await {
|
|
Ok(reading) => {
|
|
log::info!("temp: {}, humid: {}", reading.temperature, reading.humidity);
|
|
}
|
|
Err(err) => match err {
|
|
DhtError::Timeout => log::info!("dht timeout error"),
|
|
DhtError::CrcMismatch(data, crc) => {
|
|
log::info!("dht checksum error:\ndata: {data:032b}\ncrc: {crc:032b}");
|
|
}
|
|
DhtError::ReadError => log::info!("dht read error"),
|
|
},
|
|
}
|
|
|
|
Timer::after_secs(1).await;
|
|
}
|
|
};
|
|
|
|
dht_reading.await;
|
|
}
|