-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhiprog.py
63 lines (49 loc) · 1.47 KB
/
hiprog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
serial_prog.py
This program talks to an RP2040 via serial port. It sends an
iCE40 FPGA bitstream (.bin) file to the RP which uploads it to the
FPGA via SPI.
Author: Mahesh Venkitachalam (electronut.in)
"""
import serial
import argparse
import os
def main():
print("Starting serial_prog...")
# parse arguments
parser = argparse.ArgumentParser(description="This programs sends bitsteram to RP2040.")
# add arguments
parser.add_argument('--p', dest='port', required=True)
parser.add_argument('--f', dest='filename', required=True)
args = parser.parse_args()
# serial port
port = args.port
# file name
filename = args.filename
file_size = os.path.getsize(filename)
print("File size = {} bytes.".format(file_size))
f = open(filename, "rb")
chunk_size = 64
# open serial
ser = serial.Serial(port, 115200)
print("writing...")
# send file size in 32 bit header
count = 0
try:
nbytes = 0
while True:
bytes = f.read(chunk_size)
nbytes += len(bytes)
#print(bytes)
if not bytes:
break
ser.write(bytes)
#print("wrote {} bytes...".format(nbytes))
except Exception as e:
print(e)
ser.close()
f.close()
print("wrote {} bytes...".format(nbytes))
print("done.")
if __name__ == "__main__":
main()