Skip to content

Questions about STM32 adc driver layout. #4018

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

Open
Sandvoxel opened this issue Mar 28, 2025 · 3 comments
Open

Questions about STM32 adc driver layout. #4018

Sandvoxel opened this issue Mar 28, 2025 · 3 comments

Comments

@Sandvoxel
Copy link

Hello Embassy Team,

I have a question regarding the structure of the STM32 ADC driver. I understand that the design must accommodate specifying the pin for measurement, but I have noticed some inconsistencies compared to other drivers.

Currently, there is a trait for a single channel, AdcChannel, and an abstraction, AnyAdcChannel. It appears that AnyAdcChannel has not been updated with the new PeripheralType trait, which prevents its use in contexts such as:

voltage_sense: Peri<'a, impl AdcChannel<T>>,

This oversight limits the ability to store ADC channels in structs. While I understand that the degrade_adc function allows conversion between types, I believe it should be possible to directly use voltage_sense.into(), similar to how other peripherals operate.

Thank you for your time and assistance.

I am currently working with the relatively new adc driver for the stm32u5.

@Sandvoxel
Copy link
Author

For some more context I'm trying to make a general purpose struct for handling power buses. I'm also willing to concede that I may just be going out this the wrong way. It just feels so wrong making it store the specific peripheral instead of the generalized definition.

@i509VCB
Copy link
Member

i509VCB commented Mar 29, 2025

Looking at the docs, it appears that AnyAdcChannel implements PeripheralType: https://docs.embassy.dev/embassy-stm32/git/stm32u595ri/adc/struct.AnyAdcChannel.html#impl-PeripheralType-for-AnyAdcChannel%3CT%3E

You might need an into() to go from the concrete adc channel type to a Peri<'a, AnyAdcChannel>

@Sandvoxel
Copy link
Author

Doing this as sugested leads to a different Error as Peri Expects a trait.

impl<'a, T: Instance> VoltageMonitor<'a, T> {
    pub fn new(
        enable: Output<'a>,
        reset: Output<'a>,
        voltage_sense: Peri<'a, impl AnyAdcChannel>,
        current_sense: Peri<'a, impl AnyAdcChannel>,
    ) -> Self {

        Self {
            enable,
            reset,
            voltage_sense: voltage_sense.into(),
            current_sense: current_sense.into(),
        }
    }
}
error[E0404]: expected trait, found struct `AnyAdcChannel`
   --> src/voltage_monitor/mod.rs:20:38
    |
20  |         voltage_sense: Peri<'a, impl AnyAdcChannel>,
    |                                      ^^^^^^^^^^^^^ help: a trait with a similar name exists: `AdcChannel`
    |
   ::: /Users/kylekovalchick/Documents/projects/embassy/embassy-stm32/src/adc/mod.rs:142:1
    |
142 | pub trait AdcChannel<T>: SealedAdcChannel<T> + Sized  {
    | ---------------------------------------------------- similarly named trait `AdcChannel` defined here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants