Extended ADC library #3

Merged
muellerr merged 2 commits from update-adc-code into main 2021-12-14 14:34:17 +01:00
3 changed files with 184 additions and 83 deletions
Showing only changes of commit 403407547d - Show all commits

View File

@ -1,6 +1,6 @@
[package] [package]
name = "vorago-reb1" name = "vorago-reb1"
version = "0.3.0" version = "0.3.1"
authors = ["Robin Mueller <robin.mueller.m@gmail.com>"] authors = ["Robin Mueller <robin.mueller.m@gmail.com>"]
edition = "2021" edition = "2021"
description = "Board Support Crate for the Vorago REB1 development board" description = "Board Support Crate for the Vorago REB1 development board"
@ -15,7 +15,7 @@ cortex-m = "0.7.3"
cortex-m-rt = "0.7.0" cortex-m-rt = "0.7.0"
embedded-hal = "0.2.6" embedded-hal = "0.2.6"
dummy-pin = "0.1.1" dummy-pin = "0.1.1"
max116xx-10bit = "0.1.0" max116xx-10bit = "0.2.1"
[dependencies.va108xx-hal] [dependencies.va108xx-hal]
version = "0.4.2" version = "0.4.2"

View File

@ -1,13 +1,13 @@
//! MAX11619 ADC example applikcation //! MAX11619 ADC example applikcation
#![no_main] #![no_main]
#![no_std] #![no_std]
use core::panic;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embedded_hal::spi; use embedded_hal::{blocking::delay::DelayUs, spi};
use max116xx_10bit::VoltageRefMode;
use max116xx_10bit::{AveragingConversions, AveragingResults};
use panic_rtt_target as _; use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print}; use rtt_target::{rprintln, rtt_init_print};
use va108xx_hal::timer::CountDownTimer;
use va108xx_hal::{ use va108xx_hal::{
gpio::PinsA, gpio::PinsA,
pac::{self, interrupt, SPIB}, pac::{self, interrupt, SPIB},
@ -16,14 +16,15 @@ use va108xx_hal::{
timer::{default_ms_irq_handler, set_up_ms_timer, Delay}, timer::{default_ms_irq_handler, set_up_ms_timer, Delay},
}; };
use vorago_reb1::max11619::{ use vorago_reb1::max11619::{
max11619_externally_clocked, max11619_internally_clocked, EocPin, AN2_CHANNEL, max11619_externally_clocked_no_wakeup, max11619_externally_clocked_with_wakeup,
POTENTIOMETER_CHANNEL, max11619_internally_clocked, EocPin, AN2_CHANNEL, POTENTIOMETER_CHANNEL,
}; };
#[derive(Debug, PartialEq, Copy, Clone)] #[derive(Debug, PartialEq, Copy, Clone)]
pub enum ExampleMode { pub enum ExampleMode {
UsingEoc, UsingEoc,
NotUsingEoc, NotUsingEoc,
NotUsingEocWithDelay,
} }
#[derive(Debug, PartialEq, Copy, Clone)] #[derive(Debug, PartialEq, Copy, Clone)]
@ -31,9 +32,10 @@ pub enum ReadMode {
Single, Single,
Multiple, Multiple,
MultipleNToHighest, MultipleNToHighest,
AverageN,
} }
const EXAMPLE_MODE: ExampleMode = ExampleMode::NotUsingEoc; const EXAMPLE_MODE: ExampleMode = ExampleMode::UsingEoc;
const READ_MODE: ReadMode = ReadMode::Multiple; const READ_MODE: ReadMode = ReadMode::Multiple;
#[entry] #[entry]
@ -89,6 +91,10 @@ fn main() -> ! {
ExampleMode::UsingEoc => { ExampleMode::UsingEoc => {
spi_example_internally_clocked(spi, delay, pinsa.pa14.into_floating_input()); spi_example_internally_clocked(spi, delay, pinsa.pa14.into_floating_input());
} }
ExampleMode::NotUsingEocWithDelay => {
let delay_us = CountDownTimer::new(&mut dp.SYSCONFIG, 50.mhz(), dp.TIM2);
spi_example_externally_clocked_with_delay(spi, delay, delay_us);
}
} }
} }
@ -97,9 +103,17 @@ fn OC0() {
default_ms_irq_handler(); default_ms_irq_handler();
} }
/// Use the SPI clock as the conversion clock
fn spi_example_externally_clocked(spi: SpiBase<SPIB>, mut delay: Delay) -> ! { fn spi_example_externally_clocked(spi: SpiBase<SPIB>, mut delay: Delay) -> ! {
let mut adc = max11619_externally_clocked(spi) let mut adc = max11619_externally_clocked_no_wakeup(spi)
.expect("Creating externally clocked MAX11619 device failed"); .expect("Creating externally clocked MAX11619 device failed");
if READ_MODE == ReadMode::AverageN {
adc.averaging(
AveragingConversions::FourConversions,
AveragingResults::FourResults,
)
.expect("Error setting up averaging register");
}
let mut cmd_buf: [u8; 32] = [0; 32]; let mut cmd_buf: [u8; 32] = [0; 32];
let mut counter = 0; let mut counter = 0;
loop { loop {
@ -108,50 +122,36 @@ fn spi_example_externally_clocked(spi: SpiBase<SPIB>, mut delay: Delay) -> ! {
match READ_MODE { match READ_MODE {
ReadMode::Single => { ReadMode::Single => {
rprintln!("Reading single potentiometer channel"); rprintln!("Reading single potentiometer channel");
let pot_val = match adc.read_single_channel(&mut cmd_buf, POTENTIOMETER_CHANNEL) { let pot_val = adc
Ok(pot_val) => pot_val, .read_single_channel(&mut cmd_buf, POTENTIOMETER_CHANNEL)
_ => { .expect("Creating externally clocked MAX11619 ADC failed");
panic!("Creating externally clocked MAX11619 ADC failed");
}
};
rprintln!("Single channel read:"); rprintln!("Single channel read:");
rprintln!("\tPotentiometer value: {}", pot_val); rprintln!("\tPotentiometer value: {}", pot_val);
} }
ReadMode::Multiple => { ReadMode::Multiple => {
let mut res_buf: [u16; 4] = [0; 4]; let mut res_buf: [u16; 4] = [0; 4];
match adc.read_multiple_channels_0_to_n( adc.read_multiple_channels_0_to_n(
&mut cmd_buf, &mut cmd_buf,
&mut res_buf.iter_mut(), &mut res_buf.iter_mut(),
POTENTIOMETER_CHANNEL, POTENTIOMETER_CHANNEL,
) { )
Ok(_) => { .expect("Multi-Channel read failed");
rprintln!("Multi channel read from 0 to 3:"); print_res_buf(&res_buf);
rprintln!("\tAN0 value: {}", res_buf[0]);
rprintln!("\tAN1 value: {}", res_buf[1]);
rprintln!("\tAN2 value: {}", res_buf[2]);
rprintln!("\tAN3 / Potentiometer value: {}", res_buf[3]);
}
_ => {
panic!("Multi-Channel read failed");
}
}
} }
ReadMode::MultipleNToHighest => { ReadMode::MultipleNToHighest => {
let mut res_buf: [u16; 2] = [0; 2]; let mut res_buf: [u16; 2] = [0; 2];
match adc.read_multiple_channels_n_to_highest( adc.read_multiple_channels_n_to_highest(
&mut cmd_buf, &mut cmd_buf,
&mut res_buf.iter_mut(), &mut res_buf.iter_mut(),
AN2_CHANNEL, AN2_CHANNEL,
) { )
Ok(_) => { .expect("Multi-Channel read failed");
rprintln!("Multi channel read from 2 to 3:"); rprintln!("Multi channel read from 2 to 3:");
rprintln!("\tAN2 value: {}", res_buf[0]); rprintln!("\tAN2 value: {}", res_buf[0]);
rprintln!("\tAN3 / Potentiometer value: {}", res_buf[1]); rprintln!("\tAN3 / Potentiometer value: {}", res_buf[1]);
} }
_ => { ReadMode::AverageN => {
panic!("Multi-Channel read failed"); rprintln!("Scanning and averaging not possible for externally clocked mode");
}
}
} }
} }
counter += 1; counter += 1;
@ -159,22 +159,122 @@ fn spi_example_externally_clocked(spi: SpiBase<SPIB>, mut delay: Delay) -> ! {
} }
} }
fn spi_example_internally_clocked(spi: SpiBase<SPIB>, mut delay: Delay, mut eoc_pin: EocPin) -> ! { fn spi_example_externally_clocked_with_delay(
let mut adc = max11619_internally_clocked(spi).expect("Creaintg MAX116xx device failed"); spi: SpiBase<SPIB>,
mut delay: Delay,
mut delay_us: impl DelayUs<u8>,
) -> ! {
let mut adc =
max11619_externally_clocked_with_wakeup(spi).expect("Creating MAX116xx device failed");
let mut cmd_buf: [u8; 32] = [0; 32];
let mut counter = 0; let mut counter = 0;
loop { loop {
rprintln!("-- Measurement {} --", counter); rprintln!("-- Measurement {} --", counter);
match adc.request_single_channel(POTENTIOMETER_CHANNEL) {
Ok(_) => (),
_ => panic!("Requesting single channel value failed"),
};
let pot_val = match nb::block!(adc.get_single_channel(&mut eoc_pin)) { match READ_MODE {
Ok(pot_val) => pot_val, ReadMode::Single => {
_ => panic!("Reading single channel value failed"), rprintln!("Reading single potentiometer channel");
}; let pot_val = adc
rprintln!("\tPotentiometer value: {}", pot_val); .read_single_channel(&mut cmd_buf, POTENTIOMETER_CHANNEL, &mut delay_us)
.expect("Creating externally clocked MAX11619 ADC failed");
rprintln!("Single channel read:");
rprintln!("\tPotentiometer value: {}", pot_val);
}
ReadMode::Multiple => {
let mut res_buf: [u16; 4] = [0; 4];
adc.read_multiple_channels_0_to_n(
&mut cmd_buf,
&mut res_buf.iter_mut(),
POTENTIOMETER_CHANNEL,
&mut delay_us,
)
.expect("Multi-Channel read failed");
print_res_buf(&res_buf);
}
ReadMode::MultipleNToHighest => {
let mut res_buf: [u16; 2] = [0; 2];
adc.read_multiple_channels_n_to_highest(
&mut cmd_buf,
&mut res_buf.iter_mut(),
AN2_CHANNEL,
&mut delay_us,
)
.expect("Multi-Channel read failed");
rprintln!("Multi channel read from 2 to 3:");
rprintln!("\tAN2 value: {}", res_buf[0]);
rprintln!("\tAN3 / Potentiometer value: {}", res_buf[1]);
}
ReadMode::AverageN => {
rprintln!("Scanning and averaging not possible for externally clocked mode");
}
}
counter += 1; counter += 1;
delay.delay_ms(500); delay.delay_ms(500);
} }
} }
/// This function uses the EOC pin to determine whether the conversion finished
fn spi_example_internally_clocked(spi: SpiBase<SPIB>, mut delay: Delay, eoc_pin: EocPin) -> ! {
let mut adc = max11619_internally_clocked(
spi,
eoc_pin,
VoltageRefMode::ExternalSingleEndedNoWakeupDelay,
)
.expect("Creating MAX116xx device failed");
let mut counter = 0;
loop {
rprintln!("-- Measurement {} --", counter);
match READ_MODE {
ReadMode::Single => {
adc.request_single_channel(POTENTIOMETER_CHANNEL)
.expect("Requesting single channel value failed");
let pot_val = nb::block!(adc.get_single_channel())
.expect("Reading single channel value failed");
rprintln!("\tPotentiometer value: {}", pot_val);
}
ReadMode::Multiple => {
adc.request_multiple_channels_0_to_n(POTENTIOMETER_CHANNEL)
.expect("Requesting single channel value failed");
let mut res_buf: [u16; 4] = [0; 4];
nb::block!(adc.get_multi_channel(&mut res_buf.iter_mut()))
.expect("Requesting multiple channel values failed");
print_res_buf(&res_buf);
}
ReadMode::MultipleNToHighest => {
adc.request_multiple_channels_n_to_highest(AN2_CHANNEL)
.expect("Requesting single channel value failed");
let mut res_buf: [u16; 4] = [0; 4];
nb::block!(adc.get_multi_channel(&mut res_buf.iter_mut()))
.expect("Requesting multiple channel values failed");
rprintln!("Multi channel read from 2 to 3:");
rprintln!("\tAN2 value: {}", res_buf[0]);
rprintln!("\tAN3 / Potentiometer value: {}", res_buf[1]);
}
ReadMode::AverageN => {
adc.request_channel_n_repeatedly(POTENTIOMETER_CHANNEL)
.expect("Reading channel multiple times failed");
let mut res_buf: [u16; 16] = [0; 16];
nb::block!(adc.get_multi_channel(&mut res_buf.iter_mut()))
.expect("Requesting multiple channel values failed");
rprintln!("Reading potentiometer 4 times");
rprintln!("\tValue 0: {}", res_buf[0]);
rprintln!("\tValue 1: {}", res_buf[1]);
rprintln!("\tValue 2: {}", res_buf[2]);
rprintln!("\tValue 3: {}", res_buf[3]);
}
}
counter += 1;
delay.delay_ms(500);
}
}
fn print_res_buf(buf: &[u16; 4]) {
rprintln!("Multi channel read from 0 to 3:");
rprintln!("\tAN0 value: {}", buf[0]);
rprintln!("\tAN1 value: {}", buf[1]);
rprintln!("\tAN2 value: {}", buf[2]);
rprintln!("\tAN3 / Potentiometer value: {}", buf[3]);
}

View File

@ -4,24 +4,20 @@
//! //!
//! - [ADC example](https://egit.irs.uni-stuttgart.de/rust/vorago-reb1/src/branch/main/examples/max11619-adc.rs) //! - [ADC example](https://egit.irs.uni-stuttgart.de/rust/vorago-reb1/src/branch/main/examples/max11619-adc.rs)
use core::convert::Infallible; use core::convert::Infallible;
use dummy_pin::DummyPin; use dummy_pin::DummyPin;
use embedded_hal::{blocking::spi::Transfer, spi::FullDuplex}; use embedded_hal::{blocking::spi::Transfer, spi::FullDuplex};
use max116xx_10bit::{ use max116xx_10bit::{
Error, ExternallyClocked, InternallyClockedInternallyTimedSerialInterface, Max11619, Error, ExternallyClocked, InternallyClockedInternallyTimedSerialInterface, Max116xx10Bit,
Max116xx10Bit, RefMode, WithoutWakeupDelay, Max116xx10BitEocExt, VoltageRefMode, WithWakeupDelay, WithoutWakeupDelay,
}; };
use va108xx_hal::gpio::{Floating, Input, Pin, PA14}; use va108xx_hal::gpio::{Floating, Input, Pin, PA14};
pub type Max11619ExternallyClocked<SPI> = pub type Max11619ExternallyClockedNoWakeup<SPI> =
Max116xx10Bit<SPI, DummyPin, Max11619, ExternallyClocked, WithoutWakeupDelay>; Max116xx10Bit<SPI, DummyPin, ExternallyClocked, WithoutWakeupDelay>;
pub type Max11619InternallyClocked<SPI> = Max116xx10Bit< pub type Max11619ExternallyClockedWithWakeup<SPI> =
SPI, Max116xx10Bit<SPI, DummyPin, ExternallyClocked, WithWakeupDelay>;
DummyPin, pub type Max11619InternallyClocked<SPI, EOC> =
Max11619, Max116xx10BitEocExt<SPI, DummyPin, EOC, InternallyClockedInternallyTimedSerialInterface>;
InternallyClockedInternallyTimedSerialInterface,
WithoutWakeupDelay,
>;
pub type EocPin = Pin<PA14, Input<Floating>>; pub type EocPin = Pin<PA14, Input<Floating>>;
pub const AN0_CHANNEL: u8 = 0; pub const AN0_CHANNEL: u8 = 0;
@ -29,37 +25,42 @@ pub const AN1_CHANNEL: u8 = 1;
pub const AN2_CHANNEL: u8 = 2; pub const AN2_CHANNEL: u8 = 2;
pub const POTENTIOMETER_CHANNEL: u8 = 3; pub const POTENTIOMETER_CHANNEL: u8 = 3;
pub fn max11619_externally_clocked<SpiE, SPI>( pub fn max11619_externally_clocked_no_wakeup<SpiE, SPI>(
spi: SPI, spi: SPI,
) -> Result<Max11619ExternallyClocked<SPI>, Error<SpiE, Infallible>> ) -> Result<Max11619ExternallyClockedNoWakeup<SPI>, Error<SpiE, Infallible>>
where where
SPI: Transfer<u8, Error = SpiE> + FullDuplex<u8, Error = SpiE>, SPI: Transfer<u8, Error = SpiE> + FullDuplex<u8, Error = SpiE>,
{ {
let adc: Max116xx10Bit<SPI, DummyPin, Max11619, ExternallyClocked, WithoutWakeupDelay> = let mut adc = Max116xx10Bit::max11619(spi, DummyPin::new_low())?;
Max116xx10Bit::new( adc.reset(false)?;
spi, adc.setup()?;
DummyPin::new_low(), Ok(adc)
RefMode::ExternalSingleEndedNoWakeupDelay, }
)?;
pub fn max11619_externally_clocked_with_wakeup<SpiE, SPI>(
spi: SPI,
) -> Result<Max11619ExternallyClockedWithWakeup<SPI>, Error<SpiE, Infallible>>
where
SPI: Transfer<u8, Error = SpiE> + FullDuplex<u8, Error = SpiE>,
{
let mut adc = Max116xx10Bit::max11619(spi, DummyPin::new_low())?
.into_ext_clkd_with_int_ref_wakeup_delay();
adc.reset(false)?;
adc.setup()?;
Ok(adc) Ok(adc)
} }
pub fn max11619_internally_clocked<SpiE, SPI>( pub fn max11619_internally_clocked<SpiE, SPI>(
spi: SPI, spi: SPI,
) -> Result<Max11619InternallyClocked<SPI>, Error<SpiE, Infallible>> eoc: EocPin,
v_ref: VoltageRefMode,
) -> Result<Max11619InternallyClocked<SPI, EocPin>, Error<SpiE, Infallible>>
where where
SPI: Transfer<u8, Error = SpiE> + FullDuplex<u8, Error = SpiE>, SPI: Transfer<u8, Error = SpiE> + FullDuplex<u8, Error = SpiE>,
{ {
let adc: Max116xx10Bit< let mut adc = Max116xx10Bit::max11619(spi, DummyPin::new_low())?
SPI, .into_int_clkd_int_timed_through_ser_if_without_wakeup(v_ref, eoc)?;
DummyPin, adc.reset(false)?;
Max11619, adc.setup()?;
InternallyClockedInternallyTimedSerialInterface,
WithoutWakeupDelay,
> = Max116xx10Bit::new(
spi,
DummyPin::new_low(),
RefMode::ExternalSingleEndedNoWakeupDelay,
)?;
Ok(adc) Ok(adc)
} }