-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
910 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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,70 @@ | ||
//! Simple ADC example. | ||
#![no_main] | ||
#![no_std] | ||
|
||
use cortex_m_rt::entry; | ||
use embedded_hal::delay::DelayNs; | ||
use panic_rtt_target as _; | ||
use rtt_target::{rprintln, rtt_init_print}; | ||
use simple_examples::peb1; | ||
use va416xx_hal::{ | ||
adc::{Adc, ChannelSelect, ChannelValue, MultiChannelSelect}, | ||
pac, | ||
prelude::*, | ||
timer::CountdownTimer, | ||
}; | ||
|
||
// Quite spammy and disabled by default. | ||
const ENABLE_BUF_PRINTOUT: bool = false; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
rtt_init_print!(); | ||
rprintln!("VA416xx ADC example"); | ||
|
||
let mut dp = pac::Peripherals::take().unwrap(); | ||
// Use the external clock connected to XTAL_N. | ||
let clocks = dp | ||
.clkgen | ||
.constrain() | ||
.xtal_n_clk_with_src_freq(peb1::EXTCLK_FREQ) | ||
.freeze(&mut dp.sysconfig) | ||
.unwrap(); | ||
|
||
let adc = Adc::new_with_channel_tag(&mut dp.sysconfig, dp.adc, &clocks); | ||
let mut delay_provider = CountdownTimer::new(&mut dp.sysconfig, dp.tim0, &clocks); | ||
let mut read_buf: [ChannelValue; 8] = [ChannelValue::default(); 8]; | ||
loop { | ||
let single_value = adc | ||
.trigger_and_read_single_channel(va416xx_hal::adc::ChannelSelect::AnIn0) | ||
.expect("reading single channel value failed"); | ||
rprintln!("Read single ADC value on channel 0: {:?}", single_value); | ||
let read_num = adc | ||
.sweep_and_read_range(0, 7, &mut read_buf) | ||
.expect("ADC range read failed"); | ||
if ENABLE_BUF_PRINTOUT { | ||
rprintln!("ADC Range Read (0-8) read {} values", read_num); | ||
rprintln!("ADC Range Read (0-8): {:?}", read_buf); | ||
} | ||
assert_eq!(read_num, 8); | ||
for (idx, ch_val) in read_buf.iter().enumerate() { | ||
assert_eq!( | ||
ch_val.channel(), | ||
ChannelSelect::try_from(idx as u8).unwrap() | ||
); | ||
} | ||
|
||
adc.sweep_and_read_multiselect( | ||
MultiChannelSelect::AnIn0 | MultiChannelSelect::AnIn2 | MultiChannelSelect::TempSensor, | ||
&mut read_buf[0..3], | ||
) | ||
.expect("ADC multiselect read failed"); | ||
if ENABLE_BUF_PRINTOUT { | ||
rprintln!("ADC Multiselect Read(0, 2 and 10): {:?}", &read_buf[0..3]); | ||
} | ||
assert_eq!(read_buf[0].channel(), ChannelSelect::AnIn0); | ||
assert_eq!(read_buf[1].channel(), ChannelSelect::AnIn2); | ||
assert_eq!(read_buf[2].channel(), ChannelSelect::TempSensor); | ||
delay_provider.delay_ms(500); | ||
} | ||
} |
This file contains 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 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,78 @@ | ||
//! Simple DAC-ADC example. | ||
#![no_main] | ||
#![no_std] | ||
|
||
use cortex_m_rt::entry; | ||
use embedded_hal::delay::DelayNs; | ||
use panic_rtt_target as _; | ||
use rtt_target::{rprintln, rtt_init_print}; | ||
use simple_examples::peb1; | ||
use va416xx_hal::{adc::Adc, dac::Dac, pac, prelude::*, timer::CountdownTimer}; | ||
|
||
const DAC_INCREMENT: u16 = 256; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum AppMode { | ||
// Measurements on AIN0. | ||
AdcOnly, | ||
// AOUT0. You can use a multi-meter to measure the changing voltage on the pin. | ||
DacOnly, | ||
/// AOUT0 needs to be wired to AIN0. | ||
DacAndAdc, | ||
} | ||
|
||
const APP_MODE: AppMode = AppMode::DacAndAdc; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
rtt_init_print!(); | ||
rprintln!("VA416xx DAC/ADC example"); | ||
|
||
let mut dp = pac::Peripherals::take().unwrap(); | ||
// Use the external clock connected to XTAL_N. | ||
let clocks = dp | ||
.clkgen | ||
.constrain() | ||
.xtal_n_clk_with_src_freq(peb1::EXTCLK_FREQ) | ||
.freeze(&mut dp.sysconfig) | ||
.unwrap(); | ||
let mut dac = None; | ||
if APP_MODE == AppMode::DacOnly || APP_MODE == AppMode::DacAndAdc { | ||
dac = Some(Dac::new( | ||
&mut dp.sysconfig, | ||
dp.dac0, | ||
va416xx_hal::dac::DacSettling::Apb2Times100, | ||
&clocks, | ||
)); | ||
} | ||
let mut adc = None; | ||
if APP_MODE == AppMode::AdcOnly || APP_MODE == AppMode::DacAndAdc { | ||
adc = Some(Adc::new(&mut dp.sysconfig, dp.adc, &clocks)); | ||
} | ||
let mut delay_provider = CountdownTimer::new(&mut dp.sysconfig, dp.tim0, &clocks); | ||
let mut current_val = 0; | ||
loop { | ||
if let Some(dac) = &dac { | ||
rprintln!("loading DAC with value {}", current_val); | ||
dac.load_and_trigger_manually(current_val) | ||
.expect("loading DAC value failed"); | ||
if current_val + DAC_INCREMENT >= 4096 { | ||
current_val = 0; | ||
} else { | ||
current_val += DAC_INCREMENT; | ||
} | ||
} | ||
if let Some(dac) = &dac { | ||
// This should never block. | ||
nb::block!(dac.is_settled()).unwrap(); | ||
} | ||
if let Some(adc) = &adc { | ||
let ch_value = adc | ||
.trigger_and_read_single_channel(va416xx_hal::adc::ChannelSelect::AnIn0) | ||
.expect("reading ADC channel 0 failed"); | ||
rprintln!("Received channel value {:?}", ch_value); | ||
} | ||
|
||
delay_provider.delay_ms(500); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.