Skip to content

Commit

Permalink
fix(rfc2217_server): Use new reset sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
radimkarnis committed Feb 15, 2023
1 parent 0095a26 commit 4f13cf6
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions esp_rfc2217_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
# SPDX-License-Identifier: BSD-3-Clause

import logging
import os
import socket
import sys
import threading
import time

from esptool.config import load_config_file
from esptool.reset import ClassicReset, CustomReset, DEFAULT_RESET_DELAY, UnixTightReset

import serial
import serial.rfc2217
from serial.rfc2217 import (
Expand All @@ -46,6 +50,9 @@
SET_CONTROL_RTS_ON,
)

cfg, _ = load_config_file(verbose=True)
cfg = cfg["esptool"]


class EspPortManager(serial.rfc2217.PortManager):
"""
Expand Down Expand Up @@ -78,34 +85,25 @@ 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 _setDTR(self, state):
self.serial.setDTR(state)

def _setRTS(self, state):
self.serial.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.serial.setDTR(self.serial.dtr)

def _reset_thread(self):
"""
The reset logic is used from esptool.py because the RTS and DTR signals
cannot be retransmitted through RFC 2217 with proper timing.
"""
if self.logger:
self.logger.info("Activating reset in thread")
self._setDTR(False) # IO0=HIGH
self._setRTS(True) # EN=LOW, chip in reset
time.sleep(0.1)
if self.esp32r0_delay:
time.sleep(1.2)
self._setDTR(True) # IO0=LOW
self._setRTS(False) # EN=HIGH, chip out of reset

delay = DEFAULT_RESET_DELAY
if self.esp32r0_delay:
time.sleep(0.4)
time.sleep(0.05)
self._setDTR(False) # IO0=HIGH, done
delay += 0.5

cfg_custom_reset_sequence = cfg.get("custom_reset_sequence")
if cfg_custom_reset_sequence is not None:
CustomReset(self.serial, cfg_custom_reset_sequence)()
elif os.name != "nt":
UnixTightReset(self.serial, delay)()
else:
ClassicReset(self.serial, delay)()


class Redirector(object):
Expand Down

0 comments on commit 4f13cf6

Please sign in to comment.