@@ -676,8 +676,7 @@ impl Flasher {
676676
677677 let detected_chip = if before_operation != ResetBeforeOperation :: NoResetNoSync {
678678 // Detect which chip we are connected to.
679- let magic = connection. read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?;
680- let detected_chip = Chip :: from_magic ( magic) ?;
679+ let detected_chip = detect_chip ( & mut connection, use_stub) ?;
681680 if let Some ( chip) = chip {
682681 if chip != detected_chip {
683682 return Err ( Error :: ChipMismatch (
@@ -686,6 +685,7 @@ impl Flasher {
686685 ) ) ;
687686 }
688687 }
688+
689689 detected_chip
690690 } else if before_operation == ResetBeforeOperation :: NoResetNoSync && chip. is_some ( ) {
691691 chip. unwrap ( )
@@ -792,8 +792,7 @@ impl Flasher {
792792 } ?;
793793
794794 // Re-detect chip to check stub is up
795- let magic = self . connection . read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?;
796- let chip = Chip :: from_magic ( magic) ?;
795+ let chip = detect_chip ( & mut self . connection , self . use_stub ) ?;
797796 debug ! ( "Re-detected chip: {:?}" , chip) ;
798797
799798 Ok ( ( ) )
@@ -1126,25 +1125,7 @@ impl Flasher {
11261125
11271126 /// Get security info
11281127 pub fn get_security_info ( & mut self ) -> Result < SecurityInfo , Error > {
1129- self . connection
1130- . with_timeout ( CommandType :: GetSecurityInfo . timeout ( ) , |connection| {
1131- let response = connection. command ( Command :: GetSecurityInfo ) ?;
1132- // Extract raw bytes and convert them into `SecurityInfo`
1133- if let crate :: connection:: CommandResponseValue :: Vector ( data) = response {
1134- // HACK: Not quite sure why there seem to be 4 extra bytes at the end of the
1135- // response when the stub is not being used...
1136- let end = if self . use_stub {
1137- data. len ( )
1138- } else {
1139- data. len ( ) - 4
1140- } ;
1141- SecurityInfo :: try_from ( & data[ ..end] )
1142- } else {
1143- Err ( Error :: InvalidResponse (
1144- "response was not a vector of bytes" . into ( ) ,
1145- ) )
1146- }
1147- } )
1128+ get_security_info ( & mut self . connection , self . use_stub )
11481129 }
11491130
11501131 pub fn change_baud ( & mut self , speed : u32 ) -> Result < ( ) , Error > {
@@ -1367,3 +1348,39 @@ impl Flasher {
13671348 Ok ( ( ) )
13681349 }
13691350}
1351+
1352+ #[ cfg( feature = "serialport" ) ]
1353+ fn get_security_info ( connection : & mut Connection , use_stub : bool ) -> Result < SecurityInfo , Error > {
1354+ connection. with_timeout ( CommandType :: GetSecurityInfo . timeout ( ) , |connection| {
1355+ let response = connection. command ( Command :: GetSecurityInfo ) ?;
1356+ // Extract raw bytes and convert them into `SecurityInfo`
1357+ if let crate :: connection:: CommandResponseValue :: Vector ( data) = response {
1358+ // HACK: Not quite sure why there seem to be 4 extra bytes at the end of the
1359+ // response when the stub is not being used...
1360+ let end = if use_stub { data. len ( ) } else { data. len ( ) - 4 } ;
1361+ SecurityInfo :: try_from ( & data[ ..end] )
1362+ } else {
1363+ Err ( Error :: InvalidResponse (
1364+ "response was not a vector of bytes" . into ( ) ,
1365+ ) )
1366+ }
1367+ } )
1368+ }
1369+
1370+ #[ cfg( feature = "serialport" ) ]
1371+ fn detect_chip ( connection : & mut Connection , use_stub : bool ) -> Result < Chip , Error > {
1372+ match get_security_info ( connection, use_stub) {
1373+ Ok ( info) if info. chip_id . is_some ( ) => {
1374+ let chip_id = info. chip_id . unwrap ( ) as u16 ;
1375+ let chip = Chip :: try_from ( chip_id) ?;
1376+
1377+ Ok ( chip)
1378+ }
1379+ _ => {
1380+ let magic = connection. read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?;
1381+ let chip = Chip :: from_magic ( magic) ?;
1382+
1383+ Ok ( chip)
1384+ }
1385+ }
1386+ }
0 commit comments