Skip to content

Commit 528f4bf

Browse files
committed
docs/reference/repl.rst: Add information about new raw-paste mode.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 72f8639 commit 528f4bf

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

docs/reference/repl.rst

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,68 @@ So you can use the underscore to save the result in a variable. For example:
196196
15
197197
>>>
198198

199-
Raw mode
200-
--------
199+
Raw mode and raw-paste mode
200+
---------------------------
201201

202-
Raw mode is not something that a person would normally use. It is intended for
203-
programmatic use. It essentially behaves like paste mode with echo turned off.
202+
Raw mode (also called raw REPL) is not something that a person would normally use.
203+
It is intended for programmatic use and essentially behaves like paste mode with
204+
echo turned off, and with optional flow control.
204205

205206
Raw mode is entered using Ctrl-A. You then send your python code, followed by
206207
a Ctrl-D. The Ctrl-D will be acknowledged by 'OK' and then the python code will
207208
be compiled and executed. Any output (or errors) will be sent back. Entering
208209
Ctrl-B will leave raw mode and return the the regular (aka friendly) REPL.
209210

210-
The ``tools/pyboard.py`` program uses the raw REPL to execute python files on the
211-
MicroPython board.
212-
211+
Raw-paste mode is an additional mode within the raw REPL that includes flow control,
212+
and which compiles code as it receives it. This makes it more robust for high-speed
213+
transfer of code into the device, and it also uses less RAM when receiving because
214+
it does not need to store a verbatim copy of the code before compiling (unlike
215+
standard raw mode).
216+
217+
Raw-paste mode uses the following protocol:
218+
1. Enter raw REPL as usual via ctrl-A.
219+
2. Write 3 bytes: b"\x05A\x01" (ie ctrl-E then "A" then ctrl-A).
220+
3. Read 2 bytes to determine if the device entered raw-paste mode:
221+
- 3a: If the result is b"R\x00" then the device understands the command but
222+
doesn't support raw paste.
223+
- 3b: If the result is b"R\x01" then the device does support raw paste and has
224+
entered this mode.
225+
- 3c: Otherwise the result should be b"ra" and the device doesn't support raw
226+
paste and the string b"w REPL; CTRL-B to exit\r\n>" should be read and
227+
discarded.
228+
4. If the device is in raw-paste mode then continue, otherwise fallback to standard
229+
raw mode.
230+
5. Read 2 bytes, this is the flow-control window size (in bytes) stored as a 16-bit
231+
unsigned little endian integer. The remaining window size should be set to this
232+
number.
233+
6. Write out the code to the device:
234+
- 6a. While there are bytes to send, write up to the remaining window size.
235+
- 6b. If the window size is 0, or there is a byte waiting to read, read 1 byte.
236+
If this byte is b"\x01" (ctrl-A) then increase the remaining window size
237+
by the window size from step 5. If this byte is b"\x04" (ctrl-D) then the
238+
device wants to end the data reception, and b"\x04" (ctrl-D) should be
239+
written to the device and no more code sent after that.
240+
7. When all code has been written to the device, write b"\x04" (ctrl-D) to
241+
indicate end-of-data.
242+
8. Read from the device until b"\x04" (ctrl-D) is received. At this point the
243+
device has received and compiled all of the code that was sent and is executing
244+
it.
245+
9. The device outputs any characters produced by the executing code. When (if)
246+
the code finishes b"\x04" (ctrl-D) will be output, followed by any exception
247+
that was uncaught, followed again by b"\x04" (ctrl-D). It then goes back to
248+
the standard raw REPL and outputs b">".
249+
250+
For example, starting at a new line at the normal (friendly) REPL, if you write:
251+
252+
b'\x01\x05A\x01print(123)\x04'
253+
254+
Then the device will respond with something like:
255+
256+
b'\r\nraw REPL; CTRL-B to exit\r\n>R\x01\x80\x00\x01\x04123\r\n\x04\x04>'
257+
258+
In this case the flow-control window size is 128 and there is a new window
259+
immediately available at the start (ie up to 256 bytes can be written to begin
260+
with before waiting or checking for more incoming flow-control characters).
261+
262+
The ``tools/pyboard.py`` program uses the raw REPL, including raw-paste mode, to
263+
execute Python code on a MicroPython-enabled board.

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