<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import argparse , os , sys <EOL> from time import sleep , time <EOL> from random import randrange <EOL> import math <EOL> import struct <EOL> import traceback <EOL> import argparse <EOL> parentdir = os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , parentdir ) <EOL> import pyOCD <EOL> from pyOCD . board import MbedBoard <EOL> from pyOCD . utility . conversion import float32beToU32be <EOL> from pyOCD . flash . flash import Flash <EOL> from pyOCD . flash . flash_builder import FlashBuilder <EOL> from test_util import Test , TestResult <EOL> addr = <NUM_LIT:0> <EOL> size = <NUM_LIT:0> <EOL> board = None <EOL> import logging <EOL> class FlashTestResult ( TestResult ) : <EOL> def __init__ ( self ) : <EOL> super ( FlashTestResult , self ) . __init__ ( None , None , None ) <EOL> self . chip_erase_rate_erased = None <EOL> self . page_erase_rate_same = None <EOL> self . page_erase_rate = None <EOL> self . analyze = None <EOL> self . analyze_rate = None <EOL> self . chip_erase_rate = None <EOL> class FlashTest ( Test ) : <EOL> def __init__ ( self ) : <EOL> super ( FlashTest , self ) . __init__ ( "<STR_LIT>" , flash_test ) <EOL> def print_perf_info ( self , result_list , output_file = None ) : <EOL> result_list = filter ( lambda x : isinstance ( x , FlashTestResult ) , result_list ) <EOL> print ( "<STR_LIT>" , file = output_file ) <EOL> perf_format_str = "<STR_LIT>" <EOL> print ( perf_format_str . format ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> file = output_file ) <EOL> print ( "<STR_LIT>" , file = output_file ) <EOL> for result in result_list : <EOL> if result . passed : <EOL> analyze_rate = "<STR_LIT>" % ( result . analyze_rate / float ( <NUM_LIT:1000> ) ) <EOL> analyze_time = "<STR_LIT>" % result . analyze_time <EOL> else : <EOL> analyze_rate = "<STR_LIT>" <EOL> analyze_time = "<STR_LIT>" <EOL> print ( perf_format_str . format ( result . board . target_type , <EOL> result . analyze , analyze_rate , <EOL> analyze_time ) , <EOL> file = output_file ) <EOL> print ( "<STR_LIT>" , file = output_file ) <EOL> print ( "<STR_LIT>" , file = output_file ) <EOL> rate_format_str = "<STR_LIT>" <EOL> print ( rate_format_str . format ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> file = output_file ) <EOL> print ( "<STR_LIT>" , file = output_file ) <EOL> for result in result_list : <EOL> if result . passed : <EOL> chip_erase_rate = "<STR_LIT>" % ( result . chip_erase_rate / float ( <NUM_LIT:1000> ) ) <EOL> page_erase_rate = "<STR_LIT>" % ( result . page_erase_rate / float ( <NUM_LIT:1000> ) ) <EOL> page_erase_rate_same = "<STR_LIT>" % ( result . page_erase_rate_same / float ( <NUM_LIT:1000> ) ) <EOL> else : <EOL> chip_erase_rate = "<STR_LIT>" <EOL> page_erase_rate = "<STR_LIT>" <EOL> page_erase_rate_same = "<STR_LIT>" <EOL> print ( rate_format_str . format ( result . board . target_type , <EOL> chip_erase_rate , page_erase_rate , <EOL> page_erase_rate_same ) , <EOL> file = output_file ) <EOL> print ( "<STR_LIT>" , file = output_file ) <EOL> def run ( self , board ) : <EOL> try : <EOL> result = self . test_function ( board . getUniqueID ( ) ) <EOL> except Exception as e : <EOL> result = FlashTestResult ( ) <EOL> result . passed = False <EOL> print ( "<STR_LIT>" % ( e , board . getUniqueID ( ) ) ) <EOL> traceback . print_exc ( file = sys . stdout ) <EOL> result . board = board <EOL> result . test = self <EOL> return result <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> def flash_test ( board_id ) : <EOL> with MbedBoard . chooseBoard ( board_id = board_id , frequency = <NUM_LIT> ) as board : <EOL> target_type = board . getTargetType ( ) <EOL> test_clock = <NUM_LIT> <EOL> if target_type == "<STR_LIT>" : <EOL> test_clock = <NUM_LIT> <EOL> memory_map = board . target . getMemoryMap ( ) <EOL> ram_regions = [ region for region in memory_map if region . type == '<STR_LIT>' ] <EOL> ram_region = ram_regions [ <NUM_LIT:0> ] <EOL> rom_region = memory_map . getBootMemory ( ) <EOL> ram_start = ram_region . start <EOL> ram_size = ram_region . length <EOL> rom_start = rom_region . start <EOL> rom_size = rom_region . length <EOL> target = board . target <EOL> link = board . link <EOL> flash = board . flash <EOL> link . set_clock ( test_clock ) <EOL> link . set_deferred_transfer ( True ) <EOL> test_pass_count = <NUM_LIT:0> <EOL> test_count = <NUM_LIT:0> <EOL> result = FlashTestResult ( ) <EOL> def print_progress ( progress ) : <EOL> assert progress >= <NUM_LIT:0.0> <EOL> assert progress <= <NUM_LIT:1.0> <EOL> assert ( progress == <NUM_LIT:0> and print_progress . prev_progress == <NUM_LIT:1.0> ) or ( progress >= print_progress . prev_progress ) <EOL> if progress == <NUM_LIT:0.0> : <EOL> print_progress . prev_progress = <NUM_LIT:0> <EOL> print_progress . backwards_progress = False <EOL> print_progress . done = False <EOL> if progress < print_progress . prev_progress : <EOL> print_progress . backwards_progress = True <EOL> print_progress . prev_progress = progress <EOL> if not print_progress . done : <EOL> sys . stdout . write ( '<STR_LIT:\r>' ) <EOL> i = int ( progress * <NUM_LIT> ) <EOL> sys . stdout . write ( "<STR_LIT>" % ( '<STR_LIT:=>' * i , round ( progress * <NUM_LIT:100> ) ) ) <EOL> sys . stdout . flush ( ) <EOL> if progress >= <NUM_LIT:1.0> : <EOL> if not print_progress . done : <EOL> print_progress . done = True <EOL> sys . stdout . write ( "<STR_LIT:\n>" ) <EOL> if print_progress . backwards_progress : <EOL> print ( "<STR_LIT>" ) <EOL> print_progress . prev_progress = <NUM_LIT:0> <EOL> binary_file = os . path . join ( parentdir , '<STR_LIT>' , board . getTestBinary ( ) ) <EOL> with open ( binary_file , "<STR_LIT:rb>" ) as f : <EOL> data = f . read ( ) <EOL> data = struct . unpack ( "<STR_LIT>" % len ( data ) , data ) <EOL> unused = rom_size - len ( data ) <EOL> addr = rom_start <EOL> size = len ( data ) <EOL> flash . setFlashAlgoDebug ( True ) <EOL> print ( "<STR_LIT>" ) <EOL> info = flash . flashBlock ( addr , data , False , False , progress_cb = print_progress ) <EOL> data_flashed = target . readBlockMemoryUnaligned8 ( addr , size ) <EOL> if same ( data_flashed , data ) and info . program_type is FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> info = flash . flashBlock ( addr , data , False , True , progress_cb = print_progress ) <EOL> data_flashed = target . readBlockMemoryUnaligned8 ( addr , size ) <EOL> if same ( data_flashed , data ) and info . program_type is FlashBuilder . FLASH_CHIP_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> info = flash . flashBlock ( addr , data , True , False , progress_cb = print_progress ) <EOL> data_flashed = target . readBlockMemoryUnaligned8 ( addr , size ) <EOL> if same ( data_flashed , data ) and info . program_type is FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> info = flash . flashBlock ( addr , data , True , True , progress_cb = print_progress ) <EOL> data_flashed = target . readBlockMemoryUnaligned8 ( addr , size ) <EOL> if same ( data_flashed , data ) and info . program_type is FlashBuilder . FLASH_CHIP_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> flash . setFlashAlgoDebug ( False ) <EOL> print ( "<STR_LIT>" ) <EOL> new_data = list ( data ) <EOL> new_data . extend ( unused * [ <NUM_LIT> ] ) <EOL> info = flash . flashBlock ( <NUM_LIT:0> , new_data , False , False , progress_cb = print_progress ) <EOL> if info . program_type == FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> result . page_erase_rate = float ( len ( new_data ) ) / float ( info . program_time ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> info = flash . flashBlock ( <NUM_LIT:0> , new_data , progress_cb = print_progress , fast_verify = True ) <EOL> if info . program_type == FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> addr = rom_start + rom_size / <NUM_LIT:2> <EOL> page_size = flash . getPageInfo ( addr ) . size <EOL> new_data = [ <NUM_LIT> ] * page_size * <NUM_LIT:2> <EOL> info = flash . flashBlock ( addr , new_data , progress_cb = print_progress ) <EOL> data_flashed = target . readBlockMemoryUnaligned8 ( addr , len ( new_data ) ) <EOL> if same ( data_flashed , new_data ) and info . program_type is FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> addr = rom_start + rom_size / <NUM_LIT:2> <EOL> page_size = flash . getPageInfo ( addr ) . size <EOL> more_data = [ <NUM_LIT> ] * page_size * <NUM_LIT:2> <EOL> addr = ( rom_start + rom_size / <NUM_LIT:2> ) + <NUM_LIT:1> <EOL> fb = flash . getFlashBuilder ( ) <EOL> fb . addData ( rom_start , data ) <EOL> fb . addData ( addr , more_data ) <EOL> fb . program ( progress_cb = print_progress ) <EOL> data_flashed = target . readBlockMemoryUnaligned8 ( rom_start , len ( data ) ) <EOL> data_flashed_more = target . readBlockMemoryUnaligned8 ( addr , len ( more_data ) ) <EOL> if same ( data_flashed , data ) and same ( data_flashed_more , more_data ) : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> test_pass = False <EOL> addr = ( rom_start + rom_size / <NUM_LIT:2> ) <EOL> page_size = flash . getPageInfo ( addr ) . size <EOL> new_data = [ <NUM_LIT> ] * page_size <EOL> fb = flash . getFlashBuilder ( ) <EOL> fb . addData ( addr , new_data ) <EOL> try : <EOL> fb . addData ( addr + <NUM_LIT:1> , new_data ) <EOL> except ValueError as e : <EOL> print ( "<STR_LIT>" % e ) <EOL> test_pass = True <EOL> if test_pass : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> fb = flash . getFlashBuilder ( ) <EOL> fb . program ( ) <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> addr = rom_start <EOL> flash . flashBlock ( rom_start , data , True ) <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> test_count += <NUM_LIT:1> <EOL> if rom_start == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> non_thumb_data = list ( data ) <EOL> non_thumb_data [ <NUM_LIT:4> ] = non_thumb_data [ <NUM_LIT:4> ] & ~ <NUM_LIT:1> <EOL> flash . flashBlock ( rom_start , non_thumb_data ) <EOL> flash . flashBlock ( rom_start , data ) <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> new_data = list ( data ) <EOL> new_data . extend ( [ <NUM_LIT> ] * unused ) <EOL> info = flash . flashBlock ( <NUM_LIT:0> , new_data , progress_cb = print_progress ) <EOL> if info . program_type == FlashBuilder . FLASH_CHIP_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> result . chip_erase_rate_erased = float ( len ( new_data ) ) / float ( info . program_time ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> new_data = list ( data ) <EOL> new_data . extend ( [ <NUM_LIT> ] * unused ) <EOL> info = flash . flashBlock ( <NUM_LIT:0> , new_data , progress_cb = print_progress ) <EOL> if info . program_type == FlashBuilder . FLASH_CHIP_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> result . chip_erase_rate = float ( len ( new_data ) ) / float ( info . program_time ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> new_data = list ( data ) <EOL> new_data . extend ( [ <NUM_LIT> ] * unused ) <EOL> info = flash . flashBlock ( <NUM_LIT:0> , new_data , progress_cb = print_progress ) <EOL> if info . program_type == FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> result . page_erase_rate_same = float ( len ( new_data ) ) / float ( info . program_time ) <EOL> result . analyze = info . analyze_type <EOL> result . analyze_time = info . analyze_time <EOL> result . analyze_rate = float ( len ( new_data ) ) / float ( info . analyze_time ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> new_data = list ( data ) <EOL> size_same = unused * <NUM_LIT:5> / <NUM_LIT:6> <EOL> size_differ = unused - size_same <EOL> new_data . extend ( [ <NUM_LIT> ] * size_same ) <EOL> new_data . extend ( [ <NUM_LIT> ] * size_differ ) <EOL> info = flash . flashBlock ( <NUM_LIT:0> , new_data , progress_cb = print_progress ) <EOL> if info . program_type == FlashBuilder . FLASH_PAGE_ERASE : <EOL> print ( "<STR_LIT>" ) <EOL> test_pass_count += <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> test_count += <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % ( test_pass_count , test_count ) ) <EOL> if test_pass_count == test_count : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> target . reset ( ) <EOL> result . passed = test_count == test_pass_count <EOL> return result <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> level = logging . DEBUG if args . debug else logging . INFO <EOL> logging . basicConfig ( level = level ) <EOL> board = pyOCD . board . mbed_board . MbedBoard . getAllConnectedBoards ( close = True ) [ <NUM_LIT:0> ] <EOL> test = FlashTest ( ) <EOL> result = [ test . run ( board ) ] <EOL> test . print_perf_info ( result ) </s>
