From 45e825147958e8bc5a10e921ddc58e98ecc118a9 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 23 Feb 2015 19:01:03 +0200 Subject: [PATCH] Improve firmware uploading and auto detecting of UPLOAD_PORT // Resolve #93 --- platformio/builder/scripts/atmelavr.py | 36 +++++++++---------------- platformio/builder/scripts/atmelsam.py | 31 +++++---------------- platformio/builder/scripts/digistump.py | 16 ++++++++--- platformio/builder/scripts/stm32.py | 2 +- platformio/builder/scripts/teensy.py | 2 +- platformio/builder/tools/platformio.py | 14 ++++++++++ 6 files changed, 47 insertions(+), 54 deletions(-) diff --git a/platformio/builder/scripts/atmelavr.py b/platformio/builder/scripts/atmelavr.py index fb2aad2c..b60ef381 100644 --- a/platformio/builder/scripts/atmelavr.py +++ b/platformio/builder/scripts/atmelavr.py @@ -9,31 +9,24 @@ from os.path import join from time import sleep from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default, - DefaultEnvironment, Exit, SConscript) + DefaultEnvironment, SConscript) from platformio.util import get_serialports -def before_upload(target, source, env): # pylint: disable=W0613,W0621 - - def _autodetect_upload_port(): - if "UPLOAD_PORT" not in env: - for item in get_serialports(): - if "VID:PID" in item['hwid']: - print "Auto-detected UPLOAD_PORT: %s" % item['port'] - env.Replace(UPLOAD_PORT=item['port']) - break - - if "UPLOAD_PORT" not in env: - Exit("Error: Please specify `upload_port` for environment or use " - "global `--upload-port` option.\n") +def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621 def _rpi_sysgpio(path, value): with open(path, "w") as f: f.write(str(value)) - if env.subst("$UPLOAD_PROTOCOL") != "usbtiny": - _autodetect_upload_port() + if "UPLOAD_SPEED" in env: + env.Append( + UPLOADERFLAGS=["-b", "$UPLOAD_SPEED"] + ) + + if "usb" not in env.subst("$UPLOAD_PROTOCOL"): + env.AutodetectUploadPort() env.Append( UPLOADERFLAGS=["-P", "$UPLOAD_PORT"] ) @@ -79,11 +72,6 @@ env.Replace( UPLOADEEPCMD='"$UPLOADER" $UPLOADERFLAGS -U eeprom:w:$SOURCES:i' ) -if "UPLOAD_SPEED" in env: - env.Append( - UPLOADERFLAGS=["-b", "$UPLOAD_SPEED"] - ) - CORELIBS = env.ProcessGeneral() # @@ -119,8 +107,8 @@ AlwaysBuild(target_size) # Target: Upload by default .hex file # -upload = env.Alias(["upload", "uploadlazy"], target_firm, [ - before_upload, "$UPLOADHEXCMD"]) +upload = env.Alias(["upload", "uploadlazy"], target_firm, + [BeforeUpload, "$UPLOADHEXCMD"]) AlwaysBuild(upload) # @@ -128,7 +116,7 @@ AlwaysBuild(upload) # uploadeep = env.Alias("uploadeep", target_eep, [ - before_upload, "$UPLOADEEPCMD"]) + BeforeUpload, "$UPLOADEEPCMD"]) AlwaysBuild(uploadeep) # diff --git a/platformio/builder/scripts/atmelsam.py b/platformio/builder/scripts/atmelsam.py index 8b6e5117..568ce4f9 100644 --- a/platformio/builder/scripts/atmelsam.py +++ b/platformio/builder/scripts/atmelsam.py @@ -10,7 +10,10 @@ from os.path import join from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default, DefaultEnvironment, SConscript) -from platformio.util import get_serialports + +def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621 + env.AutodetectUploadPort() + env = DefaultEnvironment() env = SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")), @@ -27,9 +30,8 @@ env.Replace( "--verify", "--boot", "--reset" - ], - UPLOADBINCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES' + UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES' ) env.Append( @@ -71,29 +73,10 @@ AlwaysBuild(target_size) # Target: Upload by default .bin file # -upload = env.Alias( - ["upload", "uploadlazy"], target_firm, ("$UPLOADBINCMD")) +upload = env.Alias(["upload", "uploadlazy"], target_firm, + [BeforeUpload, "$UPLOADCMD"]) AlwaysBuild(upload) -# -# Check for $UPLOAD_PORT variable -# - -is_uptarget = (set(["upload", "uploadlazy"]) & set(COMMAND_LINE_TARGETS)) - -if is_uptarget: - # try autodetect upload port - if "UPLOAD_PORT" not in env: - for item in get_serialports(): - if "VID:PID" in item['hwid']: - print "Auto-detected UPLOAD_PORT: %s" % item['port'] - env.Replace(UPLOAD_PORT=item['port']) - break - - if "UPLOAD_PORT" not in env: - print("WARNING!!! Please specify environment 'upload_port' or use " - "global --upload-port option.\n") - # # Setup default targets # diff --git a/platformio/builder/scripts/digistump.py b/platformio/builder/scripts/digistump.py index 7e3a263c..6820857b 100644 --- a/platformio/builder/scripts/digistump.py +++ b/platformio/builder/scripts/digistump.py @@ -10,9 +10,15 @@ from os.path import join from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default, DefaultEnvironment, SConscript) + +def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621 + if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu"): + env.AutodetectUploadPort() + + env = DefaultEnvironment() -if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu", ""): +if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu"): env = SConscript( env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")), exports="env") @@ -25,7 +31,8 @@ if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu", ""): "--erase", "--write", "--verify", - "--boot" + "--boot", + "--reset" ], UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES' ) @@ -64,7 +71,7 @@ target_elf = env.BuildFirmware(["m"] + CORELIBS) # Target: Build the firmware file # -if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu", ""): +if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu"): if "uploadlazy" in COMMAND_LINE_TARGETS: target_firm = join("$BUILD_DIR", "firmware.bin") else: @@ -86,7 +93,8 @@ AlwaysBuild(target_size) # Target: Upload by default firmware file # -upload = env.Alias(["upload", "uploadlazy"], target_firm, ("$UPLOADCMD")) +upload = env.Alias(["upload", "uploadlazy"], target_firm, + [BeforeUpload, "$UPLOADCMD"]) AlwaysBuild(upload) # diff --git a/platformio/builder/scripts/stm32.py b/platformio/builder/scripts/stm32.py index 234a36c3..433400c7 100644 --- a/platformio/builder/scripts/stm32.py +++ b/platformio/builder/scripts/stm32.py @@ -66,7 +66,7 @@ AlwaysBuild(target_size) # Target: Upload by default .bin file # -upload = env.Alias(["upload", "uploadlazy"], target_firm, ("$UPLOADCMD")) +upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADCMD") AlwaysBuild(upload) # diff --git a/platformio/builder/scripts/teensy.py b/platformio/builder/scripts/teensy.py index 1f48b60d..24ef8b2d 100644 --- a/platformio/builder/scripts/teensy.py +++ b/platformio/builder/scripts/teensy.py @@ -85,7 +85,7 @@ AlwaysBuild(target_size) # Target: Upload by default firmware file # -upload = env.Alias(["upload", "uploadlazy"], target_firm, ("$UPLOADHEXCMD")) +upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADHEXCMD") AlwaysBuild(upload) # diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index 0952a0d7..149018eb 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -337,6 +337,19 @@ def WaitForNewSerialPort(_, before): return new_port +def AutodetectUploadPort(env): + if "UPLOAD_PORT" not in env: + for item in get_serialports(): + if "VID:PID" in item['hwid']: + print ("Auto-detected UPLOAD_PORT: %s" % item['port']) + env.Replace(UPLOAD_PORT=item['port']) + break + + if "UPLOAD_PORT" not in env: + Exit("Error: Please specify `upload_port` for environment or use " + "global `--upload-port` option.\n") + + def exists(_): return True @@ -352,4 +365,5 @@ def generate(env): env.AddMethod(FlushSerialBuffer) env.AddMethod(TouchSerialPort) env.AddMethod(WaitForNewSerialPort) + env.AddMethod(AutodetectUploadPort) return env