Improve checking for the program size before uploading // Issue #689

This commit is contained in:
Ivan Kravets
2016-06-11 15:39:06 +03:00
parent a4345cedc5
commit 5398dbef95
12 changed files with 36 additions and 61 deletions

View File

@ -10,8 +10,8 @@ PlatformIO 2.0
* Added support for `emonPi <https://github.com/openenergymonitor/emonpi>`__, * Added support for `emonPi <https://github.com/openenergymonitor/emonpi>`__,
the OpenEnergyMonitor system the OpenEnergyMonitor system
(`issue #687 <https://github.com/platformio/platformio/issues/687>`_) (`issue #687 <https://github.com/platformio/platformio/issues/687>`_)
* Added support for STM32F0 boards for `SPL <http://platformio.org/frameworks/spl>`__ * Added support for `SPL <http://platformio.org/frameworks/spl>`__
framework framework for STM32F0 boards
(`issue #683 <https://github.com/platformio/platformio/issues/683>`_) (`issue #683 <https://github.com/platformio/platformio/issues/683>`_)
* Added support for `Arduboy DevKit <https://www.arduboy.com>`__, the game system * Added support for `Arduboy DevKit <https://www.arduboy.com>`__, the game system
the size of a credit card the size of a credit card

View File

@ -14,7 +14,7 @@
import sys import sys
VERSION = (2, 9, "5.dev2") VERSION = (2, 9, "5.dev3")
__version__ = ".".join([str(s) for s in VERSION]) __version__ = ".".join([str(s) for s in VERSION])
__title__ = "platformio" __title__ = "platformio"

View File

@ -142,25 +142,23 @@ AlwaysBuild(target_size)
# #
upload = env.Alias(["upload", "uploadlazy"], target_firm, upload = env.Alias(["upload", "uploadlazy"], target_firm,
[env.CheckUploadSize, BeforeUpload, "$UPLOADHEXCMD"]) [BeforeUpload, "$UPLOADHEXCMD"])
AlwaysBuild(upload) AlwaysBuild(upload)
# #
# Target: Upload EEPROM data (from EEMEM directive) # Target: Upload EEPROM data (from EEMEM directive)
# #
uploadeep = env.Alias( uploadeep = env.Alias("uploadeep",
"uploadeep", env.ElfToEep(join("$BUILD_DIR", "firmware"), target_elf),
env.ElfToEep(join("$BUILD_DIR", "firmware"), target_elf), [BeforeUpload, "$UPLOADEEPCMD"])
[BeforeUpload, "$UPLOADEEPCMD"])
AlwaysBuild(uploadeep) AlwaysBuild(uploadeep)
# #
# Target: Upload firmware using external programmer # Target: Upload firmware using external programmer
# #
program = env.Alias("program", target_firm, program = env.Alias("program", target_firm, [BeforeUpload, "$PROGRAMHEXCMD"])
[env.CheckUploadSize, BeforeUpload, "$PROGRAMHEXCMD"])
AlwaysBuild(program) AlwaysBuild(program)
# #

View File

