stm32-rs / stm32f3xx-hal

A Rust embedded-hal HAL for all MCUs in the STM32 F3 family
https://crates.io/crates/stm32f3xx-hal
Apache License 2.0
162 stars 67 forks source link

Issue with ADC channel/pin mapping #332

Closed jack-greenberg closed 1 year ago

jack-greenberg commented 1 year ago

I'm not 100% sure if I'm just implementing incorrectly, but I am unable to use one of the ADC channels for a pin that I believe it is supposed to work for.

Chip: STM32F303RBTx ADC: ADC4 Channel: 5 Pin: 15

Here's the code:

    // Setup clocks and such...

    // Gpio setup
    let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);

    let adc_34_common = adc::CommonAdc::new(dp.ADC3_4, &clocks, &mut rcc.ahb);

    let mut _adc3 = adc::Adc::new(
        dp.ADC3, // The ADC we are going to control
        adc::config::Config::default(),
        &clocks,
        &adc_34_common,
    ).into_oneshot();

    let mut adc4 = adc::Adc::new(
        dp.ADC4, // The ADC we are going to control
        adc::config::Config::default(),
        &clocks,
        &adc_34_common,
    ).into_oneshot();

    let mut test_pin = gpiob.pb15.into_analog(&mut gpiob.moder, &mut gpiob.pupdr);

    loop {
        let test_value: u16 = adc4.read(&mut test_pin).unwrap();

        defmt::trace!("{}", test_value as f32 * 3.3 / 4096.0);
    }
And the output: ``` error[E0277]: the trait bound `stm32f3xx_hal::gpio::Pin, Analog>: stm32f3xx_hal::embedded_hal::adc::Channel` is not satisfied --> roadkill/src/main.rs:71:46 | 71 | let pressure_before: u16 = adc4.read(&mut pressure_before_pin).unwrap(); | ---- ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `stm32f3xx_hal::embedded_hal::adc::Channel` is not implemented for `stm32f3xx_hal::gpio::Pin, Analog>` | | | required by a bound introduced by this call | = help: the following other types implement trait `stm32f3xx_hal::embedded_hal::adc::Channel`: <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> and 34 others note: required by a bound in `stm32f3xx_hal::prelude::_embedded_hal_adc_OneShot::read` --> /home/jack/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-hal-0.2.7/src/adc.rs:89:35 | 89 | pub trait OneShot> { | ^^^^^^^^^^^^ required by this bound in `stm32f3xx_hal::prelude::_embedded_hal_adc_OneShot::read` error[E0277]: the trait bound `stm32f3xx_hal::gpio::Pin, Analog>: stm32f3xx_hal::embedded_hal::adc::Channel` is not satisfied --> roadkill/src/main.rs:71:36 | 71 | let pressure_before: u16 = adc4.read(&mut pressure_before_pin).unwrap(); | ^^^^ ---- required by a bound introduced by this call | | | the trait `stm32f3xx_hal::embedded_hal::adc::Channel` is not implemented for `stm32f3xx_hal::gpio::Pin, Analog>` | = help: the following other types implement trait `stm32f3xx_hal::embedded_hal::adc::Channel`: <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> <&stm32f3xx_hal::gpio::Pin, Analog> as stm32f3xx_hal::embedded_hal::adc::Channel> and 34 others = note: required for `Adc` to implement `stm32f3xx_hal::prelude::_embedded_hal_adc_OneShot, Analog>>` For more information about this error, try `rustc --explain E0277`. warning: `roadkill` (bin "roadkill") generated 1 warning error: could not compile `roadkill` due to 2 previous errors; 1 warning emitted ```

The datasheet indicates that PB15 is connected to ADC4 Channel 5.

image

Sh3Rm4n commented 1 year ago

Thanks for reporting! :)

Hm. This does indeed seem like a missing trait implementation, so that this port is supported. I've originally generated those implementations from the bases of the stm32cubemx database. Apparently my script for that wasn't as fleshed out as I hoped to be, so I missed some channels.

See #337

Sh3Rm4n commented 1 year ago

I've released version v0.9.2, which includes the fixes. Can you report, if this fixes your problem?

https://crates.io/crates/stm32f3xx-hal/0.9.2