Skip to content

Commit 705805c

Browse files
Merge pull request #41 from FrameworkComputer/temp-sensor-errors
thermal: Handle temp sensor errors
2 parents 70595f7 + 3a8cd12 commit 705805c

1 file changed

Lines changed: 52 additions & 30 deletions

File tree

framework_lib/src/power.rs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloc::string::String;
44
use alloc::vec;
55
use alloc::vec::Vec;
66
use core::convert::TryInto;
7+
use core::fmt;
78
use core::prelude::v1::derive;
89
use log::Level;
910

@@ -69,6 +70,35 @@ const EC_BATT_FLAG_DISCHARGING: u8 = 0x04;
6970
const EC_BATT_FLAG_CHARGING: u8 = 0x08;
7071
const EC_BATT_FLAG_LEVEL_CRITICAL: u8 = 0x10;
7172

73+
#[derive(Debug)]
74+
enum TempSensor {
75+
Ok(u8),
76+
NotPresent,
77+
Error,
78+
NotPowered,
79+
NotCalibrated,
80+
}
81+
impl From<u8> for TempSensor {
82+
fn from(t: u8) -> Self {
83+
match t {
84+
0xFF => TempSensor::NotPresent,
85+
0xFE => TempSensor::Error,
86+
0xFD => TempSensor::NotPowered,
87+
0xFC => TempSensor::NotCalibrated,
88+
_ => TempSensor::Ok(t - 73),
89+
}
90+
}
91+
}
92+
impl fmt::Display for TempSensor {
93+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94+
if let TempSensor::Ok(t) = self {
95+
write!(f, "{} C", t)
96+
} else {
97+
write!(f, "{:?}", self)
98+
}
99+
}
100+
}
101+
72102
#[derive(Debug, Clone, PartialEq, Eq)]
73103
pub struct BatteryInformation {
74104
pub present_voltage: u32,
@@ -161,49 +191,41 @@ pub fn print_memmap_version_info(ec: &CrosEc) {
161191
let _events_ver = ec.read_memory(EC_MEMMAP_EVENTS_VERSION, 2).unwrap();
162192
}
163193

164-
fn in_c(t: u8) -> u8 {
165-
if t == 255 {
166-
t
167-
} else {
168-
t - 73
169-
}
170-
}
171-
172194
pub fn print_thermal(ec: &CrosEc) {
173195
let temps = ec.read_memory(EC_MEMMAP_TEMP_SENSOR, 0x0F).unwrap();
174196
let fans = ec.read_memory(EC_MEMMAP_FAN, 0x08).unwrap();
175197

176198
let platform = smbios::get_platform();
177199
match platform {
178200
Some(Platform::IntelGen11) | Some(Platform::IntelGen12) | Some(Platform::IntelGen13) => {
179-
println!(" F75303_Local: {:>4} C", in_c(temps[0]));
180-
println!(" F75303_CPU: {:>4} C", in_c(temps[1]));
181-
println!(" F75303_DDR: {:>4} C", in_c(temps[2]));
182-
println!(" Battery: {:>4} C", in_c(temps[3]));
183-
println!(" PECI: {:>4} C", in_c(temps[4]));
184-
println!(" F57397_VCCGT: {:>4} C", in_c(temps[5]));
201+
println!(" F75303_Local: {:>4}", TempSensor::from(temps[0]));
202+
println!(" F75303_CPU: {:>4}", TempSensor::from(temps[1]));
203+
println!(" F75303_DDR: {:>4}", TempSensor::from(temps[2]));
204+
println!(" Battery: {:>4}", TempSensor::from(temps[3]));
205+
println!(" PECI: {:>4}", TempSensor::from(temps[4]));
206+
println!(" F57397_VCCGT: {:>4}", TempSensor::from(temps[5]));
185207
}
186208
Some(Platform::Framework13Amd | Platform::Framework16) => {
187-
println!(" F75303_Local: {:>4} C", in_c(temps[0]));
188-
println!(" F75303_CPU: {:>4} C", in_c(temps[1]));
189-
println!(" F75303_DDR: {:>4} C", in_c(temps[2]));
190-
println!(" APU: {:>4} C", in_c(temps[3]));
209+
println!(" F75303_Local: {:>4}", TempSensor::from(temps[0]));
210+
println!(" F75303_CPU: {:>4}", TempSensor::from(temps[1]));
211+
println!(" F75303_DDR: {:>4}", TempSensor::from(temps[2]));
212+
println!(" APU: {:>4}", TempSensor::from(temps[3]));
191213
if matches!(platform, Some(Platform::Framework16)) {
192-
println!(" dGPU VR: {:>4} C", in_c(temps[4]));
193-
println!(" dGPU VRAM: {:>4} C", in_c(temps[5]));
194-
println!(" dGPU AMB: {:>4} C", in_c(temps[6]));
195-
println!(" dGPU temp: {:>4} C", in_c(temps[7]));
214+
println!(" dGPU VR: {:>4}", TempSensor::from(temps[4]));
215+
println!(" dGPU VRAM: {:>4}", TempSensor::from(temps[5]));
216+
println!(" dGPU AMB: {:>4}", TempSensor::from(temps[6]));
217+
println!(" dGPU temp: {:>4}", TempSensor::from(temps[7]));
196218
}
197219
}
198220
_ => {
199-
println!(" Temp 0: {:>4} C", in_c(temps[0]));
200-
println!(" Temp 1: {:>4} C", in_c(temps[1]));
201-
println!(" Temp 2: {:>4} C", in_c(temps[2]));
202-
println!(" Temp 3: {:>4} C", in_c(temps[3]));
203-
println!(" Temp 4: {:>4} C", in_c(temps[4]));
204-
println!(" Temp 5: {:>4} C", in_c(temps[5]));
205-
println!(" Temp 6: {:>4} C", in_c(temps[6]));
206-
println!(" Temp 7: {:>4} C", in_c(temps[7]));
221+
println!(" Temp 0: {:>4}", TempSensor::from(temps[0]));
222+
println!(" Temp 1: {:>4}", TempSensor::from(temps[1]));
223+
println!(" Temp 2: {:>4}", TempSensor::from(temps[2]));
224+
println!(" Temp 3: {:>4}", TempSensor::from(temps[3]));
225+
println!(" Temp 4: {:>4}", TempSensor::from(temps[4]));
226+
println!(" Temp 5: {:>4}", TempSensor::from(temps[5]));
227+
println!(" Temp 6: {:>4}", TempSensor::from(temps[6]));
228+
println!(" Temp 7: {:>4}", TempSensor::from(temps[7]));
207229
}
208230
}
209231

0 commit comments

Comments
 (0)