Skip to content

Commit 70e53b3

Browse files
committed
ccgx/hid: Refactor command into own function
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent dc3b948 commit 70e53b3

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

framework_lib/src/ccgx/hid.rs

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ enum CmdId {
5050
CmdJump = 0x01,
5151
/// Seems to enter flashing mode
5252
CmdFlash = 0x02,
53+
/// Not quite sure what it does
54+
Cmd0x04 = 0x04,
5355
/// Some sort of mode switch
5456
Cmd0x06 = 0x06,
5557
}
@@ -87,17 +89,7 @@ enum ReportIdCmd {
8789
fn flashing_mode(device: &HidDevice) {
8890
// Probably enter flashing mode?
8991
info!("Enter flashing mode");
90-
let _ = device
91-
.write(&[
92-
ReportIdCmd::E1Cmd as u8,
93-
CmdId::CmdFlash as u8,
94-
CmdParam::Enable as u8,
95-
0x00,
96-
0xCC,
97-
0xCC,
98-
0xCC,
99-
0xCC,
100-
])
92+
let _ = send_command(device, CmdId::CmdFlash, CmdParam::Enable as u8)
10193
.expect("Failed to enter flashing mode");
10294
}
10395

@@ -123,16 +115,7 @@ fn magic_unlock(device: &HidDevice) {
123115
// TODO: I have a feeling the last five bytes are ignored. They're the same in all commands.
124116
// Seems to work with all of them set to 0x00
125117
info!("Bridge Mode");
126-
let _ = device.write(&[
127-
ReportIdCmd::E1Cmd as u8,
128-
CmdId::Cmd0x06 as u8,
129-
CmdParam::BridgeMode as u8,
130-
0x00,
131-
0xCC,
132-
0xCC,
133-
0xCC,
134-
0xCC,
135-
]);
118+
let _ = send_command(device, CmdId::Cmd0x06, CmdParam::BridgeMode as u8);
136119
}
137120

138121
fn get_fw_info(device: &HidDevice) -> HidFirmwareInfo {
@@ -425,33 +408,24 @@ fn flash_firmware_image(
425408
// Not quite sure what this is. But on the first update it has
426409
// 0x01 and on the second it has 0x02. So I think this switches the boot order?
427410
info!("Bootswitch");
428-
let _ = device
429-
.write(&[
430-
ReportIdCmd::E1Cmd as u8,
431-
0x04,
432-
no,
433-
0x00,
434-
0xCC,
435-
0xCC,
436-
0xCC,
437-
0xCC,
438-
])
439-
.unwrap();
411+
let _ = send_command(device, CmdId::Cmd0x04, no).unwrap();
440412

441413
// Seems to reset the device, since the USB device number changes
442414
info!("Reset");
443-
let _ = device
444-
.write(&[
445-
ReportIdCmd::E1Cmd as u8,
446-
CmdId::CmdJump as u8,
447-
CmdParam::Reset as u8,
448-
0x00,
449-
0xCC,
450-
0xCC,
451-
0xCC,
452-
0xCC,
453-
])
454-
.unwrap();
415+
let _ = send_command(device, CmdId::CmdJump, CmdParam::Reset as u8).unwrap();
416+
}
417+
418+
fn send_command(device: &HidDevice, cmd_id: CmdId, cmd_param: u8) -> Result<usize, HidError> {
419+
device.write(&[
420+
ReportIdCmd::E1Cmd as u8,
421+
cmd_id as u8,
422+
cmd_param,
423+
0x00,
424+
0xCC,
425+
0xCC,
426+
0xCC,
427+
0xCC,
428+
])
455429
}
456430

457431
fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) -> Result<usize, HidError> {

0 commit comments

Comments
 (0)