Skip to content

Commit a5a209c

Browse files
committed
tools/pyboard.py: Add fast raw-paste mode.
1 parent af84dce commit a5a209c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tools/pyboard.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,54 @@ def exec_raw_no_follow(self, command):
370370
if not data.endswith(b">"):
371371
raise PyboardError("could not enter raw repl")
372372

373+
t0 = time.time_ns()
374+
375+
if True:
376+
# raw-paste mode
377+
self.serial.write(b"\x05A\x01")
378+
data = self.serial.read(2)
379+
if data == b"R\x00":
380+
# unsupported raw-paste mode
381+
pass
382+
elif data == b"R\x01":
383+
# board supports raw-paste mode
384+
data = self.serial.read(2)
385+
window_size = data[0] | data[1] << 8
386+
window_remain = window_size
387+
i = 0
388+
while i < len(command_bytes):
389+
while window_remain == 0 or self.serial.inWaiting():
390+
data = self.serial.read(1)
391+
if data == b"\x01":
392+
window_remain += window_size
393+
elif data == b"\x04":
394+
self.serial.write(b"\x04")
395+
return
396+
else:
397+
assert 0, data
398+
b = command_bytes[i : min(i + window_remain, len(command_bytes))]
399+
self.serial.write(b)
400+
window_remain -= len(b)
401+
i += len(b)
402+
self.serial.write(b"\x04")
403+
404+
data = self.read_until(1, b"\x04")
405+
if not data.endswith(b"\x04"):
406+
print(data)
407+
raise PyboardError("could not complete raw paste")
408+
409+
dt = (time.time_ns() - t0) // 1000000 + 1
410+
print(
411+
"raw-paste took", dt, len(command_bytes), len(command_bytes) * 10 / (dt * 1e-3)
412+
)
413+
return
414+
else:
415+
# board doesn't support raw-paste
416+
data = self.read_until(1, b"w REPL; CTRL-B to exit\r\n>")
417+
if not data.endswith(b"w REPL; CTRL-B to exit\r\n>"):
418+
print(data)
419+
raise PyboardError("could not enter raw repl")
420+
373421
# write command
374422
for i in range(0, len(command_bytes), 256):
375423
self.serial.write(command_bytes[i : min(i + 256, len(command_bytes))])
@@ -381,6 +429,9 @@ def exec_raw_no_follow(self, command):
381429
if data != b"OK":
382430
raise PyboardError("could not exec command (response: %r)" % data)
383431

432+
dt = (time.time_ns() - t0) // 1000000 + 1
433+
print("raw took", dt, len(command_bytes), len(command_bytes) * 10 / (dt * 1e-3))
434+
384435
def exec_raw(self, command, timeout=10, data_consumer=None):
385436
self.exec_raw_no_follow(command)
386437
return self.follow(timeout, data_consumer)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy