@@ -750,35 +750,29 @@ impl Connection {
750
750
& mut self ,
751
751
use_stub : bool ,
752
752
) -> Result < crate :: target:: Chip , crate :: error:: Error > {
753
- // First try to detect chip using security_info when possible
754
- if let Ok ( security_info) = self . security_info ( use_stub) {
755
- if let Some ( chip_id) = security_info. chip_id {
756
- debug ! ( "Detected chip using security info chip_id: {chip_id}" ) ;
757
- // Convert u32 chip_id to u16 for compatibility with Chip::try_from
758
- match Chip :: try_from ( chip_id as u16 ) {
759
- Ok ( chip) => return Ok ( chip) ,
760
- Err ( _) => {
761
- debug ! (
762
- "Unknown chip_id from security_info: {chip_id}, falling back to magic register"
763
- ) ;
764
- }
765
- }
753
+ match self . security_info ( use_stub) {
754
+ Ok ( info) if info. chip_id . is_some ( ) => {
755
+ let chip_id = info. chip_id . unwrap ( ) as u16 ;
756
+ let chip = Chip :: try_from ( chip_id) ?;
757
+
758
+ Ok ( chip)
759
+ }
760
+ _ => {
761
+ // Fall back to reading the magic value from the chip
762
+ let magic = if use_stub {
763
+ self . with_timeout ( CommandType :: ReadReg . timeout ( ) , |connection| {
764
+ connection. command ( Command :: ReadReg {
765
+ address : CHIP_DETECT_MAGIC_REG_ADDR ,
766
+ } )
767
+ } ) ?
768
+ . try_into ( ) ?
769
+ } else {
770
+ self . read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?
771
+ } ;
772
+ debug ! ( "Read chip magic value: 0x{magic:08x}" ) ;
773
+ Chip :: from_magic ( magic)
766
774
}
767
775
}
768
-
769
- // Fall back to reading the magic value from the chip
770
- let magic = if use_stub {
771
- self . with_timeout ( CommandType :: ReadReg . timeout ( ) , |connection| {
772
- connection. command ( Command :: ReadReg {
773
- address : CHIP_DETECT_MAGIC_REG_ADDR ,
774
- } )
775
- } ) ?
776
- . try_into ( ) ?
777
- } else {
778
- self . read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?
779
- } ;
780
- debug ! ( "Read chip magic value: 0x{magic:08x}" ) ;
781
- Chip :: from_magic ( magic)
782
776
}
783
777
}
784
778
0 commit comments