Skip to content

Commit

Permalink
fix: ignore resetting on unsupported ports
Browse files Browse the repository at this point in the history
Closes #762
  • Loading branch information
peterdragun authored and dobairoland committed Jan 4, 2024
1 parent 6ca9b81 commit e948993
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
32 changes: 25 additions & 7 deletions esptool/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later

import errno
import os
import struct
import time

from .util import FatalError
from .util import FatalError, PrintOnce

# Used for resetting into bootloader on Unix-like systems
if os.name != "nt":
Expand All @@ -26,11 +27,28 @@


class ResetStrategy(object):
print_once = PrintOnce()

def __init__(self, port, reset_delay=DEFAULT_RESET_DELAY):
self.port = port
self.reset_delay = reset_delay

def __call__():
def __call__(self):
try:
self.reset()
except OSError as e:
# ENOTTY for TIOCMSET; EINVAL for TIOCMGET
if e.errno in [errno.ENOTTY, errno.EINVAL]:
self.print_once(
"WARNING: Chip was NOT reset. Setting RTS/DTR lines is not "
f"supported for port '{self.port.name}'. Set --before and --after "
"arguments to 'no_reset' and switch to bootloader manually to "
"avoid this warning."
)
else:
raise

def reset(self):
pass

def _setDTR(self, state):
Expand Down Expand Up @@ -63,7 +81,7 @@ class ClassicReset(ResetStrategy):
Classic reset sequence, sets DTR and RTS lines sequentially.
"""

def __call__(self):
def reset(self):
self._setDTR(False) # IO0=HIGH
self._setRTS(True) # EN=LOW, chip in reset
time.sleep(0.1)
Expand All @@ -79,7 +97,7 @@ class UnixTightReset(ResetStrategy):
which allows setting DTR and RTS lines at the same time.
"""

def __call__(self):
def reset(self):
self._setDTRandRTS(False, False)
self._setDTRandRTS(True, True)
self._setDTRandRTS(False, True) # IO0=HIGH & EN=LOW, chip in reset
Expand All @@ -96,7 +114,7 @@ class USBJTAGSerialReset(ResetStrategy):
is connecting via its USB-JTAG-Serial peripheral.
"""

def __call__(self):
def reset(self):
self._setRTS(False)
self._setDTR(False) # Idle
time.sleep(0.1)
Expand All @@ -121,7 +139,7 @@ def __init__(self, port, uses_usb_otg=False):
super().__init__(port)
self.uses_usb_otg = uses_usb_otg

def __call__(self):
def reset(self):
self._setRTS(True) # EN->LOW
if self.uses_usb_otg:
# Give the chip some time to come out of reset,
Expand Down Expand Up @@ -162,7 +180,7 @@ class CustomReset(ResetStrategy):
"U": "self._setDTRandRTS({})",
}

def __call__(self):
def reset(self):
exec(self.constructed_strategy)

def __init__(self, port, seq_str):
Expand Down
14 changes: 14 additions & 0 deletions esptool/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ def get_file_size(path_to_file):
return file_size


class PrintOnce:
"""
Class for printing messages just once. Can be useful when running in a loop
"""

def __init__(self) -> None:
self.already_printed = False

def __call__(self, text) -> None:
if not self.already_printed:
print(text)
self.already_printed = True


class FatalError(RuntimeError):
"""
Wrapper class for runtime errors that aren't caused by internal bugs, but by
Expand Down
24 changes: 24 additions & 0 deletions test/test_esptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,30 @@ def test_highspeed_flash_virtual_port(self):
)
self.verify_readback(0, 50 * 1024, "images/fifty_kb.bin")

@pytest.fixture
def pty_port(self):
import pty

master_fd, slave_fd = pty.openpty()
yield os.ttyname(slave_fd)
os.close(master_fd)
os.close(slave_fd)

@pytest.mark.host_test
def test_pty_port(self, pty_port):
cmd = [sys.executable, "-m", "esptool", "--port", pty_port, "chip_id"]
output = subprocess.run(
cmd,
cwd=TEST_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
# no chip connected so command should fail
assert output.returncode != 0
output = output.stdout.decode("utf-8")
print(output) # for logging
assert "WARNING: Chip was NOT reset." in output


@pytest.mark.quick_test
class TestReadWriteMemory(EsptoolTestCase):
Expand Down

0 comments on commit e948993

Please sign in to comment.