diff --git a/esp_rfc2217_server.py b/esp_rfc2217_server.py index 9cf116aa2..8b9a1678e 100755 --- a/esp_rfc2217_server.py +++ b/esp_rfc2217_server.py @@ -37,7 +37,13 @@ import time from esptool.config import load_config_file -from esptool.reset import ClassicReset, CustomReset, DEFAULT_RESET_DELAY, UnixTightReset +from esptool.reset import ( + ClassicReset, + CustomReset, + DEFAULT_RESET_DELAY, + HardReset, + UnixTightReset, +) import serial import serial.rfc2217 @@ -63,14 +69,23 @@ class EspPortManager(serial.rfc2217.PortManager): def __init__(self, serial_port, connection, esp32r0_delay, logger=None): self.esp32r0_delay = esp32r0_delay + self.is_download_mode = False super(EspPortManager, self).__init__(serial_port, connection, logger) def _telnet_process_subnegotiation(self, suboption): if suboption[0:1] == COM_PORT_OPTION and suboption[1:2] == SET_CONTROL: if suboption[2:3] == SET_CONTROL_DTR_OFF: + self.is_download_mode = False self.serial.dtr = False return - elif suboption[2:3] == SET_CONTROL_RTS_ON and not self.serial.dtr: + elif suboption[2:3] == SET_CONTROL_RTS_OFF and not self.is_download_mode: + reset_thread = threading.Thread(target=self._hard_reset_thread) + reset_thread.daemon = True + reset_thread.name = "hard_reset_thread" + reset_thread.start() + return + elif suboption[2:3] == SET_CONTROL_DTR_ON and not self.is_download_mode: + self.is_download_mode = True reset_thread = threading.Thread(target=self._reset_thread) reset_thread.daemon = True reset_thread.name = "reset_thread" @@ -85,6 +100,14 @@ def _telnet_process_subnegotiation(self, suboption): # only in cases not handled above do the original implementation in PortManager super(EspPortManager, self)._telnet_process_subnegotiation(suboption) + def _hard_reset_thread(self): + """ + The reset logic used for hard resetting the chip. + """ + if self.logger: + self.logger.info("Activating hard reset in thread") + HardReset(self.serial)() + def _reset_thread(self): """ The reset logic is used from esptool.py because the RTS and DTR signals