@@ -311,6 +311,10 @@ pub trait ReadEFuse {
311
311
/// Returns the base address of the eFuse register
312
312
fn efuse_reg ( & self ) -> u32 ;
313
313
314
+ #[ cfg( feature = "serialport" ) ]
315
+ /// Return fields common to most/all chips
316
+ fn common_fields ( & self ) -> HashMap < & ' static str , EfuseField > ;
317
+
314
318
#[ cfg( feature = "serialport" ) ]
315
319
/// Given an active connection, read the nth word of the eFuse region
316
320
fn read_efuse ( & self , connection : & mut Connection , n : u32 ) -> Result < u32 , Error > {
@@ -319,75 +323,22 @@ pub trait ReadEFuse {
319
323
}
320
324
321
325
#[ cfg( feature = "serialport" ) ]
322
- /// Read an eFuse field defined by its position parameters
323
- fn read_efuse_field (
324
- & self ,
325
- connection : & mut Connection ,
326
- word_offset : u32 ,
327
- bit_offset : u32 ,
328
- bit_count : u32 ,
329
- ) -> Result < u32 , Error > {
326
+ /// Read an eFuse field defined by a structured EfuseField
327
+ fn read_field ( & self , connection : & mut Connection , field : EfuseField ) -> Result < u32 , Error > {
328
+ let EfuseField {
329
+ word_offset ,
330
+ bit_offset ,
331
+ bit_count ,
332
+ } = field ;
333
+
330
334
let value = self . read_efuse ( connection, word_offset) ?;
331
335
let mask = if bit_count == 32 {
332
- 0xFFFF_FFFF
336
+ u32 :: MAX
333
337
} else {
334
338
( 1 << bit_count) - 1
335
339
} ;
336
- Ok ( ( value >> bit_offset) & mask)
337
- }
338
340
339
- #[ cfg( feature = "serialport" ) ]
340
- /// Read an eFuse field defined by a structured EfuseField
341
- fn read_field ( & self , connection : & mut Connection , field : EfuseField ) -> Result < u32 , Error > {
342
- self . read_efuse_field (
343
- connection,
344
- field. word_offset ,
345
- field. bit_offset ,
346
- field. bit_count ,
347
- )
348
- }
349
-
350
- #[ cfg( feature = "serialport" ) ]
351
- /// Read a MAC address from eFuse fields
352
- fn read_mac_address (
353
- & self ,
354
- connection : & mut Connection ,
355
- fields : & [ EfuseField ; 6 ] ,
356
- ) -> Result < String , Error > {
357
- let mut mac_bytes = [ 0u8 ; 6 ] ;
358
-
359
- for ( i, field) in fields. iter ( ) . enumerate ( ) {
360
- let byte_value = self . read_field ( connection, * field) ? as u8 ;
361
- mac_bytes[ i] = byte_value;
362
- }
363
-
364
- Ok ( bytes_to_mac_addr ( & mac_bytes) )
365
- }
366
-
367
- #[ cfg( feature = "serialport" ) ]
368
- /// Read a MAC address using word-based fields
369
- fn read_mac_address_from_words (
370
- & self ,
371
- connection : & mut Connection ,
372
- word_field_1 : EfuseField ,
373
- word_field_2 : EfuseField ,
374
- ) -> Result < String , Error > {
375
- let word1 = self . read_field ( connection, word_field_1) ?;
376
- let word2 = self . read_field ( connection, word_field_2) ?;
377
-
378
- let bytes = ( ( word2 as u64 ) << 32 ) | word1 as u64 ;
379
- let bytes = bytes. to_be_bytes ( ) ;
380
- // Skip first two bytes as MAC address is typically stored in the last 6 bytes
381
- let mac_bytes = & bytes[ 2 ..8 ] ;
382
-
383
- Ok ( bytes_to_mac_addr ( mac_bytes) )
384
- }
385
-
386
- #[ cfg( feature = "serialport" ) ]
387
- /// Return fields common to most/all chips
388
- fn common_fields ( & self ) -> HashMap < & ' static str , EfuseField > {
389
- HashMap :: new ( ) // Default empty implementation, to be overridden by
390
- // chips
341
+ Ok ( ( value >> bit_offset) & mask)
391
342
}
392
343
}
393
344
@@ -445,7 +396,19 @@ pub trait Target: ReadEFuse {
445
396
446
397
#[ cfg( feature = "serialport" ) ]
447
398
/// What is the MAC address?
448
- fn mac_address ( & self , connection : & mut Connection ) -> Result < String , Error > ;
399
+ fn mac_address ( & self , connection : & mut Connection ) -> Result < String , Error > {
400
+ let fields = self . common_fields ( ) ;
401
+
402
+ let word1 = self . read_field ( connection, fields[ "MAC_FACTORY_0" ] ) ?;
403
+ let word2 = self . read_field ( connection, fields[ "MAC_FACTORY_1" ] ) ?;
404
+
405
+ let bytes = ( ( word2 as u64 ) << 32 ) | word1 as u64 ;
406
+ let bytes = bytes. to_be_bytes ( ) ;
407
+
408
+ // Skip the first two bytes, as the MAC address is typically stored in
409
+ // the last 6 bytes:
410
+ Ok ( bytes_to_mac_addr ( & bytes[ 2 ..8 ] ) )
411
+ }
449
412
450
413
#[ cfg( feature = "serialport" ) ]
451
414
/// Maximum RAM block size for writing
0 commit comments