Compare commits

...

2 Commits

Author SHA1 Message Date
b9562c04e0 Add ability to add output marker via external signal
For example, a script to flash the chip can use this to mark the
output so that it's clear when the target was actually reset.
2020-05-19 10:23:15 -04:00
b3399e6134 Improve throughput by reading data in larger chunks
Once a first byte is received, read as many as are immediately
available in larger chunks before going back to single-byte reads.
This prevents dropped data when the host is sending it quickly.
2020-05-19 10:21:51 -04:00

View File

@ -13,6 +13,20 @@ color_lookup = { "red": 31, "green": 32, "cyan": 36, "yellow": 33 }
def color(name, text):
return "\033[%dm%s\033[0m" % (color_lookup[name], text);
try:
# On Unix systems, print an additional marker in the output stream
# when SIGUSR1 or SIGUSR2 is received. This is just to help clarify
# output when the chip is externally reset, etc.
import signal
def sigusr1_handler(signum, frame):
print(color("yellow", "--- mark ---"))
def sigusr2_handler(signum, frame):
print(color("yellow", "--- reset ---"))
signal.signal(signal.SIGUSR1, sigusr1_handler)
signal.signal(signal.SIGUSR2, sigusr2_handler)
except AttributeError as e:
pass
class ResyncException(Exception):
pass
@ -69,6 +83,7 @@ class ITMParser:
except TimeoutException as e:
# Timeout inside a parser should be reported.
print(color("red", "Timeout"))
break
except ResyncException as e:
print(color("red", "Resync"))
@ -153,15 +168,32 @@ def main(argv):
args = parser.parse_args()
ser = serial.Serial(args.device, args.baudrate)
ser.timeout = 1
print(color('green', 'ready'))
def input_stream():
while True:
# Read one byte with a 1s timeout.
ser.timeout = 1
data = ser.read(1)
if len(data) == 0:
# Timeout
yield None
else:
yield data[0]
continue
yield data[0]
# Then read as many more as there are immediately
# available, and send them. This is more efficient than
# reading each individual byte, when they're coming in
# fast. Once they stop, we'll go back to the normal
# 1 byte read with timeout.
ser.timeout = 0
while True:
data = ser.read(65536)
if len(data) == 0:
break
for c in data:
yield c
try:
ITMParser(input_stream()).process()