@@ -196,17 +196,105 @@ 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.
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).
212
216
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-increment (in bytes)
239
+ stored as a 16-bit unsigned little endian integer. The initial value for the
240
+ remaining-window-size variable should be 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 worth
245
+ of bytes, and decrease the remaining-window-size by the number of bytes
246
+ written.
247
+
248
+ * If the remaining-window-size is 0, or there is a byte waiting to read, read
249
+ 1 byte. If this byte is ``b"\x01" `` then increase the remaining-window-size
250
+ by the window-size-increment from step 5. If this byte is ``b"\x04" `` then
251
+ the device wants to end the data reception, and ``b"\x04" `` should be
252
+ written to the device and no more code sent after that. (Note: if there is
253
+ a byte waiting to be read from the device then it does not need to be read
254
+ and acted upon immediately, the device will continue to consume incoming
255
+ bytes as long as reamining-window-size is greater than 0.)
256
+
257
+ #. When all code has been written to the device, write ``b"\x04" `` to indicate
258
+ end-of-data.
259
+
260
+ #. Read from the device until ``b"\x04" `` is received. At this point the device
261
+ has received and compiled all of the code that was sent and is executing it.
262
+
263
+ #. The device outputs any characters produced by the executing code. When (if)
264
+ the code finishes ``b"\x04" `` will be output, followed by any exception that
265
+ was uncaught, followed again by ``b"\x04" ``. It then goes back to the
266
+ standard raw REPL and outputs ``b">" ``.
267
+
268
+ For example, starting at a new line at the normal (friendly) REPL, if you write::
269
+
270
+ b"\x01\x05A\x01print(123)\x04"
271
+
272
+ Then the device will respond with something like::
273
+
274
+ b"\r\nraw REPL; CTRL-B to exit\r\n>R\x01\x80\x00\x01\x04123\r\n\x04\x04>"
275
+
276
+ Broken down over time this looks like::
277
+
278
+ # Step 1: enter raw REPL
279
+ write: b"\x01"
280
+ read: b"\r\nraw REPL; CTRL-B to exit\r\n>"
281
+
282
+ # Step 2-5: enter raw-paste mode
283
+ write: b"\x05A\x01"
284
+ read: b"R\x01\x80\x00\x01"
285
+
286
+ # Step 6-8: write out code
287
+ write: b"print(123)\x04"
288
+ read: b"\x04"
289
+
290
+ # Step 9: code executes and result is read
291
+ read: b"123\r\n\x04\x04>"
292
+
293
+ In this case the flow control window-size-increment is 128 and there are two
294
+ windows worth of data immediately available at the start, one from the initial
295
+ window-size-increment value and one from the explicit ``b"\x01" `` value that
296
+ is sent. So this means up to 256 bytes can be written to begin with before
297
+ waiting or checking for more incoming flow-control characters.
298
+
299
+ The ``tools/pyboard.py `` program uses the raw REPL, including raw-paste mode, to
300
+ execute Python code on a MicroPython-enabled board.
0 commit comments