<s> """<STR_LIT>""" <EOL> from pyOCD . target . target import Target <EOL> import logging <EOL> from struct import unpack <EOL> from time import time <EOL> from flash_builder import FlashBuilder <EOL> DEFAULT_PAGE_PROGRAM_WEIGHT = <NUM_LIT> <EOL> DEFAULT_PAGE_ERASE_WEIGHT = <NUM_LIT> <EOL> DEFAULT_CHIP_ERASE_WEIGHT = <NUM_LIT> <EOL> analyzer = ( <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> ) <EOL> def _msb ( n ) : <EOL> ndx = <NUM_LIT:0> <EOL> while ( <NUM_LIT:1> < n ) : <EOL> n = ( n >> <NUM_LIT:1> ) <EOL> ndx += <NUM_LIT:1> <EOL> return ndx <EOL> def _same ( d1 , d2 ) : <EOL> if len ( d1 ) != len ( d2 ) : <EOL> return False <EOL> for i in range ( len ( d1 ) ) : <EOL> if d1 [ i ] != d2 [ i ] : <EOL> return False <EOL> return True <EOL> class PageInfo ( object ) : <EOL> def __init__ ( self ) : <EOL> self . base_addr = None <EOL> self . erase_weight = None <EOL> self . program_weight = None <EOL> self . size = None <EOL> self . crc_supported = None <EOL> class FlashInfo ( object ) : <EOL> def __init__ ( self ) : <EOL> self . rom_start = None <EOL> self . erase_weight = None <EOL> class Flash ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , target , flash_algo ) : <EOL> self . target = target <EOL> self . flash_algo = flash_algo <EOL> self . flash_algo_debug = False <EOL> if flash_algo is not None : <EOL> self . end_flash_algo = flash_algo [ '<STR_LIT>' ] + len ( flash_algo ) * <NUM_LIT:4> <EOL> self . begin_stack = flash_algo [ '<STR_LIT>' ] <EOL> self . begin_data = flash_algo [ '<STR_LIT>' ] <EOL> self . static_base = flash_algo [ '<STR_LIT>' ] <EOL> self . min_program_length = flash_algo . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if flash_algo . has_key ( '<STR_LIT>' ) : <EOL> self . page_buffers = flash_algo [ '<STR_LIT>' ] <EOL> else : <EOL> self . page_buffers = [ self . begin_data ] <EOL> self . double_buffer_supported = len ( self . page_buffers ) > <NUM_LIT:1> <EOL> else : <EOL> self . end_flash_algo = None <EOL> self . begin_stack = None <EOL> self . begin_data = None <EOL> self . static_base = None <EOL> @ property <EOL> def minimumProgramLength ( self ) : <EOL> return self . min_program_length <EOL> def init ( self ) : <EOL> """<STR_LIT>""" <EOL> self . target . halt ( ) <EOL> self . target . setTargetState ( "<STR_LIT>" ) <EOL> result = self . callFunctionAndWait ( self . flash_algo [ '<STR_LIT>' ] , init = True ) <EOL> if result != <NUM_LIT:0> : <EOL> logging . error ( '<STR_LIT>' , result ) <EOL> def computeCrcs ( self , sectors ) : <EOL> data = [ ] <EOL> for addr , size in sectors : <EOL> size_val = _msb ( size ) <EOL> addr_val = addr // size <EOL> assert ( <NUM_LIT:1> << size_val ) == size <EOL> assert ( addr % size ) == <NUM_LIT:0> <EOL> val = ( size_val << <NUM_LIT:0> ) | ( addr_val << <NUM_LIT:16> ) <EOL> data . append ( val ) <EOL> self . target . writeBlockMemoryAligned32 ( self . begin_data , data ) <EOL> result = self . callFunctionAndWait ( self . flash_algo [ '<STR_LIT>' ] , self . begin_data , len ( data ) ) <EOL> data = self . target . readBlockMemoryAligned32 ( self . begin_data , len ( data ) ) <EOL> return data <EOL> def eraseAll ( self ) : <EOL> """<STR_LIT>""" <EOL> result = self . callFunctionAndWait ( self . flash_algo [ '<STR_LIT>' ] ) <EOL> if result != <NUM_LIT:0> : <EOL> logging . error ( '<STR_LIT>' , result ) <EOL> def erasePage ( self , flashPtr ) : <EOL> """<STR_LIT>""" <EOL> result = self . callFunctionAndWait ( self . flash_algo [ '<STR_LIT>' ] , flashPtr ) <EOL> if result != <NUM_LIT:0> : <EOL> logging . error ( '<STR_LIT>' , flashPtr , result ) <EOL> def programPage ( self , flashPtr , bytes ) : <EOL> """<STR_LIT>""" <EOL> bytes = self . overrideSecurityBits ( flashPtr , bytes ) <EOL> self . target . writeBlockMemoryUnaligned8 ( self . begin_data , bytes ) <EOL> page_info = self . getPageInfo ( flashPtr ) <EOL> result = self . callFunctionAndWait ( self . flash_algo [ '<STR_LIT>' ] , flashPtr , page_info . size , self . begin_data ) <EOL> if result != <NUM_LIT:0> : <EOL> logging . error ( '<STR_LIT>' , flashPtr , result ) <EOL> def getPageBufferCount ( self ) : <EOL> return len ( self . page_buffers ) <EOL> def isDoubleBufferingSupported ( self ) : <EOL> return self . double_buffer_supported <EOL> def startProgramPageWithBuffer ( self , bufferNumber , flashPtr ) : <EOL> """<STR_LIT>""" <EOL> assert bufferNumber < len ( self . page_buffers ) , "<STR_LIT>" <EOL> page_info = self . getPageInfo ( flashPtr ) <EOL> result = self . callFunction ( self . flash_algo [ '<STR_LIT>' ] , flashPtr , page_info . size , self . page_buffers [ bufferNumber ] ) <EOL> def loadPageBuffer ( self , bufferNumber , flashPtr , bytes ) : <EOL> assert bufferNumber < len ( self . page_buffers ) , "<STR_LIT>" <EOL> bytes = self . overrideSecurityBits ( flashPtr , bytes ) <EOL> self . target . writeBlockMemoryUnaligned8 ( self . page_buffers [ bufferNumber ] , bytes ) <EOL> def programPhrase ( self , flashPtr , bytes ) : <EOL> """<STR_LIT>""" <EOL> if self . min_program_length : <EOL> min_len = self . min_program_length <EOL> else : <EOL> min_len = self . getPageInfo ( flashPtr ) . size <EOL> if ( flashPtr % min_len ) or ( len ( bytes ) % min_len ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> bytes = self . overrideSecurityBits ( flashPtr , bytes ) <EOL> self . target . writeBlockMemoryUnaligned8 ( self . begin_data , bytes ) <EOL> result = self . callFunctionAndWait ( self . flash_algo [ '<STR_LIT>' ] , flashPtr , len ( bytes ) , self . begin_data ) <EOL> if result != <NUM_LIT:0> : <EOL> logging . error ( '<STR_LIT>' , flashPtr , result ) <EOL> def getPageInfo ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> region = self . target . getMemoryMap ( ) . getRegionForAddress ( addr ) <EOL> if not region : <EOL> return None <EOL> info = PageInfo ( ) <EOL> info . erase_weight = DEFAULT_PAGE_ERASE_WEIGHT <EOL> info . program_weight = DEFAULT_PAGE_PROGRAM_WEIGHT <EOL> info . size = region . blocksize <EOL> info . base_addr = addr - ( addr % info . size ) <EOL> return info <EOL> def getFlashInfo ( self ) : <EOL> """<STR_LIT>""" <EOL> boot_region = self . target . getMemoryMap ( ) . getBootMemory ( ) <EOL> info = FlashInfo ( ) <EOL> info . rom_start = boot_region . start if boot_region else <NUM_LIT:0> <EOL> info . erase_weight = DEFAULT_CHIP_ERASE_WEIGHT <EOL> info . crc_supported = self . flash_algo [ '<STR_LIT>' ] <EOL> return info <EOL> def getFlashBuilder ( self ) : <EOL> return FlashBuilder ( self , self . getFlashInfo ( ) . rom_start ) <EOL> def flashBlock ( self , addr , data , smart_flash = True , chip_erase = None , progress_cb = None , fast_verify = False ) : <EOL> """<STR_LIT>""" <EOL> flash_start = self . getFlashInfo ( ) . rom_start <EOL> fb = FlashBuilder ( self , flash_start ) <EOL> fb . addData ( addr , data ) <EOL> info = fb . program ( chip_erase , progress_cb , smart_flash , fast_verify ) <EOL> return info <EOL> def flashBinary ( self , path_file , flashPtr = None , smart_flash = True , chip_erase = None , progress_cb = None , fast_verify = False ) : <EOL> """<STR_LIT>""" <EOL> if flashPtr is None : <EOL> flashPtr = self . getFlashInfo ( ) . rom_start <EOL> f = open ( path_file , "<STR_LIT:rb>" ) <EOL> with open ( path_file , "<STR_LIT:rb>" ) as f : <EOL> data = f . read ( ) <EOL> data = unpack ( str ( len ( data ) ) + '<STR_LIT:B>' , data ) <EOL> self . flashBlock ( flashPtr , data , smart_flash , chip_erase , progress_cb , fast_verify ) <EOL> def callFunction ( self , pc , r0 = None , r1 = None , r2 = None , r3 = None , init = False ) : <EOL> reg_list = [ ] <EOL> data_list = [ ] <EOL> if self . flash_algo_debug : <EOL> self . _vector_catch_enabled = self . target . getVectorCatchFault ( ) <EOL> self . _reset_catch_enabled = self . target . getVectorCatchReset ( ) <EOL> self . target . setVectorCatchFault ( True ) <EOL> self . target . setVectorCatchReset ( True ) <EOL> if init : <EOL> self . target . writeBlockMemoryAligned32 ( self . flash_algo [ '<STR_LIT>' ] , self . flash_algo [ '<STR_LIT>' ] ) <EOL> if self . flash_algo [ '<STR_LIT>' ] : <EOL> self . target . writeBlockMemoryAligned32 ( self . flash_algo [ '<STR_LIT>' ] , analyzer ) <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( pc ) <EOL> if r0 is not None : <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( r0 ) <EOL> if r1 is not None : <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( r1 ) <EOL> if r2 is not None : <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( r2 ) <EOL> if r3 is not None : <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( r3 ) <EOL> if init : <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( self . static_base ) <EOL> if init : <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( self . begin_stack ) <EOL> reg_list . append ( '<STR_LIT>' ) <EOL> data_list . append ( self . flash_algo [ '<STR_LIT>' ] + <NUM_LIT:1> ) <EOL> self . target . writeCoreRegistersRaw ( reg_list , data_list ) <EOL> self . target . resume ( ) <EOL> def waitForCompletion ( self ) : <EOL> while ( self . target . getState ( ) == Target . TARGET_RUNNING ) : <EOL> pass <EOL> if self . flash_algo_debug : <EOL> analyzer_supported = self . flash_algo [ '<STR_LIT>' ] <EOL> expected_fp = self . flash_algo [ '<STR_LIT>' ] <EOL> expected_sp = self . flash_algo [ '<STR_LIT>' ] <EOL> expected_pc = self . flash_algo [ '<STR_LIT>' ] <EOL> expected_flash_algo = self . flash_algo [ '<STR_LIT>' ] <EOL> if analyzer_supported : <EOL> expected_analyzer = analyzer <EOL> final_fp = self . target . readCoreRegister ( '<STR_LIT>' ) <EOL> final_sp = self . target . readCoreRegister ( '<STR_LIT>' ) <EOL> final_pc = self . target . readCoreRegister ( '<STR_LIT>' ) <EOL> error = False <EOL> if final_fp != expected_fp : <EOL> logging . error ( "<STR_LIT>" % ( expected_fp , final_fp ) ) <EOL> error = True <EOL> if final_sp != expected_sp : <EOL> logging . error ( "<STR_LIT>" % ( expected_sp , final_sp ) ) <EOL> error = True <EOL> if final_pc != expected_pc : <EOL> logging . error ( "<STR_LIT>" % ( expected_pc , final_pc ) ) <EOL> error = True <EOL> assert error == False <EOL> self . target . setVectorCatchFault ( self . _vector_catch_enabled ) <EOL> self . target . setVectorCatchReset ( self . _reset_catch_enabled ) <EOL> return self . target . readCoreRegister ( '<STR_LIT>' ) <EOL> def callFunctionAndWait ( self , pc , r0 = None , r1 = None , r2 = None , r3 = None , init = False ) : <EOL> self . callFunction ( pc , r0 , r1 , r2 , r3 , init ) <EOL> return self . waitForCompletion ( ) <EOL> def setFlashAlgoDebug ( self , enable ) : <EOL> """<STR_LIT>""" <EOL> self . flash_algo_debug = enable <EOL> def overrideSecurityBits ( self , address , data ) : <EOL> return data </s>
