@@ -196,17 +196,68 @@ So you can use the underscore to save the result in a variable. For example:
196
196
15
197
197
>>>
198
198
199
- Raw mode
200
- --------
199
+ Raw mode and raw-paste mode
200
+ ---------------------------
201
201
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.
204
205
205
206
Raw mode is entered using Ctrl-A. You then send your python code, followed by
206
207
a Ctrl-D. The Ctrl-D will be acknowledged by 'OK' and then the python code will
207
208
be compiled and executed. Any output (or errors) will be sent back. Entering
208
209
Ctrl-B will leave raw mode and return the the regular (aka friendly) REPL.
209
210
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"\x 05A\x 01" (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\x 00" then the device understands the command but
222
+ doesn't support raw paste.
223
+ - 3b: If the result is b"R\x 01" 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"\x 01" (ctrl-A) then increase the remaining window size
237
+ by the window size from step 5. If this byte is b"\x 04" (ctrl-D) then the
238
+ device wants to end the data reception, and b"\x 04" (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"\x 04" (ctrl-D) to
241
+ indicate end-of-data.
242
+ 8. Read from the device until b"\x 04" (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"\x 04" (ctrl-D) will be output, followed by any exception
247
+ that was uncaught, followed again by b"\x 04" (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'\x 01\x 05A\x 01print(123)\x 04'
253
+
254
+ Then the device will respond with something like:
255
+
256
+ b'\r\n raw REPL; CTRL-B to exit\r\n >R\x 01\x 80\x 00\x 01\x 04123\r\n\x 04\x 04>'
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