@@ -13,9 +13,11 @@ pub const HDMI_CARD_PID: u16 = 0x0002;
13
13
pub const DP_CARD_PID : u16 = 0x0003 ;
14
14
pub const ALL_CARD_PIDS : [ u16 ; 2 ] = [ DP_CARD_PID , HDMI_CARD_PID ] ;
15
15
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 ;
19
21
20
22
const ROW_SIZE : usize = 128 ;
21
23
const FW1_START : usize = 0x0030 ;
@@ -96,7 +98,7 @@ fn flashing_mode(device: &HidDevice) {
96
98
0xCC ,
97
99
0xCC ,
98
100
] )
99
- . unwrap ( ) ;
101
+ . expect ( "Failed to enter flashing mode" ) ;
100
102
}
101
103
102
104
fn magic_unlock ( device : & HidDevice ) {
@@ -115,7 +117,7 @@ fn magic_unlock(device: &HidDevice) {
115
117
0x00 ,
116
118
0x0B ,
117
119
] )
118
- . unwrap ( ) ;
120
+ . expect ( "Failed to unlock device" ) ;
119
121
120
122
// Returns Err but seems to work anyway. OH! Probably because it resets the device!!
121
123
// 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 {
138
140
let mut buf = [ 0u8 ; 0x40 ] ;
139
141
buf[ 0 ] = ReportIdCmd :: E0Read as u8 ;
140
142
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" ) ;
142
146
143
147
flashing_mode ( device) ;
144
148
@@ -337,7 +341,9 @@ pub fn flash_firmware(fw_binary: &[u8]) {
337
341
// Would be nice to show that instead of the serial number.
338
342
// We want to show that so the user knows that multiple *different*
339
343
// 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" ) ;
341
347
let dev_name = device_name ( dev_info. vendor_id ( ) , dev_info. product_id ( ) ) . unwrap ( ) ;
342
348
println ! ( ) ;
343
349
println ! ( "Updating {} with SN: {:?}" , dev_name, sn) ;
@@ -355,13 +361,8 @@ pub fn flash_firmware(fw_binary: &[u8]) {
355
361
println ! ( " Updating Firmware Image 1" ) ;
356
362
flash_firmware_image ( & device, fw_binary, FW1_START , FW1_METADATA , fw1_rows, 1 ) ;
357
363
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" ) ;
365
366
366
367
println ! ( " Updating Firmware Image 2" ) ;
367
368
flash_firmware_image ( & device, fw_binary, FW2_START , FW2_METADATA , fw2_rows, 2 ) ;
@@ -370,13 +371,8 @@ pub fn flash_firmware(fw_binary: &[u8]) {
370
371
println ! ( " Updating Firmware Image 2" ) ;
371
372
flash_firmware_image ( & device, fw_binary, FW2_START , FW2_METADATA , fw2_rows, 2 ) ;
372
373
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" ) ;
380
376
381
377
println ! ( " Updating Firmware Image 1" ) ;
382
378
flash_firmware_image ( & device, fw_binary, FW1_START , FW1_METADATA , fw1_rows, 1 ) ;
@@ -385,15 +381,10 @@ pub fn flash_firmware(fw_binary: &[u8]) {
385
381
}
386
382
387
383
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" ) ;
390
386
391
387
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) ;
397
388
print_fw_info ( & info) ;
398
389
}
399
390
}
@@ -465,7 +456,7 @@ fn flash_firmware_image(
465
456
466
457
fn write_row ( device : & HidDevice , row_no : u16 , row : & [ u8 ] ) {
467
458
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) ;
469
460
470
461
// First image start 0x1800 (row 0x30)
471
462
// row from 0x0030 to 0x01fb
@@ -488,6 +479,34 @@ fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) {
488
479
device. write ( & buffer) . unwrap ( ) ;
489
480
}
490
481
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
+
491
510
// HID Report on device before updating Firmware 2
492
511
// Usage Page (Vendor)
493
512
// Header
0 commit comments