From 2059283707fb0145dcf920d76e90afb6d80a20fb Mon Sep 17 00:00:00 2001 From: rainero84 Date: Mon, 9 May 2022 07:21:43 +0200 Subject: [PATCH] Early pin init (#3439) * Added early_pin_init configuration parameter for ESP8266 platform * Added #include to core * Updated test3.yaml to include early_pin_init parameter Co-authored-by: Rainer Oellermann --- esphome/components/esp8266/__init__.py | 5 +++++ esphome/components/esp8266/const.py | 1 + esphome/components/esp8266/core.cpp | 3 +++ tests/test3.yaml | 6 ++++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/esphome/components/esp8266/__init__.py b/esphome/components/esp8266/__init__.py index 7b1be32e38..41d7688d44 100644 --- a/esphome/components/esp8266/__init__.py +++ b/esphome/components/esp8266/__init__.py @@ -19,6 +19,7 @@ from esphome.helpers import copy_file_if_changed from .const import ( CONF_RESTORE_FROM_FLASH, + CONF_EARLY_PIN_INIT, KEY_BOARD, KEY_ESP8266, KEY_PIN_INITIAL_STATES, @@ -148,6 +149,7 @@ CONFIG_SCHEMA = cv.All( cv.Required(CONF_BOARD): cv.string_strict, cv.Optional(CONF_FRAMEWORK, default={}): ARDUINO_FRAMEWORK_SCHEMA, cv.Optional(CONF_RESTORE_FROM_FLASH, default=False): cv.boolean, + cv.Optional(CONF_EARLY_PIN_INIT, default=True): cv.boolean, cv.Optional(CONF_BOARD_FLASH_MODE, default="dout"): cv.one_of( *BUILD_FLASH_MODES, lower=True ), @@ -197,6 +199,9 @@ async def to_code(config): if config[CONF_RESTORE_FROM_FLASH]: cg.add_define("USE_ESP8266_PREFERENCES_FLASH") + if config[CONF_EARLY_PIN_INIT]: + cg.add_define("USE_ESP8266_EARLY_PIN_INIT") + # 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 diff --git a/esphome/components/esp8266/const.py b/esphome/components/esp8266/const.py index 70429297e0..7740a97ff4 100644 --- a/esphome/components/esp8266/const.py +++ b/esphome/components/esp8266/const.py @@ -4,6 +4,7 @@ KEY_ESP8266 = "esp8266" KEY_BOARD = "board" KEY_PIN_INITIAL_STATES = "pin_initial_states" CONF_RESTORE_FROM_FLASH = "restore_from_flash" +CONF_EARLY_PIN_INIT = "early_pin_init" # esp8266 namespace is already defined by arduino, manually prefix esphome esp8266_ns = cg.global_ns.namespace("esphome").namespace("esp8266") diff --git a/esphome/components/esp8266/core.cpp b/esphome/components/esp8266/core.cpp index a9460f51f2..2d3959b031 100644 --- a/esphome/components/esp8266/core.cpp +++ b/esphome/components/esp8266/core.cpp @@ -1,6 +1,7 @@ #ifdef USE_ESP8266 #include "core.h" +#include "esphome/core/defines.h" #include "esphome/core/hal.h" #include "esphome/core/helpers.h" #include "preferences.h" @@ -55,6 +56,7 @@ extern "C" void resetPins() { // NOLINT // ourselves and this causes pins to toggle during reboot. force_link_symbols(); +#ifdef USE_ESP8266_EARLY_PIN_INIT for (int i = 0; i < 16; i++) { uint8_t mode = ESPHOME_ESP8266_GPIO_INITIAL_MODE[i]; uint8_t level = ESPHOME_ESP8266_GPIO_INITIAL_LEVEL[i]; @@ -63,6 +65,7 @@ extern "C" void resetPins() { // NOLINT if (level != 255) digitalWrite(i, level); // NOLINT } +#endif } } // namespace esphome diff --git a/tests/test3.yaml b/tests/test3.yaml index 29a70d3cc3..e3818d87ec 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -1,8 +1,6 @@ esphome: name: $device_name comment: $device_comment - platform: ESP8266 - board: d1_mini build_path: build/test3 on_boot: - if: @@ -15,6 +13,10 @@ esphome: includes: - custom.h +esp8266: + board: d1_mini + early_pin_init: True + substitutions: device_name: test3 device_comment: test3 device