rp-rs / rp-hal

A Rust Embedded-HAL for the rp series microcontrollers
https://crates.io/crates/rp2040-hal
Apache License 2.0
1.39k stars 226 forks source link

DynPin not usable with ADC #556

Closed Guelakais closed 1 year ago

Guelakais commented 1 year ago

The following code creates a problem. `#![no_std]

![no_main]

use embedded_hal::{adc::OneShot, digital::v2::ToggleableOutputPin}; use panichalt as ; use rp2040_hal; use rp2040_hal::{ adc::Adc, clocks::Clock, gpio::{DynPin, Pins}, pac, };

[link_section = ".boot2"]

[used]

pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H; const XTAL_FREQ_HZ: u32 = 12_000_000u32;

[rp2040_hal::entry]

fn main() -> ! { let mut pac = pac::Peripherals::take().unwrap(); let core = pac::CorePeripherals::take().unwrap();

let mut watchdog = rp2040_hal::Watchdog::new(pac.WATCHDOG);
let clocks = rp2040_hal::clocks::init_clocks_and_plls(
    XTAL_FREQ_HZ,
    pac.XOSC,
    pac.CLOCKS,
    pac.PLL_SYS,
    pac.PLL_USB,
    &mut pac.RESETS,
    &mut watchdog,
)
.ok()
.unwrap();
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let mut adc = Adc::new(pac.ADC, &mut pac.RESETS);
let sio = rp2040_hal::Sio::new(pac.SIO);
let pins = Pins::new(
    pac.IO_BANK0,
    pac.PADS_BANK0,
    sio.gpio_bank0,
    &mut pac.RESETS,
);
let mut analog_pins: [DynPin; 4] = [
    pins.gpio26.into(),
    pins.gpio27.into(),
    pins.gpio28.into(),
    pins.gpio29.into(),
];
loop {
    for i in 0..analog_pins.len() {
        analog_pins[i].into_floating_input();

        let pin_adc_counts: u16 = adc.read(&mut analog_pins[i]).unwrap();
        delay.delay_ms(500);
    }
}
} The Error is: error[E0277]: the trait bound DynPin: embedded_hal::adc::Channel<Adc> is not satisfied --> src/bin/adc.rs:52:48 52 let pin_adc_counts: u16 = adc.read(&mut analog_pins[i]).unwrap(); ---- ^^^^^^^^^^^^^^^^^^^ the trait embedded_hal::adc::Channel<Adc> is not implemented for DynPin
required by a bound introduced by this call

` Is there a way to get the pins back so that I can process them normally with ADC? Do I need that at all to work with analog sensors?

jannic commented 1 year ago

It looks like it's indeed not possible with the current interface.

I wrote an implementation of Channel for DynPin: https://github.com/rp-rs/rp-hal/pull/557 For now just a draft as I didn't have time to test it, and the returned error type should probably more descriptive.

If you want to try it, you can add this to your Cargo.toml:

[patch.crates-io]
rp2040-hal = { version = "0.8.0", git = "https://github.com/rp-rs/rp-hal", rev = "204b5b2683009d29bab4f9632b3864440f125d36" }