Add option to invert pin reset logic

This commit is contained in:
Fabio Battaglia 2023-11-11 10:31:18 +01:00
parent 6e8e73669e
commit 38dfb08d98
3 changed files with 13 additions and 8 deletions

View File

@ -22,6 +22,7 @@ options:
-h, --help show this help message and exit
-e, --erase only erase flash memory
-a, --autoreset cycle power automatically by asserting DTR
-i, --invertreset invert pin reset logic
-A {dtr,rts}, --resetpin {dtr,rts}
pin to hold down when using --autoreset (default: DTR)
-r RESETCMD, --resetcmd RESETCMD

View File

@ -183,7 +183,7 @@ class StcGal:
return 0
try:
self.protocol.connect(autoreset=self.opts.autoreset, resetcmd=self.opts.resetcmd, resetpin=self.opts.resetpin)
self.protocol.connect(autoreset=self.opts.autoreset, resetcmd=self.opts.resetcmd, resetpin=self.opts.resetpin, invertreset=self.opts.invertreset)
if isinstance(self.protocol, StcAutoProtocol):
if not self.protocol.protocol_name:
raise StcProtocolException("cannot detect protocol")
@ -264,6 +264,7 @@ def cli():
parser.add_argument("-a", "--autoreset", help="cycle power automatically by asserting DTR", action="store_true")
parser.add_argument("-A", "--resetpin", help="pin to hold down when using --autoreset (default: DTR)",
choices=["dtr", "rts"], default="dtr")
parser.add_argument("-i", "--invertreset", help="invert autoreset pin logic", action="store_true")
parser.add_argument("-r", "--resetcmd", help="shell command for board power-cycling (instead of DTR assertion)", action="store")
parser.add_argument("-P", "--protocol", help="protocol version (default: auto)",
choices=["stc89", "stc89a", "stc12a", "stc12b", "stc12", "stc15a", "stc15", "stc8", "stc8d", "stc8g", "usb15", "auto"], default="auto")

View File

@ -265,22 +265,25 @@ class StcBaseProtocol(ABC):
def set_option(self, name, value):
self.options.set_option(name, value)
def reset_device(self, resetcmd=False, resetpin=False):
def reset_device(self, resetcmd=False, resetpin=False, invertreset=False):
if not resetcmd:
print("Cycling power: ", end="")
sys.stdout.flush()
# If invertreset is enabled, start with pin at low
pin_state = False if invertreset else True
if resetpin == "rts":
self.ser.setRTS(True)
self.ser.setRTS(pin_state)
else:
self.ser.setDTR(True)
self.ser.setDTR(pin_state)
time.sleep(0.25)
if resetpin == "rts":
self.ser.setRTS(False)
self.ser.setRTS(not pin_state)
else:
self.ser.setDTR(False)
self.ser.setDTR(not pin_state)
time.sleep(0.030)
print("done")
@ -291,7 +294,7 @@ class StcBaseProtocol(ABC):
print("Waiting for MCU: ", end="")
sys.stdout.flush()
def connect(self, autoreset=False, resetcmd=False, resetpin=False):
def connect(self, autoreset=False, resetcmd=False, resetpin=False, invertreset=False):
"""Connect to MCU and initialize communication.
Set up serial port, send sync sequence and get part info.
@ -310,7 +313,7 @@ class StcBaseProtocol(ABC):
self.ser.flushInput()
if autoreset:
self.reset_device(resetcmd, resetpin)
self.reset_device(resetcmd, resetpin, invertreset)
else:
print("Waiting for MCU, please cycle power: ", end="")
sys.stdout.flush()