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