mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 05:03:48 +00: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:
@@ -1,9 +1,15 @@
|
||||
#include "logger.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#ifdef USE_ESP_IDF
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include <driver/uart.h>
|
||||
#endif
|
||||
|
||||
#if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
|
||||
#include <esp_log.h>
|
||||
#endif
|
||||
#include <HardwareSerial.h>
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace logger {
|
||||
@@ -63,11 +69,11 @@ void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStr
|
||||
recursion_guard_ = true;
|
||||
this->reset_buffer_();
|
||||
// copy format string
|
||||
const char *format_pgm_p = (PGM_P) format;
|
||||
auto *format_pgm_p = reinterpret_cast<const uint8_t *>(format);
|
||||
size_t len = 0;
|
||||
char ch = '.';
|
||||
while (!this->is_buffer_full_() && ch != '\0') {
|
||||
this->tx_buffer_[this->tx_buffer_at_++] = ch = pgm_read_byte(format_pgm_p++);
|
||||
this->tx_buffer_[this->tx_buffer_at_++] = ch = (char) progmem_read_byte(format_pgm_p++);
|
||||
}
|
||||
// Buffer full form copying format
|
||||
if (this->is_buffer_full_())
|
||||
@@ -105,19 +111,26 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) {
|
||||
this->set_null_terminator_();
|
||||
|
||||
const char *msg = this->tx_buffer_ + offset;
|
||||
#ifdef USE_ARDUINO
|
||||
if (this->baud_rate_ > 0)
|
||||
this->hw_serial_->println(msg);
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#endif // USE_ARDUINO
|
||||
#ifdef USE_ESP_IDF
|
||||
uart_write_bytes(uart_num_, msg, strlen(msg));
|
||||
uart_write_bytes(uart_num_, "\n", 1);
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32
|
||||
// Suppress network-logging if memory constrained, but still log to serial
|
||||
// ports. In some configurations (eg BLE enabled) there may be some transient
|
||||
// memory exhaustion, and trying to log when OOM can lead to a crash. Skipping
|
||||
// here usually allows the stack to recover instead.
|
||||
// See issue #1234 for analysis.
|
||||
if (xPortGetFreeHeapSize() > 2048)
|
||||
this->log_callback_.call(level, tag, msg);
|
||||
#else
|
||||
this->log_callback_.call(level, tag, msg);
|
||||
if (xPortGetFreeHeapSize() < 2048)
|
||||
return;
|
||||
#endif
|
||||
|
||||
this->log_callback_.call(level, tag, msg);
|
||||
}
|
||||
|
||||
Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size, UARTSelection uart)
|
||||
@@ -128,9 +141,10 @@ Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size, UARTSelection uart)
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
#ifdef USE_ARDUINO
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#ifdef USE_ESP8266
|
||||
case UART_SELECTION_UART0_SWAP:
|
||||
#endif
|
||||
this->hw_serial_ = &Serial;
|
||||
@@ -138,7 +152,7 @@ void Logger::pre_setup() {
|
||||
case UART_SELECTION_UART1:
|
||||
this->hw_serial_ = &Serial1;
|
||||
break;
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#ifdef USE_ESP32
|
||||
case UART_SELECTION_UART2:
|
||||
#if !CONFIG_IDF_TARGET_ESP32S2 && !CONFIG_IDF_TARGET_ESP32C3
|
||||
// FIXME: Validate in config that UART2 can't be set for ESP32-S2 (only has
|
||||
@@ -148,23 +162,51 @@ void Logger::pre_setup() {
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
#endif // USE_ARDUINO
|
||||
#ifdef USE_ESP_IDF
|
||||
uart_num_ = UART_NUM_0;
|
||||
switch (uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
uart_num_ = UART_NUM_0;
|
||||
break;
|
||||
case UART_SELECTION_UART1:
|
||||
uart_num_ = UART_NUM_1;
|
||||
break;
|
||||
case UART_SELECTION_UART2:
|
||||
uart_num_ = UART_NUM_2;
|
||||
break;
|
||||
}
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = (int) baud_rate_,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
};
|
||||
uart_param_config(uart_num_, &uart_config);
|
||||
const int uart_buffer_size = tx_buffer_size_;
|
||||
// Install UART driver using an event queue here
|
||||
uart_driver_install(uart_num_, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
|
||||
#endif
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
this->hw_serial_->begin(this->baud_rate_);
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#ifdef USE_ESP8266
|
||||
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
|
||||
this->hw_serial_->swap();
|
||||
}
|
||||
this->hw_serial_->setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
|
||||
#endif
|
||||
#endif // USE_ARDUINO
|
||||
}
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#ifdef USE_ESP8266
|
||||
else {
|
||||
uart_set_debug(UART_NO);
|
||||
}
|
||||
#endif
|
||||
|
||||
global_logger = this;
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO)
|
||||
esp_log_set_vprintf(esp_idf_log_vprintf_);
|
||||
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
|
||||
esp_log_level_set("*", ESP_LOG_VERBOSE);
|
||||
@@ -183,10 +225,10 @@ void Logger::add_on_log_callback(std::function<void(int, const char *, const cha
|
||||
}
|
||||
float Logger::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
|
||||
const char *const LOG_LEVELS[] = {"NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE"};
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#ifdef USE_ESP32
|
||||
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "UART2"};
|
||||
#endif
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#ifdef USE_ESP8266
|
||||
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "UART0_SWAP"};
|
||||
#endif
|
||||
void Logger::dump_config() {
|
||||
|
||||
Reference in New Issue
Block a user