3 from RAConnect import *
8 def verify_img(dev, img, start_addr, end_addr):
9 raise Exception("Not implemented")
11 def write_img(dev, img, start_addr, end_addr, verify=False):
13 if os.path.exists(img):
14 file_size = os.path.getsize(img)
16 raise Exception(f'file {img} does not exist')
18 # calculate / check start and end address
19 if start_addr == None or end_addr == None:
20 if start_addr == None:
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
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)
37 # chunk = f.read(chunk_size)
38 packed = pack_pkt(WRI_CMD, chunk)
39 print(f'Sending {len(chunk)} bytes')
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)
49 dev = RAConnect(vendor_id=0x1a86, product_id=0x7523)
50 write_img(dev, 'src/sample.bin', 0x800, None, True)