-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpower.rs
More file actions
1037 lines (952 loc) · 36.4 KB
/
power.rs
File metadata and controls
1037 lines (952 loc) · 36.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Get information about system power (battery, AC, PD ports)
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use core::convert::TryInto;
use core::fmt;
use core::prelude::v1::derive;
use log::Level;
use crate::ccgx::{AppVersion, Application, BaseVersion, ControllerVersion, MainPdVersions};
use crate::chromium_ec::command::EcRequestRaw;
use crate::chromium_ec::commands::*;
use crate::chromium_ec::*;
use crate::smbios;
use crate::util::{Platform, PlatformFamily};
/// Maximum length of strings in memmap
const EC_MEMMAP_TEXT_MAX: u16 = 8;
// The offset address of each type of data in mapped memory.
// TODO: Move non-power values to other modules
const EC_MEMMAP_TEMP_SENSOR: u16 = 0x00; // Temp sensors 0x00 - 0x0f
const EC_MEMMAP_FAN: u16 = 0x10; // Fan speeds 0x10 - 0x17
const _EC_MEMMAP_TEMP_SENSOR_B: u16 = 0x18; // More temp sensors 0x18 - 0x1f
const _EC_MEMMAP_ID: u16 = 0x2120; // 0x20 == 'E', 0x21 == 'C'
const EC_MEMMAP_ID_VERSION: u16 = 0x22; // Version of data in 0x20 - 0x2f
const EC_MEMMAP_THERMAL_VERSION: u16 = 0x23; // Version of data in 0x00 - 0x1f
const EC_MEMMAP_BATTERY_VERSION: u16 = 0x24; // Version of data in 0x40 - 0x7f
const EC_MEMMAP_SWITCHES_VERSION: u16 = 0x25; // Version of data in 0x30 - 0x33
const EC_MEMMAP_EVENTS_VERSION: u16 = 0x26; // Version of data in 0x34 - 0x3f
const _EC_MEMMAP_HOST_CMD_FLAGS: u16 = 0x27; // Host cmd interface flags (8 bits)
// Unused 0x28 - 0x2f
const _EC_MEMMAP_SWITCHES: u16 = 0x30; // 8 bits
// Unused 0x31 - 0x33
const _EC_MEMMAP_HOST_EVENTS: u16 = 0x34; // 64 bits
// Battery values are all 32 bits, unless otherwise noted.
const EC_MEMMAP_BATT_VOLT: u16 = 0x40; // Battery Present Voltage
const EC_MEMMAP_BATT_RATE: u16 = 0x44; // Battery Present Rate
const EC_MEMMAP_BATT_CAP: u16 = 0x48; // Battery Remaining Capacity
const EC_MEMMAP_BATT_FLAG: u16 = 0x4c; // Battery State, see below (8-bit)
const EC_MEMMAP_BATT_COUNT: u16 = 0x4d; // Battery Count (8-bit)
const EC_MEMMAP_BATT_INDEX: u16 = 0x4e; // Current Battery Data Index (8-bit)
// Unused 0x4f
const EC_MEMMAP_BATT_DCAP: u16 = 0x50; // Battery Design Capacity
const EC_MEMMAP_BATT_DVLT: u16 = 0x54; // Battery Design Voltage
const EC_MEMMAP_BATT_LFCC: u16 = 0x58; // Battery Last Full Charge Capacity
const EC_MEMMAP_BATT_CCNT: u16 = 0x5c; // Battery Cycle Count
// Strings are all 8 bytes (EC_MEMMAP_TEXT_MAX)
const EC_MEMMAP_BATT_MFGR: u16 = 0x60; // Battery Manufacturer String
const EC_MEMMAP_BATT_MODEL: u16 = 0x68; // Battery Model Number String
const EC_MEMMAP_BATT_SERIAL: u16 = 0x70; // Battery Serial Number String
const EC_MEMMAP_BATT_TYPE: u16 = 0x78; // Battery Type String
const EC_MEMMAP_ALS: u16 = 0x80; // ALS readings in lux (2 X 16 bits)
// Unused 0x84 - 0x8f
const EC_MEMMAP_ACC_STATUS: u16 = 0x90; // Accelerometer status (8 bits )
// Unused 0x91
const EC_MEMMAP_ACC_DATA: u16 = 0x92; // Accelerometers data 0x92 - 0x9f
// 0x92: u16Lid Angle if available, LID_ANGLE_UNRELIABLE otherwise
// 0x94 - 0x99: u161st Accelerometer
// 0x9a - 0x9f: u162nd Accelerometer
const LID_ANGLE_UNRELIABLE: u16 = 500;
const _EC_MEMMAP_GYRO_DATA: u16 = 0xa0; // Gyroscope data 0xa0 - 0xa5
// Unused 0xa6 - 0xdf
// Battery bit flags at EC_MEMMAP_BATT_FLAG.
const EC_BATT_FLAG_AC_PRESENT: u8 = 0x01;
const EC_BATT_FLAG_BATT_PRESENT: u8 = 0x02;
const EC_BATT_FLAG_DISCHARGING: u8 = 0x04;
const EC_BATT_FLAG_CHARGING: u8 = 0x08;
const EC_BATT_FLAG_LEVEL_CRITICAL: u8 = 0x10;
const EC_FAN_SPEED_ENTRIES: usize = 4;
/// Used on old EC firmware (before 2023)
const EC_FAN_SPEED_STALLED_DEPRECATED: u16 = 0xFFFE;
const EC_FAN_SPEED_NOT_PRESENT: u16 = 0xFFFF;
#[derive(Debug, PartialEq)]
enum TempSensor {
Ok(u8),
NotPresent,
Error,
NotPowered,
NotCalibrated,
}
impl From<u8> for TempSensor {
fn from(t: u8) -> Self {
match t {
0xFF => TempSensor::NotPresent,
0xFE => TempSensor::Error,
0xFD => TempSensor::NotPowered,
0xFC => TempSensor::NotCalibrated,
_ => TempSensor::Ok(t - 73),
}
}
}
impl fmt::Display for TempSensor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let TempSensor::Ok(t) = self {
write!(f, "{} C", t)
} else {
write!(f, "{:?}", self)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BatteryInformation {
pub present_voltage: u32,
pub present_rate: u32,
pub remaining_capacity: u32,
pub battery_count: u8,
pub current_battery_index: u8,
pub design_capacity: u32,
pub design_voltage: u32,
/// LFCC in mAH
pub last_full_charge_capacity: u32,
pub cycle_count: u32,
pub charge_percentage: u32, // Calculated based on Remaining Capacity / LFCC
pub manufacturer: String,
pub model_number: String,
pub serial_number: String,
pub battery_type: String,
// TODO: Can both charging and discharging be true/falses at the same time?
// Otherwise we can reduce them to a single flag
pub discharging: bool,
pub charging: bool,
pub level_critical: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PowerInfo {
pub ac_present: bool,
pub battery: Option<BatteryInformation>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReducedBatteryInformation {
pub cycle_count: u32,
pub charge_percentage: u32, // Calculated based on Remaining Capacity / LFCC
pub charging: bool,
}
/// Reduced version of PowerInfo
///
/// Usually you won't need all of the fields.
/// Some of them (e.g. present_voltage) will vary with a high frequency, so it's not good to
/// compare two PowerInfo structs to see whether the battery status has changed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReducedPowerInfo {
pub ac_present: bool,
pub battery: Option<ReducedBatteryInformation>,
}
impl From<PowerInfo> for ReducedPowerInfo {
fn from(val: PowerInfo) -> Self {
ReducedPowerInfo {
ac_present: val.ac_present,
battery: if let Some(b) = val.battery {
Some(ReducedBatteryInformation {
cycle_count: b.cycle_count,
charge_percentage: b.charge_percentage,
charging: b.charging,
})
} else {
None
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AccelData {
pub x: i16,
pub y: i16,
pub z: i16,
}
impl From<Vec<u8>> for AccelData {
fn from(t: Vec<u8>) -> Self {
Self {
x: i16::from_le_bytes([t[0], t[1]]),
y: i16::from_le_bytes([t[2], t[3]]),
z: i16::from_le_bytes([t[4], t[5]]),
}
}
}
impl fmt::Display for AccelData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let quarter: f32 = 0xFFFF as f32 / 4.0;
let x = (self.x as f32) / quarter;
let y = (self.y as f32) / quarter;
let z = (self.z as f32) / quarter;
write!(f, "X={:+.2}G Y={:+.2}G, Z={:+.2}G", x, y, z)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LidAngle {
Angle(u16),
Unreliable,
}
impl From<u16> for LidAngle {
fn from(a: u16) -> Self {
match a {
LID_ANGLE_UNRELIABLE => Self::Unreliable,
_ => Self::Angle(a),
}
}
}
impl fmt::Display for LidAngle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Angle(deg) => write!(f, "{}", deg),
Self::Unreliable => write!(f, "Unreliable"),
}
}
}
fn read_string(ec: &CrosEc, address: u16) -> String {
let bytes = ec.read_memory(address, EC_MEMMAP_TEXT_MAX).unwrap();
String::from_utf8_lossy(bytes.as_slice()).replace(['\0'], "")
}
fn read_u32(ec: &CrosEc, address: u16) -> u32 {
let bytes = ec.read_memory(address, 4).unwrap();
if bytes.len() != 4 {
debug_assert!(
bytes.len() == 4,
"Tried to read 4 bytes but got {}",
bytes.len()
);
error!("Unexpected length returned: {:?} instead of 4", bytes.len());
return 0;
}
u32::from_ne_bytes(bytes[0..4].try_into().unwrap())
}
pub fn print_memmap_version_info(ec: &CrosEc) {
// TODO: I don't think these are very useful
let _id_ver = ec.read_memory(EC_MEMMAP_ID_VERSION, 2).unwrap(); /* Version of data in 0x20 - 0x2f */
let _thermal_ver = ec.read_memory(EC_MEMMAP_THERMAL_VERSION, 2).unwrap(); /* Version of data in 0x00 - 0x1f */
let _battery_ver = ec.read_memory(EC_MEMMAP_BATTERY_VERSION, 2).unwrap(); /* Version of data in 0x40 - 0x7f */
let _switches_ver = ec.read_memory(EC_MEMMAP_SWITCHES_VERSION, 2).unwrap(); /* Version of data in 0x30 - 0x33 */
let _events_ver = ec.read_memory(EC_MEMMAP_EVENTS_VERSION, 2).unwrap();
}
/// Not supported on TGL EC
pub fn get_als_reading(ec: &CrosEc, index: usize) -> Option<u32> {
let als = ec.read_memory(EC_MEMMAP_ALS, 0x04)?;
let offset = index + 4 * index;
Some(u32::from_le_bytes([
als[offset],
als[1 + offset],
als[2 + offset],
als[3 + offset],
]))
}
pub fn get_accel_data(ec: &CrosEc) -> (AccelData, AccelData, LidAngle) {
// bit 4 = busy
// bit 7 = present
// #define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK 0x0f
let _acc_status = ec.read_memory(EC_MEMMAP_ACC_STATUS, 0x01).unwrap()[0];
// While busy, keep reading
let lid_angle = ec.read_memory(EC_MEMMAP_ACC_DATA, 0x02).unwrap();
let lid_angle = u16::from_le_bytes([lid_angle[0], lid_angle[1]]);
let accel_1 = ec.read_memory(EC_MEMMAP_ACC_DATA + 2, 0x06).unwrap();
let accel_2 = ec.read_memory(EC_MEMMAP_ACC_DATA + 8, 0x06).unwrap();
// TODO: Make sure we got a new sample
// println!(" Status Bit: {} 0x{:X}", acc_status, acc_status);
// println!(" Present: {}", (acc_status & 0x80) > 0);
// println!(" Busy: {}", (acc_status & 0x8) > 0);
(
AccelData::from(accel_1),
AccelData::from(accel_2),
LidAngle::from(lid_angle),
)
}
pub fn print_sensors(ec: &CrosEc) {
let mut has_als = false;
let mut accel_locations = vec![];
match ec.motionsense_sensor_info() {
Ok(sensors) => {
info!("Sensors: {}", sensors.len());
for sensor in sensors {
info!(" Type: {:?}", sensor.sensor_type);
info!(" Location: {:?}", sensor.location);
info!(" Chip: {:?}", sensor.chip);
if sensor.sensor_type == MotionSenseType::Light {
has_als = true;
}
if sensor.sensor_type == MotionSenseType::Accel {
accel_locations.push(sensor.location);
}
}
}
Err(EcError::Response(EcResponseStatus::InvalidCommand)) => {
debug!("Motionsense commands not supported")
}
err => _ = print_err(err),
}
// If we can't detect it based on motionsense, check the system family
// If family is unknown, assume it has
let als_family = matches!(
smbios::get_family(),
Some(PlatformFamily::Framework13) | Some(PlatformFamily::Framework16) | None
);
if has_als || als_family {
let als_int = get_als_reading(ec, 0).unwrap();
println!("ALS: {:>4} Lux", als_int);
}
// bit 4 = busy
// bit 7 = present
// #define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK 0x0f
let acc_status = ec.read_memory(EC_MEMMAP_ACC_STATUS, 0x01).unwrap()[0];
// While busy, keep reading
let lid_angle = ec.read_memory(EC_MEMMAP_ACC_DATA, 0x02).unwrap();
let lid_angle = u16::from_le_bytes([lid_angle[0], lid_angle[1]]);
let accel_1 = ec.read_memory(EC_MEMMAP_ACC_DATA + 2, 0x06).unwrap();
let accel_2 = ec.read_memory(EC_MEMMAP_ACC_DATA + 8, 0x06).unwrap();
let present = (acc_status & 0x80) > 0;
if present {
println!("Accelerometers:");
debug!(" Status Bit: {} 0x{:X}", acc_status, acc_status);
debug!(" Present: {}", present);
debug!(" Busy: {}", (acc_status & 0x8) > 0);
print!(" Lid Angle: ");
if lid_angle == LID_ANGLE_UNRELIABLE {
println!("Unreliable");
} else {
println!("{} Deg", lid_angle);
}
println!(
" {:<12} {}",
format!("{:?} Sensor:", accel_locations[0]),
AccelData::from(accel_1)
);
println!(
" {:<12} {}",
format!("{:?} Sensor:", accel_locations[1]),
AccelData::from(accel_2)
);
}
}
pub fn print_thermal(ec: &CrosEc) {
let temps = ec.read_memory(EC_MEMMAP_TEMP_SENSOR, 0x0F).unwrap();
let fans = ec.read_memory(EC_MEMMAP_FAN, 0x08).unwrap();
let platform = smbios::get_platform();
let family = smbios::get_family();
let remaining_sensors = match platform {
Some(Platform::IntelGen11) | Some(Platform::IntelGen12) | Some(Platform::IntelGen13) => {
println!(" F75303_Local: {:>4}", TempSensor::from(temps[0]));
println!(" F75303_CPU: {:>4}", TempSensor::from(temps[1]));
println!(" F75303_DDR: {:>4}", TempSensor::from(temps[2]));
println!(" Battery: {:>4}", TempSensor::from(temps[3]));
println!(" PECI: {:>4}", TempSensor::from(temps[4]));
if matches!(
platform,
Some(Platform::IntelGen12) | Some(Platform::IntelGen13)
) {
println!(" F57397_VCCGT: {:>4}", TempSensor::from(temps[5]));
}
2
}
Some(Platform::IntelCoreUltra1) => {
println!(" F75303_Local: {:>4}", TempSensor::from(temps[0]));
println!(" F75303_CPU: {:>4}", TempSensor::from(temps[1]));
println!(" Battery: {:>4}", TempSensor::from(temps[2]));
println!(" F75303_DDR: {:>4}", TempSensor::from(temps[3]));
println!(" PECI: {:>4}", TempSensor::from(temps[4]));
3
}
Some(Platform::Framework12IntelGen13) => {
println!(" F75303_CPU: {:>4}", TempSensor::from(temps[0]));
println!(" F75303_Skin: {:>4}", TempSensor::from(temps[1]));
println!(" F75303_Local: {:>4}", TempSensor::from(temps[2]));
println!(" Battery: {:>4}", TempSensor::from(temps[3]));
println!(" PECI: {:>4}", TempSensor::from(temps[4]));
println!(" Charger IC {:>4}", TempSensor::from(temps[5]));
2
}
Some(
Platform::Framework13Amd7080
| Platform::Framework13AmdAi300
| Platform::Framework16Amd7080
| Platform::Framework16AmdAi300,
) => {
println!(" F75303_Local: {:>4}", TempSensor::from(temps[0]));
println!(" F75303_CPU: {:>4}", TempSensor::from(temps[1]));
println!(" F75303_DDR: {:>4}", TempSensor::from(temps[2]));
println!(" APU: {:>4}", TempSensor::from(temps[3]));
if family == Some(PlatformFamily::Framework16) {
println!(" dGPU VR: {:>4}", TempSensor::from(temps[4]));
println!(" dGPU VRAM: {:>4}", TempSensor::from(temps[5]));
println!(" dGPU AMB: {:>4}", TempSensor::from(temps[6]));
println!(" dGPU temp: {:>4}", TempSensor::from(temps[7]));
0
} else {
4
}
}
Some(Platform::FrameworkDesktopAmdAiMax300) => {
println!(" F75303_APU: {:>4}", TempSensor::from(temps[0]));
println!(" F75303_DDR: {:>4}", TempSensor::from(temps[1]));
println!(" F75303_AMB: {:>4}", TempSensor::from(temps[2]));
println!(" APU: {:>4}", TempSensor::from(temps[3]));
4
}
_ => {
println!(" Temp 0: {:>4}", TempSensor::from(temps[0]));
println!(" Temp 1: {:>4}", TempSensor::from(temps[1]));
println!(" Temp 2: {:>4}", TempSensor::from(temps[2]));
println!(" Temp 3: {:>4}", TempSensor::from(temps[3]));
println!(" Temp 4: {:>4}", TempSensor::from(temps[4]));
println!(" Temp 5: {:>4}", TempSensor::from(temps[5]));
println!(" Temp 6: {:>4}", TempSensor::from(temps[6]));
println!(" Temp 7: {:>4}", TempSensor::from(temps[7]));
0
}
};
// Just in case EC has more sensors than we know about, print them
for (i, temp) in temps.iter().enumerate().take(8).skip(8 - remaining_sensors) {
let temp = TempSensor::from(*temp);
if temp != TempSensor::NotPresent {
println!(" Temp {}: {:>4}", i, temp);
}
}
for i in 0..EC_FAN_SPEED_ENTRIES {
let fan = u16::from_le_bytes([fans[i * 2], fans[1 + i * 2]]);
if fan == EC_FAN_SPEED_STALLED_DEPRECATED {
println!(" Fan Speed: {:>4} RPM (Stalled)", fan);
} else if fan == EC_FAN_SPEED_NOT_PRESENT {
info!(" Fan Speed: Not present");
} else {
println!(" Fan Speed: {:>4} RPM", fan);
}
}
}
pub fn get_fan_num(ec: &CrosEc) -> EcResult<usize> {
let fans = ec.read_memory(EC_MEMMAP_FAN, 0x08).unwrap();
let mut count = 0;
for i in 0..EC_FAN_SPEED_ENTRIES {
let fan = u16::from_le_bytes([fans[i * 2], fans[1 + i * 2]]);
if fan == EC_FAN_SPEED_NOT_PRESENT {
continue;
}
count += 1;
}
Ok(count)
}
// TODO: Use Result
pub fn power_info(ec: &CrosEc) -> Option<PowerInfo> {
let battery_flag = ec.read_memory(EC_MEMMAP_BATT_FLAG, 1)?[0];
debug!("AC/Battery flag: {:#X}", battery_flag);
let battery_lfcc = read_u32(ec, EC_MEMMAP_BATT_LFCC);
let battery_cap = read_u32(ec, EC_MEMMAP_BATT_CAP);
let present_voltage = read_u32(ec, EC_MEMMAP_BATT_VOLT);
let present_rate = read_u32(ec, EC_MEMMAP_BATT_RATE);
let _remaining_capacity = read_u32(ec, EC_MEMMAP_BATT_CAP); // TODO: Why didn't I use this?
let battery_count = ec.read_memory(EC_MEMMAP_BATT_COUNT, 1).unwrap()[0]; // 8 bit
let current_battery_index = ec.read_memory(EC_MEMMAP_BATT_INDEX, 1).unwrap()[0]; // 8 bit
let design_capacity = read_u32(ec, EC_MEMMAP_BATT_DCAP);
let design_voltage = read_u32(ec, EC_MEMMAP_BATT_DVLT);
let cycle_count = read_u32(ec, EC_MEMMAP_BATT_CCNT);
let manufacturer = read_string(ec, EC_MEMMAP_BATT_MFGR);
let model_number = read_string(ec, EC_MEMMAP_BATT_MODEL);
let serial_number = read_string(ec, EC_MEMMAP_BATT_SERIAL);
let battery_type = read_string(ec, EC_MEMMAP_BATT_TYPE);
Some(PowerInfo {
ac_present: 0 != (battery_flag & EC_BATT_FLAG_AC_PRESENT),
battery: if 0 != (battery_flag & EC_BATT_FLAG_BATT_PRESENT) {
Some(BatteryInformation {
// TODO: Add some more information
present_voltage,
present_rate,
remaining_capacity: battery_cap,
battery_count,
current_battery_index,
design_capacity,
design_voltage,
last_full_charge_capacity: battery_lfcc,
cycle_count,
charge_percentage: (100 * battery_cap) / battery_lfcc,
manufacturer,
model_number,
serial_number,
battery_type,
// TODO: Can both be true/falses at the same time?
discharging: 0 != (battery_flag & EC_BATT_FLAG_DISCHARGING),
charging: 0 != (battery_flag & EC_BATT_FLAG_CHARGING),
level_critical: 0 != (battery_flag & EC_BATT_FLAG_LEVEL_CRITICAL),
})
} else {
None
},
})
}
// When no battery is present and we're running on AC
pub fn is_standalone(ec: &CrosEc) -> bool {
if let Some(info) = power_info(ec) {
debug_assert!(
info.battery.is_some() || info.ac_present,
"If there's no battery, we must be running off AC"
);
info.battery.is_none()
} else {
true // Safe default
}
}
pub fn get_and_print_power_info(ec: &CrosEc) -> i32 {
if let Some(power_info) = power_info(ec) {
print_err_ref(&ec.get_charge_state(&power_info));
print_battery_information(&power_info);
if let Some(_battery) = &power_info.battery {
return 0;
}
}
1
}
fn print_battery_information(power_info: &PowerInfo) {
println!("Battery Status");
print!(" AC is: ");
if power_info.ac_present {
println!("connected");
} else {
println!("not connected");
}
print!(" Battery is: ");
if let Some(battery) = &power_info.battery {
println!("connected");
println!(
" Battery LFCC: {:#?} mAh (Last Full Charge Capacity)",
battery.last_full_charge_capacity
);
println!(" Battery Capacity: {} mAh", battery.remaining_capacity);
let wah = battery.remaining_capacity * battery.present_voltage / 1000;
println!(" {}.{:2} Wh", wah / 1000, wah % 1000);
println!(" Charge level: {:?}%", battery.charge_percentage);
if log_enabled!(Level::Info) {
println!(" Manufacturer: {}", battery.manufacturer);
println!(" Model Number: {}", battery.model_number);
println!(" Serial Number: {}", battery.serial_number);
println!(" Battery Type: {}", battery.battery_type);
println!(
" Present Voltage: {}.{} V",
battery.present_voltage / 1000,
battery.present_voltage % 1000
);
println!(" Present Rate: {} mA", battery.present_rate);
// We only have a single battery in all our systems
// Both values are always 0
// println!(" Battery Count: {}", battery.battery_count);
// println!(" Current Battery#: {}", battery.current_battery_index);
println!(" Design Capacity: {} mAh", battery.design_capacity);
let design_wah = battery.design_capacity * battery.design_voltage / 1000;
println!(
" {}.{} Wh",
design_wah / 1000,
design_wah % 1000
);
println!(
" Design Voltage: {}.{} V",
battery.design_voltage / 1000,
battery.design_voltage % 1000
);
println!(" Cycle Count: {}", battery.cycle_count);
}
if battery.discharging {
println!(" Battery discharging");
}
if battery.charging {
println!(" Battery charging");
}
if battery.level_critical {
println!(" Battery level CRITICAL!");
}
} else {
println!("not connected");
}
}
pub fn check_update_ready(power_info: &PowerInfo) -> bool {
// Checking if battery/AC conditions are enough for FW update
// Either standalone mode or AC+20% charge
match &power_info.battery {
None => true,
Some(battery) if power_info.ac_present && battery.charge_percentage > 20 => true,
Some(battery) => {
println!("Please plug in AC. If the battery is connected, charge it to at least 20% before proceeding.");
println!("Current charge is: {}%", battery.charge_percentage);
false
}
}
}
#[derive(Debug, PartialEq)]
pub enum UsbChargingType {
None = 0,
PD = 1,
TypeC = 2,
Proprietary = 3,
Bc12Dcp = 4,
Bc12Cdp = 5,
Bc12Sdp = 6,
Other = 7,
VBus = 8,
Unknown = 9,
}
#[derive(Debug, PartialEq)]
pub enum UsbPowerRoles {
Disconnected = 0,
Source = 1,
Sink = 2,
SinkNotCharging = 3,
}
pub struct UsbChargeMeasures {
pub voltage_max: u16,
pub voltage_now: u16,
pub current_max: u16,
pub current_lim: u16,
}
pub struct UsbPdPowerInfo {
pub role: UsbPowerRoles,
pub charging_type: UsbChargingType,
pub dualrole: bool,
pub meas: UsbChargeMeasures,
pub max_power: u32,
}
fn check_ac(ec: &CrosEc, port: u8) -> EcResult<UsbPdPowerInfo> {
// port=0 or port=1 to check right
// port=2 or port=3 to check left
// If dest returns 0x2 that means it's powered
let info = EcRequestUsbPdPowerInfo { port }.send_command(ec)?;
Ok(UsbPdPowerInfo {
role: match info.role {
0 => UsbPowerRoles::Disconnected,
1 => UsbPowerRoles::Source,
2 => UsbPowerRoles::Sink,
3 => UsbPowerRoles::SinkNotCharging,
_ => {
debug_assert!(false, "Unknown Role!!");
UsbPowerRoles::Disconnected
}
},
charging_type: match info.charging_type {
0 => UsbChargingType::None,
1 => UsbChargingType::PD,
2 => UsbChargingType::TypeC,
3 => UsbChargingType::Proprietary,
4 => UsbChargingType::Bc12Dcp,
5 => UsbChargingType::Bc12Cdp,
6 => UsbChargingType::Bc12Sdp,
7 => UsbChargingType::Other,
8 => UsbChargingType::VBus,
9 => UsbChargingType::Unknown,
_ => {
debug_assert!(false, "Unknown Role!!");
UsbChargingType::Unknown
}
},
dualrole: info.dualrole != 0,
meas: UsbChargeMeasures {
voltage_max: info.meas.voltage_max,
voltage_now: info.meas.voltage_now,
current_lim: info.meas.current_lim,
current_max: info.meas.current_max,
},
max_power: info.max_power,
})
}
pub fn get_pd_info(ec: &CrosEc, ports: u8) -> Vec<EcResult<UsbPdPowerInfo>> {
// 4 ports on our current laptops
let mut info = vec![];
for port in 0..ports {
info.push(check_ac(ec, port));
}
info
}
#[derive(Debug)]
enum CypdTypeCState {
Nothing,
Sink,
Source,
Debug,
Audio,
PoweredAccessory,
Unsupported,
Invalid,
}
impl From<u8> for CypdTypeCState {
fn from(v: u8) -> Self {
match v {
0 => CypdTypeCState::Nothing,
1 => CypdTypeCState::Sink,
2 => CypdTypeCState::Source,
3 => CypdTypeCState::Debug,
4 => CypdTypeCState::Audio,
5 => CypdTypeCState::PoweredAccessory,
6 => CypdTypeCState::Unsupported,
_ => CypdTypeCState::Invalid,
}
}
}
#[derive(Debug, PartialEq)]
enum CypdPdPowerRole {
Sink,
Source,
Unknown,
}
impl From<u8> for CypdPdPowerRole {
fn from(v: u8) -> Self {
match v {
0 => CypdPdPowerRole::Sink,
1 => CypdPdPowerRole::Source,
_ => CypdPdPowerRole::Unknown,
}
}
}
#[derive(Debug)]
enum CypdPdDataRole {
Ufp,
Dfp,
Disconnected,
Unknown,
}
impl From<u8> for CypdPdDataRole {
fn from(v: u8) -> Self {
match v {
0 => CypdPdDataRole::Ufp,
1 => CypdPdDataRole::Dfp,
2 => CypdPdDataRole::Disconnected,
_ => CypdPdDataRole::Unknown,
}
}
}
pub fn get_and_print_cypd_pd_info(ec: &CrosEc) {
let ports = 4u8;
for port in 0..ports {
println!("USB-C Port {}:", port);
let result = EcRequestGetPdPortState { port }.send_command(ec);
match result {
Ok(info) => {
let c_state = CypdTypeCState::from(info.c_state);
let connected = !matches!(c_state, CypdTypeCState::Nothing);
let power_role = CypdPdPowerRole::from(info.power_role);
let data_role = CypdPdDataRole::from(info.data_role);
let voltage = { info.voltage };
let current = { info.current };
let watts_mw = voltage as u32 * current as u32 / 1000;
let has_pd_contract = info.pd_state != 0;
println!(
" PD Contract: {}",
if info.pd_state != 0 { "Yes" } else { "No" }
);
println!(" Power Role: {:?}", power_role);
println!(" Data Role: {:?}", data_role);
if connected {
println!(
" VCONN: {}",
if info.vconn != 0 { "On" } else { "Off" }
);
println!(
" Negotiated: {}.{:03} V, {} mA, {}.{} W",
voltage / 1000,
voltage % 1000,
current,
watts_mw / 1000,
watts_mw % 1000,
);
println!(
" CC Polarity: {}",
match info.cc_polarity {
0 => "CC1",
1 => "CC2",
2 => "CC1 (Debug)",
3 => "CC2 (Debug)",
_ => "Unknown",
}
);
}
if has_pd_contract {
println!(" Port Partner: {:?}", c_state);
println!(
" EPR: {}{}",
if info.epr_active != 0 {
"Active"
} else {
"Inactive"
},
if info.epr_support != 0 {
" (Supported)"
} else {
""
}
);
if power_role == CypdPdPowerRole::Sink {
println!(
" Sink Active: {}",
if info.active_port != 0 { "Yes" } else { "No" }
);
}
}
let alt = info.pd_alt_mode_status;
// Bits 0-1 indicate DP alt mode is active (bit 0 = DFP_D/TBT,
// bit 1 = UFP_D). Only show when actually in DP alt mode.
if connected && (alt & 0x03) != 0 {
let mut modes = vec![];
if alt & 0x01 != 0 {
modes.push("DFP_D Connected");
}
if alt & 0x02 != 0 {
modes.push("UFP_D Connected");
}
if alt & 0x04 != 0 {
modes.push("Power Low");
}
if alt & 0x08 != 0 {
modes.push("Enabled");
}
if alt & 0x10 != 0 {
modes.push("Multi-Function");
}
if alt & 0x20 != 0 {
modes.push("USB Config");
}
if alt & 0x40 != 0 {
modes.push("Exit Request");
}
if alt & 0x80 != 0 {
modes.push("HPD High");
}
println!(" DP Alt Mode: {} (0x{:02X})", modes.join(", "), alt);
}
}
Err(e) => {
print_err::<()>(Err(e));
}
}
}
}
pub fn get_and_print_pd_info(ec: &CrosEc) {
let fl16 = Some(PlatformFamily::Framework16) == smbios::get_family();
let ports = 4; // All our platforms have 4 PD ports so far
let infos = get_pd_info(ec, ports);
for (port, info) in infos.iter().enumerate().take(ports.into()) {
println!(
"USB-C Port {} ({}):",
port,
match port {
0 => "Right Back",
1 =>
if fl16 {
"Right Middle"
} else {
"Right Front"
},
2 =>
if fl16 {
"Left Middle"
} else {
"Left Front"
},
3 => "Left Back",
_ => "??",
}
);
print_err_ref(info);
// TODO: I haven't checked the encoding/endianness of these numbers. They're likely incorrectly decoded
if let Ok(info) = info {
println!(" Role: {:?}", info.role);
println!(" Charging Type: {:?}", info.charging_type);
let volt_max = { info.meas.voltage_max };
let volt_now = { info.meas.voltage_now };
println!(
" Voltage Now: {}.{} V, Max: {}.{} V",
volt_now / 1000,
volt_now % 1000,
volt_max / 1000,
volt_max % 1000,
);
let cur_lim = { info.meas.current_lim };
let cur_max = { info.meas.current_max };
println!(" Current Lim: {} mA, Max: {} mA", cur_lim, cur_max);
println!(
" Dual Role: {}",
if info.dualrole { "DRP" } else { "Charger" }
);
let max_power_mw = { info.max_power } / 1000;
println!(
" Max Power: {}.{} W",
max_power_mw / 1000,
max_power_mw % 1000
);
} else {
println!(" Role: Unknown");
println!(" Charging Type: Unknown");
println!(" Voltage Max: Unknown, Now: Unknown");
println!(" Current Max: Unknown, Lim: Unknown");
println!(" Dual Role: Unknown");
println!(" Max Power: Unknown");
}
}
}
// TODO: Improve return type to be more obvious
// (right, left)
pub fn is_charging(ec: &CrosEc) -> EcResult<(bool, bool)> {
let port0 = check_ac(ec, 0)?.role == UsbPowerRoles::Sink;
let port1 = check_ac(ec, 1)?.role == UsbPowerRoles::Sink;
let port2 = check_ac(ec, 2)?.role == UsbPowerRoles::Sink;
let port3 = check_ac(ec, 3)?.role == UsbPowerRoles::Sink;
Ok((port0 || port1, port2 || port3))
}
fn parse_pd_ver_slice(data: &[u8]) -> ControllerVersion {
parse_pd_ver(&[
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
])
}
fn parse_pd_ver(data: &[u8; 8]) -> ControllerVersion {
ControllerVersion {
base: BaseVersion {
major: (data[3] >> 4) & 0xF,
minor: (data[3]) & 0xF,
patch: data[2],
build_number: u16::from_le_bytes([data[0], data[1]]),
},
app: AppVersion {
application: Application::Notebook,
major: (data[7] >> 4) & 0xF,
minor: (data[7]) & 0xF,
circuit: data[6],
},
}
}
// NOTE: TGL (hx20) does not have this host command
pub fn read_pd_version(ec: &CrosEc) -> EcResult<MainPdVersions> {
let info = EcRequestReadPdVersionV1 {}.send_command_vec(ec);
// If v1 not available, fall back
if let Err(EcError::Response(EcResponseStatus::InvalidVersion)) = info {
let info = EcRequestReadPdVersionV0 {}.send_command(ec)?;