Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reset): Automatically reconnect if port disconnects during reset. (ESPTOOL-862) #980

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions esptool/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@
DEFAULT_RESET_DELAY = 0.05 # default time to wait before releasing boot pin after reset


def reconnect(f):
def wrapper(*args):
"""
On targets with native USB, the reset process can cause the port to
disconnect / reconnect during reset.
This will retry reconnections for up to 10 seconds on ports that drop
out during the RTS/DTS reset process.
"""
self = args[0]
for retry in reversed(range(20)):
try:
if not self.port.isOpen():
self.port.open()
ret = f(*args)
break
except OSError:
if not retry:
raise
self.port.close()
time.sleep(0.5)
return ret

return wrapper


class ResetStrategy(object):
print_once = PrintOnce()

Expand All @@ -51,16 +76,19 @@ def __call__(self):
def reset(self):
pass

@reconnect
def _setDTR(self, state):
self.port.setDTR(state)

@reconnect
def _setRTS(self, state):
self.port.setRTS(state)
# Work-around for adapters on Windows using the usbser.sys driver:
# generate a dummy change to DTR so that the set-control-line-state
# request is sent with the updated RTS state and the same DTR state
self.port.setDTR(self.port.dtr)

@reconnect
def _setDTRandRTS(self, dtr=False, rts=False):
status = struct.unpack(
"I", fcntl.ioctl(self.port.fileno(), TIOCMGET, struct.pack("I", 0))
Expand Down
Loading