Implement power-cycling via custom shell command

Sometimes instead of DTR line some custom way (e.g SoC gpio line)
may be used to reset the device. This commit implements
automated power-cycling using a a custom shell command that can
be specified via -r option

Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
This commit is contained in:
Andrew Andrianov 2017-10-07 22:41:26 +03:00
parent f1bafb1e0d
commit ba4faf9c43
2 changed files with 16 additions and 12 deletions

View File

@ -132,8 +132,7 @@ class StcGal:
"""Run programmer, main entry point."""
try:
self.protocol.connect(autoreset=self.opts.autoreset)
self.protocol.connect(autoreset=self.opts.autoreset, resetcmd=self.opts.resetcmd)
if self.opts.protocol == "auto":
if not self.protocol.protocol_name:
raise StcProtocolException("cannot detect protocol")
@ -203,6 +202,7 @@ def cli():
parser.add_argument("code_image", help="code segment file to flash (BIN/HEX)", type=argparse.FileType("rb"), nargs='?')
parser.add_argument("eeprom_image", help="eeprom segment file to flash (BIN/HEX)", type=argparse.FileType("rb"), nargs='?')
parser.add_argument("-a", "--autoreset", help="cycle power automatically by asserting DTR", action="store_true")
parser.add_argument("-r", "--resetcmd", help="Use this shell cmd for board power-cycling (instead of DTR assertion)", action="store")
parser.add_argument("-P", "--protocol", help="protocol version (default: auto)", choices=["stc89", "stc12a", "stc12b", "stc12", "stc15a", "stc15", "usb15", "auto"], default="auto")
parser.add_argument("-p", "--port", help="serial port device", default="/dev/ttyUSB0")
parser.add_argument("-b", "--baud", help="transfer baud rate (default: 19200)", type=BaudType(), default=19200)

View File

@ -241,7 +241,7 @@ class StcBaseProtocol:
def set_option(self, name, value):
self.options.set_option(name, value)
def connect(self, autoreset=False):
def connect(self, autoreset=False, resetcmd=False):
"""Connect to MCU and initialize communication.
Set up serial port, send sync sequence and get part info.
@ -260,14 +260,18 @@ class StcBaseProtocol:
self.ser.flushInput()
if autoreset:
print("Cycling power: ", end="")
sys.stdout.flush()
self.ser.setDTR(True)
time.sleep(0.5)
self.ser.setDTR(False)
print("done")
print("Waiting for MCU: ", end="")
sys.stdout.flush()
if not resetcmd:
print("Cycling power: ", end="")
sys.stdout.flush()
self.ser.setDTR(True)
time.sleep(0.5)
self.ser.setDTR(False)
print("done")
print("Waiting for MCU: ", end="")
sys.stdout.flush()
else:
print("Cycling power via shell cmd: " + resetcmd)
os.system(resetcmd)
else:
print("Waiting for MCU, please cycle power: ", end="")
sys.stdout.flush()
@ -1504,7 +1508,7 @@ class StcUsb15Protocol(Stc15Protocol):
host2dev = usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_RECIPIENT_DEVICE | usb.util.CTRL_OUT
self.dev.ctrl_transfer(host2dev, request, value, index, chunks)
def connect(self, autoreset=False):
def connect(self, autoreset=False, resetcmd=False):
"""Connect to USB device and read info packet"""
# USB support is optional. Provide an error if pyusb is not available.