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