Skip to content

Passing multiple ADCs as arguments to generic function using embedded-hal. #449

@noonien

Description

@noonien

I've got a generic function that requires access to multiple ADC pins, however, I can't think of a way to pass these as arguments.

Here's the code I have so far:

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use esp32c3_hal::{
    adc::{AdcConfig, Attenuation, ADC},
    clock::ClockControl,
    peripherals::Peripherals,
    prelude::*,
    timer::TimerGroup,
    Rtc, IO,
};

use esp_backtrace as _;
use esp_println::println;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let mut system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    // Disable the RTC and TIMG watchdog timers
    let mut rtc = Rtc::new(peripherals.RTC_CNTL);
    let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
    let mut wdt0 = timer_group0.wdt;
    let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
    let mut wdt1 = timer_group1.wdt;

    rtc.rwdt.disable();
    wdt0.disable();
    wdt1.disable();

    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

    let throttle_pin = io.pins.gpio0;
    let regen_pin = io.pins.gpio1;

    let analog = peripherals.APB_SARADC.split();
    let mut adc_config = AdcConfig::new();
    let throttle = adc_config.enable_pin(throttle_pin.into_analog(), Attenuation::Attenuation11dB);
    let regen = adc_config.enable_pin(regen_pin.into_analog(), Attenuation::Attenuation11dB);

    let adc = ADC::adc(
        &mut system.peripheral_clock_control,
        analog.adc1,
        adc_config,
    )
    .unwrap();

    do_io(adc, throttle, adc, regen);

    loop {}
}

use embedded_hal::adc;
use nb;

pub fn do_io<TADC, TADCI, TPIN, RADC, RADCI, RPIN>(
    mut tadc: TADC,
    mut throttle: TPIN,
    mut radc: RADC,
    mut regen: RPIN,
) where
    TADC: adc::OneShot<TADCI, u16, TPIN, Error = ()>,
    RADC: adc::OneShot<RADCI, u16, RPIN, Error = ()>,
    TPIN: adc::Channel<TADCI>,
    RPIN: adc::Channel<RADCI>,
{
    let throttle_val: u16 = nb::block!(tadc.read(&mut throttle)).unwrap();
    let regen_val: u16 = nb::block!(radc.read(&mut regen)).unwrap();

    println!("throttle = {}, regen = {}", throttle_val, regen_val);
}

I've tried .clone() on adc and on analog.adc1, none work because their respective types do not implement Clone.

Is there a way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions