Skip to content

Commit

Permalink
Use smaller block size in paste mode on Windows, thonny#2143
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Feb 5, 2022
1 parent bdf903c commit 79d4b06
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions thonny/plugins/micropython/bare_metal_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
serialize_message,
EOFCommand,
)
from thonny.misc_utils import find_volumes_by_name
from thonny.misc_utils import find_volumes_by_name, running_on_windows
from thonny.plugins.micropython.backend import (
MicroPythonBackend,
ManagementError,
Expand Down Expand Up @@ -130,11 +130,13 @@ def __init__(self, connection, clean, args):
self._startup_time = time.time()
self._last_inferred_fs_mount: Optional[str] = None

self._submit_mode = args.get("submit_mode", None)

# https://forum.micropython.org/viewtopic.php?f=15&t=3698
# https://forum.micropython.org/viewtopic.php?f=15&t=4896&p=28132
self._write_block_size = args.get("write_block_size", None)
if self._write_block_size is None:
self._write_block_size = 255
self._write_block_size = self._infer_write_block_size()

# write delay is used only with original raw submit mode
self._write_block_delay = args.get("write_block_delay", None)
Expand All @@ -147,7 +149,6 @@ def __init__(self, connection, clean, args):
if self._read_block_size is None:
self._read_block_size = self._infer_read_block_size()

self._submit_mode = args.get("submit_mode", None)
logger.debug(
"Initial submit_mode: %s, "
"write_block_size: %s, "
Expand Down Expand Up @@ -230,6 +231,22 @@ def controlled_print(cls, *args, sep=" ", end="\\n"):

return result

def _infer_write_block_size(self):
# M5Stack Atom (FT232 USB) may produce corrupted output on Windows with
# paste mode and larger block sizes (problem confirmed with 128 and 64 bytes blocks)
# https://github.com/thonny/thonny/issues/2143
from thonny.plugins.micropython.serial_connection import SerialConnection

if (
self._submit_mode == "paste"
and running_on_windows()
and isinstance(self._connection, SerialConnection)
):
# TODO: Only reduce the block size when VID:PID is 0403:6001
return 30
else:
return 255

def _infer_read_block_size(self):
# TODO:
# in Windows it should be > 0 if the port is bluetooth over serial
Expand All @@ -240,7 +257,9 @@ def _infer_read_block_size(self):
return 0

def _infer_write_block_delay(self):
if self._connected_over_webrepl():
if self._submit_mode in ("paste", "raw_paste"):
return 0
elif self._connected_over_webrepl():
# ESP-32 needs long delay to work reliably over raw mode WebREPL
# TODO: consider removing when this gets fixed
return 0.5
Expand Down Expand Up @@ -598,11 +617,7 @@ def _submit_code_via_paste_mode(self, script_bytes: bytes) -> None:
break

self._write(block)
logger.debug("Expecting to read %r", expected_echo)
discarded = self._connection.read_all_expected(
expected_echo, timeout=WAIT_OR_CRASH_TIMEOUT
)
logger.debug("Discarding %r", discarded)
self._connection.read_all_expected(expected_echo, timeout=WAIT_OR_CRASH_TIMEOUT)

# push and read confirmation
self._write(EOT)
Expand Down

0 comments on commit 79d4b06

Please sign in to comment.