@@ -253,6 +253,7 @@ def inWaiting(self):
253
253
254
254
class Pyboard :
255
255
def __init__ (self , device , baudrate = 115200 , user = "micro" , password = "python" , wait = 0 ):
256
+ self .use_raw_paste = True
256
257
if device .startswith ("exec:" ):
257
258
self .serial = ProcessToSerial (device [len ("exec:" ) :])
258
259
elif device .startswith ("execpty:" ):
@@ -359,6 +360,41 @@ def follow(self, timeout, data_consumer=None):
359
360
# return normal and error output
360
361
return data , data_err
361
362
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
+
362
398
def exec_raw_no_follow (self , command ):
363
399
if isinstance (command , bytes ):
364
400
command_bytes = command
@@ -370,7 +406,24 @@ def exec_raw_no_follow(self, command):
370
406
if not data .endswith (b">" ):
371
407
raise PyboardError ("could not enter raw repl" )
372
408
373
- # write command
409
+ if self .use_raw_paste :
410
+ # Try to enter raw-paste mode.
411
+ self .serial .write (b"\x05 A\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.
374
427
for i in range (0 , len (command_bytes ), 256 ):
375
428
self .serial .write (command_bytes [i : min (i + 256 , len (command_bytes ))])
376
429
time .sleep (0.01 )
0 commit comments