embassy-rs / embassy

Modern embedded framework, using Rust and async.
https://embassy.dev
Apache License 2.0
5.31k stars 732 forks source link

STM32L431 hangs forever during ADC calibration #3384

Closed JanekGraff closed 1 hour ago

JanekGraff commented 2 hours ago

When initializing the ADC on the STM32L431 with embassy_stm32::adc::Adc::new(p.ADC1) it hangs forever waiting for the ADCAL bit to be cleared (see embassy_stm32/src/adc/v3.rs).

I have tested a similar configuration in a C Project generated with CubeMX to confirm that it is not a hardware problem, the generated C Code runs without any problems.

I'll investigate into this further and report back any findings.

JanekGraff commented 1 hour ago

Found the issue by investigating the clock configuration in my CubeMX project. It was indeed a clock issue.

It seems like the clock configuration defaults to Adcsel::DISABLE and the following fixes the issue:

use panic_halt as _;

use embassy_stm32::pac::rcc::vals::Adcsel;
use embassy_stm32::rcc::*;
use defmt::info;

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let mut config = embassy_stm32::Config::default();
    // This needs to be set.
    conf.rcc.mux.adcsel = Adcsel::PLL1_Q;
    p = embassy_stm32::init(config);

    // This call previously hung forever
    let mut adc = Adc::new(p.ADC1);

    loop {
        info!("Hello, World!");
        Timer::after(Duration::from_millis(500)).await;
    }
}

I'll close this and leave this here in case someone else stumbles upon this