@@ -442,32 +442,23 @@ impl Chip {
442442 ..
443443 } = field;
444444
445- fn block_address ( chip : & Chip , block : u32 ) -> u32 {
446- let block0_addr = chip. efuse_reg ( ) + chip. block0_offset ( ) ;
447-
448- let mut block_offset = 0 ;
449- for b in 0 ..block {
450- block_offset += chip. block_size ( b as usize ) ;
451- }
452-
453- block0_addr + block_offset
454- }
455-
456445 fn read_raw ( connection : & mut Connection , addr : u32 ) -> Result < u32 , Error > {
457446 connection. read_reg ( addr)
458447 }
459448
460449 // Represent output value as a bytes slice:
461450 let mut output = std:: mem:: MaybeUninit :: < T > :: uninit ( ) ;
462451 let mut bytes = unsafe {
452+ // see https://docs.rs/bytemuck/1.24.0/bytemuck/trait.AnyBitPattern.html
453+ // and https://docs.rs/bytemuck/1.24.0/bytemuck/trait.Pod.html
463454 std:: slice:: from_raw_parts_mut ( output. as_mut_ptr ( ) as * mut u8 , std:: mem:: size_of :: < T > ( ) )
464455 } ;
465456
466457 let bit_off = bit_start;
467458 let bit_end = std:: cmp:: min ( bit_count, ( bytes. len ( ) * 8 ) as u32 ) + bit_off;
468459
469460 let mut last_word_off = bit_off / 32 ;
470- let mut last_word = read_raw ( connection, block_address ( self , block) + last_word_off * 4 ) ?;
461+ let mut last_word = read_raw ( connection, self . block_address ( block) + last_word_off * 4 ) ?;
471462
472463 let word_bit_off = bit_off % 32 ;
473464 let word_bit_ext = 32 - word_bit_off;
@@ -477,7 +468,7 @@ impl Chip {
477468 if word_off != last_word_off {
478469 // Read a new word:
479470 last_word_off = word_off;
480- last_word = read_raw ( connection, block_address ( self , block) + last_word_off * 4 ) ?;
471+ last_word = read_raw ( connection, self . block_address ( block) + last_word_off * 4 ) ?;
481472 }
482473
483474 let mut word = last_word >> word_bit_off;
@@ -487,7 +478,7 @@ impl Chip {
487478 if word_bit_len > word_bit_ext {
488479 // Read the next word:
489480 last_word_off = word_off;
490- last_word = read_raw ( connection, block_address ( self , block) + last_word_off * 4 ) ?;
481+ last_word = read_raw ( connection, self . block_address ( block) + last_word_off * 4 ) ?;
491482 // Append bits from a beginning of the next word:
492483 word |= last_word. wrapping_shl ( 32 - word_bit_off) ;
493484 } ;
@@ -516,6 +507,17 @@ impl Chip {
516507 Ok ( unsafe { output. assume_init ( ) } )
517508 }
518509
510+ fn block_address ( & self , block : u32 ) -> u32 {
511+ let block0_addr = self . efuse_reg ( ) + self . block0_offset ( ) ;
512+
513+ let mut block_offset = 0 ;
514+ for b in 0 ..block {
515+ block_offset += self . block_size ( b as usize ) ;
516+ }
517+
518+ block0_addr + block_offset
519+ }
520+
519521 /// Read the raw word in the specified eFuse block, without performing any
520522 /// bit-shifting or masking of the read value.
521523 #[ cfg( feature = "serialport" ) ]
@@ -525,14 +527,7 @@ impl Chip {
525527 block : u32 ,
526528 word : u32 ,
527529 ) -> Result < u32 , Error > {
528- let block0_addr = self . efuse_reg ( ) + self . block0_offset ( ) ;
529-
530- let mut block_offset = 0 ;
531- for b in 0 ..block {
532- block_offset += self . block_size ( b as usize ) ;
533- }
534-
535- let addr = block0_addr + block_offset + ( word * 0x4 ) ;
530+ let addr = self . block_address ( block) + ( word * 0x4 ) ;
536531
537532 connection. read_reg ( addr)
538533 }
0 commit comments