Skip to content

Commit

Permalink
esptool: add configurable connection attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoelker committed Sep 29, 2019
1 parent 4f1e825 commit 3bf35e0
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions esptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import copy
import hashlib
import inspect
import itertools
import io
import os
import shlex
Expand Down Expand Up @@ -259,7 +260,8 @@ def _set_port_baudrate(self, baud):
raise FatalError("Failed to set baud rate %d. The driver may not support this rate." % baud)

@staticmethod
def detect_chip(port=DEFAULT_PORT, baud=ESP_ROM_BAUD, connect_mode='default_reset', trace_enabled=False):
def detect_chip(port=DEFAULT_PORT, baud=ESP_ROM_BAUD, connect_mode='default_reset', trace_enabled=False,
connect_attempts=7):
""" Use serial access to detect the chip type.
We use the UART's datecode register for this, it's mapped at
Expand All @@ -271,7 +273,7 @@ def detect_chip(port=DEFAULT_PORT, baud=ESP_ROM_BAUD, connect_mode='default_rese
connect_mode parameter) as part of querying the chip.
"""
detect_port = ESPLoader(port, baud, trace_enabled=trace_enabled)
detect_port.connect(connect_mode)
detect_port.connect(connect_mode, connect_attempts)
try:
print('Detecting chip type...', end='')
sys.stdout.flush()
Expand Down Expand Up @@ -464,14 +466,16 @@ def _connect_attempt(self, mode='default_reset', esp32r0_delay=False):
last_error = e
return last_error

def connect(self, mode='default_reset'):
def connect(self, mode='default_reset', attempts=7):
""" Try connecting repeatedly until successful, or giving up """
print('Connecting...', end='')
sys.stdout.flush()
last_error = None

try:
for _ in range(7):
for attempt in itertools.count():
if attempts > 0 and attempt >= attempts:
break
last_error = self._connect_attempt(mode=mode, esp32r0_delay=False)
if last_error is None:
return
Expand Down Expand Up @@ -2669,6 +2673,13 @@ def main(custom_commandline=None):
choices=ESP32ROM.OVERRIDE_VDDSDIO_CHOICES,
nargs='?')

parser.add_argument(
'--connect-attempts',
help=('Number of attempts to connect, negative or 0 for infinate. '
'Default: 7.'),
type=int,
default=os.environ.get('ESPTOOL_CONNECT_ATTEMPTS', 7))

subparsers = parser.add_subparsers(
dest='operation',
help='Run esptool {command} -h for additional help')
Expand Down Expand Up @@ -2880,14 +2891,15 @@ def add_spi_flash_subparsers(parent, is_elf2image):
print("Serial port %s" % each_port)
try:
if args.chip == 'auto':
esp = ESPLoader.detect_chip(each_port, initial_baud, args.before, args.trace)
esp = ESPLoader.detect_chip(each_port, initial_baud, args.before, args.trace,
args.connect_attempts)
else:
chip_class = {
'esp8266': ESP8266ROM,
'esp32': ESP32ROM,
}[args.chip]
esp = chip_class(each_port, initial_baud, args.trace)
esp.connect(args.before)
esp.connect(args.before, args.connect_attempts)
break
except (FatalError, OSError) as err:
if args.port is not None:
Expand Down

0 comments on commit 3bf35e0

Please sign in to comment.