From 76e30c0f7c09ab3144dcd3c890cf056804e2fe73 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 14 Sep 2019 00:46:48 +0200 Subject: [PATCH] some too detailed adc config --- firmware/src/ad7172/adc.rs | 6 ++--- firmware/src/ad7172/mod.rs | 39 ++++++++++++++++++++++++++- firmware/src/ad7172/regs.rs | 2 +- firmware/src/command_parser.rs | 43 +++++++++++++++++++++++++++++- firmware/src/main.rs | 48 +++++++++++++++++++++++++--------- 5 files changed, 120 insertions(+), 18 deletions(-) diff --git a/firmware/src/ad7172/adc.rs b/firmware/src/ad7172/adc.rs index ea1d4de..1bc7d5e 100644 --- a/firmware/src/ad7172/adc.rs +++ b/firmware/src/ad7172/adc.rs @@ -68,7 +68,7 @@ impl, NSS: OutputPin> Adc { .map(|data| data.data()) } - fn read_reg(&mut self, reg: &R) -> Result> { + pub fn read_reg(&mut self, reg: &R) -> Result> { let mut reg_data = R::Data::empty(); let address = 0x40 | reg.address(); let mut checksum = Checksum::new(self.checksum_mode); @@ -85,7 +85,7 @@ impl, NSS: OutputPin> Adc { Ok(reg_data) } - fn write_reg(&mut self, reg: &R, reg_data: &mut R::Data) -> Result<(), AdcError> { + pub fn write_reg(&mut self, reg: &R, reg_data: &mut R::Data) -> Result<(), AdcError> { let address = reg.address(); let mut checksum = Checksum::new(match self.checksum_mode { ChecksumMode::Off => ChecksumMode::Off, @@ -102,7 +102,7 @@ impl, NSS: OutputPin> Adc { Ok(()) } - fn update_reg(&mut self, reg: &R, f: F) -> Result> + pub fn update_reg(&mut self, reg: &R, f: F) -> Result> where R: Register, F: FnOnce(&mut R::Data) -> A, diff --git a/firmware/src/ad7172/mod.rs b/firmware/src/ad7172/mod.rs index da69710..59ae29e 100644 --- a/firmware/src/ad7172/mod.rs +++ b/firmware/src/ad7172/mod.rs @@ -1,4 +1,6 @@ -mod regs; +use core::fmt; + +pub mod regs; mod checksum; pub use checksum::ChecksumMode; mod adc; @@ -16,6 +18,7 @@ impl From for AdcError { } } +#[derive(Clone, Copy, Debug)] #[repr(u8)] pub enum Input { Ain0 = 0, @@ -51,6 +54,27 @@ impl From for Input { } } +impl fmt::Display for Input { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use Input::*; + + match self { + Ain0 => "ain0", + Ain1 => "ain1", + Ain2 => "ain2", + Ain3 => "ain3", + Ain4 => "ain4", + TemperaturePos => "temperature+", + TemperatureNeg => "temperature-", + AnalogSupplyPos => "analogsupply+", + AnalogSupplyNeg => "analogsupply-", + RefPos => "ref+", + RefNeg => "ref-", + _ => "", + }.fmt(fmt) + } +} + /// Reference source for ADC conversion #[repr(u8)] pub enum RefSource { @@ -74,6 +98,19 @@ impl From for RefSource { } } +impl fmt::Display for RefSource { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use RefSource::*; + + match self { + External => "external", + Internal => "internal", + Avdd1MinusAvss => "avdd1-avss", + _ => "", + }.fmt(fmt) + } +} + #[repr(u8)] pub enum PostFilter { F27SPS = 0b010, diff --git a/firmware/src/ad7172/regs.rs b/firmware/src/ad7172/regs.rs index f823452..d7c8564 100644 --- a/firmware/src/ad7172/regs.rs +++ b/firmware/src/ad7172/regs.rs @@ -40,7 +40,7 @@ macro_rules! def_reg { } }; ($Reg: ident, $index: ty, $reg: ident, $addr: expr, $size: expr) => { - struct $Reg { pub index: $index, } + pub struct $Reg { pub index: $index, } impl Register for $Reg { type Data = $reg::Data; fn address(&self) -> u8 { diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index 6db8362..9e97b25 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -30,6 +30,18 @@ enum Token { Disable, #[token = "setup"] Setup, + #[token = "ref+"] + RefPos, + #[token = "ref-"] + RefNeg, + #[token = "ain+"] + AinPos, + #[token = "ain-"] + AinNeg, + #[token = "unipolar"] + Unipolar, + #[token = "burnout"] + Burnout, #[regex = "[0-9]+"] Number, @@ -47,14 +59,24 @@ pub enum Error { #[derive(Debug)] pub enum ShowCommand { ReportMode, + Channel(u8), } - #[derive(Debug)] pub enum ChannelCommand { Enable, Disable, Setup(u8), + EnableInput(InputBuffer), + DisableInput(InputBuffer), +} + +#[derive(Clone, Copy, Debug)] +pub enum InputBuffer { + RefPos, + RefNeg, + AinPos, + AinNeg, } #[derive(Debug)] @@ -99,6 +121,19 @@ impl Command { }; } + macro_rules! channel_input { + ($channel: expr, $input: tt) => { + choice![ + End => + Ok(Command::Show(ShowCommand::Channel($channel))), + Enable => + end![Command::Channel($channel, ChannelCommand::EnableInput(InputBuffer::$input))], + Disable => + end![Command::Channel($channel, ChannelCommand::DisableInput(InputBuffer::$input))], + ] + }; + } + // Command grammar choice![ Quit => Ok(Command::Quit), @@ -120,6 +155,8 @@ impl Command { }, ]? as u8; choice![ + End => + Ok(Command::Show(ShowCommand::Channel(channel))), Enable => Ok(Command::Channel(channel, ChannelCommand::Enable)), Disable => @@ -134,6 +171,10 @@ impl Command { ]? as u8; end!(Command::Channel(channel, ChannelCommand::Setup(setup))) }, + RefPos => channel_input!(channel, RefPos), + RefNeg => channel_input!(channel, RefNeg), + AinPos => channel_input!(channel, AinPos), + AinNeg => channel_input!(channel, AinNeg), ] }, ] diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 2ac9b88..5354b00 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -231,20 +231,44 @@ fn main() -> ! { } if socket.may_recv() && socket.may_send() { - let command = socket.recv(|buf| session.feed(buf)); + let output = socket.recv(|buf| session.feed(buf)); - match command { + match output { Ok(SessionOutput::Nothing) => {} - Ok(SessionOutput::Command(Command::Quit)) => - socket.close(), - Ok(SessionOutput::Command(Command::Report(mode))) => { - let _ = writeln!(socket, "Report mode: {:?}", mode); - } - Ok(SessionOutput::Command(Command::Show(ShowCommand::ReportMode))) => { - let _ = writeln!(socket, "Report mode: {:?}", session.report_mode()); - } - Ok(SessionOutput::Command(command)) => { - let _ = writeln!(socket, "Not implemented: {:?}", command); + Ok(SessionOutput::Command(command)) => match command { + Command::Quit => + socket.close(), + Command::Report(mode) => { + let _ = writeln!(socket, "Report mode: {:?}", mode); + } + Command::Show(ShowCommand::ReportMode) => { + let _ = writeln!(socket, "Report mode: {:?}", session.report_mode()); + } + Command::Show(ShowCommand::Channel(index)) => { + let _ = writeln!(socket, "Channel {:?} configuration", index); + adc.read_reg(&ad7172::regs::Channel { index }) + .map(|data| { + let _ = writeln!(socket, "{} setup={}", + if data.enabled() { "enabled" } else { "disabled" }, + data.setup() + ); + let _ = writeln!(socket, "ain+={} ain-={}", + data.a_in_pos(), data.a_in_neg() + ); + }); + adc.read_reg(&ad7172::regs::SetupCon { index }) + .map(|data| { + let _ = writeln!(socket, "{} burnout={}, ref={}", + if data.bi_unipolar() { "bipolar" } else { "unipolar" }, + if data.burnout_en() { "enabled" } else { "disabled" }, + data.ref_sel() + ); + }); + } + command => { + // TODO: remove for exhaustion check + let _ = writeln!(socket, "Not implemented: {:?}", command); + } } Ok(SessionOutput::Error(e)) => { let _ = writeln!(socket, "Command error: {:?}", e);