Skip to content

Commit

Permalink
Add "esptool" and "espota" to board configuration // Resolve #134
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankravets committed Mar 4, 2019
1 parent 7e3d42a commit ae96496
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 59 deletions.
128 changes: 69 additions & 59 deletions builder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# pylint: disable=redefined-outer-name

import re
import socket
import sys
from os.path import join


Expand Down Expand Up @@ -150,29 +150,6 @@ def _update_max_upload_size(env):
"framework-arduinoespressif8266"),
SDK_ESP8266_DIR=platform.get_package_dir("sdk-esp8266"),

#
# Upload
#

UPLOADER="esptool",
UPLOADEROTA=join(platform.get_package_dir("tool-espotapy") or "",
"espota.py"),

UPLOADERFLAGS=[
"-cd", "$UPLOAD_RESETMETHOD",
"-cb", "$UPLOAD_SPEED",
"-cp", '"$UPLOAD_PORT"'
],
UPLOADEROTAFLAGS=[
"--debug",
"--progress",
"-i", "$UPLOAD_PORT",
"$UPLOAD_FLAGS"
],

UPLOADCMD='$UPLOADER $UPLOADERFLAGS -cf $SOURCE',
UPLOADOTACMD='"$PYTHONEXE" "$UPLOADEROTA" $UPLOADEROTAFLAGS -f $SOURCE',

#
# Misc
#
Expand Down Expand Up @@ -227,11 +204,6 @@ def _update_max_upload_size(env):
)
)

if "uploadfs" in COMMAND_LINE_TARGETS:
env.Append(
UPLOADERFLAGS=["-ca", "${hex(SPIFFS_START)}"],
UPLOADEROTAFLAGS=["-s"]
)

#
# Framework and SDK specific configuration
Expand Down Expand Up @@ -264,17 +236,6 @@ def _update_max_upload_size(env):
)
)
)

# Handle uploading via OTA
ota_port = None
if env.get("UPLOAD_PORT"):
try:
ota_port = socket.gethostbyname(env.get("UPLOAD_PORT"))
except socket.error:
pass
if ota_port:
env.Replace(UPLOADCMD="$UPLOADOTACMD")

else:
# ESP8266 RTOS SDK and Native SDK common configuration
env.Append(
Expand All @@ -300,20 +261,6 @@ def _update_max_upload_size(env):
)
)

env.Replace(
UPLOADERFLAGS=[
"-vv",
"-cd", "$UPLOAD_RESETMETHOD",
"-cb", "$UPLOAD_SPEED",
"-cp", '"$UPLOAD_PORT"',
"-ca", "0x00000",
"-cf", "${SOURCES[0]}",
"-ca", "$UPLOAD_ADDRESS",
"-cf", "${SOURCES[1]}"
],
UPLOADCMD='$UPLOADER $UPLOADERFLAGS',
)

if not env.get("PIOFRAMEWORK"):
env.SConscript("frameworks/_bare.py", exports="env")

Expand Down Expand Up @@ -376,11 +323,74 @@ def _update_max_upload_size(env):
# Target: Upload firmware or SPIFFS image
#

target_upload = env.Alias(
["upload", "uploadfs"], target_firm,
[env.VerboseAction(env.AutodetectUploadPort, "Looking for upload port..."),
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")])
env.AlwaysBuild(target_upload)
upload_protocol = env.subst("$UPLOAD_PROTOCOL")
upload_actions = []

# Compatibility with old OTA configurations
if (upload_protocol != "espota"
and re.match(r"\"?((([0-9]{1,3}\.){3}[0-9]{1,3})|[^\\/]+\.local)\"?$",
env.get("UPLOAD_PORT", ""))):
upload_protocol = "espota"
sys.stderr.write(
"Warning! We have just detected `upload_port` as IP address or host "
"name of ESP device. `upload_protocol` is switched to `espota`.\n"
"Please specify `upload_protocol = espota` in `platformio.ini` "
"project configuration file.\n")

if upload_protocol == "espota":
if not env.subst("$UPLOAD_PORT"):
sys.stderr.write(
"Error: Please specify IP address or host name of ESP device "
"using `upload_port` for build environment or use "
"global `--upload-port` option.\n"
"See https://docs.platformio.org/page/platforms/"
"espressif8266.html#over-the-air-ota-update\n")
env.Replace(
UPLOADER=join(
platform.get_package_dir("tool-espotapy") or "", "espota.py"),
UPLOADERFLAGS=["--debug", "--progress", "-i", "$UPLOAD_PORT"],
UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS -f $SOURCE'
)
if "uploadfs" in COMMAND_LINE_TARGETS:
env.Append(UPLOADERFLAGS=["-s"])
upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")]

elif upload_protocol == "esptool":
env.Replace(
UPLOADER="esptool",
UPLOADERFLAGS=[
"-cd", "$UPLOAD_RESETMETHOD",
"-cb", "$UPLOAD_SPEED",
"-cp", '"$UPLOAD_PORT"'
],
UPLOADCMD='$UPLOADER $UPLOADERFLAGS -cf $SOURCE',
)
if env.subst("$PIOFRAMEWORK") not in ("arduino", "simba"): # SDK
env.Append(
UPLOADERFLAGS=[
"-ca", "0x00000",
"-cf", "${SOURCES[0]}",
"-ca", "$UPLOAD_ADDRESS",
"-cf", "${SOURCES[1]}"
]
)
env.Replace(UPLOADCMD="$UPLOADER $UPLOADERFLAGS")
elif "uploadfs" in COMMAND_LINE_TARGETS:
env.Append(UPLOADERFLAGS=["-ca", "${hex(SPIFFS_START)}"])
upload_actions = [
env.VerboseAction(
env.AutodetectUploadPort, "Looking for upload port..."),
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")
]

# custom upload tool
elif upload_protocol == "custom":
upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")]

else:
sys.stderr.write("Warning! Unknown upload protocol %s\n" % upload_protocol)

env.AlwaysBuild(env.Alias(["upload", "uploadfs"], target_firm, upload_actions))

#
# Target: Erase Flash
Expand Down
18 changes: 18 additions & 0 deletions platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ def configure_default_packages(self, variables, targets):
self.packages['tool-mkspiffs']['optional'] = False
return PlatformBase.configure_default_packages(
self, variables, targets)

def get_boards(self, id_=None):
result = PlatformBase.get_boards(self, id_)
if not result:
return result
if id_:
return self._add_upload_protocols(result)
else:
for key, value in result.items():
result[key] = self._add_upload_protocols(result[key])
return result

def _add_upload_protocols(self, board):
if not board.get("upload.protocols", []):
board.manifest['upload']['protocols'] = ["esptool", "espota"]
if not board.get("upload.protocol", ""):
board.manifest['upload']['protocol'] = "esptool"
return board

0 comments on commit ae96496

Please sign in to comment.