RAFlasher.py: initial hardware test
[renesas-ra-flasher] / src / RAFlasher.py
1 import os
2 import math
3 import argparse
4 from RAConnect import *
5 from RAPacker import *
6
7 SECTOR_SIZE = 2048
8
9 def int_to_hex_list(num):
10     hex_string = hex(num)[2:].upper()  # convert to hex string
11     hex_string = hex_string.zfill(8) # pad for 8 char's long
12     hex_list = [f'0x{hex_string[c:c+2]}' for c in range(0, 8, 2)]
13     return hex_list
14
15 def get_dev_info(dev):
16
17     packed = pack_pkt(SIG_CMD, "")
18     dev.send_data(packed)
19     print(packed)
20     info = dev.recv_data(18)
21     print(info, len(info))
22     #info = b'\x81\x00\x0D\x3A\x01\x31\x2d\x00\x00\x1e\x84\x80\x04\x02\x0a\x08' # test
23     fmt = '>IIIBBHH'
24     _HEADER, SCI, RMB, NOA, TYP, BFV, _FOOTER = struct.unpack(fmt, info)
25     print(f'Ver{BFV >> 8}.{BFV & 0xFF}')
26
27 def verify_img(dev, img, start_addr, end_addr):
28     raise Exception("Not implemented")
29
30 def write_img(dev, img, start_addr, end_addr, verify=False):
31
32     if os.path.exists(img):
33         file_size = os.path.getsize(img)
34     else:
35         raise Exception(f'file {img} does not exist')
36
37     # calculate / check start and end address 
38     if start_addr == None or end_addr == None:
39         if start_addr == None:
40             start_addr = 0
41         # align start addr
42         if start_addr % SECTOR_SIZE:
43             raise ValueError(f"start addr not aligned on sector size {SECTOR_SIZE}")
44         blocks = (file_size + SECTOR_SIZE - 1) // SECTOR_SIZE
45         end_addr = blocks * SECTOR_SIZE + start_addr
46         print(end_addr)
47     
48     chunk_size = 64 # max is 1024
49     if (start_addr > 0xFF800): # for RA4 series
50         raise ValueError("start address value error")
51     if (end_addr <= start_addr or end_addr > 0xFF800):
52         raise ValueError("end address value error")
53
54     # setup initial communication
55     SAD = int_to_hex_list(start_addr)
56     EAD = int_to_hex_list(end_addr)
57     packed = pack_pkt(WRI_CMD, SAD + EAD)
58     dev.send_data(packed)
59     ret = dev.recv_data(7)
60
61     with open(img, 'rb') as f:
62         chunk = f.read(chunk_size)
63         while chunk:
64             packed = pack_pkt(WRI_CMD, chunk)
65             print(f'Sending {len(chunk)} bytes')
66             dev.send_data(packed)
67             reply_len = 7
68             reply = dev.recv_data(reply_len)
69             #reply = b'\x81\x00\x02\x00\x00\xFE\x03' # test reply
70             if not reply == False:
71                 msg = unpack_pkt(reply)
72                 print(msg)
73             chunk = f.read(chunk_size)
74
75 def main():
76     parser = argparse.ArgumentParser(description="RA Flasher Tool")
77
78     subparsers = parser.add_subparsers(dest="command", title="Commands")
79
80     # Subparser for the write command
81     write_parser = subparsers.add_parser("write", help="Write data to flash")
82     write_parser.add_argument("--start_address", type=int, default=0x0000, help="Start address")
83     write_parser.add_argument("--end_address", type=int, help="End address")
84     write_parser.add_argument("--verify", action="store_true", help="Verify after writing")
85     write_parser.add_argument("file_name", type=str, help="File name")
86
87     # Subparser for the read command
88     read_parser = subparsers.add_parser("read", help="Read data from flash")
89     read_parser.add_argument("--start_address", type=int, default=0x000, help="Start address")
90     read_parser.add_argument("--size", type=int, default=1024, help="Size in bytes")
91     read_parser.add_argument("file_name", type=str, help="File name")
92
93     # Subparser for the info command
94     subparsers.add_parser("info", help="Show flasher information")
95
96     args = parser.parse_args()
97
98     if args.command == "write":
99         dev = RAConnect(vendor_id=0x045B, product_id=0x0261)
100         print(args)
101         write_img(dev, args.file_name, args.start_address, args.end_address, args.verify)
102     elif args.command == "read":
103         print('read command')
104     elif args.command == "info":
105         dev = RAConnect(vendor_id=0x045B, product_id=0x0261)
106         dev.establish_connection()
107         dev.confirm_connection()
108         get_dev_info(dev)
109     else:
110         parser.print_help()
111
112 if __name__ == "__main__":
113     main()
114