Skip to content

Commit 70438f6

Browse files
committed
ccg3: Instead of waiting fixed amount, wait until it's back
Then we can be quicker on average and wait a longer maximum timeout. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 42a79ac commit 70438f6

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

framework_lib/src/ccgx/hid.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ pub const HDMI_CARD_PID: u16 = 0x0002;
1313
pub const DP_CARD_PID: u16 = 0x0003;
1414
pub const ALL_CARD_PIDS: [u16; 2] = [DP_CARD_PID, HDMI_CARD_PID];
1515

16-
/// It takes as little as 3 but up to 6s for the HDMI/DP cards to restart and
17-
/// enumerate in the OS
18-
pub const RESTART_TIMEOUT: u64 = 6_000_000;
16+
/// It takes as little as 3s but sometimes more than 5s for the HDMI/DP cards
17+
/// to restart and enumerate in the OS
18+
/// Check every 0.5s for up to 10s
19+
pub const RESTART_TIMEOUT: u64 = 10_000_000;
20+
pub const RESTART_PERIOD: u64 = 500_000;
1921

2022
const ROW_SIZE: usize = 128;
2123
const FW1_START: usize = 0x0030;
@@ -96,7 +98,7 @@ fn flashing_mode(device: &HidDevice) {
9698
0xCC,
9799
0xCC,
98100
])
99-
.unwrap();
101+
.expect("Failed to enter flashing mode");
100102
}
101103

102104
fn magic_unlock(device: &HidDevice) {
@@ -115,7 +117,7 @@ fn magic_unlock(device: &HidDevice) {
115117
0x00,
116118
0x0B,
117119
])
118-
.unwrap();
120+
.expect("Failed to unlock device");
119121

120122
// Returns Err but seems to work anyway. OH! Probably because it resets the device!!
121123
// TODO: I have a feeling the last five bytes are ignored. They're the same in all commands.
@@ -138,7 +140,9 @@ fn get_fw_info(device: &HidDevice) -> HidFirmwareInfo {
138140
let mut buf = [0u8; 0x40];
139141
buf[0] = ReportIdCmd::E0Read as u8;
140142
info!("Get Report E0");
141-
device.get_feature_report(&mut buf).unwrap();
143+
device
144+
.get_feature_report(&mut buf)
145+
.expect("Failed to get device FW info");
142146

143147
flashing_mode(device);
144148

@@ -337,7 +341,9 @@ pub fn flash_firmware(fw_binary: &[u8]) {
337341
// Would be nice to show that instead of the serial number.
338342
// We want to show that so the user knows that multiple *different*
339343
// cards are being updated.
340-
let sn = dev_info.serial_number().unwrap();
344+
let sn = dev_info
345+
.serial_number()
346+
.expect("Device has no serial number");
341347
let dev_name = device_name(dev_info.vendor_id(), dev_info.product_id()).unwrap();
342348
println!();
343349
println!("Updating {} with SN: {:?}", dev_name, sn);
@@ -355,13 +361,8 @@ pub fn flash_firmware(fw_binary: &[u8]) {
355361
println!(" Updating Firmware Image 1");
356362
flash_firmware_image(&device, fw_binary, FW1_START, FW1_METADATA, fw1_rows, 1);
357363

358-
println!(" Waiting 6s for device to restart");
359-
os_specific::sleep(RESTART_TIMEOUT);
360-
api.refresh_devices().unwrap();
361-
let dev_info = &find_devices(&api, &filter_devs, Some(sn))[0];
362-
let device = dev_info.open_device(&api).unwrap();
363-
magic_unlock(&device);
364-
let _info = get_fw_info(&device);
364+
let (device, _) =
365+
wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
365366

366367
println!(" Updating Firmware Image 2");
367368
flash_firmware_image(&device, fw_binary, FW2_START, FW2_METADATA, fw2_rows, 2);
@@ -370,13 +371,8 @@ pub fn flash_firmware(fw_binary: &[u8]) {
370371
println!(" Updating Firmware Image 2");
371372
flash_firmware_image(&device, fw_binary, FW2_START, FW2_METADATA, fw2_rows, 2);
372373

373-
println!(" Waiting 6s for device to restart");
374-
os_specific::sleep(RESTART_TIMEOUT);
375-
api.refresh_devices().unwrap();
376-
let dev_info = &find_devices(&api, &filter_devs, Some(sn))[0];
377-
let device = dev_info.open_device(&api).unwrap();
378-
magic_unlock(&device);
379-
let _info = get_fw_info(&device);
374+
let (device, _) =
375+
wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
380376

381377
println!(" Updating Firmware Image 1");
382378
flash_firmware_image(&device, fw_binary, FW1_START, FW1_METADATA, fw1_rows, 1);
@@ -385,15 +381,10 @@ pub fn flash_firmware(fw_binary: &[u8]) {
385381
}
386382

387383
println!(" Firmware Update done.");
388-
println!(" Waiting 6s for device to restart");
389-
os_specific::sleep(RESTART_TIMEOUT);
384+
let (_, info) =
385+
wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
390386

391387
println!("After Updating");
392-
api.refresh_devices().unwrap();
393-
let dev_info = &find_devices(&api, &filter_devs, Some(sn))[0];
394-
let device = dev_info.open_device(&api).unwrap();
395-
magic_unlock(&device);
396-
let info = get_fw_info(&device);
397388
print_fw_info(&info);
398389
}
399390
}
@@ -465,7 +456,7 @@ fn flash_firmware_image(
465456

466457
fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) {
467458
let row_no_bytes = row_no.to_le_bytes();
468-
debug!("Writing row {:04X}. Data: {:X?}", row_no, row);
459+
trace!("Writing row {:04X}. Data: {:X?}", row_no, row);
469460

470461
// First image start 0x1800 (row 0x30)
471462
// row from 0x0030 to 0x01fb
@@ -488,6 +479,34 @@ fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) {
488479
device.write(&buffer).unwrap();
489480
}
490481

482+
/// Wait for the specific card to reappear
483+
/// Waiting a maximum timeout, if it hasn't appeared by then, return None
484+
fn wait_to_reappear(
485+
api: &mut HidApi,
486+
filter_devs: &[u16],
487+
sn: &str,
488+
) -> Option<(HidDevice, HidFirmwareInfo)> {
489+
println!(" Waiting for Expansion Card to restart");
490+
let retries = RESTART_TIMEOUT / RESTART_PERIOD;
491+
492+
for i in (0..retries).rev() {
493+
os_specific::sleep(RESTART_PERIOD);
494+
api.refresh_devices().unwrap();
495+
let new_devices = find_devices(api, filter_devs, Some(sn));
496+
if new_devices.is_empty() {
497+
debug!("No devices found, retrying #{}/{}", retries - i, retries);
498+
continue;
499+
}
500+
let dev_info = &new_devices[0];
501+
if let Ok(device) = dev_info.open_device(api) {
502+
magic_unlock(&device);
503+
let info = get_fw_info(&device);
504+
return Some((device, info));
505+
}
506+
}
507+
None
508+
}
509+
491510
// HID Report on device before updating Firmware 2
492511
// Usage Page (Vendor)
493512
// Header

0 commit comments

Comments
 (0)