Skip to content

Commit 1841437

Browse files
committed
tools/pyboard.py: Add fast raw-paste mode.
This commit adds support to pyboard.py for the new raw REPL paste mode. Note that this new pyboard.py is fully backwards compatible with old devices (it detects if the device supports the new raw REPL paste mode). Signed-off-by: Damien George <damien@micropython.org>
1 parent b68f779 commit 1841437

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

tools/pyboard.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def inWaiting(self):
253253

254254
class Pyboard:
255255
def __init__(self, device, baudrate=115200, user="micro", password="python", wait=0):
256+
self.use_raw_paste = True
256257
if device.startswith("exec:"):
257258
self.serial = ProcessToSerial(device[len("exec:") :])
258259
elif device.startswith("execpty:"):
@@ -359,6 +360,41 @@ def follow(self, timeout, data_consumer=None):
359360
# return normal and error output
360361
return data, data_err
361362

363+
def raw_paste_write(self, command_bytes):
364+
# Read initial header, with window size.
365+
data = self.serial.read(2)
366+
window_size = data[0] | data[1] << 8
367+
window_remain = window_size
368+
369+
# Write out the command_bytes data.
370+
i = 0
371+
while i < len(command_bytes):
372+
while window_remain == 0 or self.serial.inWaiting():
373+
data = self.serial.read(1)
374+
if data == b"\x01":
375+
# Device indicated that a new window of data can be sent.
376+
window_remain += window_size
377+
elif data == b"\x04":
378+
# Device indicated abrupt end. Acknowledge it and finish.
379+
self.serial.write(b"\x04")
380+
return
381+
else:
382+
# Unexpected data from device.
383+
raise PyboardError("unexpected read during raw paste: {}".format(data))
384+
# Send out as much data as possible that fits within the allowed window.
385+
b = command_bytes[i : min(i + window_remain, len(command_bytes))]
386+
self.serial.write(b)
387+
window_remain -= len(b)
388+
i += len(b)
389+
390+
# Indicate end of data.
391+
self.serial.write(b"\x04")
392+
393+
# Wait for device to acknowledge end of data.
394+
data = self.read_until(1, b"\x04")
395+
if not data.endswith(b"\x04"):
396+
raise PyboardError("could not complete raw paste: {}".format(data))
397+
362398
def exec_raw_no_follow(self, command):
363399
if isinstance(command, bytes):
364400
command_bytes = command
@@ -370,7 +406,24 @@ def exec_raw_no_follow(self, command):
370406
if not data.endswith(b">"):
371407
raise PyboardError("could not enter raw repl")
372408

373-
# write command
409+
if self.use_raw_paste:
410+
# Try to enter raw-paste mode.
411+
self.serial.write(b"\x05A\x01")
412+
data = self.serial.read(2)
413+
if data == b"R\x00":
414+
# Device understood raw-paste command but doesn't support it.
415+
pass
416+
elif data == b"R\x01":
417+
# Device supports raw-paste mode, write out the command using this mode.
418+
return self.raw_paste_write(command_bytes)
419+
else:
420+
# Device doesn't support raw-paste, fall back to normal raw REPL.
421+
data = self.read_until(1, b"w REPL; CTRL-B to exit\r\n>")
422+
if not data.endswith(b"w REPL; CTRL-B to exit\r\n>"):
423+
print(data)
424+
raise PyboardError("could not enter raw repl")
425+
426+
# Write command using standard raw REPL, 256 bytes every 10ms.
374427
for i in range(0, len(command_bytes), 256):
375428
self.serial.write(command_bytes[i : min(i + 256, len(command_bytes))])
376429
time.sleep(0.01)

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