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