Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32U5: Add ADC drivers #3688

Merged
merged 17 commits into from
Dec 31, 2024
Prev Previous commit
Next Next commit
better u5 adc example
  • Loading branch information
klownfish committed Dec 18, 2024
commit 8eeff8502d56612ce6fea55d01990a21e665ac43
67 changes: 44 additions & 23 deletions examples/stm32u5/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use defmt::{*};
use defmt_rtt as _;

use embassy_stm32::adc;
use embassy_stm32::adc::AdcChannel;
use embassy_stm32::adc::adc4;
use panic_probe as _;

Expand Down Expand Up @@ -33,37 +34,57 @@ async fn main(spawner: embassy_executor::Spawner) {

}

let p = embassy_stm32::init(config);
info!("Hello World!");
let mut p = embassy_stm32::init(config);

let mut adc = adc::Adc::new(p.ADC1);
let mut adc_pin = p.PA3;
let mut adc_pin1 = p.PA3; // A0 on nucleo u5a5
let mut adc_pin2 = p.PA2; // A1 on nucleo u5a5
adc.set_resolution(adc::Resolution::BITS14);
adc.set_averaging(adc::Averaging::Samples1024);
adc.set_sample_time(adc::SampleTime::CYCLES1_5);

let mut adc2 = adc::Adc::new(p.ADC2);
let mut adc_pin2 = p.PA5;
adc2.set_resolution(adc::Resolution::BITS14);
adc2.set_averaging(adc::Averaging::Samples1024);
adc2.set_sample_time(adc::SampleTime::CYCLES1_5);
adc.set_sample_time(adc::SampleTime::CYCLES160_5);
let max = adc::resolution_to_max_count(adc::Resolution::BITS14);

let mut adc4 = adc4::Adc4::new(p.ADC4);
let mut adc_pin4 = p.PD11;
let mut adc4_pin1 = p.PD11;
let mut adc4_pin2 = p.PC0;
adc4.set_resolution(adc4::Resolution::BITS12);
adc4.set_averaging(adc4::Averaging::Samples256);
adc4.set_sample_time(adc4::SampleTime::CYCLES1_5);
let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12);

let raw: u16 = adc.blocking_read(&mut adc_pin1);
let volt: f32 = 3.3 * raw as f32 / max as f32;
info!("Read 1 pin 1 {}", volt);

let raw: u16 = adc.blocking_read(&mut adc_pin2);
let volt: f32 = 3.3 * raw as f32 / max as f32;
info!("Read 1 pin 2 {}", volt);

let raw4: u16 = adc4.blocking_read(&mut adc4_pin1);
let volt4: f32 = 3.3 * raw4 as f32 / max4 as f32;
info!("Read 4 pin 1 {}", volt4);

let raw4: u16 = adc4.blocking_read(&mut adc4_pin2);
let volt4: f32 = 3.3 * raw4 as f32 / max4 as f32;
info!("Read 4 pin 2 {}", volt4);

let mut degraded1 = adc_pin1.degrade_adc();
let mut degraded2 = adc_pin2.degrade_adc();
let mut measurements = [0u16; 2];

adc.read(
&mut p.GPDMA1_CH0,
[
(&mut degraded2, adc::SampleTime::CYCLES160_5),
(&mut degraded1, adc::SampleTime::CYCLES160_5),
]
.into_iter(),
&mut measurements,
).await;
let volt1: f32 = 3.3 * measurements[1] as f32 / max as f32;
let volt2: f32 = 3.3 * measurements[0] as f32 / max as f32;

info!("Async read 1 pin 1 {}", volt1);
info!("Async read 1 pin 2 {}", volt2);

loop {
embassy_time::Timer::after_millis(100).await;
let raw :u16 = adc.blocking_read(&mut adc_pin);
let max = adc::resolution_to_max_count(adc::Resolution::BITS14);
let volt: f32 = 3.3 * raw as f32 / max as f32;
info!("Read ADC1 {}", volt);

let raw4 :u16 = adc4.blocking_read(&mut adc_pin4);
let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12);
let volt4: f32 = 3.3 * raw4 as f32 / max4 as f32;
info!("Read ADC4 {}", volt4);
}
}