From dbe40d8b59f85cc7b840dd21f37cedc387e06328 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 3 Jul 2026 18:10:53 -0700 Subject: [PATCH] enable PLL for ADC --- src/main.rs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 11f9f21..c063da1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,10 @@ use defmt::unwrap; use embassy_executor::Spawner; use embassy_stm32::{ + Config, Peri, adc::{Adc, SampleTime}, gpio::{AnyPin, Level, Output, Speed}, peripherals::{ADC1, PA0}, - Config, Peri, }; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; @@ -16,32 +16,49 @@ use {defmt_rtt as _, panic_probe as _}; async fn main(spawner: Spawner) { let mut config = Config::default(); - config.rcc.mux.adc12sel = embassy_stm32::rcc::mux::Adcsel::SYS; + { + use embassy_stm32::rcc::{Pll, PllMul, PllPreDiv, PllRDiv, PllSource, Sysclk, mux}; + config.rcc.pll = Some(Pll { + source: PllSource::HSI, + prediv: PllPreDiv::DIV4, + mul: PllMul::MUL85, + divp: None, + divq: None, + // Main system clock at 170 MHz + divr: Some(PllRDiv::DIV2), + }); + config.rcc.mux.adc12sel = mux::Adcsel::SYS; + config.rcc.sys = Sysclk::PLL1_R; + } let p = embassy_stm32::init(config); - let mut a = Adc::new(p.ADC1, Default::default()); + let a = Adc::new(p.ADC1, Default::default()); - let mut temp = a.enable_temperature(); + // let mut temp = a.enable_temperature(); - let t = a.blocking_read(&mut temp, SampleTime::CYCLES247_5); - defmt::info!("got temp: {}", t); + // let t = a.blocking_read(&mut temp, SampleTime::CYCLES247_5); + // defmt::info!("got temp: {}", t); let channel = p.PA0.into(); spawner.spawn(unwrap!(read_adc1_pa0(a, channel))); - spawner.spawn(unwrap!(blinky(p.PA5.into()))); + // spawner.spawn(unwrap!(blinky(p.PA5.into()))); - Timer::after_secs(5).await; + Timer::after_secs(60).await; treatbot_5k::exit() } #[embassy_executor::task] async fn read_adc1_pa0(mut a: Adc<'static, ADC1>, mut channel: Peri<'static, PA0>) { loop { - let val = a.blocking_read(&mut channel, SampleTime::CYCLES12_5); - defmt::info!("read {}", val); - Timer::after_secs(1).await; + let val = a.blocking_read(&mut channel.reborrow(), SampleTime::CYCLES640_5); + if val < 20 { + defmt::info!("low: {}", val); + } else { + defmt::info!("high {}", val); + } + Timer::after_millis(100).await; } }