def main(): argparser = argparse.ArgumentParser(description="IAP downloader for hm51") argparser.add_argument( '-s', '--sent-bytes-dump-file', dest='sent_bytes_dump_file', help= 'simulate a promgramming process, store the sent bytes to the file', action='store', type=argparse.FileType('wb'), default=None) argparser.add_argument( '-r', '--expected-received-bytes-dump-file', dest='expected_received_bytes_dump_file', help= 'simulate a promgramming process, store the expected received bytes to the file', action='store', type=argparse.FileType('wb'), default=None) argparser.add_argument('-p', '--port', dest='port', help='target port', action='store') argparser.add_argument('-b', '--baudrate', dest='baudrate', help='target baudrate', action='store', type=int, default=62500) argparser.add_argument( '-f', '--file', dest='file', help= 'the ROM file, the file with suffix of "hex" or "ihex" will be decoded as intel hex format' ', otherwise it will be read as binary.', action='store') argparser.add_argument( '--test', dest='test', nargs=2, type=lambda s: int(s, 0), metavar=('START_ADDR', 'END_ADDR'), help= 'test the ROM by filling it with generated data, first int is start address' ', second argument is end address. end address is als included', action='store') argparser.add_argument('-t', '--timeout', dest='timeout', help='target baud rate', action='store', type=float, default=1) p = argparser.parse_args() print(">> connecting device...") if p.sent_bytes_dump_file is None and p.expected_received_bytes_dump_file is None: print("chose a real device.") s = Serial(port=p.port, baudrate=p.baudrate, timeout=0.001) s = ByteIOProtocolCodec(s, ROM_tWC, p.timeout) else: print("chose 'dumping to file'.") s = DumpProtocolCodec(ROM_tWC, p.sent_bytes_dump_file, p.expected_received_bytes_dump_file) print("[OK]") print(">> handshaking...") for i in range(RETRY_HANDSHAKE): try: s.handshake(SEQ_HANDSHAKE) break except ProtocolCodecTimeoutException as e: print("[INFO] no response, retry.") if i + 1 == RETRY_HANDSHAKE: print("[ERROR] handshake failed after retrying {} times.".format( RETRY_HANDSHAKE)) exit(-1) print("[OK]") print(">> proramming the ROM...") data = None if p.test: start_address = p.test[0] end_address = p.test[1] print('test the ROM at range 0x{:4>0X}-0x{:4>0X}'.format( start_address, end_address)) data = [] for i in range(end_address - start_address + 1): i %= 257 data.append((i) % 256) data = {start_address: data} else: if pathlib.Path(p.file).suffix in ('.hex', '.ihex'): print('load ihex file "{}"'.format(p.file)) with open(p.file, 'r') as f: data = decode_ihex(f.read()) else: print('load binary file "{}"'.format(p.file)) with open(p.file, "rb") as f: data = {0: f.read()} programming_file(s, data) s.exit() print("[END]")