@ -25,8 +25,6 @@ from platformio.util import get_serialports
def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621 def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621
env.AutodetectUploadPort()
board_type = env.subst("$BOARD") board_type = env.subst("$BOARD")
if "zero" not in board_type: if "zero" not in board_type:
env.Append( env.Append(
@ -177,8 +175,9 @@ if env.subst("$BOARD") == "zero":
upload = env.Alias(["upload", "uploadlazy"], target_firm, upload = env.Alias(["upload", "uploadlazy"], target_firm,
[env.CheckUploadSize, "$UPLOADCMD"]) [env.CheckUploadSize, "$UPLOADCMD"])
else: else:
upload = env.Alias(["upload", "uploadlazy"], target_firm, upload = env.Alias(
[env.CheckUploadSize, BeforeUpload, "$UPLOADCMD"]) ["upload", "uploadlazy"], target_firm,
[env.AutodetectUploadPort, BeforeUpload, "$UPLOADCMD"])
AlwaysBuild(upload) AlwaysBuild(upload)

View File

@ -346,7 +346,7 @@ AlwaysBuild(target_size)
target_upload = env.Alias( target_upload = env.Alias(
["upload", "uploadlazy", "uploadfs"], target_firm, ["upload", "uploadlazy", "uploadfs"], target_firm,
[lambda target, source, env: env.AutodetectUploadPort(), "$UPLOADCMD"]) [env.AutodetectUploadPort, "$UPLOADCMD"])
env.AlwaysBuild(target_upload) env.AlwaysBuild(target_upload)

View File

@ -21,20 +21,6 @@ from os.path import join
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default, from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default,
DefaultEnvironment) DefaultEnvironment)
def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621
if "program" in COMMAND_LINE_TARGETS:
return
env.AutodetectUploadPort()
env.Prepend(UPLOADERFLAGS=['"$UPLOAD_PORT"'])
if env.get("BOARD_OPTIONS", {}).get("upload", {}).get(
"use_1200bps_touch", False):
env.TouchSerialPort("$UPLOAD_PORT", 1200)
env = DefaultEnvironment() env = DefaultEnvironment()
env.Replace( env.Replace(
@ -112,6 +98,9 @@ env.Replace(
SIZEPRINTCMD='"$SIZETOOL" -B -d $SOURCES', SIZEPRINTCMD='"$SIZETOOL" -B -d $SOURCES',
UPLOADER=join("$PIOPACKAGES_DIR", "tool-arduino101load", "arduino101load"), UPLOADER=join("$PIOPACKAGES_DIR", "tool-arduino101load", "arduino101load"),
UPLOADERFLAGS=[
'"$UPLOAD_PORT"'
],
DFUUTIL=join("$PIOPACKAGES_DIR", "tool-arduino101load", "dfu-util"), DFUUTIL=join("$PIOPACKAGES_DIR", "tool-arduino101load", "dfu-util"),
UPLOADCMD='"$UPLOADER" "$DFUUTIL" $SOURCES $UPLOADERFLAGS verbose', UPLOADCMD='"$UPLOADER" "$DFUUTIL" $SOURCES $UPLOADERFLAGS verbose',
@ -189,8 +178,8 @@ AlwaysBuild(target_size)
# Target: Upload firmware # Target: Upload firmware
# #
upload = env.Alias( upload = env.Alias(["upload", "uploadlazy"], target_firm,
["upload", "uploadlazy"], target_firm, [BeforeUpload, "$UPLOADCMD"]) [env.AutodetectUploadPort, "$UPLOADCMD"])
AlwaysBuild(upload) AlwaysBuild(upload)
# #

View File

@ -21,12 +21,6 @@ from os.path import join
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default, from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default,
DefaultEnvironment) DefaultEnvironment)
def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621
env.AutodetectUploadPort()
env.Prepend(UPLOADERFLAGS=["-d", '"$UPLOAD_PORT"'])
env = DefaultEnvironment() env = DefaultEnvironment()
env.Replace( env.Replace(
@ -86,7 +80,8 @@ env.Replace(
UPLOADER=join("$PIOPACKAGES_DIR", "tool-pic32prog", "pic32prog"), UPLOADER=join("$PIOPACKAGES_DIR", "tool-pic32prog", "pic32prog"),
UPLOADERFLAGS=[ UPLOADERFLAGS=[
"-b", "$UPLOAD_SPEED" "-b", "$UPLOAD_SPEED",
"-d", '"$UPLOAD_PORT"'
], ],
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES', UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES',
@ -179,8 +174,8 @@ AlwaysBuild(target_size)
# Target: Upload firmware # Target: Upload firmware
# #
upload = env.Alias( upload = env.Alias(["upload", "uploadlazy"], target_firm,
["upload", "uploadlazy"], target_firm, [BeforeUpload, "$UPLOADCMD"]) [env.AutodetectUploadPort, "$UPLOADCMD"])
AlwaysBuild(upload) AlwaysBuild(upload)
# #

View File

@ -63,9 +63,8 @@ AlwaysBuild(target_size)
# #
if env.subst("$BOARD") == "rfduino": if env.subst("$BOARD") == "rfduino":
upload = env.Alias( upload = env.Alias(["upload", "uploadlazy"], target_firm,
["upload", "uploadlazy"], target_firm, [env.AutodetectUploadPort, "$UPLOADCMD"])
[lambda target, source, env: env.AutodetectUploadPort(), "$UPLOADCMD"])
else: else:
upload = env.Alias(["upload", "uploadlazy"], target_firm, env.UploadToDisk) upload = env.Alias(["upload", "uploadlazy"], target_firm, env.UploadToDisk)
AlwaysBuild(upload) AlwaysBuild(upload)

View File

@ -17,19 +17,10 @@
""" """
from os.path import join from os.path import join
from shutil import copyfile
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default, from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default,
DefaultEnvironment, SConscript) DefaultEnvironment, SConscript)
def UploadToDisk(target, source, env): # pylint: disable=W0613,W0621
env.AutodetectUploadPort()
copyfile(join(env.subst("$BUILD_DIR"), "firmware.bin"),
join(env.subst("$UPLOAD_PORT"), "firmware.bin"))
print("Firmware has been successfully uploaded.\n"
"Please restart your board.")
env = DefaultEnvironment() env = DefaultEnvironment()
SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py"))) SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts", "basearm.py")))
@ -60,7 +51,7 @@ AlwaysBuild(target_size)
# Target: Upload by default .bin file # Target: Upload by default .bin file
# #
upload = env.Alias(["upload", "uploadlazy"], target_firm, UploadToDisk) upload = env.Alias(["upload", "uploadlazy"], target_firm, env.UploadToDisk)
AlwaysBuild(upload) AlwaysBuild(upload)
# #

