-
Notifications
You must be signed in to change notification settings - Fork 212
New ADC V2 with Async support #814
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
base: master
Are you sure you want to change the base?
Changes from all commits
8f3b47b
f18b0df
8791027
45151bb
77ef9a0
5b9ba80
aec6b6b
60318cd
65d30f0
0371cf2
b4a2bde
36c8701
3566693
ea1569c
0d3bfce
0c0593c
3580e53
5acd430
eaf1d2d
c7c0425
050c73a
32ac4da
967e7a7
403724e
79193bd
dc4b83d
229e704
0988b38
375e2d0
41b2c8c
af9f15e
1a88b13
132e77d
42193ae
c149b28
0be0cd6
52f15e2
af12b9a
3e603c6
ec04b58
f9991ac
066051a
7468e40
ba31cd0
5cff7ed
87775b6
b2210b7
111b91e
895e745
0da7e72
a840d6b
1178cbe
9a7f7ad
768ca17
39f1ec6
fb5a2b7
99ffb07
0a7f78b
1203d5e
b30de24
bc156ed
0306288
36ba224
751ee67
900dafb
ad6974a
5bcf403
cf3accc
e1ec823
9279035
2a07681
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ use the async APIs. | |
* DMAC | ||
* EIC (GPIO interrupts) | ||
* Timers | ||
* ADC | ||
|
||
### Examples | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,56 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atsamd_hal::adc::AdcBuilder; | ||
use feather_m0 as bsp; | ||
|
||
use bsp::hal; | ||
use bsp::pac; | ||
|
||
#[cfg(not(feature = "use_semihosting"))] | ||
use panic_halt as _; | ||
#[cfg(feature = "use_semihosting")] | ||
use panic_semihosting as _; | ||
|
||
use cortex_m_semihosting::hprintln; | ||
|
||
use bsp::hal; | ||
use bsp::pac; | ||
use feather_m0 as bsp; | ||
|
||
use bsp::entry; | ||
use hal::adc::Adc; | ||
use hal::clock::GenericClockController; | ||
use hal::prelude::*; | ||
use bsp::Pins; | ||
use pac::{CorePeripherals, Peripherals}; | ||
|
||
use hal::{ | ||
adc::{Accumulation, Adc, Prescaler, Resolution}, | ||
clock::GenericClockController, | ||
}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let mut peripherals = Peripherals::take().unwrap(); | ||
let core = CorePeripherals::take().unwrap(); | ||
let _core = CorePeripherals::take().unwrap(); | ||
|
||
let pins = Pins::new(peripherals.port); | ||
|
||
// SAMD21 targets currently don't support clock::v2 | ||
let mut clocks = GenericClockController::with_external_32kosc( | ||
peripherals.gclk, | ||
&mut peripherals.pm, | ||
&mut peripherals.sysctrl, | ||
&mut peripherals.nvmctrl, | ||
); | ||
let pins = bsp::Pins::new(peripherals.port); | ||
let mut delay = hal::delay::Delay::new(core.SYST, &mut clocks); | ||
let mut adc = Adc::adc(peripherals.adc, &mut peripherals.pm, &mut clocks); | ||
let mut a0: bsp::A0 = pins.a0.into(); | ||
let gclk0 = clocks.gclk0(); | ||
let adc_clock = clocks.adc(&gclk0).unwrap(); | ||
|
||
let mut adc = AdcBuilder::new(Accumulation::single(atsamd_hal::adc::AdcResolution::_12)) | ||
.with_clock_cycles_per_sample(5) | ||
// Overruns if clock divider < 128 in debug mode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we shouldn't re-work the example, so that it's a little more robust - eg maybe just get one sample at a time so that it is impossible to get an overrun scenario. That limitation could motivate using DMA, which I think a lot of practical ADC applications will need to use anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, DMA is my next thing to work on with the ADC generally speaking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in rnd-ash#10 |
||
.with_clock_divider(Prescaler::Div128) | ||
.with_vref(atsamd_hal::adc::Reference::Intvcc0) | ||
.enable(peripherals.adc, &mut peripherals.pm, &adc_clock) | ||
.unwrap(); | ||
let mut adc_pin = pins.a0.into_alternate(); | ||
|
||
loop { | ||
let data: u16 = adc.read(&mut a0).unwrap(); | ||
hprintln!("{}", data).ok(); | ||
delay.delay_ms(1000u16); | ||
let mut _buffer = [0; 16]; | ||
adc.read_buffer(&mut adc_pin, &mut _buffer).unwrap(); | ||
#[cfg(feature = "use_semihosting")] | ||
cortex_m_semihosting::hprintln!("Buffer: {:?}", _buffer).unwrap(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atsamd_hal::adc::AdcBuilder; | ||
use feather_m0 as bsp; | ||
|
||
use bsp::hal; | ||
use bsp::pac; | ||
|
||
#[cfg(not(feature = "use_semihosting"))] | ||
use panic_halt as _; | ||
#[cfg(feature = "use_semihosting")] | ||
use panic_semihosting as _; | ||
|
||
use bsp::Pins; | ||
use pac::{CorePeripherals, Peripherals}; | ||
|
||
use hal::{ | ||
adc::{Accumulation, Adc, Adc0, Prescaler, Resolution}, | ||
clock::GenericClockController, | ||
}; | ||
|
||
atsamd_hal::bind_interrupts!(struct Irqs { | ||
ADC => atsamd_hal::adc::InterruptHandler<Adc0>; | ||
}); | ||
|
||
#[embassy_executor::main] | ||
async fn main(_s: embassy_executor::Spawner) -> ! { | ||
let mut peripherals = Peripherals::take().unwrap(); | ||
let _core = CorePeripherals::take().unwrap(); | ||
|
||
let pins = Pins::new(peripherals.port); | ||
|
||
// SAMD21 targets currently don't support clock::v2 | ||
let mut clocks = GenericClockController::with_external_32kosc( | ||
peripherals.gclk, | ||
&mut peripherals.pm, | ||
&mut peripherals.sysctrl, | ||
&mut peripherals.nvmctrl, | ||
); | ||
let gclk0 = clocks.gclk0(); | ||
let adc_clock = clocks.adc(&gclk0).unwrap(); | ||
|
||
let mut adc = AdcBuilder::new(Accumulation::single(atsamd_hal::adc::AdcResolution::_12)) | ||
.with_clock_cycles_per_sample(5) | ||
// Overruns if clock divider < 128 in debug mode | ||
.with_clock_divider(Prescaler::Div128) | ||
.with_vref(atsamd_hal::adc::Reference::Arefa) | ||
.enable(peripherals.adc, &mut peripherals.pm, &adc_clock) | ||
.unwrap() | ||
.into_future(Irqs); | ||
|
||
let mut adc_pin = pins.a0.into_alternate(); | ||
|
||
loop { | ||
let mut _buffer = [0; 16]; | ||
adc.read_buffer(&mut adc_pin, &mut _buffer).await.unwrap(); | ||
#[cfg(feature = "use_semihosting")] | ||
cortex_m_semihosting::hprintln!("Result: {:?}", &_buffer).unwrap(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atsamd_hal::adc::AdcBuilder; | ||
use feather_m4 as bsp; | ||
|
||
use bsp::hal; | ||
use bsp::pac; | ||
|
||
#[cfg(not(feature = "use_semihosting"))] | ||
use panic_halt as _; | ||
#[cfg(feature = "use_semihosting")] | ||
use panic_semihosting as _; | ||
|
||
use bsp::entry; | ||
use bsp::Pins; | ||
use pac::{CorePeripherals, Peripherals}; | ||
|
||
use hal::{ | ||
adc::{Accumulation, Adc, Prescaler, Resolution}, | ||
clock::v2::{clock_system_at_reset, pclk::Pclk}, | ||
}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let mut peripherals = Peripherals::take().unwrap(); | ||
let _core = CorePeripherals::take().unwrap(); | ||
|
||
let pins = Pins::new(peripherals.port); | ||
|
||
let (mut buses, clocks, tokens) = clock_system_at_reset( | ||
peripherals.oscctrl, | ||
peripherals.osc32kctrl, | ||
peripherals.gclk, | ||
peripherals.mclk, | ||
&mut peripherals.nvmctrl, | ||
); | ||
|
||
// Enable the ADC0 ABP clock... | ||
let apb_adc0 = buses.apb.enable(tokens.apbs.adc0); | ||
// ...and enable the ADC0 PCLK. Both of these are required for the | ||
// ADC to run. | ||
let (pclk_adc0, _gclk0) = Pclk::enable(tokens.pclks.adc0, clocks.gclk0); | ||
|
||
let mut adc = AdcBuilder::new(Accumulation::single(atsamd_hal::adc::AdcResolution::_12)) | ||
.with_clock_cycles_per_sample(5) | ||
// Overruns if clock divider < 32 in debug mode | ||
.with_clock_divider(Prescaler::Div32) | ||
.with_vref(atsamd_hal::adc::Reference::Arefa) | ||
.enable(peripherals.adc0, apb_adc0, &pclk_adc0) | ||
.unwrap(); | ||
let mut adc_pin = pins.a0.into_alternate(); | ||
|
||
loop { | ||
let mut _buffer = [0; 16]; | ||
adc.read_buffer(&mut adc_pin, &mut _buffer).unwrap(); | ||
#[cfg(feature = "use_semihosting")] | ||
cortex_m_semihosting::hprintln!("Result: {:?}", &_buffer); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.