From d820c9b31062304f9c9c5ad9d6597b61ee105f2a Mon Sep 17 00:00:00 2001 From: nigelb Date: Wed, 9 Jun 2021 14:32:08 +1000 Subject: [PATCH 1/4] Fix for issue sparkfun/Apollo3_Uploader_SVL#4 --- svl.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/svl.py b/svl.py index f62c4b6..f2f9055 100644 --- a/svl.py +++ b/svl.py @@ -36,11 +36,13 @@ import argparse import serial import serial.tools.list_ports as list_ports +import termios import sys import time import math import os.path from sys import exit +import platform as sys_pf SCRIPT_VERSION_MAJOR = "1" SCRIPT_VERSION_MINOR = "7" @@ -336,9 +338,24 @@ def main(): bl_success = False entered_bootloader = False + system_type = sys_pf.system().lower() for _ in range(num_tries): with serial.Serial(args.port, args.baud, timeout=args.timeout) as ser: + # Set port to known state + if system_type == "linux": + attrs = termios.tcgetattr(ser.fd) + c_iflag = 0 + c_oflag = 0 + port_speed = attrs[4] + c_cflag = termios.CS8 | termios.CREAD | termios.HUPCL | termios.CLOCAL | port_speed + c_lflag = 0 + c_ispeed = port_speed + c_ospeed = port_speed + termios.tcsetattr(ser.fd, termios.TCSANOW, + [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, attrs[6]]) + ser.close() + ser.open() # startup time for Artemis bootloader (experimentally determined - 0.095 sec min delay) t_su = 0.15 From 9840d6a767b266d19293fe44e5cdc56d483ff53c Mon Sep 17 00:00:00 2001 From: nigelb Date: Wed, 9 Jun 2021 14:48:58 +1000 Subject: [PATCH 2/4] Extra Fix for issue sparkfun/Apollo3_Uploader_SVL#4 --- svl.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/svl.py b/svl.py index f2f9055..fe3a70b 100644 --- a/svl.py +++ b/svl.py @@ -352,10 +352,12 @@ def main(): c_lflag = 0 c_ispeed = port_speed c_ospeed = port_speed - termios.tcsetattr(ser.fd, termios.TCSANOW, - [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, attrs[6]]) - ser.close() - ser.open() + + new_attrs = [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, attrs[6]] + if attrs != new_attrs: + termios.tcsetattr(ser.fd, termios.TCSANOW, new_attrs) + ser.close() + ser.open() # startup time for Artemis bootloader (experimentally determined - 0.095 sec min delay) t_su = 0.15 From 8f0d16a18c8718bca924494a87becb1380c2ea65 Mon Sep 17 00:00:00 2001 From: nigelb Date: Wed, 9 Jun 2021 14:51:15 +1000 Subject: [PATCH 3/4] Add verbose print statement when change port settings --- svl.py | 1 + 1 file changed, 1 insertion(+) diff --git a/svl.py b/svl.py index fe3a70b..d5cc251 100644 --- a/svl.py +++ b/svl.py @@ -355,6 +355,7 @@ def main(): new_attrs = [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, attrs[6]] if attrs != new_attrs: + verboseprint("Resetting Port config") termios.tcsetattr(ser.fd, termios.TCSANOW, new_attrs) ser.close() ser.open() From 0d31d95114e02077b9f3c662bdf2ec2de9f62331 Mon Sep 17 00:00:00 2001 From: nigelb Date: Sat, 12 Jun 2021 01:16:15 +1000 Subject: [PATCH 4/4] Add --reset-port CLI option to reset the serial port. Made resetting the port optional with the --reset-port CLI option. The idea is that if you need to use it you can add: upload_flags = --reset-port to your platformio.ini file and it will be enabled. --- svl.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/svl.py b/svl.py index d5cc251..2978ca2 100644 --- a/svl.py +++ b/svl.py @@ -343,22 +343,23 @@ def main(): with serial.Serial(args.port, args.baud, timeout=args.timeout) as ser: # Set port to known state - if system_type == "linux": - attrs = termios.tcgetattr(ser.fd) - c_iflag = 0 - c_oflag = 0 - port_speed = attrs[4] - c_cflag = termios.CS8 | termios.CREAD | termios.HUPCL | termios.CLOCAL | port_speed - c_lflag = 0 - c_ispeed = port_speed - c_ospeed = port_speed - - new_attrs = [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, attrs[6]] - if attrs != new_attrs: - verboseprint("Resetting Port config") - termios.tcsetattr(ser.fd, termios.TCSANOW, new_attrs) - ser.close() - ser.open() + if args.reset_port: + if system_type == "linux": + attrs = termios.tcgetattr(ser.fd) + c_iflag = 0 + c_oflag = 0 + port_speed = attrs[4] + c_cflag = termios.CS8 | termios.CREAD | termios.HUPCL | termios.CLOCAL | port_speed + c_lflag = 0 + c_ispeed = port_speed + c_ospeed = port_speed + + new_attrs = [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, attrs[6]] + if attrs != new_attrs: + verboseprint("Resetting Port config") + termios.tcsetattr(ser.fd, termios.TCSANOW, new_attrs) + ser.close() + ser.open() # startup time for Artemis bootloader (experimentally determined - 0.095 sec min delay) t_su = 0.15 @@ -413,6 +414,9 @@ def main(): parser.add_argument("-t", "--timeout", default=0.50, help="Communication timeout in seconds (default 0.5)", type=float) + parser.add_argument("-r", "--reset-port", action="store_true", default=False, + help="Reset the port configuration.") + if len(sys.argv) < 2: print("No port selected. Detected Serial Ports:") devices = list_ports.comports()