Skip to content
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

ATSAME53N20A ADC question #706

Closed
ianrrees opened this issue Dec 4, 2023 · 10 comments
Closed

ATSAME53N20A ADC question #706

ianrrees opened this issue Dec 4, 2023 · 10 comments

Comments

@ianrrees
Copy link
Contributor

ianrrees commented Dec 4, 2023

Hey, i have a question about the adc. I'm trying to read an adc pin on a ATSAME53N20A.

My Test code

#[embassy_executor::task]
pub async fn mytest() {
    let mut peripherals = Peripherals::take().unwrap();
    let pins = myboard::bsp::pin::Pins::new(peripherals.PORT);
    let _core = CorePeripherals::take().unwrap();
    let mut clocks = GenericClockController::with_external_32kosc(
        peripherals.GCLK,
        &mut peripherals.MCLK,
        &mut peripherals.OSC32KCTRL,
        &mut peripherals.OSCCTRL,
        &mut peripherals.NVMCTRL,
    );

    let mut adc1 = Adc::adc1(
        peripherals.ADC1,
        &mut peripherals.MCLK,
        &mut clocks,
        GEN_A::GCLK1,
    );

    let mut hardware_info_pin = pins.hardware_info.into_alternate::<B>();

    let data: u16 = adc1.read(&mut hardware_info_pin).unwrap();
    debug_rprintln!("Value: {}", data);
}

Error

13:44:59.863 panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/atsamd-hal-0.16.0/src/thumbv7em/adc.rs:268:1:
13:44:59.863 adc clock setup failed

What could be the cause for this error? Other Tasks run as expected and reading a simple GPIO also works.

Originally posted by @pustekuchen91 in #439 (comment)

@ianrrees
Copy link
Contributor Author

ianrrees commented Dec 4, 2023

Hi @pustekuchen91, I hope it's OK that I have separated this issue from where you posted. I'm not very familiar with embassy nor the SAME53, but offhand I think it's a little suspect to use GCLK1 for the ADC as usually (always?) that will be the main core clock.

Could you please try adapting the ADC example from one of the M4 BSPs (eg Metro M4) to your board?

@ianrrees
Copy link
Contributor Author

ianrrees commented Dec 4, 2023

Whoops, off-by-one error: it's GCLK0 that generates the the main core clock, not GCLK1...

@pustekuchen91
Copy link

Thank you for splitting this up.

Yeah GCLK1 should be fine as mentiond. I'm parallel using the microchip harmony framework and configured it like this

grafik
grafik

With the harmony code it works like intended.

@pustekuchen91
Copy link

pustekuchen91 commented Dec 4, 2023

Could you please try adapting the ADC example from one of the M4 BSPs (eg Metro M4) to your board?

The above code is nearly the same as the example from the metro_m4 board. I just changed from adc0 to adc1 because i use a custom board.

Edit: Okay i found the issue. I changed the GLCK to 11 or 10 and it works. Why should GLCK1 not be allowed?

@sakian
Copy link
Contributor

sakian commented Dec 4, 2023

Is GCLK1 configured in your code? Harmony will generate the setup code for you, but you need to manually setup the clock in your rust code, I believe.

@pustekuchen91
Copy link

pustekuchen91 commented Dec 4, 2023

Is GCLK1 configured in your code? Harmony will generate the setup code for you, but you need to manually setup the clock in your rust code, I believe.

@sakian No i didnt explizit configured GCLK1 in rust. I didnt for GLCK10/11 either.

The clock is initalized in the adc module itself, which I think is not the best option. because the clock configuration should be freely configured.
For this i've opened a new Issue , this would give anyone more flexibility.

@ianrrees
Copy link
Contributor Author

ianrrees commented Dec 4, 2023

@pustekuchen91 - is it OK to close this issue, or is there more to it that is not captured by #707 ?

@pustekuchen91
Copy link

In this Issue we could take a closer look at why GLCK1 is not usable as clock.

@sakian
Copy link
Contributor

sakian commented Dec 5, 2023

It is strange. Standard v1 clocking sets up GCLK1 (code) then ADC tries to configure it as well, which panics since it's already configured. Maybe I'm missing something here, but I wonder how this ever worked with v1 clocking.

Solution, imo, is to move to v2 clocking, and move peripherals to support v2 properly and not configure their own clocks.

@pustekuchen91
Copy link

Thank you @sakian for poiting this out. That should be the reason.

GLCK5 is also intialized . I think this is not ideally for everyone.

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

3 participants