-
Notifications
You must be signed in to change notification settings - Fork 244
Adc implementation #23
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
238a9bd
initial adc
lperlaki 0b44436
working adc
lperlaki 1995f7e
.
lperlaki fbbc84b
revert dependencie overide
lperlaki 17592ab
initial adc
lperlaki 0ff7635
working adc
lperlaki 9da8d59
.
lperlaki 80afd1d
revert dependencie overide
lperlaki f088e33
Merge branch 'adc' of github.com:lperlaki/avr-hal into adc
lperlaki 5a67d83
fixed issues
lperlaki a4259d1
add example
lperlaki 86539b5
Update boards/arduino-uno/examples/uno-adc.rs
lperlaki 549b862
fix typo
lperlaki fe4e6d2
fix typo
lperlaki f4d4a4c
make didr generic
lperlaki c704eda
use enum instead of bools
lperlaki 1ed8ff2
remove patter binding
lperlaki 5ff24c9
change adc wrong channel poll handeling
lperlaki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
|
||
/// The division factor between the system clock frequency and the input clock to the AD converter. | ||
/// | ||
/// To get 10bit precision, clock from 50kHz to 200kHz must be supplied. If you need less precision, you can supply higher clock. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum ClockRateDivision { | ||
Factor2, | ||
Factor4, | ||
Factor8, | ||
Factor16, | ||
Factor32, | ||
Factor64, | ||
Factor128, | ||
} | ||
|
||
|
||
impl Default for ClockRateDivision { | ||
fn default() -> Self { | ||
Self::Factor128 | ||
} | ||
} | ||
|
||
|
||
/// Select the voltage reference for the ADC peripheral | ||
/// | ||
/// The inernal voltage reference options may not be used if an external reference voltage isbeing applied to the AREF pin. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum ReferenceVoltage { | ||
/// Voltage applied to AREF pin. | ||
Aref, | ||
/// Default referece voltage. | ||
lperlaki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AVcc, | ||
/// Internal reference voltage | ||
Internal, | ||
} | ||
|
||
impl Default for ReferenceVoltage { | ||
fn default() -> Self { | ||
Self::AVcc | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct AdcSettings{ | ||
pub clock_divider: ClockRateDivision, | ||
pub ref_voltage: ReferenceVoltage | ||
} | ||
|
||
|
||
#[macro_export] | ||
macro_rules! impl_adc { | ||
( | ||
pub struct $Adc:ident { | ||
type ChannelID = $ID:ty; | ||
peripheral: $ADC:ty, | ||
pins: {$($pxi:ident: ($PXi:ident, $ChannelIDExpr:expr, $ChannelIDPat:pat, $name:ident),)+} | ||
} | ||
) => { | ||
|
||
use $crate::void::Void; | ||
use $crate::hal::adc::{Channel, OneShot}; | ||
use $crate::nb; | ||
use $crate::port::mode::Analog; | ||
pub use avr_hal::adc::*; | ||
|
||
pub struct $Adc { | ||
peripheral: $ADC, | ||
$( $pxi: bool,)+ | ||
} | ||
|
||
impl $Adc { | ||
pub fn new(peripheral: $ADC, settings: AdcSettings) -> $Adc { | ||
let s = Self { peripheral, $( $pxi: false,)+ } ; | ||
s.enable(settings); | ||
s | ||
} | ||
|
||
fn enable(&self, settings: AdcSettings) { | ||
self.peripheral.adcsra.write(|w| { | ||
w.aden().set_bit(); | ||
match settings.clock_divider { | ||
ClockRateDivision::Factor2 => w.adps().prescaler_2(), | ||
ClockRateDivision::Factor4 => w.adps().prescaler_4(), | ||
ClockRateDivision::Factor8 => w.adps().prescaler_8(), | ||
ClockRateDivision::Factor16 => w.adps().prescaler_16(), | ||
ClockRateDivision::Factor32 => w.adps().prescaler_32(), | ||
ClockRateDivision::Factor64 => w.adps().prescaler_64(), | ||
ClockRateDivision::Factor128 => w.adps().prescaler_128(), | ||
}}); | ||
self.peripheral.admux.write(|w| match settings.ref_voltage { | ||
ReferenceVoltage::Aref => w.refs().aref(), | ||
ReferenceVoltage::AVcc => w.refs().avcc(), | ||
ReferenceVoltage::Internal => w.refs().internal(), | ||
}); | ||
|
||
} | ||
|
||
fn disable(&self) { | ||
self.peripheral.adcsra.reset(); | ||
} | ||
|
||
fn set_reading(&mut self, channel: $ID, state: bool) { | ||
match channel { | ||
$( $ChannelIDPat => self.$pxi = state, )+ | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
fn is_reading(&self, channel: $ID) -> bool { | ||
match channel { | ||
$( $ChannelIDPat => self.$pxi, )+ | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
pub fn release(self) -> $ADC { | ||
self.disable(); | ||
self.peripheral | ||
} | ||
} | ||
|
||
impl<WORD, PIN> OneShot<$Adc, WORD, PIN> for $Adc | ||
where | ||
WORD: From<u16>, | ||
PIN: Channel<$Adc, ID=$ID>, | ||
{ | ||
type Error = (); | ||
|
||
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> { | ||
let channel = PIN::channel(); | ||
|
||
match (self.is_reading(channel), self.peripheral.adcsra.read().adsc().bit_is_set()) { | ||
(true, true) => Err(nb::Error::WouldBlock), | ||
(true, false) => { | ||
self.set_reading(channel, false); | ||
Ok(self.peripheral.adc.read().bits().into()) | ||
}, | ||
(false, _) => { | ||
self.set_reading(channel, true); | ||
self.peripheral.admux.modify(|_, w| w.mux().variant(channel)); | ||
self.peripheral.adcsra.modify(|_, w| w.adsc().set_bit()); | ||
Err(nb::Error::WouldBlock) | ||
} | ||
} | ||
} | ||
} | ||
|
||
$( | ||
impl Channel<$Adc> for $PXi<Analog> { | ||
type ID = $ID; | ||
fn channel() -> Self::ID { | ||
$ChannelIDExpr | ||
} | ||
} | ||
|
||
impl<MODE> $PXi<MODE> { | ||
/// Make this pin a analog input and enable the internal pull-up | ||
pub fn into_analog_input(self, adc: &mut $Adc) -> $PXi<Analog> { | ||
adc.peripheral.didr0.modify(|_, w| w.$name().set_bit()); | ||
$PXi { _mode: core::marker::PhantomData } | ||
} | ||
Rahix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
)+ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate panic_halt; | ||
use arduino_uno::prelude::*; | ||
|
||
// This example opens a serial connection to the host computer. On most POSIX operating systems (like GNU/Linux or | ||
// OSX), you can interface with the program by running (assuming the device appears as ttyACM0) | ||
// | ||
// $ sudo screen /dev/ttyACM0 9600 | ||
|
||
#[no_mangle] | ||
pub extern fn main() -> ! { | ||
let dp = arduino_uno::Peripherals::take().unwrap(); | ||
|
||
let mut pins = arduino_uno::Pins::new( | ||
dp.PORTB, | ||
dp.PORTC, | ||
dp.PORTD, | ||
); | ||
|
||
let mut serial = arduino_uno::Serial::new( | ||
dp.USART0, | ||
pins.d0, | ||
pins.d1.into_output(&mut pins.ddr), | ||
9600, | ||
); | ||
|
||
|
||
// create the Analog Digital Converter | ||
let mut adc = arduino_uno::adc::Adc::new(dp.ADC, arduino_uno::adc::AdcSettings::default()); | ||
|
||
// Convert pin to Analog input | ||
let mut a0 = pins.a0.into_analog_input(&mut adc); | ||
|
||
|
||
ufmt::uwriteln!(&mut serial, "Reading Analog Input on PORT a0\r").unwrap(); | ||
|
||
loop { | ||
// Read the Analog value | ||
let aread: u16 = nb::block!{adc.read(&mut a0)}.unwrap(); | ||
|
||
// Write it to Serial | ||
ufmt::uwriteln!(&mut serial, "read: {:?}", aread); | ||
lperlaki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
extern crate avr_hal_generic as avr_hal; | ||
|
||
use crate::port::portc::{PC0, PC1, PC2, PC3, PC4, PC5}; | ||
|
||
use crate::atmega328p::adc::admux::MUX_A; | ||
|
||
avr_hal::impl_adc! { | ||
pub struct Adc { | ||
type ChannelID = MUX_A; | ||
peripheral: crate::atmega328p::ADC, | ||
pins: { | ||
pc0: (PC0, MUX_A::ADC0, MUX_A::ADC0, adc0d), | ||
pc1: (PC1, MUX_A::ADC1, MUX_A::ADC1, adc1d), | ||
pc2: (PC2, MUX_A::ADC2, MUX_A::ADC2, adc2d), | ||
pc3: (PC3, MUX_A::ADC3, MUX_A::ADC3, adc3d), | ||
pc4: (PC4, MUX_A::ADC4, MUX_A::ADC4, adc4d), | ||
pc5: (PC5, MUX_A::ADC5, MUX_A::ADC5, adc5d), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.