From 07c5aae6b71bc969e0688842056be5763685f8b7 Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Sun, 7 Jul 2024 04:12:41 +0200 Subject: [PATCH] clean up code --- esphome/__main__.py | 1 - esphome/components/logger/__init__.py | 22 ++++++ esphome/components/logger/logger.cpp | 10 ++- esphome/components/logger/logger.h | 13 +++- esphome/components/logger/logger_zephyr.cpp | 83 +++++++++++++++++++++ esphome/components/nrf52/gpio.py | 19 +---- esphome/components/nrf52/power.cpp | 41 ---------- esphome/components/zephyr/__init__.py | 37 --------- 8 files changed, 125 insertions(+), 101 deletions(-) create mode 100644 esphome/components/logger/logger_zephyr.cpp delete mode 100644 esphome/components/nrf52/power.cpp diff --git a/esphome/__main__.py b/esphome/__main__.py index 11905a987a..e1ed2240af 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -387,7 +387,6 @@ def upload_program(config, args, host): raise EsphomeError(f"Unknown target platform: {CORE.target_platform}") if host == "PYOCD": - print(CORE) return upload_using_platformio(config, host, ["-t", "flash_pyocd"]) if host.startswith("mcumgr"): firmware = os.path.abspath( diff --git a/esphome/components/logger/__init__.py b/esphome/components/logger/__init__.py index 99aa39c4ba..f8ac71877b 100644 --- a/esphome/components/logger/__init__.py +++ b/esphome/components/logger/__init__.py @@ -22,6 +22,7 @@ from esphome.const import ( PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_RP2040, + PLATFORM_NRF52, ) from esphome.core import CORE, EsphomeError, Lambda, coroutine_with_priority from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant @@ -39,6 +40,12 @@ from esphome.components.libretiny.const import ( COMPONENT_BK72XX, COMPONENT_RTL87XX, ) +from esphome.components.zephyr import ( + zephyr_add_overlay, + zephyr_add_prj_conf, + zephyr_add_cdc_acm, +) + CODEOWNERS = ["@esphome/core"] logger_ns = cg.esphome_ns.namespace("logger") @@ -101,6 +108,8 @@ ESP_ARDUINO_UNSUPPORTED_USB_UARTS = [USB_SERIAL_JTAG] UART_SELECTION_RP2040 = [USB_CDC, UART0, UART1] +UART_SELECTION_NRF52 = [USB_CDC, UART0] + HARDWARE_UART_TO_UART_SELECTION = { UART0: logger_ns.UART_SELECTION_UART0, UART0_SWAP: logger_ns.UART_SELECTION_UART0_SWAP, @@ -153,6 +162,8 @@ def uart_selection(value): return cv.one_of(*UART_SELECTION_LIBRETINY[component], upper=True)(value) if CORE.is_host: raise cv.Invalid("Uart selection not valid for host platform") + if CORE.is_nrf52: + return cv.one_of(*UART_SELECTION_NRF52, upper=True)(value) raise NotImplementedError @@ -192,6 +203,7 @@ CONFIG_SCHEMA = cv.All( rp2040=USB_CDC, bk72xx=DEFAULT, rtl87xx=DEFAULT, + nrf52=USB_CDC, ): cv.All( cv.only_on( [ @@ -200,6 +212,7 @@ CONFIG_SCHEMA = cv.All( PLATFORM_RP2040, PLATFORM_BK72XX, PLATFORM_RTL87XX, + PLATFORM_NRF52, ] ), uart_selection, @@ -304,6 +317,15 @@ async def to_code(config): except cv.Invalid: pass + if CORE.using_zephyr: + if config[CONF_HARDWARE_UART] == UART0: + zephyr_add_overlay("""&uart0 { status = "okay";};""") + if config[CONF_HARDWARE_UART] == UART1: + zephyr_add_overlay("""&uart1 { status = "okay";};""") + if config[CONF_HARDWARE_UART] == USB_CDC: + zephyr_add_prj_conf("UART_LINE_CTRL", True) + zephyr_add_cdc_acm(config, 0) + # Register at end for safe mode await cg.register_component(log, config) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index dac08fbbce..977f9e06a0 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -41,6 +41,8 @@ void Logger::write_header_(int level, const char *tag, int line) { const char *letter = LOG_LEVEL_LETTERS[level]; #if defined(USE_ESP32) || defined(USE_LIBRETINY) TaskHandle_t current_task = xTaskGetCurrentTaskHandle(); +#elif defined(USE_ZEPHYR) + k_tid_t current_task = k_current_get(); #else void *current_task = nullptr; #endif @@ -52,6 +54,8 @@ void Logger::write_header_(int level, const char *tag, int line) { thread_name = pcTaskGetName(current_task); #elif defined(USE_LIBRETINY) thread_name = pcTaskGetTaskName(current_task); +#elif defined(USE_ZEPHYR) + thread_name = k_thread_name_get(current_task); #endif this->printf_to_buffer_("%s[%s][%s:%03u]%s[%s]%s: ", color, letter, tag, line, ESPHOME_LOG_BOLD(ESPHOME_LOG_COLOR_RED), thread_name, color); @@ -145,10 +149,13 @@ Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate this->tx_buffer_ = new char[this->tx_buffer_size_ + 1]; // NOLINT #if defined(USE_ESP32) || defined(USE_LIBRETINY) this->main_task_ = xTaskGetCurrentTaskHandle(); +#elif defined(USE_ZEPHYR) + this->main_task_ = k_current_get(); #endif } #ifdef USE_LOGGER_USB_CDC +#ifndef USE_ZEPHYR void Logger::loop() { #ifdef USE_ARDUINO if (this->uart_ != UART_SELECTION_USB_CDC) { @@ -165,13 +172,14 @@ void Logger::loop() { #endif } #endif +#endif void Logger::set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; } void Logger::set_log_level(const std::string &tag, int log_level) { this->log_levels_.push_back(LogLevelOverride{tag, log_level}); } -#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) +#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) UARTSelection Logger::get_uart() const { return this->uart_; } #endif diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index b55cfb0771..a3aaadb137 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -21,11 +21,15 @@ #include #endif // USE_ESP_IDF +#ifdef USE_ZEPHYR +struct device; +#endif + namespace esphome { namespace logger { -#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) +#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) /** Enum for logging UART selection * * Advanced configuration (pin selection, etc) is not supported. @@ -68,7 +72,7 @@ class Logger : public Component { #ifdef USE_ESP_IDF uart_port_t get_uart_num() const { return uart_num_; } #endif -#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) +#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) void set_uart_selection(UARTSelection uart_selection) { uart_ = uart_selection; } /// Get the UART used by the logger. UARTSelection get_uart() const; @@ -147,7 +151,7 @@ class Logger : public Component { char *tx_buffer_{nullptr}; int tx_buffer_at_{0}; int tx_buffer_size_{0}; -#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) +#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_ZEPHYR) UARTSelection uart_{UART_SELECTION_UART0}; #endif #ifdef USE_LIBRETINY @@ -158,6 +162,9 @@ class Logger : public Component { #endif #ifdef USE_ESP_IDF uart_port_t uart_num_; +#endif +#if defined(USE_ZEPHYR) + const device *uart_dev_{nullptr}; #endif struct LogLevelOverride { std::string tag; diff --git a/esphome/components/logger/logger_zephyr.cpp b/esphome/components/logger/logger_zephyr.cpp new file mode 100644 index 0000000000..2810aa0b52 --- /dev/null +++ b/esphome/components/logger/logger_zephyr.cpp @@ -0,0 +1,83 @@ +#ifdef USE_ZEPHYR + +#include "logger.h" +#include "esphome/core/log.h" +#include "esphome/core/application.h" + +#include +#include +#include + +namespace esphome { +namespace logger { + +static const char *const TAG = "logger"; + +void Logger::loop() { + if (this->uart_ != UART_SELECTION_USB_CDC || nullptr == uart_dev_) { + return; + } + static bool opened = false; + uint32_t dtr = 0; + uart_line_ctrl_get(uart_dev_, UART_LINE_CTRL_DTR, &dtr); + + /* Poll if the DTR flag was set, optional */ + if (opened == dtr) { + return; + } + + if (false == opened) { + App.schedule_dump_config(); + } + opened = !opened; +} + +void Logger::pre_setup() { + if (this->baud_rate_ > 0) { + static const struct device *uart_dev = nullptr; + switch (this->uart_) { + case UART_SELECTION_UART0: + uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0)); + break; + case UART_SELECTION_UART1: + uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1)); + break; + case UART_SELECTION_USB_CDC: + uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0)); + if (device_is_ready(uart_dev)) { + usb_enable(NULL); + } + break; + } + if (!device_is_ready(uart_dev)) { + ESP_LOGE(TAG, "%s is not ready.", get_uart_selection_()); + } else { + uart_dev_ = uart_dev; + } + } + global_logger = this; + ESP_LOGI(TAG, "Log initialized"); +} + +void HOT Logger::write_msg_(const char *msg) { +#ifdef CONFIG_PRINTK + printk("%s\n", msg); +#endif + if (nullptr == uart_dev_) { + return; + } + while (*msg) { + uart_poll_out(uart_dev_, *msg); + ++msg; + } + uart_poll_out(uart_dev_, '\n'); +} + +const char *const UART_SELECTIONS[] = {"UART0", "UART1", "USB_CDC"}; + +const char *Logger::get_uart_selection_() { return UART_SELECTIONS[this->uart_]; } + +} // namespace logger +} // namespace esphome + +#endif diff --git a/esphome/components/nrf52/gpio.py b/esphome/components/nrf52/gpio.py index 5b31d63b57..4abe8f4d83 100644 --- a/esphome/components/nrf52/gpio.py +++ b/esphome/components/nrf52/gpio.py @@ -7,7 +7,6 @@ from esphome.const import ( CONF_MODE, CONF_INVERTED, CONF_NUMBER, - CONF_ANALOG, ) from esphome.components.zephyr.const import ( zephyr_ns, @@ -36,23 +35,7 @@ def _translate_pin(value): raise cv.Invalid(f"Invalid pin: {value}") -ADC_INPUTS = [ - "AIN0", - "AIN1", - "AIN2", - "AIN3", - "AIN4", - "AIN5", - "AIN6", - "AIN7", - "VDD", - "VDDHDIV5", -] - - def validate_gpio_pin(value): - if value in ADC_INPUTS: - return value value = _translate_pin(value) if value < 0 or value > (32 + 16): raise cv.Invalid(f"NRF52: Invalid pin number: {value}") @@ -63,7 +46,7 @@ NRF52_PIN_SCHEMA = cv.All( pins.gpio_base_schema( GPIOPin, validate_gpio_pin, - modes=pins.GPIO_STANDARD_MODES + (CONF_ANALOG,), + modes=pins.GPIO_STANDARD_MODES, ), ) diff --git a/esphome/components/nrf52/power.cpp b/esphome/components/nrf52/power.cpp deleted file mode 100644 index 88e5228aa4..0000000000 --- a/esphome/components/nrf52/power.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#ifdef USE_NRF52 -#include -#include - -namespace esphome { -namespace nrf52 { - -static int board_esphome_init(void) { - /* if the board is powered from USB - * (high voltage mode), GPIO output voltage is set to 1.8 volts by - * default and that is not enough to turn the green and blue LEDs on. - * Increase GPIO voltage to 3.3 volts. - */ - if ((nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) && - ((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) == (UICR_REGOUT0_VOUT_DEFAULT << UICR_REGOUT0_VOUT_Pos))) { - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos; - while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { - ; - } - - NRF_UICR->REGOUT0 = - (NRF_UICR->REGOUT0 & ~((uint32_t) UICR_REGOUT0_VOUT_Msk)) | (UICR_REGOUT0_VOUT_3V0 << UICR_REGOUT0_VOUT_Pos); - - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos; - while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { - ; - } - /* a reset is required for changes to take effect */ - NVIC_SystemReset(); - } - - return 0; -} -} // namespace nrf52 -} // namespace esphome - -static int board_esphome_init(void) { return esphome::nrf52::board_esphome_init(); } - -SYS_INIT(board_esphome_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); - -#endif diff --git a/esphome/components/zephyr/__init__.py b/esphome/components/zephyr/__init__.py index 076bed0fd1..dc56ed61af 100644 --- a/esphome/components/zephyr/__init__.py +++ b/esphome/components/zephyr/__init__.py @@ -25,15 +25,12 @@ from .const import ( AUTO_LOAD = ["preferences"] KEY_BOARD = "board" -KEY_USER = "user" - def zephyr_set_core_data(config): CORE.data[KEY_ZEPHYR] = {} CORE.data[KEY_ZEPHYR][KEY_BOARD] = config[CONF_BOARD] CORE.data[KEY_ZEPHYR][KEY_PRJ_CONF] = {} CORE.data[KEY_ZEPHYR][KEY_OVERLAY] = "" - CORE.data[KEY_ZEPHYR][KEY_USER] = {} CORE.data[KEY_ZEPHYR][KEY_BOOTLOADER] = config[KEY_BOOTLOADER] CORE.data[KEY_ZEPHYR][KEY_EXTRA_BUILD_FILES] = {} return config @@ -58,12 +55,6 @@ def zephyr_add_prj_conf(name: str, value: PrjConfValueType, required: bool = Tru CORE.data[KEY_ZEPHYR][KEY_PRJ_CONF][name] = (value, required) -def zephyr_add_user(key, value): - if key not in CORE.data[KEY_ZEPHYR][KEY_USER]: - CORE.data[KEY_ZEPHYR][KEY_USER][key] = [] - CORE.data[KEY_ZEPHYR][KEY_USER][key] += [value] - - def zephyr_add_overlay(content): CORE.data[KEY_ZEPHYR][KEY_OVERLAY] += content @@ -109,7 +100,6 @@ def zephyr_to_code(conf): # disable console zephyr_add_prj_conf("UART_CONSOLE", False) zephyr_add_prj_conf("CONSOLE", False, False) - # TODO move to nrf52 # use NFC pins as GPIO zephyr_add_prj_conf("NFCT_PINS_AS_GPIOS", True) @@ -163,38 +153,11 @@ def copy_files(): write_file_if_changed(CORE.relative_build_path("zephyr/prj.conf"), prj_conf) - if CORE.data[KEY_ZEPHYR][KEY_USER]: - zephyr_add_overlay( - f""" -/ {{ - zephyr,user {{ - {[f"{key} = {', '.join(value)};" for key, value in CORE.data[KEY_ZEPHYR][KEY_USER].items()][0]} -}}; -}};""" - ) - write_file_if_changed( CORE.relative_build_path("zephyr/app.overlay"), CORE.data[KEY_ZEPHYR][KEY_OVERLAY], ) - # write_file_if_changed( - # CORE.relative_build_path("zephyr/child_image/mcuboot.conf"), - # """ - # CONFIG_MCUBOOT_SERIAL=y - # CONFIG_BOOT_SERIAL_PIN_RESET=y - # CONFIG_UART_CONSOLE=n - # CONFIG_BOOT_SERIAL_ENTRANCE_GPIO=n - # CONFIG_BOOT_SERIAL_CDC_ACM=y - # CONFIG_UART_NRFX=n - # CONFIG_LOG=n - # CONFIG_ASSERT_VERBOSE=n - # CONFIG_BOOT_BANNER=n - # CONFIG_PRINTK=n - # CONFIG_CBPRINTF_LIBC_SUBSTS=n - # """, - # ) - if CORE.data[KEY_ZEPHYR][KEY_BOOTLOADER] == BOOTLOADER_MCUBOOT: fake_board_manifest = """ {