2023-07-05 20:05:34 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
#[path = "../common.rs"]
|
|
|
|
mod common;
|
|
|
|
|
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
2023-07-21 23:34:12 +02:00
|
|
|
use embassy_rp::adc::{Adc, Channel, Config, InterruptHandler, Sample};
|
2023-07-05 20:05:34 +02:00
|
|
|
use embassy_rp::bind_interrupts;
|
2023-08-03 22:56:39 +02:00
|
|
|
use embassy_rp::gpio::{Level, Output, Pull};
|
2023-07-05 20:05:34 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
ADC_IRQ_FIFO => InterruptHandler;
|
|
|
|
});
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let mut p = embassy_rp::init(Default::default());
|
2023-08-03 22:56:39 +02:00
|
|
|
let _power_reg_pwm_mode = Output::new(p.PIN_23, Level::High);
|
|
|
|
let _wifi_off = Output::new(p.PIN_25, Level::High);
|
2023-07-05 20:05:34 +02:00
|
|
|
let mut adc = Adc::new(p.ADC, Irqs, Config::default());
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_26, Pull::Down);
|
2023-07-05 20:05:34 +02:00
|
|
|
defmt::assert!(adc.blocking_read(&mut p).unwrap() < 0b01_0000_0000);
|
|
|
|
defmt::assert!(adc.read(&mut p).await.unwrap() < 0b01_0000_0000);
|
|
|
|
}
|
|
|
|
{
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_26, Pull::Up);
|
2023-07-05 20:05:34 +02:00
|
|
|
defmt::assert!(adc.blocking_read(&mut p).unwrap() > 0b11_0000_0000);
|
|
|
|
defmt::assert!(adc.read(&mut p).await.unwrap() > 0b11_0000_0000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// not bothering with async reads from now on
|
|
|
|
{
|
|
|
|
{
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_27, Pull::Down);
|
2023-07-05 20:05:34 +02:00
|
|
|
defmt::assert!(adc.blocking_read(&mut p).unwrap() < 0b01_0000_0000);
|
|
|
|
}
|
|
|
|
{
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_27, Pull::Up);
|
2023-07-05 20:05:34 +02:00
|
|
|
defmt::assert!(adc.blocking_read(&mut p).unwrap() > 0b11_0000_0000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
{
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_28, Pull::Down);
|
2023-07-05 20:05:34 +02:00
|
|
|
defmt::assert!(adc.blocking_read(&mut p).unwrap() < 0b01_0000_0000);
|
|
|
|
}
|
|
|
|
{
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_28, Pull::Up);
|
2023-07-05 20:05:34 +02:00
|
|
|
defmt::assert!(adc.blocking_read(&mut p).unwrap() > 0b11_0000_0000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// gp29 is connected to vsys through a 200k/100k divider,
|
|
|
|
// adding pulls should change the value
|
|
|
|
let low = {
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_29, Pull::Down);
|
2023-07-05 20:05:34 +02:00
|
|
|
adc.blocking_read(&mut p).unwrap()
|
|
|
|
};
|
|
|
|
let none = {
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_29, Pull::None);
|
2023-07-05 20:05:34 +02:00
|
|
|
adc.blocking_read(&mut p).unwrap()
|
|
|
|
};
|
|
|
|
let up = {
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p = Channel::new_pin(&mut p.PIN_29, Pull::Up);
|
2023-07-05 20:05:34 +02:00
|
|
|
adc.blocking_read(&mut p).unwrap()
|
|
|
|
};
|
|
|
|
defmt::assert!(low < none);
|
|
|
|
defmt::assert!(none < up);
|
|
|
|
}
|
2023-07-21 23:34:12 +02:00
|
|
|
{
|
|
|
|
let temp = convert_to_celsius(
|
|
|
|
adc.read(&mut Channel::new_temp_sensor(&mut p.ADC_TEMP_SENSOR))
|
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
defmt::assert!(temp > 0.0);
|
|
|
|
defmt::assert!(temp < 60.0);
|
|
|
|
}
|
2023-07-05 20:05:34 +02:00
|
|
|
|
2023-07-21 23:34:12 +02:00
|
|
|
// run a bunch of conversions. we'll only check gp29 and the temp
|
|
|
|
// sensor here for brevity, if those two work the rest will too.
|
|
|
|
{
|
|
|
|
// gp29 is connected to vsys through a 200k/100k divider,
|
|
|
|
// adding pulls should change the value
|
|
|
|
let mut low = [0u16; 16];
|
|
|
|
let mut none = [0u8; 16];
|
|
|
|
let mut up = [Sample::default(); 16];
|
|
|
|
adc.read_many(
|
|
|
|
&mut Channel::new_pin(&mut p.PIN_29, Pull::Down),
|
|
|
|
&mut low,
|
|
|
|
&mut p.DMA_CH0,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
adc.read_many(
|
|
|
|
&mut Channel::new_pin(&mut p.PIN_29, Pull::None),
|
|
|
|
&mut none,
|
|
|
|
&mut p.DMA_CH0,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
adc.read_many_raw(&mut Channel::new_pin(&mut p.PIN_29, Pull::Up), &mut up, &mut p.DMA_CH0)
|
|
|
|
.await;
|
|
|
|
defmt::assert!(low.iter().zip(none.iter()).all(|(l, n)| *l >> 4 < *n as u16));
|
|
|
|
defmt::assert!(up.iter().all(|s| s.good()));
|
|
|
|
defmt::assert!(none.iter().zip(up.iter()).all(|(n, u)| (*n as u16) < u.value()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut temp = [0u16; 16];
|
|
|
|
adc.read_many(
|
|
|
|
&mut Channel::new_temp_sensor(&mut p.ADC_TEMP_SENSOR),
|
|
|
|
&mut temp,
|
|
|
|
&mut p.DMA_CH0,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let temp = temp.map(convert_to_celsius);
|
|
|
|
defmt::assert!(temp.iter().all(|t| *t > 0.0));
|
|
|
|
defmt::assert!(temp.iter().all(|t| *t < 60.0));
|
|
|
|
}
|
2023-07-05 20:05:34 +02:00
|
|
|
|
|
|
|
info!("Test OK");
|
|
|
|
cortex_m::asm::bkpt();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_to_celsius(raw_temp: u16) -> f32 {
|
|
|
|
// According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet
|
|
|
|
27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721 as f32
|
|
|
|
}
|