Skip to content

Commit d9d5693

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 d9d5693

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

docs/reference/repl.rst

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,81 @@ 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.
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).
212216

217+
Raw-paste mode uses the following protocol:
218+
219+
#. Enter raw REPL as usual via ctrl-A.
220+
221+
#. Write 3 bytes: ``b"\x05A\x01"`` (ie ctrl-E then "A" then ctrl-A).
222+
223+
#. Read 2 bytes to determine if the device entered raw-paste mode:
224+
225+
* If the result is ``b"R\x00"`` then the device understands the command but
226+
doesn't support raw paste.
227+
228+
* If the result is ``b"R\x01"`` then the device does support raw paste and
229+
has entered this mode.
230+
231+
* Otherwise the result should be ``b"ra"`` and the device doesn't support raw
232+
paste and the string ``b"w REPL; CTRL-B to exit\r\n>"`` should be read and
233+
discarded.
234+
235+
#. If the device is in raw-paste mode then continue, otherwise fallback to
236+
standard raw mode.
237+
238+
#. Read 2 bytes, this is the flow-control window size (in bytes) stored as a
239+
16-bit unsigned little endian integer. The remaining window size should be
240+
set to this number.
241+
242+
#. Write out the code to the device:
243+
244+
* While there are bytes to send, write up to the remaining window size.
245+
246+
* If the window size is 0, or there is a byte waiting to read, read 1 byte.
247+
If this byte is ``b"\x01"`` then increase the remaining window size by the
248+
window size from step 5. If this byte is ``b"\x04"`` then the device wants
249+
to end the data reception, and ``b"\x04"`` should be written to the device
250+
and no more code sent after that.
251+
252+
#. When all code has been written to the device, write ``b"\x04"`` to indicate
253+
end-of-data.
254+
255+
#. Read from the device until ``b"\x04"`` is received. At this point the device
256+
has received and compiled all of the code that was sent and is executing it.
257+
258+
#. The device outputs any characters produced by the executing code. When (if)
259+
the code finishes ``b"\x04"`` will be output, followed by any exception that
260+
was uncaught, followed again by ``b"\x04"``. It then goes back to the
261+
standard raw REPL and outputs ``b">"``.
262+
263+
For example, starting at a new line at the normal (friendly) REPL, if you write::
264+
265+
b'\x01\x05A\x01print(123)\x04'
266+
267+
Then the device will respond with something like::
268+
269+
b'\r\nraw REPL; CTRL-B to exit\r\n>R\x01\x80\x00\x01\x04123\r\n\x04\x04>'
270+
271+
In this case the flow-control window size is 128 and there is a new window
272+
immediately available at the start (ie up to 256 bytes can be written to begin
273+
with before waiting or checking for more incoming flow-control characters).
274+
275+
The ``tools/pyboard.py`` program uses the raw REPL, including raw-paste mode, to
276+
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