1#!/usr/bin/env python 2import os, sys, re, time, json 3from flash_program_ll import burn_bin_files 4 5try: 6 import serial 7 from serial.tools import miniterm 8except: 9 print("\nNot found pyserial, please install it by: \nsudo python%d -m pip install pyserial" % (sys.version_info.major)) 10 sys.exit(-1) 11 12def get_bin_file(): 13 """ get binary file from sys.argv --bin=/xxx/yyy/zzz.bin """ 14 bin_files = [] 15 pattern = re.compile(r'--(.*)=(.*)') 16 for arg in sys.argv[1:]: 17 if arg.startswith("--"): 18 match = pattern.match(arg) 19 if match: 20 key = match.group(1) 21 value = match.group(2) 22 if key == 'bin': 23 bin_files.append(value) 24 return bin_files 25 26def read_json(json_file): 27 data = None 28 if os.path.isfile(json_file): 29 with open(json_file, 'r') as f: 30 data = json.load(f) 31 return data 32 33def write_json(json_file, data): 34 with open(json_file, 'w') as f: 35 f.write(json.dumps(data, indent=4, separators=(',', ': '))) 36 37def get_config(): 38 """ get configuration from .config_burn file, if it is not existed, 39 generate default configuration of chip_haas1000 """ 40 configs = {} 41 config_file = os.path.join(os.getcwd(), '.config_burn') 42 if os.path.isfile(config_file): 43 configs = read_json(config_file) 44 if not configs: 45 configs = {} 46 if 'chip_haas1000' not in configs: 47 configs['chip_haas1000'] = {} 48 if 'serialport' not in configs['chip_haas1000']: 49 configs['chip_haas1000']['serialport'] = "" 50 if 'baudrate' not in configs['chip_haas1000']: 51 configs['chip_haas1000']['baudrate'] = "1500000" 52 if 'binfile' not in configs['chip_haas1000']: 53 configs['chip_haas1000']['binfile'] = [] 54 55 return configs['chip_haas1000'] 56 57def save_config(config): 58 """ save configuration to .config_burn file, only update chip_haas1000 portion """ 59 if config: 60 configs = {} 61 config_file = os.path.join(os.getcwd(), '.config_burn') 62 if os.path.isfile(config_file): 63 configs = read_json(config_file) 64 if not configs: 65 configs = {} 66 configs['chip_haas1000'] = config 67 write_json(config_file, configs) 68 69def check_uart(portnum, baudrate): 70 serialport = serial.Serial() 71 serialport.port = portnum 72 serialport.baudrate = baudrate 73 serialport.parity = "N" 74 serialport.bytesize = 8 75 serialport.stopbits = 1 76 serialport.timeout = 1 77 78 try: 79 serialport.open() 80 except Exception as e: 81 print("check_uart open serialport: %s error " % portnum) 82 return False 83 84 serialport.close() 85 return True 86 87def main(): 88 # step 1: get binary file 89 needsave = False 90 myconfig = get_config() 91 bin_files = get_bin_file() 92 if bin_files: 93 myconfig["binfile"] = bin_files 94 needsave = True 95 if not myconfig["binfile"]: 96 print("no specified binary file") 97 return 98 print("binary file is %s" % myconfig["binfile"]) 99 100 # step 2: get serial port 101 if not myconfig["serialport"]: 102 myconfig["serialport"] = miniterm.ask_for_port() 103 if not myconfig["serialport"]: 104 print("no specified serial port") 105 return 106 else: 107 needsave = True 108 109 while check_uart(myconfig["serialport"], myconfig['baudrate']) == False: 110 myconfig["serialport"] = miniterm.ask_for_port() 111 112 print("serial port is %s" % myconfig["serialport"]) 113 print("the settings were restored in the file %s" % os.path.join(os.getcwd(), '.config_burn')) 114 115 # step 3: burn binary file into flash 116 bin_files = [] 117 for bin_file in myconfig["binfile"]: 118 filename = bin_file 119 address = "0" 120 if "#" in bin_file: 121 filename = bin_file.split("#", 1)[0] 122 address = bin_file.split("#", 1)[1] 123 bin_files.append((filename, address)) 124 print("bin_files is ", bin_files) 125 burn_bin_files(myconfig["serialport"], myconfig['baudrate'], bin_files) 126 if needsave: 127 save_config(myconfig) 128 129if __name__ == "__main__": 130 main() 131