Stm32f1xx-hal adc cannot read data

⚓ Rust    📅 2025-09-12    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 62 days ago. The information described in this article may have changed.

this is my code

#[entry]
fn main()->! {

    let cp =cortex_m::Peripherals::take().unwrap();
    let dp = pac::Peripherals::take().unwrap();
    let mut rcc = dp.RCC.constrain();
    let mut flash=dp.FLASH.constrain();
    let clocks= rcc.cfgr.sysclk(72.MHz()).pclk2(72.MHz()).adcclk(12.MHz()).freeze(&mut flash.acr);
    let mut gpioa=dp.GPIOA.split();
    let mut led= gpioa.pa1.into_push_pull_output(&mut gpioa.crl);

    let mut myadc1 = adc::Adc::adc1(dp.ADC1, clocks);
    // myadc1.set_sample_time(SampleTime::T_239);
    // let mut gpiob=dp.GPIOB.split();
    // let mut adc_pin=gpiob.pb0.into_analog(&mut gpiob.crl);
    let mut adc_pin = gpioa.pa7.into_analog(&mut gpioa.crl);
    let mut delay = cp.SYST.delay(&clocks);

    let usart_tx=gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
    let usart_rx=gpioa.pa10.into_pull_up_input(&mut gpioa.crh);
    let mut afio=dp.AFIO.constrain();

    let serial=Serial::new(dp.USART1, (usart_tx,usart_rx),&mut afio.mapr, Config::default().baudrate(9600.bps()), &clocks);
    let (mut tx,rx)=serial.split();
    let data:u16 =myadc1.read(&mut adc_pin).unwrap_or(999_u16);
    
    loop {
        delay.delay_ms(1000_u16);
        led.toggle();
        let data:u16 =myadc1.read_vref();
        writeln!(tx,"data:{}\n ,",data).unwrap();
        // writeln!(tx,"{}",data).unwrap();
        writeln!(tx,"data:\n ,").unwrap();

    }
}

When the program reaches this point, it gets stuck

let data:u16 =myadc1.read(&mut adc_pin).unwrap_or(999_u16);

I am sure that there is no problem with my proteus simulation because I can read the ADC value normally when using C

After generating the file I used this to convert to hex file

cargo  objcopy --release --target thumbv7em-none-eabi -- -O ihex txx.hex

I'm confused as to what went wrong. :sob:

1 post - 1 participant

Read full topic

🏷️ Rust_feed