From 723901b3410f04f4664b818c4c3c0784bfbd40d5 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Mar 2020 23:39:06 +0100 Subject: [PATCH] adc: read u32 for unipolar coding --- src/ad7172/adc.rs | 2 +- src/ad7172/regs.rs | 14 ++++---------- src/channel_state.rs | 4 ++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/ad7172/adc.rs b/src/ad7172/adc.rs index 9430192..0f684c0 100644 --- a/src/ad7172/adc.rs +++ b/src/ad7172/adc.rs @@ -129,7 +129,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc } /// Get data - pub fn read_data(&mut self) -> Result> { + pub fn read_data(&mut self) -> Result> { self.read_reg(®s::Data) .map(|data| data.data()) } diff --git a/src/ad7172/regs.rs b/src/ad7172/regs.rs index 52c7b87..81ce87c 100644 --- a/src/ad7172/regs.rs +++ b/src/ad7172/regs.rs @@ -160,16 +160,10 @@ impl if_mode::Data { def_reg!(Data, data, 0x04, 3); impl data::Data { - pub fn data(&self) -> i32 { - let raw = - (u32::from(self.0[0]) << 16) | - (u32::from(self.0[1]) << 8) | - u32::from(self.0[2]); - if raw & 0x80_0000 != 0 { - ((raw & 0x7F_FFFF) | 0x8000_0000) as i32 - } else { - raw as i32 - } + pub fn data(&self) -> u32 { + (u32::from(self.0[0]) << 16) | + (u32::from(self.0[1]) << 8) | + u32::from(self.0[2]) } } diff --git a/src/channel_state.rs b/src/channel_state.rs index 6caa48b..75df0b2 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -3,7 +3,7 @@ use crate::{ad5680, ad7172, pid, steinhart_hart as sh}; pub struct ChannelState { - pub adc_data: Option, + pub adc_data: Option, pub adc_time: Instant, pub dac_value: u32, pub pid_enabled: bool, @@ -26,7 +26,7 @@ impl Default for ChannelState { impl ChannelState { /// Update PID state on ADC input, calculate new DAC output - pub fn update_adc(&mut self, now: Instant, adc_data: i32) { + pub fn update_adc(&mut self, now: Instant, adc_data: u32) { self.adc_data = Some(adc_data); self.adc_time = now;