adc: read u32 for unipolar coding

softspi
Astro 2020-03-19 23:39:06 +01:00
parent 2e72a03b93
commit 723901b341
3 changed files with 7 additions and 13 deletions

View File

@ -129,7 +129,7 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
} }
/// Get data /// Get data
pub fn read_data(&mut self) -> Result<i32, AdcError<SPI::Error>> { pub fn read_data(&mut self) -> Result<u32, AdcError<SPI::Error>> {
self.read_reg(&regs::Data) self.read_reg(&regs::Data)
.map(|data| data.data()) .map(|data| data.data())
} }

View File

@ -160,16 +160,10 @@ impl if_mode::Data {
def_reg!(Data, data, 0x04, 3); def_reg!(Data, data, 0x04, 3);
impl data::Data { impl data::Data {
pub fn data(&self) -> i32 { pub fn data(&self) -> u32 {
let raw = (u32::from(self.0[0]) << 16) |
(u32::from(self.0[0]) << 16) | (u32::from(self.0[1]) << 8) |
(u32::from(self.0[1]) << 8) | u32::from(self.0[2])
u32::from(self.0[2]);
if raw & 0x80_0000 != 0 {
((raw & 0x7F_FFFF) | 0x8000_0000) as i32
} else {
raw as i32
}
} }
} }

View File

@ -3,7 +3,7 @@ use crate::{ad5680, ad7172, pid, steinhart_hart as sh};
pub struct ChannelState { pub struct ChannelState {
pub adc_data: Option<i32>, pub adc_data: Option<u32>,
pub adc_time: Instant, pub adc_time: Instant,
pub dac_value: u32, pub dac_value: u32,
pub pid_enabled: bool, pub pid_enabled: bool,
@ -26,7 +26,7 @@ impl Default for ChannelState {
impl ChannelState { impl ChannelState {
/// Update PID state on ADC input, calculate new DAC output /// 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_data = Some(adc_data);
self.adc_time = now; self.adc_time = now;