forked from platformio/platformio-core
Improve firmware uploading and auto detecting of UPLOAD_PORT // Resolve #93
This commit is contained in:
@ -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)
|
||||
|
||||
#
|
||||
|
@ -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
|
||||
#
|
||||
|
@ -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)
|
||||
|
||||
#
|
||||
|
@ -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)
|
||||
|
||||
#
|
||||
|
@ -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)
|
||||
|
||||
#
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user