@@ -263,6 +263,16 @@ pub struct DeviceInfo {
263
263
pub mac_address : String ,
264
264
}
265
265
266
+ /// Progress update callbacks
267
+ pub trait ProgressCallbacks {
268
+ /// Initialize some progress report
269
+ fn init ( & mut self , addr : u32 , total : usize ) ;
270
+ /// Update some progress report
271
+ fn update ( & mut self , current : usize ) ;
272
+ /// Finish some prgoress report
273
+ fn finish ( & mut self ) ;
274
+ }
275
+
266
276
/// Connect to and flash a target device
267
277
pub struct Flasher {
268
278
/// Connection for flash operations
@@ -352,7 +362,7 @@ impl Flasher {
352
362
addr : text_addr,
353
363
data : Cow :: Borrowed ( & text) ,
354
364
} ,
355
- None ,
365
+ & mut None ,
356
366
)
357
367
. flashing ( ) ?;
358
368
@@ -366,7 +376,7 @@ impl Flasher {
366
376
addr : data_addr,
367
377
data : Cow :: Borrowed ( & data) ,
368
378
} ,
369
- None ,
379
+ & mut None ,
370
380
)
371
381
. flashing ( ) ?;
372
382
@@ -605,7 +615,11 @@ impl Flasher {
605
615
/// Load an elf image to ram and execute it
606
616
///
607
617
/// Note that this will not touch the flash on the device
608
- pub fn load_elf_to_ram ( & mut self , elf_data : & [ u8 ] ) -> Result < ( ) , Error > {
618
+ pub fn load_elf_to_ram (
619
+ & mut self ,
620
+ elf_data : & [ u8 ] ,
621
+ mut progress : Option < & mut dyn ProgressCallbacks > ,
622
+ ) -> Result < ( ) , Error > {
609
623
let image = ElfFirmwareImage :: try_from ( elf_data) ?;
610
624
if image. rom_segments ( self . chip ) . next ( ) . is_some ( ) {
611
625
return Err ( Error :: ElfNotRamLoadable ) ;
@@ -620,20 +634,8 @@ impl Flasher {
620
634
target. begin ( & mut self . connection ) . flashing ( ) ?;
621
635
622
636
for segment in image. ram_segments ( self . chip ) {
623
- // Only display progress bars when the "cli" feature is enabled.
624
- let progress_cb = if cfg ! ( feature = "cli" ) {
625
- use crate :: cli:: { build_progress_bar_callback, progress_bar} ;
626
-
627
- let progress = progress_bar ( format ! ( "segment 0x{:X}" , segment. addr) , None ) ;
628
- let progress_cb = build_progress_bar_callback ( progress) ;
629
-
630
- Some ( progress_cb)
631
- } else {
632
- None
633
- } ;
634
-
635
637
target
636
- . write_segment ( & mut self . connection , segment. into ( ) , progress_cb )
638
+ . write_segment ( & mut self . connection , segment. into ( ) , & mut progress )
637
639
. flashing ( ) ?;
638
640
}
639
641
@@ -650,6 +652,7 @@ impl Flasher {
650
652
flash_mode : Option < FlashMode > ,
651
653
flash_size : Option < FlashSize > ,
652
654
flash_freq : Option < FlashFrequency > ,
655
+ mut progress : Option < & mut dyn ProgressCallbacks > ,
653
656
) -> Result < ( ) , Error > {
654
657
let image = ElfFirmwareImage :: try_from ( elf_data) ?;
655
658
@@ -676,20 +679,8 @@ impl Flasher {
676
679
crate :: cli:: display_image_size ( image. app_size ( ) , image. part_size ( ) ) ;
677
680
678
681
for segment in image. flash_segments ( ) {
679
- // Only display progress bars when the "cli" feature is enabled.
680
- let progress_cb = if cfg ! ( feature = "cli" ) {
681
- use crate :: cli:: { build_progress_bar_callback, progress_bar} ;
682
-
683
- let progress = progress_bar ( format ! ( "segment 0x{:X}" , segment. addr) , None ) ;
684
- let progress_cb = build_progress_bar_callback ( progress) ;
685
-
686
- Some ( progress_cb)
687
- } else {
688
- None
689
- } ;
690
-
691
682
target
692
- . write_segment ( & mut self . connection , segment, progress_cb )
683
+ . write_segment ( & mut self . connection , segment, & mut progress )
693
684
. flashing ( ) ?;
694
685
}
695
686
@@ -703,7 +694,7 @@ impl Flasher {
703
694
& mut self ,
704
695
addr : u32 ,
705
696
data : & [ u8 ] ,
706
- progress_cb : Option < Box < dyn Fn ( usize , usize ) > > ,
697
+ mut progress : Option < & mut dyn ProgressCallbacks > ,
707
698
) -> Result < ( ) , Error > {
708
699
let segment = RomSegment {
709
700
addr,
@@ -712,7 +703,7 @@ impl Flasher {
712
703
713
704
let mut target = self . chip . flash_target ( self . spi_params , self . use_stub ) ;
714
705
target. begin ( & mut self . connection ) . flashing ( ) ?;
715
- target. write_segment ( & mut self . connection , segment, progress_cb ) ?;
706
+ target. write_segment ( & mut self . connection , segment, & mut progress ) ?;
716
707
target. finish ( & mut self . connection , true ) . flashing ( ) ?;
717
708
718
709
Ok ( ( ) )
@@ -727,6 +718,7 @@ impl Flasher {
727
718
flash_mode : Option < FlashMode > ,
728
719
flash_size : Option < FlashSize > ,
729
720
flash_freq : Option < FlashFrequency > ,
721
+ progress : Option < & mut dyn ProgressCallbacks > ,
730
722
) -> Result < ( ) , Error > {
731
723
self . load_elf_to_flash_with_format (
732
724
elf_data,
@@ -736,6 +728,7 @@ impl Flasher {
736
728
flash_mode,
737
729
flash_size,
738
730
flash_freq,
731
+ progress,
739
732
)
740
733
}
741
734
0 commit comments