Arduino-Makefile/bin/ard-reset-arduino

127 lines
3.8 KiB
Text
Raw Permalink Normal View History

2020-08-05 00:49:32 +02:00
#!/usr/bin/python3
import serial
SAMD Cortex M0 device support (Zero, MKR1000, etc) Compilation improvements by adding variant as other obj but not working on device Arduino Zero devices with OpenOCD working Created ARM_TOOLS_DIR and define arm toolchain executables in Sam.mk Check avr-gcc on last AVR_TOOLS_DIR detect and indenting formatting GDB debugging and programming added Documentation updates and define ARDMK_VENDOR rather than include Sam.mk Expand all parse_boards when defined rather than when used Trim extra defines regex working on both macOS and Linux but need better fix Print USB ids and added debug usage to readme Add note on Arduino package dir and made board.txt work Do ARM ARDUINO_ARCH define in Arduino.mk] Add MZeroBlink to non-testable examples for now Remove \B from extra defines grep Add ARDUINO_PACKAGE_DIR for board support files Fix a typo in the README Fix typo in arduino-mk-vars.md Prevent re-including Arduino.mk from Sam.mk when make restarts for upload Add catrina to ARD_REST_OPTS if/else Remove realpath in Sam.mk for cygwin compatability SAMD bootloader support in ard-reset using --zero Enters bootloader using open/close of port at 1200 BAUD, then polls the attached devices for new port enumerating (bootloader). This is how the Arduino IDE operates Bossa support for Zero, MKR1000 etc Re-word Arm README section after Native USB development Reset for zero refactored like IDE Zero bootloader reset tested on macOS and comments added Re-word ARM bootloader and remove imports from testing Patch changes ARDMK_VENDOR->ARCHITECHTURE, show_config_vars, ignore CORE_VER if emtpy Common.mk header guard, openocd/bossac avoid separator, typos Documentation update for patch changes Move ARM tools to Sam.mk and auto-detect include Correct accidental commit of Blink Makefile change Lib fix with alternative core and documentation Append zero to ARD_RESET_OPTS rather than set Prioritise package ARM upload tools over path installed Add note in README on ARM tools versions Move openocd variant config script flag to OPTS
2018-01-02 18:35:47 +01:00
import serial.tools.list_ports
import os.path
import argparse
from time import sleep
parser = argparse.ArgumentParser(description='Reset an Arduino')
SAMD Cortex M0 device support (Zero, MKR1000, etc) Compilation improvements by adding variant as other obj but not working on device Arduino Zero devices with OpenOCD working Created ARM_TOOLS_DIR and define arm toolchain executables in Sam.mk Check avr-gcc on last AVR_TOOLS_DIR detect and indenting formatting GDB debugging and programming added Documentation updates and define ARDMK_VENDOR rather than include Sam.mk Expand all parse_boards when defined rather than when used Trim extra defines regex working on both macOS and Linux but need better fix Print USB ids and added debug usage to readme Add note on Arduino package dir and made board.txt work Do ARM ARDUINO_ARCH define in Arduino.mk] Add MZeroBlink to non-testable examples for now Remove \B from extra defines grep Add ARDUINO_PACKAGE_DIR for board support files Fix a typo in the README Fix typo in arduino-mk-vars.md Prevent re-including Arduino.mk from Sam.mk when make restarts for upload Add catrina to ARD_REST_OPTS if/else Remove realpath in Sam.mk for cygwin compatability SAMD bootloader support in ard-reset using --zero Enters bootloader using open/close of port at 1200 BAUD, then polls the attached devices for new port enumerating (bootloader). This is how the Arduino IDE operates Bossa support for Zero, MKR1000 etc Re-word Arm README section after Native USB development Reset for zero refactored like IDE Zero bootloader reset tested on macOS and comments added Re-word ARM bootloader and remove imports from testing Patch changes ARDMK_VENDOR->ARCHITECHTURE, show_config_vars, ignore CORE_VER if emtpy Common.mk header guard, openocd/bossac avoid separator, typos Documentation update for patch changes Move ARM tools to Sam.mk and auto-detect include Correct accidental commit of Blink Makefile change Lib fix with alternative core and documentation Append zero to ARD_RESET_OPTS rather than set Prioritise package ARM upload tools over path installed Add note in README on ARM tools versions Move openocd variant config script flag to OPTS
2018-01-02 18:35:47 +01:00
parser.add_argument('--zero', action='store_true', help='Reset Arduino Zero or similar Native USB to enter bootloader')
parser.add_argument('--caterina', action='store_true', help='Reset a Leonardo, Micro, Robot or LilyPadUSB.')
parser.add_argument('--verbose', action='store_true', help="Watch what's going on on STDERR.")
parser.add_argument('--period', default=0.1, help='Specify the DTR pulse width in seconds.')
parser.add_argument('port', nargs=1, help='Serial device e.g. /dev/ttyACM0')
args = parser.parse_args()
SAMD Cortex M0 device support (Zero, MKR1000, etc) Compilation improvements by adding variant as other obj but not working on device Arduino Zero devices with OpenOCD working Created ARM_TOOLS_DIR and define arm toolchain executables in Sam.mk Check avr-gcc on last AVR_TOOLS_DIR detect and indenting formatting GDB debugging and programming added Documentation updates and define ARDMK_VENDOR rather than include Sam.mk Expand all parse_boards when defined rather than when used Trim extra defines regex working on both macOS and Linux but need better fix Print USB ids and added debug usage to readme Add note on Arduino package dir and made board.txt work Do ARM ARDUINO_ARCH define in Arduino.mk] Add MZeroBlink to non-testable examples for now Remove \B from extra defines grep Add ARDUINO_PACKAGE_DIR for board support files Fix a typo in the README Fix typo in arduino-mk-vars.md Prevent re-including Arduino.mk from Sam.mk when make restarts for upload Add catrina to ARD_REST_OPTS if/else Remove realpath in Sam.mk for cygwin compatability SAMD bootloader support in ard-reset using --zero Enters bootloader using open/close of port at 1200 BAUD, then polls the attached devices for new port enumerating (bootloader). This is how the Arduino IDE operates Bossa support for Zero, MKR1000 etc Re-word Arm README section after Native USB development Reset for zero refactored like IDE Zero bootloader reset tested on macOS and comments added Re-word ARM bootloader and remove imports from testing Patch changes ARDMK_VENDOR->ARCHITECHTURE, show_config_vars, ignore CORE_VER if emtpy Common.mk header guard, openocd/bossac avoid separator, typos Documentation update for patch changes Move ARM tools to Sam.mk and auto-detect include Correct accidental commit of Blink Makefile change Lib fix with alternative core and documentation Append zero to ARD_RESET_OPTS rather than set Prioritise package ARM upload tools over path installed Add note in README on ARM tools versions Move openocd variant config script flag to OPTS
2018-01-02 18:35:47 +01:00
def list_ports(output=False):
""" Lists serial ports attached
:returns
A list of paths to serial ports on system
"""
ports = serial.tools.list_ports.comports()
connected = [port[0] for port in ports]
if output:
print(connected)
return connected
def new_port(old, new):
""" Checks if a new port has attached
Args:
old: previous list of ports to check
new: current list of ports to check
Returns:
index of port in 'new' if new port found, otherwise -1
"""
new_port = -1
for port in new:
if port not in old:
new_port = new.index(port)
break
return new_port
if args.zero:
# number of trys to attempt
zero_attempts = 20 # ~2 seconds
initial_ports = list_ports(args.verbose)
if args.verbose:
print('Attempting to enter bootloader using 1200 bps open/close on port %s' % args.port[0])
ser = serial.Serial(args.port[0], 57600)
ser.close()
ser.baudrate = 1200
SAMD Cortex M0 device support (Zero, MKR1000, etc) Compilation improvements by adding variant as other obj but not working on device Arduino Zero devices with OpenOCD working Created ARM_TOOLS_DIR and define arm toolchain executables in Sam.mk Check avr-gcc on last AVR_TOOLS_DIR detect and indenting formatting GDB debugging and programming added Documentation updates and define ARDMK_VENDOR rather than include Sam.mk Expand all parse_boards when defined rather than when used Trim extra defines regex working on both macOS and Linux but need better fix Print USB ids and added debug usage to readme Add note on Arduino package dir and made board.txt work Do ARM ARDUINO_ARCH define in Arduino.mk] Add MZeroBlink to non-testable examples for now Remove \B from extra defines grep Add ARDUINO_PACKAGE_DIR for board support files Fix a typo in the README Fix typo in arduino-mk-vars.md Prevent re-including Arduino.mk from Sam.mk when make restarts for upload Add catrina to ARD_REST_OPTS if/else Remove realpath in Sam.mk for cygwin compatability SAMD bootloader support in ard-reset using --zero Enters bootloader using open/close of port at 1200 BAUD, then polls the attached devices for new port enumerating (bootloader). This is how the Arduino IDE operates Bossa support for Zero, MKR1000 etc Re-word Arm README section after Native USB development Reset for zero refactored like IDE Zero bootloader reset tested on macOS and comments added Re-word ARM bootloader and remove imports from testing Patch changes ARDMK_VENDOR->ARCHITECHTURE, show_config_vars, ignore CORE_VER if emtpy Common.mk header guard, openocd/bossac avoid separator, typos Documentation update for patch changes Move ARM tools to Sam.mk and auto-detect include Correct accidental commit of Blink Makefile change Lib fix with alternative core and documentation Append zero to ARD_RESET_OPTS rather than set Prioritise package ARM upload tools over path installed Add note in README on ARM tools versions Move openocd variant config script flag to OPTS
2018-01-02 18:35:47 +01:00
# do the open/close at 1200 BAUD
ser.open()
ser.close()
if args.verbose:
print('Done. Waiting for bootloader port to attach...')
# get new list of ports
reset_ports = list_ports(args.verbose)
# wait for new port or port to return
port_index = new_port(initial_ports, reset_ports)
# keep checking until new port appears or timeout
while port_index < 0:
# count down attempts and leave if expired
zero_attempts -= 1
if zero_attempts < 0:
break
sleep(0.1)
# get list of ports after bootloader toggle performed
reset_ports = list_ports(args.verbose)
# if a port drops, set initial ports to reset ports so that
# next attached device will be new port
if (len(reset_ports) < len(initial_ports)):
initial_ports = reset_ports
# check if a new port has attached and return the index if it has
port_index = new_port(initial_ports, reset_ports)
# return the new port if detected, otherwise return passed port
2020-08-05 00:28:36 +02:00
if port_index == -1:
SAMD Cortex M0 device support (Zero, MKR1000, etc) Compilation improvements by adding variant as other obj but not working on device Arduino Zero devices with OpenOCD working Created ARM_TOOLS_DIR and define arm toolchain executables in Sam.mk Check avr-gcc on last AVR_TOOLS_DIR detect and indenting formatting GDB debugging and programming added Documentation updates and define ARDMK_VENDOR rather than include Sam.mk Expand all parse_boards when defined rather than when used Trim extra defines regex working on both macOS and Linux but need better fix Print USB ids and added debug usage to readme Add note on Arduino package dir and made board.txt work Do ARM ARDUINO_ARCH define in Arduino.mk] Add MZeroBlink to non-testable examples for now Remove \B from extra defines grep Add ARDUINO_PACKAGE_DIR for board support files Fix a typo in the README Fix typo in arduino-mk-vars.md Prevent re-including Arduino.mk from Sam.mk when make restarts for upload Add catrina to ARD_REST_OPTS if/else Remove realpath in Sam.mk for cygwin compatability SAMD bootloader support in ard-reset using --zero Enters bootloader using open/close of port at 1200 BAUD, then polls the attached devices for new port enumerating (bootloader). This is how the Arduino IDE operates Bossa support for Zero, MKR1000 etc Re-word Arm README section after Native USB development Reset for zero refactored like IDE Zero bootloader reset tested on macOS and comments added Re-word ARM bootloader and remove imports from testing Patch changes ARDMK_VENDOR->ARCHITECHTURE, show_config_vars, ignore CORE_VER if emtpy Common.mk header guard, openocd/bossac avoid separator, typos Documentation update for patch changes Move ARM tools to Sam.mk and auto-detect include Correct accidental commit of Blink Makefile change Lib fix with alternative core and documentation Append zero to ARD_RESET_OPTS rather than set Prioritise package ARM upload tools over path installed Add note in README on ARM tools versions Move openocd variant config script flag to OPTS
2018-01-02 18:35:47 +01:00
bootloader_port = args.port[0]
else:
bootloader_port = reset_ports[port_index]
# print so that `tail -1` can be piped for bootloader port
print(bootloader_port)
elif args.caterina:
2014-04-02 18:57:58 +02:00
if args.verbose: print('Forcing reset using 1200bps open/close on port %s' % args.port[0])
ser = serial.Serial(args.port[0], 57600)
ser.close()
if pyserial_version < 3:
2016-02-11 20:07:34 +01:00
ser.setBaudrate (1200)
else:
ser.baudrate = 1200
ser.open()
ser.setRTS(True) # RTS line needs to be held high and DTR low
ser.setDTR(False) # (see Arduino IDE source code)
ser.close()
sleep(1)
while not os.path.exists(args.port[0]):
2014-04-02 18:57:58 +02:00
if args.verbose: print('Waiting for %s to come back' % args.port[0])
sleep(1)
2014-04-02 18:57:58 +02:00
if args.verbose: print('%s has come back after reset' % args.port[0])
else:
2014-04-02 18:57:58 +02:00
if args.verbose: print('Setting DTR high on %s for %ss' % (args.port[0],args.period))
ser = serial.Serial(args.port[0], 115200)
ser.setDTR(False)
sleep(args.period)
ser.setDTR(True)
ser.close()