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
Merged
Prev Previous commit
Next Next commit
WIP: add u5 adc
  • Loading branch information
klownfish committed Sep 17, 2024
commit 0fe9fa631a8f905809424bd7b31a7b74568eb5b6
20 changes: 9 additions & 11 deletions embassy-stm32/src/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(missing_docs)] // TODO
#![cfg_attr(adc_f3_v2, allow(unused))]

#[cfg(not(any(adc_f3_v2, adc_u5)))]
#[cfg(not(any(adc_f3_v2)))]
#[cfg_attr(adc_f1, path = "f1.rs")]
#[cfg_attr(adc_f3, path = "f3.rs")]
#[cfg_attr(adc_f3_v1_1, path = "f3_v1_1.rs")]
Expand All @@ -20,16 +20,14 @@ mod _version;
use core::marker::PhantomData;

#[allow(unused)]
#[cfg(not(any(adc_f3_v2, adc_u5)))]
#[cfg(not(any(adc_f3_v2)))]
pub use _version::*;
#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
use embassy_sync::waitqueue::AtomicWaker;

#[cfg(not(any(adc_u5)))]
pub use crate::pac::adc::vals;
#[cfg(not(any(adc_f1, adc_f3_v2, adc_u5)))]
#[cfg(not(any(adc_f1, adc_f3_v2)))]
pub use crate::pac::adc::vals::Res as Resolution;
#[cfg(not(any(adc_u5)))]
pub use crate::pac::adc::vals::SampleTime;
use crate::peripherals;

Expand All @@ -39,7 +37,7 @@ dma_trait!(RxDma, Instance);
pub struct Adc<'d, T: Instance> {
#[allow(unused)]
adc: crate::PeripheralRef<'d, T>,
#[cfg(not(any(adc_f3_v2, adc_f3_v1_1, adc_u5)))]
#[cfg(not(any(adc_f3_v2, adc_f3_v1_1)))]
sample_time: SampleTime,
}

Expand All @@ -60,7 +58,7 @@ impl State {
trait SealedInstance {
#[allow(unused)]
fn regs() -> crate::pac::adc::Adc;
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0, adc_u5)))]
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))]
#[allow(unused)]
fn common_regs() -> crate::pac::adccommon::AdcCommon;
#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
Expand Down Expand Up @@ -168,7 +166,7 @@ foreach_adc!(
crate::pac::$inst
}

#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0, adc_u5)))]
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))]
fn common_regs() -> crate::pac::adccommon::AdcCommon {
return crate::pac::$common_inst
}
Expand Down Expand Up @@ -205,12 +203,12 @@ macro_rules! impl_adc_pin {
/// Get the maximum reading value for this resolution.
///
/// This is `2**n - 1`.
#[cfg(not(any(adc_f1, adc_f3_v2, adc_u5)))]
#[cfg(not(any(adc_f1, adc_f3_v2)))]
pub const fn resolution_to_max_count(res: Resolution) -> u32 {
match res {
#[cfg(adc_v4)]
Resolution::BITS16 => (1 << 16) - 1,
#[cfg(adc_v4)]
#[cfg(any(adc_v4, adc_u5))]
Resolution::BITS14 => (1 << 14) - 1,
#[cfg(adc_v4)]
Resolution::BITS14V => (1 << 14) - 1,
Expand All @@ -224,4 +222,4 @@ pub const fn resolution_to_max_count(res: Resolution) -> u32 {
#[allow(unreachable_patterns)]
_ => core::unreachable!(),
}
}
}
45 changes: 17 additions & 28 deletions embassy-stm32/src/adc/u5.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#[allow(unused)]
use pac::adc::vals::{Difsel, Exten, Pcsel};
use pac::adccommon::vals::Presc;
use crate::peripherals::ADC4;
use pac::PWR;

use super::{
blocking_delay_us, Adc, AdcChannel, AnyAdcChannel, Instance, Resolution, RxDma, SampleTime, SealedAdcChannel
blocking_delay_us, Adc, AdcChannel, Instance, Resolution, SampleTime, SealedAdcChannel
};
use crate::time::Hertz;
use crate::{pac, rcc, Peripheral};

// TODO: not correct
const MAX_ADC_CLK_FREQ: Hertz = Hertz::mhz(55);
const VREF_CHANNEL: u8 = 19;
const TEMP_CHANNEL: u8 = 18;
const VBAT_CHANNEL: u8 = 17;

const VREF_CHANNEL: u8 = 1;
const VBAT_CHANNEL: u8 = 18;
const TEMP_CHANNEL: u8 = 19;

/// Default VREF voltage used for sample conversion to millivolts.
pub const VREF_DEFAULT_MV: u32 = 3300;
Expand Down Expand Up @@ -140,9 +140,17 @@ pub enum Averaging {
impl<'d, T: Instance> Adc<'d, T> {
/// Create a new ADC driver.
pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self {
// move to u5 init (RCC)?
PWR.svmcr().modify(|w| {
w.set_avm1en(true);
});
while !PWR.svmsr().read().vdda1rdy() {}
PWR.svmcr().modify(|w| {
w.set_asv(true);
});

embassy_hal_internal::into_ref!(adc);
rcc::enable_and_reset::<T>();

let prescaler = Prescaler::from_ker_ck(T::frequency());

T::common_regs().ccr().modify(|w| w.set_presc(prescaler.presc()));
Expand All @@ -158,26 +166,15 @@ impl<'d, T: Instance> Adc<'d, T> {
adc,
sample_time: SampleTime::from_bits(0),
};
crate::pac::RCC.ahb2enr1().modify(|w| {
w.set_adc12en(true);
});
blocking_delay_us(100);

info!("chungus {}", line!());
s.power_up();
info!("chungus {}", line!());
s.configure_differential_inputs();

info!("chungus {}", line!());
s.calibrate();
info!("chungus {}", line!());
blocking_delay_us(1);

info!("chungus {}", line!());
s.enable();
info!("chungus {}", line!());
s.configure();
info!("chungus {}", line!());

s
}
Expand All @@ -186,19 +183,11 @@ impl<'d, T: Instance> Adc<'d, T> {
T::regs().isr().modify(|reg| {
reg.set_ldordy(true);
});
info!("yummmum {}", T::regs().cr().as_ptr() as u32);
T::regs().cr().modify(|reg| {
info!("bajssis {}", reg.0);
reg.set_deeppwd(false);
info!("bajssis {}", reg.0);
reg.set_advregen(true);
info!("bajssis {}", reg.0);
});
info!("kissis {}", T::regs().as_ptr() as u32);
info!("basdsadasadjsisssss{}", T::regs().isr().as_ptr() as u32);
while !T::regs().isr().read().ldordy() {
// info!("bajsisssss{}", T::regs().isr().read().0);
};
while !T::regs().isr().read().ldordy() { };

T::regs().isr().modify(|reg| {
reg.set_ldordy(true);
Expand Down Expand Up @@ -372,4 +361,4 @@ impl<'d, T: Instance> Adc<'d, T> {
while T::regs().cr().read().adstart() {}
}
}
}
}