From 83117db8c51fcc4b2ca0825251f2fca37da99f67 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 2 Oct 2019 21:27:35 +0200 Subject: [PATCH] ad7172: perform data sign checks --- firmware/src/ad7172/adc.rs | 2 +- firmware/src/ad7172/regs.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/firmware/src/ad7172/adc.rs b/firmware/src/ad7172/adc.rs index 5c87683..62a4115 100644 --- a/firmware/src/ad7172/adc.rs +++ b/firmware/src/ad7172/adc.rs @@ -114,7 +114,7 @@ impl, NSS: OutputPin> 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/firmware/src/ad7172/regs.rs b/firmware/src/ad7172/regs.rs index 4a9c23e..e96a066 100644 --- a/firmware/src/ad7172/regs.rs +++ b/firmware/src/ad7172/regs.rs @@ -145,10 +145,16 @@ impl if_mode::Data { def_reg!(Data, data, 0x04, 3); impl data::Data { - pub fn data(&self) -> u32 { - (u32::from(self.0[0]) << 16) | - (u32::from(self.0[1]) << 8) | - u32::from(self.0[2]) + 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 + } } }