-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupload_rom.py
53 lines (43 loc) · 1.35 KB
/
upload_rom.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
## Author: Arpan Das
## Date: Fri Jan 11 12:16:59 2019 +0530
## URL: https://github.com/Cyberster/SPI-Based-EEPROM-Reader-Writer
## It listens to serial port and send data to serial port
## requires pySerial to be installed
import sys
import serial
import time
MEMORY_SIZE = 1048576 # In bytes
serial_port = '/dev/ttyACM0'
baud_rate = 115200 # In arduino, Serial.begin(baud_rate)
ser = serial.Serial(serial_port, baud_rate)
fileName = raw_input("Please enter file name: ")
with open(fileName, mode='rb') as file: # b is important -> binary
start = time.time()
#i = 0
ser.write('H') # for handshake request
# whole file at a time
#fileContent = file.read()
# 1 byte at a time
for i in range(4096): # 4096 x 256 bytes = 1MB
# wait until arduino requests 'R' i.e. 256 bytes of data
while (ser.read() != 'R'): continue
for j in range(256):
byte_s = file.read(1)
if not byte_s: break
byte = byte_s[0]
ser.write(byte)
'''byte_s = file.read(256)
if not byte_s: break
ser.write(byte_s)'''
print str((i + 1) * 256), 'bytes of', MEMORY_SIZE, 'bytes has been uploaded.'
'''while 1:
byte_s = file.read(1)
if not byte_s:
break
byte = byte_s[0]
ser.write(byte)
print str(i + 1), 'bytes of', MEMORY_SIZE, 'bytes has been uploaded.'
i += 1'''
status = ser.readline()
print(status)
print '\nIt took', time.time()-start, 'seconds.'