Skip to content

Commit

Permalink
Removed temperature overflow error when reading internal device param…
Browse files Browse the repository at this point in the history
…eters
  • Loading branch information
marius-meissner committed Jul 1, 2022
1 parent 43ca9a5 commit 81bf26e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
44 changes: 22 additions & 22 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,6 @@ pub enum Error<B: Transfer<u8>, CS: OutputPin> {
/// PEC checksum of returned data was invalid
ChecksumMismatch,

/// Read temperature was to high for fitting in signed 16-Bit integer (> 431 °C)
TemperatureOverflow,

/// Writing to to the given register is not supported
ReadOnlyRegister,
}
Expand Down Expand Up @@ -429,6 +426,7 @@ pub struct InternalDeviceParameters {
pub digital_power: u32,

/// Die temperature in °C as fixed-point number
/// In case register value overflows 16-bit integer, this value is set to I16F16::MAX (32767.99998)
pub temperature: I16F16,
}

Expand Down Expand Up @@ -785,24 +783,7 @@ where
let mut parameters = Vec::new();

for device_index in 0..L {
let temp_raw = status_a[device_index][1];

if temp_raw >= 53744 {
return Err(Error::TemperatureOverflow);
}

// Constant of 276 °C, which needs to be subtracted
const TEMP_SUB: i16 = 20976;

// Check if temperature is negative
let temp_i32: i16 = if temp_raw >= TEMP_SUB as u16 {
temp_raw as i16 - TEMP_SUB
} else {
0 - TEMP_SUB + temp_raw as i16
};

// Applying factor 100 uV/7.6 mV
let temp_fixed = I16F16::from_num(temp_i32) / 76;
let temp_fixed = self.calc_temperature(status_a[device_index][1]);

let _ = parameters.push(InternalDeviceParameters {
total_voltage: status_a[device_index][0] as u32 * 30 * 100,
Expand Down Expand Up @@ -879,6 +860,26 @@ where
device_types: PhantomData,
}
}

/// Calculates the temperature in °C based on raw register value
fn calc_temperature(&self, value: u16) -> I16F16 {
if value >= 53744 {
return I16F16::MAX;
}

// Constant of 276 °C, which needs to be subtracted
const TEMP_SUB: i16 = 20976;

// Check if temperature is negative
let temp_i32: i16 = if value >= TEMP_SUB as u16 {
value as i16 - TEMP_SUB
} else {
0 - TEMP_SUB + value as i16
};

// Applying factor 100 uV/7.6 mV
I16F16::from_num(temp_i32) / 76
}
}

impl<B, CS, T, const L: usize> PollClient for LTC681X<B, CS, SDOLinePolling, T, L>
Expand Down Expand Up @@ -910,7 +911,6 @@ impl<B: Transfer<u8>, CS: OutputPin> Debug for Error<B, CS> {
Error::TransferError(_) => f.debug_struct("TransferError").finish(),
Error::CSPinError(_) => f.debug_struct("CSPinError").finish(),
Error::ChecksumMismatch => f.debug_struct("ChecksumMismatch").finish(),
Error::TemperatureOverflow => f.debug_struct("TemperatureOverflow").finish(),
Error::ReadOnlyRegister => f.debug_struct("ReadOnlyRegister").finish(),
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/tests/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1730,11 +1730,9 @@ fn test_ltc6813_read_internal_device_parameters_temp_overflow() {

let mut monitor: LTC681X<_, _, _, _, 1> = LTC681X::ltc6813(bus, get_cs_no_polling(2));

let result = monitor.read_internal_device_parameters();
match result.unwrap_err() {
Error::TemperatureOverflow => {}
_ => panic!("Unexpected error type"),
}
let result = monitor.read_internal_device_parameters().unwrap();
assert_eq!(1, result.len());
assert_eq!("32767.99998", result[0].temperature.to_string());
}

#[test]
Expand Down

0 comments on commit 81bf26e

Please sign in to comment.