2015-11-18 17:16:17 +02:00
|
|
|
# Copyright 2014-2015 Ivan Kravets <me@ikravets.com>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2015-04-02 20:24:22 +03:00
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
|
2015-04-02 20:24:22 +03:00
|
|
|
"""
|
|
|
|
Builder for Espressif MCUs
|
|
|
|
"""
|
|
|
|
|
2015-12-21 20:39:45 +02:00
|
|
|
import re
|
2015-12-11 15:31:36 +02:00
|
|
|
import socket
|
2015-04-02 20:24:22 +03:00
|
|
|
from os.path import join
|
|
|
|
|
|
|
|
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default,
|
|
|
|
DefaultEnvironment)
|
|
|
|
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
def _get_flash_size(env):
|
2015-12-21 20:39:45 +02:00
|
|
|
# use board's flash size by default
|
|
|
|
board_max_size = int(
|
|
|
|
env.get("BOARD_OPTIONS", {}).get("upload", {}).get("maximum_size", 0))
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
# check if user overrides LD Script
|
|
|
|
match = re.search(r"\.flash\.(\d+)(m|k).*\.ld", env.GetActualLDScript())
|
|
|
|
if match:
|
2015-12-21 20:39:45 +02:00
|
|
|
if match.group(2) == "k":
|
|
|
|
board_max_size = int(match.group(1)) * 1024
|
|
|
|
elif match.group(2) == "m":
|
|
|
|
board_max_size = int(match.group(1)) * 1024 * 1024
|
|
|
|
|
|
|
|
return ("%dK" % (board_max_size / 1024) if board_max_size < 1048576
|
|
|
|
else "%dM" % (board_max_size / 1048576))
|
|
|
|
|
|
|
|
|
2015-04-02 20:24:22 +03:00
|
|
|
env = DefaultEnvironment()
|
|
|
|
|
|
|
|
env.Replace(
|
2015-12-21 20:39:45 +02:00
|
|
|
__get_flash_size=_get_flash_size,
|
|
|
|
|
2015-04-02 20:24:22 +03:00
|
|
|
AR="xtensa-lx106-elf-ar",
|
|
|
|
AS="xtensa-lx106-elf-as",
|
|
|
|
CC="xtensa-lx106-elf-gcc",
|
|
|
|
CXX="xtensa-lx106-elf-g++",
|
|
|
|
OBJCOPY="xtensa-lx106-elf-objcopy",
|
|
|
|
RANLIB="xtensa-lx106-elf-ranlib",
|
|
|
|
SIZETOOL="xtensa-lx106-elf-size",
|
|
|
|
|
|
|
|
ARFLAGS=["rcs"],
|
|
|
|
|
|
|
|
ASPPFLAGS=["-x", "assembler-with-cpp"],
|
|
|
|
|
|
|
|
CFLAGS=[
|
2015-10-02 16:11:00 +03:00
|
|
|
"-std=gnu99",
|
2015-04-02 20:24:22 +03:00
|
|
|
"-Wpointer-arith",
|
|
|
|
"-Wno-implicit-function-declaration",
|
|
|
|
"-Wl,-EL",
|
2015-04-07 19:44:28 +03:00
|
|
|
"-fno-inline-functions",
|
2015-04-02 20:24:22 +03:00
|
|
|
"-nostdlib"
|
|
|
|
],
|
|
|
|
|
|
|
|
CPPFLAGS=[
|
|
|
|
"-Os", # optimize for size
|
|
|
|
"-mlongcalls",
|
|
|
|
"-mtext-section-literals",
|
2015-05-21 17:03:53 +03:00
|
|
|
"-falign-functions=4",
|
|
|
|
"-U__STRICT_ANSI__",
|
2015-12-01 11:21:23 +02:00
|
|
|
"-ffunction-sections",
|
|
|
|
"-fdata-sections",
|
2015-04-02 20:24:22 +03:00
|
|
|
"-MMD" # output dependancy info
|
|
|
|
],
|
|
|
|
|
|
|
|
CXXFLAGS=[
|
|
|
|
"-fno-rtti",
|
|
|
|
"-fno-exceptions",
|
|
|
|
"-std=c++11"
|
|
|
|
],
|
|
|
|
|
|
|
|
CPPDEFINES=[
|
|
|
|
"F_CPU=$BOARD_F_CPU",
|
|
|
|
"__ets__",
|
|
|
|
"ICACHE_FLASH"
|
|
|
|
],
|
|
|
|
|
|
|
|
LINKFLAGS=[
|
|
|
|
"-nostdlib",
|
2015-04-08 20:49:48 +03:00
|
|
|
"-Wl,--no-check-sections",
|
2015-04-02 20:24:22 +03:00
|
|
|
"-u", "call_user_start",
|
2015-11-26 20:19:36 +02:00
|
|
|
"-Wl,-static",
|
2015-12-01 11:21:23 +02:00
|
|
|
"-Wl,--gc-sections"
|
2015-04-02 20:24:22 +03:00
|
|
|
],
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
#
|
|
|
|
# Upload
|
|
|
|
#
|
2015-04-02 20:24:22 +03:00
|
|
|
|
2015-04-10 18:23:21 +03:00
|
|
|
UPLOADER=join("$PIOPACKAGES_DIR", "tool-esptool", "esptool"),
|
2015-12-28 01:15:06 +02:00
|
|
|
UPLOADEROTA=join("$PLATFORMFW_DIR", "tools", "espota.py"),
|
|
|
|
|
2015-04-02 20:24:22 +03:00
|
|
|
UPLOADERFLAGS=[
|
2015-04-10 18:23:21 +03:00
|
|
|
"-vv",
|
2015-12-17 00:02:22 +02:00
|
|
|
"-cd", "${BOARD_OPTIONS['upload']['resetmethod']}",
|
2015-04-10 18:23:21 +03:00
|
|
|
"-cb", "$UPLOAD_SPEED",
|
|
|
|
"-cp", "$UPLOAD_PORT",
|
2015-10-02 16:11:00 +03:00
|
|
|
"-cf", "$SOURCE"
|
2015-04-02 20:24:22 +03:00
|
|
|
],
|
2015-12-28 01:15:06 +02:00
|
|
|
UPLOADERFSFLAGS=[
|
|
|
|
"$UPLOADERFLAGS",
|
|
|
|
"-ca", "${int(SPIFFS_START, 16)}"
|
|
|
|
],
|
|
|
|
UPLOADEROTAFLAGS=[
|
|
|
|
"--debug",
|
|
|
|
"--progress",
|
|
|
|
"-i", "$UPLOAD_PORT",
|
|
|
|
"-f", "$SOURCE"
|
|
|
|
],
|
|
|
|
|
2015-12-18 15:20:37 +02:00
|
|
|
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS',
|
2015-12-28 01:15:06 +02:00
|
|
|
UPLOADFSCMD='"$UPLOADER" $UPLOADERFSFLAGS',
|
|
|
|
UPLOADOTACMD='"$UPLOADEROTA" $UPLOADEROTAFLAGS',
|
|
|
|
|
|
|
|
#
|
|
|
|
# Misc
|
|
|
|
#
|
|
|
|
|
|
|
|
MKSPIFFSTOOL=join("$PIOPACKAGES_DIR", "tool-mkspiffs", "mkspiffs"),
|
|
|
|
SIZEPRINTCMD='"$SIZETOOL" -B -d $SOURCES',
|
2015-08-01 17:41:05 +03:00
|
|
|
|
|
|
|
PROGNAME="firmware",
|
|
|
|
PROGSUFFIX=".elf"
|
2015-04-02 20:24:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
env.Append(
|
|
|
|
BUILDERS=dict(
|
|
|
|
ElfToBin=Builder(
|
|
|
|
action=" ".join([
|
2015-12-18 15:20:37 +02:00
|
|
|
'"$UPLOADER"',
|
|
|
|
"-eo",
|
|
|
|
'"%s"' % join("$PLATFORMFW_DIR", "bootloaders",
|
|
|
|
"eboot", "eboot.elf"),
|
2015-10-02 16:11:00 +03:00
|
|
|
"-bo", "$TARGET",
|
|
|
|
"-bm", "dio",
|
|
|
|
"-bf", "${BOARD_OPTIONS['build']['f_cpu'][:2]}",
|
2015-12-21 20:39:45 +02:00
|
|
|
"-bz", "${__get_flash_size(__env__)}",
|
2015-10-02 16:11:00 +03:00
|
|
|
"-bs", ".text",
|
|
|
|
"-bp", "4096",
|
|
|
|
"-ec",
|
2015-04-10 18:23:21 +03:00
|
|
|
"-eo", "$SOURCES",
|
2015-10-02 16:11:00 +03:00
|
|
|
"-bs", ".irom0.text",
|
2015-04-10 18:23:21 +03:00
|
|
|
"-bs", ".text",
|
|
|
|
"-bs", ".data",
|
|
|
|
"-bs", ".rodata",
|
2015-10-02 16:11:00 +03:00
|
|
|
"-bc", "-ec"
|
2015-04-02 20:24:22 +03:00
|
|
|
]),
|
|
|
|
suffix=".bin"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# SPIFFS
|
|
|
|
#
|
|
|
|
|
|
|
|
def _fetch_spiffs_size(target, source, env):
|
|
|
|
spiffs_re = re.compile(
|
|
|
|
r"PROVIDE\s*\(\s*_SPIFFS_(\w+)\s*=\s*(0x[\dA-F]+)\s*\)")
|
|
|
|
with open(env.GetActualLDScript()) as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
match = spiffs_re.search(line)
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
env["SPIFFS_%s" % match.group(1).upper()] = match.group(2)
|
|
|
|
|
|
|
|
assert all([k in env for k in ["SPIFFS_START", "SPIFFS_END", "SPIFFS_PAGE",
|
|
|
|
"SPIFFS_BLOCK"]])
|
|
|
|
return (target, source)
|
|
|
|
|
|
|
|
|
|
|
|
env.Append(
|
|
|
|
BUILDERS=dict(
|
|
|
|
DataToBin=Builder(
|
|
|
|
action=" ".join([
|
|
|
|
'"$MKSPIFFSTOOL"',
|
|
|
|
"-c", "$SOURCES",
|
|
|
|
"-p", "${int(SPIFFS_PAGE, 16)}",
|
|
|
|
"-b", "${int(SPIFFS_BLOCK, 16)}",
|
|
|
|
"-s", "${int(SPIFFS_END, 16) - int(SPIFFS_START, 16)}",
|
|
|
|
"$TARGET"
|
|
|
|
]),
|
|
|
|
emitter=_fetch_spiffs_size,
|
|
|
|
source_factory=env.Dir,
|
|
|
|
suffix=".bin"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Framework and SDK specific configuration
|
|
|
|
#
|
|
|
|
|
2015-12-11 15:31:36 +02:00
|
|
|
if "FRAMEWORK" in env:
|
|
|
|
env.Append(
|
|
|
|
LINKFLAGS=[
|
|
|
|
"-Wl,-wrap,system_restart_local",
|
|
|
|
"-Wl,-wrap,register_chipv6_phy"
|
|
|
|
]
|
|
|
|
)
|
2015-04-09 18:50:03 +03:00
|
|
|
|
2015-12-11 15:31:36 +02:00
|
|
|
# Handle uploading via OTA
|
|
|
|
try:
|
|
|
|
if env.get("UPLOAD_PORT") and socket.inet_aton(env.get("UPLOAD_PORT")):
|
|
|
|
env.Replace(
|
2015-12-28 01:15:06 +02:00
|
|
|
UPLOADCMD="$UPLOADOTACMD"
|
2015-12-11 15:31:36 +02:00
|
|
|
)
|
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Configure native SDK
|
|
|
|
else:
|
2015-04-09 18:50:03 +03:00
|
|
|
env.Append(
|
|
|
|
CPPPATH=[
|
|
|
|
join("$PIOPACKAGES_DIR", "sdk-esp8266", "include"),
|
|
|
|
"$PROJECTSRC_DIR"
|
|
|
|
],
|
2015-10-02 16:11:00 +03:00
|
|
|
LIBPATH=[join("$PIOPACKAGES_DIR", "sdk-esp8266", "lib")],
|
|
|
|
BUILDERS=dict(
|
|
|
|
ElfToBin=Builder(
|
|
|
|
action=" ".join([
|
2015-12-18 15:20:37 +02:00
|
|
|
'"$UPLOADER"',
|
2015-10-02 16:11:00 +03:00
|
|
|
"-eo", "$SOURCES",
|
|
|
|
"-bo", "${TARGETS[0]}",
|
|
|
|
"-bm", "qio",
|
|
|
|
"-bf", "40",
|
|
|
|
"-bz", "512K",
|
|
|
|
"-bs", ".text",
|
|
|
|
"-bs", ".data",
|
|
|
|
"-bs", ".rodata",
|
|
|
|
"-bc", "-ec",
|
|
|
|
"-eo", "$SOURCES",
|
|
|
|
"-es", ".irom0.text", "${TARGETS[1]}",
|
|
|
|
"-ec", "-v"
|
|
|
|
]),
|
|
|
|
suffix=".bin"
|
|
|
|
)
|
|
|
|
)
|
2015-04-09 18:50:03 +03:00
|
|
|
)
|
|
|
|
env.Replace(
|
|
|
|
LDSCRIPT_PATH=join(
|
|
|
|
"$PIOPACKAGES_DIR", "sdk-esp8266", "ld", "eagle.app.v6.ld"),
|
|
|
|
LIBS=["c", "gcc", "phy", "pp", "net80211", "lwip", "wpa", "main",
|
2015-10-02 16:11:00 +03:00
|
|
|
"json", "upgrade", "smartconfig", "pwm", "at", "ssl"],
|
|
|
|
UPLOADERFLAGS=[
|
|
|
|
"-vv",
|
|
|
|
"-cd", "ck",
|
|
|
|
"-cb", "$UPLOAD_SPEED",
|
|
|
|
"-cp", "$UPLOAD_PORT",
|
|
|
|
"-ca", "0x00000",
|
|
|
|
"-cf", "${SOURCES[0]}",
|
|
|
|
"-ca", "0x40000",
|
|
|
|
"-cf", "${SOURCES[1]}"
|
|
|
|
]
|
2015-04-09 18:50:03 +03:00
|
|
|
)
|
|
|
|
|
2015-04-02 20:24:22 +03:00
|
|
|
#
|
|
|
|
# Target: Build executable and linkable firmware
|
|
|
|
#
|
|
|
|
|
2015-08-02 19:52:37 +03:00
|
|
|
target_elf = env.BuildProgram()
|
2015-04-02 20:24:22 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Build the .hex
|
|
|
|
#
|
|
|
|
|
|
|
|
if "uploadlazy" in COMMAND_LINE_TARGETS:
|
2015-10-02 16:11:00 +03:00
|
|
|
if "FRAMEWORK" not in env:
|
|
|
|
target_firm = [
|
|
|
|
join("$BUILD_DIR", "firmware_00000.bin"),
|
|
|
|
join("$BUILD_DIR", "firmware_40000.bin")
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
target_firm = join("$BUILD_DIR", "firmware.bin")
|
2015-04-02 20:24:22 +03:00
|
|
|
else:
|
2015-10-02 16:11:00 +03:00
|
|
|
if "FRAMEWORK" not in env:
|
|
|
|
target_firm = env.ElfToBin(
|
|
|
|
[join("$BUILD_DIR", "firmware_00000"),
|
|
|
|
join("$BUILD_DIR", "firmware_40000")], target_elf)
|
|
|
|
else:
|
|
|
|
target_firm = env.ElfToBin(join("$BUILD_DIR", "firmware"), target_elf)
|
2015-04-02 20:24:22 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Print binary size
|
|
|
|
#
|
|
|
|
|
|
|
|
target_size = env.Alias("size", target_elf, "$SIZEPRINTCMD")
|
|
|
|
AlwaysBuild(target_size)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Upload firmware
|
|
|
|
#
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
target_upload = env.Alias(
|
|
|
|
["upload", "uploadlazy"], target_firm,
|
|
|
|
[lambda target, source, env: env.AutodetectUploadPort(), "$UPLOADCMD"])
|
|
|
|
env.AlwaysBuild(target_upload)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Upload SPIFFS image
|
|
|
|
#
|
|
|
|
|
|
|
|
target_mkspiffs = env.DataToBin(join("$BUILD_DIR", "spiffs_image"),
|
|
|
|
"$PROJECTDATA_DIR")
|
|
|
|
target_uploadfs = env.Alias(
|
|
|
|
"uploadfs", target_mkspiffs,
|
|
|
|
[lambda target, source, env: env.AutodetectUploadPort(), "$UPLOADFSCMD"])
|
|
|
|
env.AlwaysBuild(target_mkspiffs, target_uploadfs)
|
2015-04-02 20:24:22 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Define targets
|
|
|
|
#
|
|
|
|
|
|
|
|
Default([target_firm, target_size])
|