b1a3f6aa006c63965d396f50f455ea1b14ec0866
[renesas-ra-flasher] / src / RAFlasher.py
1 import os
2 import math
3 from RAConnect import *
4 from RAPacker import *
5
6 SECTOR_SIZE = 2048
7
8 def verify_img(dev, img, start_addr, end_addr):
9     raise Exception("Not implemented")
10
11 def write_img(dev, img, start_addr, end_addr, verify=False):
12
13     if os.path.exists(img):
14         file_size = os.path.getsize(img)
15     else:
16         raise Exception(f'file {img} does not exist')
17
18     # calculate / check start and end address 
19     if start_addr == None or end_addr == None:
20         if start_addr == None:
21             start_addr = 0
22         # align start addr
23         if start_addr % SECTOR_SIZE:
24             raise ValueError(f"start addr not aligned on sector size {SECTOR_SIZE}")
25         blocks = (file_size + SECTOR_SIZE - 1) // SECTOR_SIZE
26         end_addr = blocks * SECTOR_SIZE + start_addr
27         print(end_addr)
28     
29     chunk_size = 64 # max is 1024
30     if (start_addr > 0xFF800): # for RA4 series
31         raise ValueError("start address value error")
32     if (end_addr <= start_addr or end_addr > 0xFF800):
33         raise ValueError("end address value error")
34     with open(img, 'rb') as f:
35         chunk = f.read(chunk_size)
36         #while chunk:
37         #    chunk = f.read(chunk_size)
38         packed = pack_pkt(WRI_CMD, chunk)
39         print(f'Sending {len(chunk)} bytes')
40         dev.send_data(packed)
41         reply_len = 7
42         reply = dev.recv_data(reply_len)
43         reply = b'\x81\x00\x02\x00\x00\xFE\x03'
44         if not reply == False:
45             msg = unpack_pkt(reply)
46             print(msg)
47
48
49 dev = RAConnect(vendor_id=0x1a86, product_id=0x7523)
50 write_img(dev, 'src/sample.bin', 0x800, None, True)
51