View File

@ -100,8 +100,7 @@ AlwaysBuild(target_size)
# #
if "mbed" in env.subst("$FRAMEWORK") and not env.subst("$UPLOAD_PROTOCOL"): if "mbed" in env.subst("$FRAMEWORK") and not env.subst("$UPLOAD_PROTOCOL"):
upload = env.Alias(["upload", "uploadlazy"], upload = env.Alias(["upload", "uploadlazy"], target_firm, env.UploadToDisk)
target_firm, env.UploadToDisk)
else: else:
upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADCMD") upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADCMD")
AlwaysBuild(upload) AlwaysBuild(upload)

View File

@ -81,7 +81,7 @@ def WaitForNewSerialPort(env, before):
def AutodetectUploadPort(*args, **kwargs): # pylint: disable=unused-argument def AutodetectUploadPort(*args, **kwargs): # pylint: disable=unused-argument
env = args[0] env = args[0]
print("Looking for upload port/disk...") print "Looking for upload port/disk..."
def _look_for_mbed_disk(): def _look_for_mbed_disk():
msdlabels = ("mbed", "nucleo", "frdm") msdlabels = ("mbed", "nucleo", "frdm")
@ -107,7 +107,7 @@ def AutodetectUploadPort(*args, **kwargs): # pylint: disable=unused-argument
return port return port
if "UPLOAD_PORT" in env: if "UPLOAD_PORT" in env:
print(env.subst("Manually specified: $UPLOAD_PORT")) print env.subst("Manually specified: $UPLOAD_PORT")
return return
if env.subst("$FRAMEWORK") == "mbed": if env.subst("$FRAMEWORK") == "mbed":
@ -124,8 +124,7 @@ def AutodetectUploadPort(*args, **kwargs): # pylint: disable=unused-argument
env.Replace(UPLOAD_PORT=_look_for_serial_port()) env.Replace(UPLOAD_PORT=_look_for_serial_port())
if env.subst("$UPLOAD_PORT"): if env.subst("$UPLOAD_PORT"):
print(env.subst("Auto-detected: $UPLOAD_PORT")) print env.subst("Auto-detected: $UPLOAD_PORT")
print("")
else: else:
env.Exit("Error: Please specify `upload_port` for environment or use " env.Exit("Error: Please specify `upload_port` for environment or use "
"global `--upload-port` option.\n" "global `--upload-port` option.\n"
@ -152,19 +151,20 @@ def CheckUploadSize(_, target, source, env): # pylint: disable=W0613,W0621
if max_size == 0 or "SIZETOOL" not in env: if max_size == 0 or "SIZETOOL" not in env:
return return
print "Check program size..."
sysenv = environ.copy() sysenv = environ.copy()
sysenv['PATH'] = str(env['ENV']['PATH']) sysenv['PATH'] = str(env['ENV']['PATH'])
cmd = [env.subst("$SIZETOOL"), "-B", str(source[0])] cmd = [env.subst("$SIZETOOL"), "-B", str(source[0])]
result = util.exec_command(cmd, env=sysenv) result = util.exec_command(cmd, env=sysenv)
if result['returncode'] != 0: if result['returncode'] != 0:
return return
print result['out'].strip()
line = result['out'].strip().splitlines()[1] line = result['out'].strip().splitlines()[1]
values = [v.strip() for v in line.split("\t")] values = [v.strip() for v in line.split("\t")]
used_size = int(values[0]) + int(values[1]) used_size = int(values[0]) + int(values[1])
if used_size > max_size: if used_size > max_size:
print result['out']
env.Exit("Error: The program size (%d bytes) is greater " env.Exit("Error: The program size (%d bytes) is greater "
"than maximum allowed (%s bytes)" % (used_size, max_size)) "than maximum allowed (%s bytes)" % (used_size, max_size))

View File

@ -94,11 +94,16 @@ def BuildProgram(env):
"Error: Nothing to build. Please put your source code files " "Error: Nothing to build. Please put your source code files "
"to '%s' folder" % env.subst("$PROJECTSRC_DIR")) "to '%s' folder" % env.subst("$PROJECTSRC_DIR"))
return env.Program( program = env.Program(
join("$BUILD_DIR", env.subst("$PROGNAME")), join("$BUILD_DIR", env.subst("$PROGNAME")),
sources sources
) )
if set(["upload", "uploadlazy", "program"]) & set(COMMAND_LINE_TARGETS):
env.AddPostAction(program, env.CheckUploadSize)
return program
def ProcessFlags(env, flags): def ProcessFlags(env, flags):
for f in flags: for f in flags: