From 6a0a1c9d87352c9a601c146d2338c2fa71b5fba3 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Thu, 12 Sep 2019 10:21:37 +0200 Subject: [PATCH] tools/litex_term/upload: bufferize only chunks of the file instead of the entire file to speedup upload when used on embedded devices (RPI for example) --- litex/tools/litex_term.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/litex/tools/litex_term.py b/litex/tools/litex_term.py index d4607b63a..39fb60aa1 100755 --- a/litex/tools/litex_term.py +++ b/litex/tools/litex_term.py @@ -193,20 +193,22 @@ class LiteXTerm: return 1 def upload(self, filename, address): - with open(filename, "rb") as f: - data = f.read() - print("[LXTERM] Uploading {} to 0x{:08x} ({} bytes)...".format(filename, address, len(data))) + f = open(filename, "rb") + f.seek(0, 2) + length = f.tell() + f.seek(0, 0) + print("[LXTERM] Uploading {} to 0x{:08x} ({} bytes)...".format(filename, address, length)) current_address = address position = 0 - length = len(data) start = time.time() - while len(data): + remaining = length + while remaining: sys.stdout.write("|{}>{}| {}%\r".format('=' * (20*position//length), ' ' * (20-20*position//length), 100*position//length)) sys.stdout.flush() frame = SFLFrame() - frame_data = data[:sfl_payload_length] + frame_data = f.read(min(remaining, sfl_payload_length)) frame.cmd = sfl_cmd_load if not self.no_crc else sfl_cmd_load_no_crc frame.payload = current_address.to_bytes(4, "big") frame.payload += frame_data @@ -214,12 +216,10 @@ class LiteXTerm: return current_address += len(frame_data) position += len(frame_data) - try: - data = data[sfl_payload_length:] - except: - data = [] + remaining -= len(frame_data) end = time.time() elapsed = end - start + f.close() print("[LXTERM] Upload complete ({0:.1f}KB/s).".format(length/(elapsed*1024))) return length