@@ -1618,22 +1618,55 @@ fn decode_lstatus(lstatus: u8) -> Vec<&'static str> {
16181618}
16191619
16201620/// Charge temperature ranges of LifeTime Data Blocks 6-12 and ChargingStatus.
1621- /// The temperature thresholds are configured in data flash, which we can only read while unsealed.
1621+ /// The temperature thresholds are configured per pack in data flash, which we
1622+ /// can only read while unsealed. See KNOWN_TEMP_RANGES for sealed packs.
16221623const TEMP_RANGES : [ & str ; 7 ] = [ "UT" , "LT" , "STL" , "RT" , "STH" , "HT" , "OT" ] ;
16231624
1624- /// Charge temperature range thresholds from data flash (R3 layout), in C.
1625- /// Returns the thresholds in ascending order (T1, T2, T5, T6, T3, T4),
1626- /// which separate the TEMP_RANGES, and the hysteresis.
1627- fn temp_range_thresholds ( data : & BatteryData ) -> Option < ( [ f32 ; 6 ] , f32 ) > {
1628- let tr = & data. temp_ranges ;
1629- if tr. len ( ) != 14 {
1630- return None ;
1631- }
1625+ /// Charge temperature range thresholds of packs we've read them from while
1626+ /// unsealed, to show them while sealed. In data flash format (0.1K):
1627+ /// T1, T2, T5, T6, T3, T4, Hysteresis.
1628+ /// The EC's battery config only has the outer limits (T1, T4) as
1629+ /// charging_min_c and charging_max_c, so the others must come from the pack.
1630+ const KNOWN_TEMP_RANGES : & [ ( & str , & str , [ i16 ; 7 ] ) ] = & [
1631+ // FW 03.09: 0, 20, 45, 45, 52, 55C, 1C
1632+ (
1633+ "NVT" ,
1634+ "FRANDBAT01" ,
1635+ [ 2732 , 2932 , 3182 , 3182 , 3252 , 3282 , 10 ] ,
1636+ ) ,
1637+ ] ;
1638+
1639+ struct TempRanges {
1640+ /// Thresholds in C, in ascending order (T1, T2, T5, T6, T3, T4),
1641+ /// which separate the TEMP_RANGES
1642+ thresholds : [ f32 ; 6 ] ,
1643+ hysteresis : f32 ,
1644+ /// Read from the battery, rather than known for this pack
1645+ from_battery : bool ,
1646+ }
1647+
1648+ /// Charge temperature range thresholds, read from data flash (R3 layout)
1649+ /// while unsealed, or known for this pack.
1650+ fn temp_range_thresholds ( data : & BatteryData ) -> Option < TempRanges > {
1651+ let ( values, from_battery) : ( [ i16 ; 7 ] , bool ) = if data. temp_ranges . len ( ) == 14 {
1652+ let tr = & data. temp_ranges ;
1653+ (
1654+ core:: array:: from_fn ( |i| i16:: from_le_bytes ( [ tr[ i * 2 ] , tr[ i * 2 + 1 ] ] ) ) ,
1655+ true ,
1656+ )
1657+ } else {
1658+ let ( _, _, values) = KNOWN_TEMP_RANGES . iter ( ) . find ( |( manuf, device, _) | {
1659+ * manuf == data. manufacturer_name && * device == data. device_name
1660+ } ) ?;
1661+ ( * values, false )
1662+ } ;
16321663 // I2 values in 0.1K, hysteresis in 0.1K
1633- let value = |i : usize | i16:: from_le_bytes ( [ tr[ i * 2 ] , tr[ i * 2 + 1 ] ] ) as f32 / 10.0 ;
16341664 // TI's defaults use 2732 for 0C, e.g. T2 = 2852 is 12C
1635- let thresholds = core:: array:: from_fn ( |i| value ( i) - 273.2 ) ;
1636- Some ( ( thresholds, value ( 6 ) ) )
1665+ Some ( TempRanges {
1666+ thresholds : core:: array:: from_fn ( |i| values[ i] as f32 / 10.0 - 273.2 ) ,
1667+ hysteresis : values[ 6 ] as f32 / 10.0 ,
1668+ from_battery,
1669+ } )
16371670}
16381671/// RSOC ranges in each of LifeTime Data Blocks 6-12, the first one is configurable
16391672const SOC_RANGES : [ & str ; 8 ] = [
@@ -1916,8 +1949,17 @@ pub fn display_battery_data(data: &BatteryData) {
19161949 println ! ( " - {}" , flag) ;
19171950 }
19181951 }
1919- if let Some ( ( thresholds, hysteresis) ) = temp_range_thresholds ( data) {
1920- println ! ( "Charge Temperature Ranges:" ) ;
1952+ if let Some ( ranges) = temp_range_thresholds ( data) {
1953+ let TempRanges {
1954+ thresholds,
1955+ hysteresis,
1956+ from_battery,
1957+ } = ranges;
1958+ if from_battery {
1959+ println ! ( "Charge Temperature Ranges:" ) ;
1960+ } else {
1961+ println ! ( "Charge Temperature Ranges (known for this pack, not read):" ) ;
1962+ }
19211963 println ! ( " {:<4}< {:.1}C" , TEMP_RANGES [ 0 ] , thresholds[ 0 ] ) ;
19221964 for ( i, name) in TEMP_RANGES [ 1 ..6 ] . iter ( ) . enumerate ( ) {
19231965 // A range is unused if both thresholds are the same
@@ -2289,9 +2331,9 @@ pub fn display_battery_data(data: &BatteryData) {
22892331 }
22902332
22912333 if let Some ( hours) = lifetime_temp_soc_hours ( data) {
2292- // Thresholds are only known when unsealed
2334+ // Thresholds are only known when unsealed or for known packs
22932335 let labels: [ String ; 7 ] = match temp_range_thresholds ( data) {
2294- Some ( ( t, _ ) ) => core:: array:: from_fn ( |i| {
2336+ Some ( TempRanges { thresholds : t, .. } ) => core:: array:: from_fn ( |i| {
22952337 let range = match i {
22962338 0 => format ! ( "<{:.0}C" , t[ 0 ] ) ,
22972339 6 => format ! ( ">{:.0}C" , t[ 5 ] ) ,
@@ -3047,12 +3089,26 @@ mod tests {
30473089 temp_ranges : encode ( & [ 2732 , 2852 , 2932 , 2982 , 3032 , 3282 , 10 ] ) ,
30483090 ..Default :: default ( )
30493091 } ;
3050- let ( thresholds , hysteresis ) = temp_range_thresholds ( & data) . unwrap ( ) ;
3092+ let ranges = temp_range_thresholds ( & data) . unwrap ( ) ;
30513093 let expected = [ 0.0 , 12.0 , 20.0 , 25.0 , 30.0 , 55.0 ] ;
3052- for ( t, e) in thresholds. iter ( ) . zip ( expected) {
3094+ for ( t, e) in ranges. thresholds . iter ( ) . zip ( expected) {
3095+ assert ! ( ( t - e) . abs( ) < 0.01 , "{} != {}" , t, e) ;
3096+ }
3097+ assert_eq ! ( ranges. hysteresis, 1.0 ) ;
3098+ assert ! ( ranges. from_battery) ;
3099+
3100+ // Sealed, but a known pack
3101+ let data = BatteryData {
3102+ manufacturer_name : "NVT" . to_string ( ) ,
3103+ device_name : "FRANDBAT01" . to_string ( ) ,
3104+ ..Default :: default ( )
3105+ } ;
3106+ let ranges = temp_range_thresholds ( & data) . unwrap ( ) ;
3107+ let expected = [ 0.0 , 20.0 , 45.0 , 45.0 , 52.0 , 55.0 ] ;
3108+ for ( t, e) in ranges. thresholds . iter ( ) . zip ( expected) {
30533109 assert ! ( ( t - e) . abs( ) < 0.01 , "{} != {}" , t, e) ;
30543110 }
3055- assert_eq ! ( hysteresis , 1.0 ) ;
3111+ assert ! ( !ranges . from_battery ) ;
30563112
30573113 // Not read (sealed), or the 24 bytes an earlier version read from
30583114 // the R4 address, which are R3 Elevated Degrade settings
0 commit comments