1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-22 21:22:22 +01:00

ESP-IDF support and generic target platforms (#2303)

* Socket refactor and SSL

* esp-idf temp

* Fixes

* Echo component and noise

* Add noise API transport support

* Updates

* ESP-IDF

* Complete

* Fixes

* Fixes

* Versions update

* New i2c APIs

* Complete i2c refactor

* SPI migration

* Revert ESP Preferences migration, too complex for now

* OTA support

* Remove echo again

* Remove ssl again

* GPIOFlags updates

* Rename esphal and ICACHE_RAM_ATTR

* Make ESP32 arduino compilable again

* Fix GPIO flags

* Complete pin registry refactor and fixes

* Fixes to make test1 compile

* Remove sdkconfig file

* Ignore sdkconfig file

* Fixes in reviewing

* Make test2 compile

* Make test4 compile

* Make test5 compile

* Run clang-format

* Fix lint errors

* Use esp-idf APIs instead of btStart

* Another round of fixes

* Start implementing ESP8266

* Make test3 compile

* Guard esp8266 code

* Lint

* Reformat

* Fixes

* Fixes v2

* more fixes

* ESP-IDF tidy target

* Convert ARDUINO_ARCH_ESPxx

* Update WiFiSignalSensor

* Update time ifdefs

* OTA needs millis from hal

* RestartSwitch needs delay from hal

* ESP-IDF Uart

* Fix OTA blank password

* Allow setting sdkconfig

* Fix idf partitions and allow setting sdkconfig from yaml

* Re-add read/write compat APIs and fix esp8266 uart

* Fix esp8266 store log strings in flash

* Fix ESP32 arduino preferences not initialized

* Update ifdefs

* Change how sdkconfig change is detected

* Add checks to ci-custom and fix them

* Run clang-format

* Add esp-idf clang-tidy target and fix errors

* Fixes from clang-tidy idf round 2

* Fixes from compiling tests with esp-idf

* Run clang-format

* Switch test5.yaml to esp-idf

* Implement ESP8266 Preferences

* Lint

* Re-do PIO package version selection a bit

* Fix arduinoespressif32 package version

* Fix unit tests

* Lint

* Lint fixes

* Fix readv/writev not defined

* Fix graphing component

* Re-add all old options from core/config.py

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Otto Winter
2021-09-20 11:47:51 +02:00
committed by GitHub
parent 1e8e471dec
commit ac0d921413
583 changed files with 9008 additions and 5420 deletions

View File

@@ -4,7 +4,7 @@ import re
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation, boards
from esphome import automation
from esphome.const import (
CONF_ARDUINO_VERSION,
CONF_BOARD,
@@ -12,6 +12,7 @@ from esphome.const import (
CONF_BUILD_PATH,
CONF_COMMENT,
CONF_ESPHOME,
CONF_FRAMEWORK,
CONF_INCLUDES,
CONF_LIBRARIES,
CONF_NAME,
@@ -23,11 +24,10 @@ from esphome.const import (
CONF_PRIORITY,
CONF_PROJECT,
CONF_TRIGGER_ID,
CONF_ESP8266_RESTORE_FROM_FLASH,
ARDUINO_VERSION_ESP8266,
ARDUINO_VERSION_ESP32,
CONF_TYPE,
CONF_VERSION,
ESP_PLATFORMS,
KEY_CORE,
TARGET_PLATFORMS,
)
from esphome.core import CORE, coroutine_with_priority
from esphome.helpers import copy_file_if_changed, walk_files
@@ -50,79 +50,6 @@ VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+(?:[ab]\d+)?$")
CONF_NAME_ADD_MAC_SUFFIX = "name_add_mac_suffix"
def validate_board(value: str):
if CORE.is_esp8266:
boardlist = boards.ESP8266_BOARD_PINS.keys()
elif CORE.is_esp32:
boardlist = list(boards.ESP32_BOARD_PINS.keys())
boardlist += list(boards.ESP32_C3_BOARD_PINS.keys())
else:
raise NotImplementedError
if value not in boardlist:
raise cv.Invalid(
f"Could not find board '{value}'. Valid boards are {', '.join(sorted(boardlist))}"
)
return value
validate_platform = cv.one_of(*ESP_PLATFORMS, upper=True)
PLATFORMIO_ESP8266_LUT = {
**ARDUINO_VERSION_ESP8266,
# Keep this in mind when updating the recommended version:
# * New framework historically have had some regressions, especially for WiFi, BLE and the
# bootloader system. The new version needs to be thoroughly validated before changing the
# recommended version as otherwise a bunch of devices could be bricked
# * The docker images need to be updated to ship the new recommended version, in order not
# to DDoS platformio servers.
# Update this file: https://github.com/esphome/esphome-docker-base/blob/main/platformio.ini
"RECOMMENDED": ARDUINO_VERSION_ESP8266["2.7.4"],
"LATEST": "espressif8266",
"DEV": ARDUINO_VERSION_ESP8266["dev"],
}
PLATFORMIO_ESP32_LUT = {
**ARDUINO_VERSION_ESP32,
# See PLATFORMIO_ESP8266_LUT for considerations when changing the recommended version
"RECOMMENDED": ARDUINO_VERSION_ESP32["1.0.6"],
"LATEST": "espressif32",
"DEV": ARDUINO_VERSION_ESP32["dev"],
}
def validate_arduino_version(value):
value = cv.string_strict(value)
value_ = value.upper()
if CORE.is_esp8266:
if (
VERSION_REGEX.match(value) is not None
and value_ not in PLATFORMIO_ESP8266_LUT
):
raise cv.Invalid(
f"Unfortunately the arduino framework version '{value}' is unsupported at this time. You can override this by manually using espressif8266@<platformio version>"
)
if value_ in PLATFORMIO_ESP8266_LUT:
return PLATFORMIO_ESP8266_LUT[value_]
return value
if CORE.is_esp32:
if (
VERSION_REGEX.match(value) is not None
and value_ not in PLATFORMIO_ESP32_LUT
):
raise cv.Invalid(
f"Unfortunately the arduino framework version '{value}' is unsupported at this time. You can override this by manually using espressif32@<platformio version>"
)
if value_ in PLATFORMIO_ESP32_LUT:
return PLATFORMIO_ESP32_LUT[value_]
return value
raise NotImplementedError
def default_build_path():
return CORE.name
VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"}
@@ -149,27 +76,17 @@ def valid_project_name(value: str):
return value
CONF_ESP8266_RESTORE_FROM_FLASH = "esp8266_restore_from_flash"
CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_NAME): cv.hostname,
cv.Required(CONF_PLATFORM): cv.one_of("ESP8266", "ESP32", upper=True),
cv.Required(CONF_BOARD): validate_board,
cv.Optional(CONF_COMMENT): cv.string,
cv.Optional(
CONF_ARDUINO_VERSION, default="recommended"
): validate_arduino_version,
cv.Optional(CONF_BUILD_PATH, default=default_build_path): cv.string,
cv.Required(CONF_BUILD_PATH): cv.string,
cv.Optional(CONF_PLATFORMIO_OPTIONS, default={}): cv.Schema(
{
cv.string_strict: cv.Any([cv.string], cv.string),
}
),
cv.SplitDefault(CONF_ESP8266_RESTORE_FROM_FLASH, esp8266=False): cv.All(
cv.only_on_esp8266, cv.boolean
),
cv.SplitDefault(CONF_BOARD_FLASH_MODE, esp8266="dout"): cv.one_of(
*BUILD_FLASH_MODES, lower=True
),
cv.Optional(CONF_ON_BOOT): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StartupTrigger),
@@ -201,38 +118,78 @@ CONFIG_SCHEMA = cv.Schema(
PRELOAD_CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_NAME): cv.valid_name,
cv.Required(CONF_PLATFORM): validate_platform,
cv.Optional(CONF_BUILD_PATH): cv.string,
# Compat options, these were moved to target-platform specific sections
# but we'll keep these around for a long time because every config would
# be impacted
cv.Optional(CONF_PLATFORM): cv.one_of(*TARGET_PLATFORMS, lower=True),
cv.Optional(CONF_BOARD): cv.string_strict,
cv.Optional(CONF_ESP8266_RESTORE_FROM_FLASH): cv.valid,
cv.Optional(CONF_BOARD_FLASH_MODE): cv.valid,
cv.Optional(CONF_ARDUINO_VERSION): cv.valid,
},
extra=cv.ALLOW_EXTRA,
)
PRELOAD_CONFIG_SCHEMA2 = PRELOAD_CONFIG_SCHEMA.extend(
{
cv.Required(CONF_BOARD): validate_board,
cv.Optional(CONF_BUILD_PATH, default=default_build_path): cv.string,
}
)
def preload_core_config(config, result):
with cv.prepend_path(CONF_ESPHOME):
conf = PRELOAD_CONFIG_SCHEMA(config[CONF_ESPHOME])
def preload_core_config(config):
core_key = "esphome"
if "esphomeyaml" in config:
_LOGGER.warning(
"The esphomeyaml section has been renamed to esphome in 1.11.0. "
"Please replace 'esphomeyaml:' in your configuration with 'esphome:'."
CORE.name = conf[CONF_NAME]
CORE.data[KEY_CORE] = {}
if CONF_BUILD_PATH not in conf:
conf[CONF_BUILD_PATH] = CORE.name
CORE.build_path = CORE.relative_config_path(conf[CONF_BUILD_PATH])
has_oldstyle = CONF_PLATFORM in conf
newstyle_found = [key for key in TARGET_PLATFORMS if key in config]
oldstyle_opts = [
CONF_ESP8266_RESTORE_FROM_FLASH,
CONF_BOARD_FLASH_MODE,
CONF_ARDUINO_VERSION,
CONF_BOARD,
]
if not has_oldstyle and not newstyle_found:
raise cv.Invalid("Platform missing for core options!", [CONF_ESPHOME])
if has_oldstyle and newstyle_found:
raise cv.Invalid(
f"Please remove the `platform` key from the [esphome] block. You're already using the new style with the [{conf[CONF_PLATFORM]}] block",
[CONF_ESPHOME, CONF_PLATFORM],
)
config[CONF_ESPHOME] = config.pop("esphomeyaml")
core_key = "esphomeyaml"
if CONF_ESPHOME not in config:
raise cv.RequiredFieldInvalid("required key not provided", CONF_ESPHOME)
with cv.prepend_path(core_key):
out = PRELOAD_CONFIG_SCHEMA(config[CONF_ESPHOME])
CORE.name = out[CONF_NAME]
CORE.esp_platform = out[CONF_PLATFORM]
with cv.prepend_path(core_key):
out2 = PRELOAD_CONFIG_SCHEMA2(config[CONF_ESPHOME])
CORE.board = out2[CONF_BOARD]
CORE.build_path = CORE.relative_config_path(out2[CONF_BUILD_PATH])
if len(newstyle_found) > 1:
raise cv.Invalid(
f"Found multiple target platform blocks: {', '.join(newstyle_found)}. Only one is allowed.",
[newstyle_found[0]],
)
if newstyle_found:
# Convert to newstyle
for key in oldstyle_opts:
if key in conf:
raise cv.Invalid(
f"Please move {key} to the [{newstyle_found[0]}] block.",
[CONF_ESPHOME, key],
)
if has_oldstyle:
plat = conf.pop(CONF_PLATFORM)
plat_conf = {}
if CONF_ESP8266_RESTORE_FROM_FLASH in conf:
plat_conf["restore_from_flash"] = conf.pop(CONF_ESP8266_RESTORE_FROM_FLASH)
if CONF_BOARD_FLASH_MODE in conf:
plat_conf[CONF_BOARD_FLASH_MODE] = conf.pop(CONF_BOARD_FLASH_MODE)
if CONF_ARDUINO_VERSION in conf:
plat_conf[CONF_FRAMEWORK] = {
CONF_TYPE: "arduino",
CONF_VERSION: conf.pop(CONF_ARDUINO_VERSION),
}
if CONF_BOARD in conf:
plat_conf[CONF_BOARD] = conf.pop(CONF_BOARD)
# Insert generated target platform config to main config
config[plat] = plat_conf
config[CONF_ESPHOME] = conf
def include_file(path, basename):
@@ -263,25 +220,10 @@ async def add_includes(includes):
@coroutine_with_priority(-1000.0)
async def _esp8266_add_lwip_type():
# If any component has already set this, do not change it
if any(
flag.startswith("-DPIO_FRAMEWORK_ARDUINO_LWIP2_") for flag in CORE.build_flags
):
return
# Default for platformio is LWIP2_LOW_MEMORY with:
# - MSS=536
# - LWIP_FEATURES enabled
# - this only adds some optional features like IP incoming packet reassembly and NAPT
# see also:
# https://github.com/esp8266/Arduino/blob/master/tools/sdk/lwip2/include/lwipopts.h
# Instead we use LWIP2_HIGHER_BANDWIDTH_LOW_FLASH with:
# - MSS=1460
# - LWIP_FEATURES disabled (because we don't need them)
# Other projects like Tasmota & ESPEasy also use this
cg.add_build_flag("-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH")
async def _add_platformio_options(pio_options):
# Add includes at the very end, so that they override everything
for key, val in pio_options.items():
cg.add_platformio_option(key, val)
@coroutine_with_priority(30.0)
@@ -315,10 +257,6 @@ async def to_code(config):
CORE.add_job(_add_automations, config)
# Set LWIP build constants for ESP8266
if CORE.is_esp8266:
CORE.add_job(_esp8266_add_lwip_type)
cg.add_build_flag("-fno-exceptions")
# Libraries
@@ -337,25 +275,16 @@ async def to_code(config):
else:
cg.add_library(lib, None)
if CORE.is_esp8266:
# Arduino 2 has a non-standards conformant new that returns a nullptr instead of failing when
# out of memory and exceptions are disabled. Since Arduino 2.6.0, this flag can be used to make
# new abort instead. Use it so that OOM fails early (on allocation) instead of on dereference of
# a NULL pointer (so the stacktrace makes more sense), and for consistency with Arduino 3,
# which always aborts if exceptions are disabled.
# For cases where nullptrs can be handled, use nothrow: `new (std::nothrow) T;`
cg.add_build_flag("-DNEW_OOM_ABORT")
cg.add_build_flag("-Wno-unused-variable")
cg.add_build_flag("-Wno-unused-but-set-variable")
cg.add_build_flag("-Wno-sign-compare")
if config.get(CONF_ESP8266_RESTORE_FROM_FLASH, False):
cg.add_define("USE_ESP8266_PREFERENCES_FLASH")
if config[CONF_INCLUDES]:
CORE.add_job(add_includes, config[CONF_INCLUDES])
cg.add_define("ESPHOME_BOARD", CORE.board)
if CONF_PROJECT in config:
cg.add_define("ESPHOME_PROJECT_NAME", config[CONF_PROJECT][CONF_NAME])
cg.add_define("ESPHOME_PROJECT_VERSION", config[CONF_PROJECT][CONF_VERSION])
if config[CONF_PLATFORMIO_OPTIONS]:
CORE.add_job(_add_platformio_options, config[CONF_PLATFORMIO_OPTIONS])