@@ -132,6 +132,11 @@ def mark_all_pages_not_same(self):
132132 for page in self .page_list :
133133 page .same = False
134134
135+ def mark_all_pages_unknown (self ):
136+ """@brief Sets the same flag to False for all pages in this sector."""
137+ for page in self .page_list :
138+ page .same = None
139+
135140 def __repr__ (self ):
136141 return "<_FlashSector@%x addr=%x size=%x wgt=%g pages=%s, subsectors=%d>" % (
137142 id (self ), self .addr , self .size , self .erase_weight , self .page_list , self .n_subsectors )
@@ -417,7 +422,7 @@ def add_page_with_existing_data():
417422 page = add_page_with_existing_data ()
418423 sector_page_addr += page .size
419424
420- def program (self , chip_erase = None , progress_cb = None , smart_flash = True , fast_verify = False , keep_unwritten = True , no_reset = False ):
425+ def program (self , chip_erase = None , progress_cb = None , smart_flash = True , fast_verify = False , keep_unwritten = True , no_reset = False , verify = True ):
421426 """@brief Determine fastest method of flashing and then run flash programming.
422427
423428 Data must have already been added with add_data().
@@ -444,6 +449,8 @@ def program(self, chip_erase=None, progress_cb=None, smart_flash=True, fast_veri
444449 be read from memory and restored while programming.
445450 @param no_reset Boolean indicating whether if the device should not be reset after the
446451 programming process has finished.
452+ @param verify Boolean indicating whether a verify pass should be performed after the
453+ programming process has finished.
447454 """
448455
449456 # Send notification that we're about to program flash.
@@ -535,12 +542,6 @@ def program(self, chip_erase=None, progress_cb=None, smart_flash=True, fast_veri
535542 else :
536543 flash_operation = self ._sector_erase_program (progress_cb )
537544
538- # Cleanup flash algo and reset target after programming.
539- self .flash .cleanup ()
540-
541- if no_reset is not True :
542- self .flash .target .reset_and_halt ()
543-
544545 program_finish = time ()
545546 self .perf .program_time = program_finish - program_start
546547 self .perf .program_type = flash_operation
@@ -571,6 +572,23 @@ def program(self, chip_erase=None, progress_cb=None, smart_flash=True, fast_veri
571572 self .perf .skipped_byte_count = skipped_byte_count
572573 self .perf .skipped_page_count = skipped_page_count
573574
575+ if verify :
576+ LOG .info ("Verifying" )
577+ # Reset same flag for all pages to enable rescanning
578+ for sector in self .sector_list :
579+ sector .mark_all_pages_unknown ()
580+ self ._scan_pages_for_same (progress_cb )
581+ failed_pages = [page for page in self .page_list if page .same is False ]
582+ if failed_pages :
583+ LOG .debug ("Verify failed for pages {}" .format (failed_pages ))
584+ raise FlashProgramFailure ('flash verify failure' , address = failed_pages [0 ].addr )
585+
586+ # Cleanup flash algo and reset target after programming.
587+ self .flash .cleanup ()
588+
589+ if no_reset is not True :
590+ self .flash .target .reset_and_halt ()
591+
574592 if self .log_performance :
575593 if chip_erase :
576594 LOG .info ("Erased chip, programmed %d bytes (%s), skipped %d bytes (%s) at %.02f kB/s" ,
@@ -899,12 +917,6 @@ def _scan_pages_for_same(self, progress_cb=_stub_progress):
899917 if self .sector_erase_weight > 0 :
900918 progress_cb (float (progress ) / float (self .sector_erase_weight ))
901919
902- # If we have to program any pages of a sector, then mark all pages of that sector
903- # as needing to be programmed, since the sector will be erased.
904- for sector in self .sector_list :
905- if sector .are_any_pages_not_same ():
906- sector .mark_all_pages_not_same ()
907-
908920 return progress
909921
910922 def _next_nonsame_page (self , i ):
@@ -937,6 +949,8 @@ def _sector_erase_program_double_buffer(self, progress_cb=_stub_progress):
937949 self .flash .init (self .flash .Operation .ERASE )
938950 for sector in self .sector_list :
939951 if sector .are_any_pages_not_same ():
952+ # If the sector is erased, all its pages must be programmed
953+ sector .mark_all_pages_not_same ()
940954 # Erase the sector
941955 for addr in sector .addrs :
942956 self .flash .erase_sector (addr )
0 commit comments