mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
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 time import sleep
|
||||||
|
|
||||||
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
|
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
|
||||||
DefaultEnvironment, Exit, SConscript)
|
DefaultEnvironment, SConscript)
|
||||||
|
|
||||||
from platformio.util import get_serialports
|
from platformio.util import get_serialports
|
||||||
|
|
||||||
|
|
||||||
def before_upload(target, source, env): # pylint: disable=W0613,W0621
|
def BeforeUpload(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 _rpi_sysgpio(path, value):
|
def _rpi_sysgpio(path, value):
|
||||||
with open(path, "w") as f:
|
with open(path, "w") as f:
|
||||||
f.write(str(value))
|
f.write(str(value))
|
||||||
|
|
||||||
if env.subst("$UPLOAD_PROTOCOL") != "usbtiny":
|
if "UPLOAD_SPEED" in env:
|
||||||
_autodetect_upload_port()
|
env.Append(
|
||||||
|
UPLOADERFLAGS=["-b", "$UPLOAD_SPEED"]
|
||||||
|
)
|
||||||
|
|
||||||
|
if "usb" not in env.subst("$UPLOAD_PROTOCOL"):
|
||||||
|
env.AutodetectUploadPort()
|
||||||
env.Append(
|
env.Append(
|
||||||
UPLOADERFLAGS=["-P", "$UPLOAD_PORT"]
|
UPLOADERFLAGS=["-P", "$UPLOAD_PORT"]
|
||||||
)
|
)
|
||||||
@ -79,11 +72,6 @@ env.Replace(
|
|||||||
UPLOADEEPCMD='"$UPLOADER" $UPLOADERFLAGS -U eeprom:w:$SOURCES:i'
|
UPLOADEEPCMD='"$UPLOADER" $UPLOADERFLAGS -U eeprom:w:$SOURCES:i'
|
||||||
)
|
)
|
||||||
|
|
||||||
if "UPLOAD_SPEED" in env:
|
|
||||||
env.Append(
|
|
||||||
UPLOADERFLAGS=["-b", "$UPLOAD_SPEED"]
|
|
||||||
)
|
|
||||||
|
|
||||||
CORELIBS = env.ProcessGeneral()
|
CORELIBS = env.ProcessGeneral()
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -119,8 +107,8 @@ AlwaysBuild(target_size)
|
|||||||
# Target: Upload by default .hex file
|
# Target: Upload by default .hex file
|
||||||
#
|
#
|
||||||
|
|
||||||
upload = env.Alias(["upload", "uploadlazy"], target_firm, [
|
upload = env.Alias(["upload", "uploadlazy"], target_firm,
|
||||||
before_upload, "$UPLOADHEXCMD"])
|
[BeforeUpload, "$UPLOADHEXCMD"])
|
||||||
AlwaysBuild(upload)
|
AlwaysBuild(upload)
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -128,7 +116,7 @@ AlwaysBuild(upload)
|
|||||||
#
|
#
|
||||||
|
|
||||||
uploadeep = env.Alias("uploadeep", target_eep, [
|
uploadeep = env.Alias("uploadeep", target_eep, [
|
||||||
before_upload, "$UPLOADEEPCMD"])
|
BeforeUpload, "$UPLOADEEPCMD"])
|
||||||
AlwaysBuild(uploadeep)
|
AlwaysBuild(uploadeep)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -10,7 +10,10 @@ from os.path import join
|
|||||||
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
|
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
|
||||||
DefaultEnvironment, SConscript)
|
DefaultEnvironment, SConscript)
|
||||||
|
|
||||||
from platformio.util import get_serialports
|
|
||||||
|
def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621
|
||||||
|
env.AutodetectUploadPort()
|
||||||
|
|
||||||
|
|
||||||
env = DefaultEnvironment()
|
env = DefaultEnvironment()
|
||||||
env = SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")),
|
env = SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")),
|
||||||
@ -27,9 +30,8 @@ env.Replace(
|
|||||||
"--verify",
|
"--verify",
|
||||||
"--boot",
|
"--boot",
|
||||||
"--reset"
|
"--reset"
|
||||||
|
|
||||||
],
|
],
|
||||||
UPLOADBINCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES'
|
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES'
|
||||||
)
|
)
|
||||||
|
|
||||||
env.Append(
|
env.Append(
|
||||||
@ -71,29 +73,10 @@ AlwaysBuild(target_size)
|
|||||||
# Target: Upload by default .bin file
|
# Target: Upload by default .bin file
|
||||||
#
|
#
|
||||||
|
|
||||||
upload = env.Alias(
|
upload = env.Alias(["upload", "uploadlazy"], target_firm,
|
||||||
["upload", "uploadlazy"], target_firm, ("$UPLOADBINCMD"))
|
[BeforeUpload, "$UPLOADCMD"])
|
||||||
AlwaysBuild(upload)
|
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
|
# Setup default targets
|
||||||
#
|
#
|
||||||
|
@ -10,9 +10,15 @@ from os.path import join
|
|||||||
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
|
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
|
||||||
DefaultEnvironment, SConscript)
|
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()
|
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 = SConscript(
|
||||||
env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")),
|
env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")),
|
||||||
exports="env")
|
exports="env")
|
||||||
@ -25,7 +31,8 @@ if "cortex" in env.get("BOARD_OPTIONS").get("build").get("cpu", ""):
|
|||||||
"--erase",
|
"--erase",
|
||||||
"--write",
|
"--write",
|
||||||
"--verify",
|
"--verify",
|
||||||
"--boot"
|
"--boot",
|
||||||
|
"--reset"
|
||||||
],
|
],
|
||||||
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES'
|
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES'
|
||||||
)
|
)
|
||||||
@ -64,7 +71,7 @@ target_elf = env.BuildFirmware(["m"] + CORELIBS)
|
|||||||
# Target: Build the firmware file
|
# 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:
|
if "uploadlazy" in COMMAND_LINE_TARGETS:
|
||||||
target_firm = join("$BUILD_DIR", "firmware.bin")
|
target_firm = join("$BUILD_DIR", "firmware.bin")
|
||||||
else:
|
else:
|
||||||
@ -86,7 +93,8 @@ AlwaysBuild(target_size)
|
|||||||
# Target: Upload by default firmware file
|
# 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)
|
AlwaysBuild(upload)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -66,7 +66,7 @@ AlwaysBuild(target_size)
|
|||||||
# Target: Upload by default .bin file
|
# Target: Upload by default .bin file
|
||||||
#
|
#
|
||||||
|
|
||||||
upload = env.Alias(["upload", "uploadlazy"], target_firm, ("$UPLOADCMD"))
|
upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADCMD")
|
||||||
AlwaysBuild(upload)
|
AlwaysBuild(upload)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -85,7 +85,7 @@ AlwaysBuild(target_size)
|
|||||||
# Target: Upload by default firmware file
|
# Target: Upload by default firmware file
|
||||||
#
|
#
|
||||||
|
|
||||||
upload = env.Alias(["upload", "uploadlazy"], target_firm, ("$UPLOADHEXCMD"))
|
upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADHEXCMD")
|
||||||
AlwaysBuild(upload)
|
AlwaysBuild(upload)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -337,6 +337,19 @@ def WaitForNewSerialPort(_, before):
|
|||||||
return new_port
|
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(_):
|
def exists(_):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -352,4 +365,5 @@ def generate(env):
|
|||||||
env.AddMethod(FlushSerialBuffer)
|
env.AddMethod(FlushSerialBuffer)
|
||||||
env.AddMethod(TouchSerialPort)
|
env.AddMethod(TouchSerialPort)
|
||||||
env.AddMethod(WaitForNewSerialPort)
|
env.AddMethod(WaitForNewSerialPort)
|
||||||
|
env.AddMethod(AutodetectUploadPort)
|
||||||
return env
|
return env
|
||||||
|
Reference in New Issue
Block a user