1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-11 21:30:29 +01:00

make cdc not sleep

This commit is contained in:
Tomasz Duda 2024-02-15 21:35:56 +01:00
parent fe48c0e97d
commit ea937d14b2
20 changed files with 167 additions and 61 deletions

View File

@ -161,6 +161,8 @@ def run_miniterm(config, port):
ser = serial.Serial()
ser.baudrate = baud_rate
ser.port = port
# prevent usb port from sleep
ser.timeout = 1
# We can't set to False by default since it leads to toggling and hence
# ESP32 resets on some platforms.
@ -175,6 +177,8 @@ def run_miniterm(config, port):
while True:
try:
raw = ser.readline()
if len(raw) == 0:
continue
except serial.SerialException:
_LOGGER.error("Serial port closed!")
return 0

View File

@ -6,7 +6,7 @@ from esphome.const import (
)
from esphome.core import CORE
from esphome.components.nrf52.zephyr import zephyr_add_prj_conf
from esphome.components.zephyr import zephyr_add_prj_conf
dfu_ns = cg.esphome_ns.namespace("dfu")
DeviceFirmwareUpdate = dfu_ns.class_("DeviceFirmwareUpdate", cg.Component)

View File

@ -15,11 +15,11 @@ class Rect {
int16_t h; ///< Height of region
Rect() : x(VALUE_NO_SET), y(VALUE_NO_SET), w(VALUE_NO_SET), h(VALUE_NO_SET) {} // NOLINT
inline Rect(int16_t x, int16_t y, int16_t w, int16_t h) ALWAYS_INLINE : x(x), y(y), w(w), h(h) {}
inline Rect(int16_t x, int16_t y, int16_t w, int16_t h) ESPHOME_ALWAYS_INLINE : x(x), y(y), w(w), h(h) {}
inline int16_t x2() const { return this->x + this->w; }; ///< X coordinate of corner
inline int16_t y2() const { return this->y + this->h; }; ///< Y coordinate of corner
inline bool is_set() const ALWAYS_INLINE { return (this->h != VALUE_NO_SET) && (this->w != VALUE_NO_SET); }
inline bool is_set() const ESPHOME_ALWAYS_INLINE { return (this->h != VALUE_NO_SET) && (this->w != VALUE_NO_SET); }
void expand(int16_t horizontal, int16_t vertical);

View File

@ -4,7 +4,7 @@ from esphome.const import (
CONF_ID,
)
from esphome.components.nrf52.zephyr import zephyr_add_prj_conf, zephyr_add_overlay
from esphome.components.zephyr import zephyr_add_prj_conf, zephyr_add_overlay
fota_ns = cg.esphome_ns.namespace("fota")
FOTAComponent = fota_ns.class_("FOTAComponent", cg.Component)

View File

@ -11,54 +11,54 @@ class ESPColorCorrection {
void set_max_brightness(const Color &max_brightness) { this->max_brightness_ = max_brightness; }
void set_local_brightness(uint8_t local_brightness) { this->local_brightness_ = local_brightness; }
void calculate_gamma_table(float gamma);
inline Color color_correct(Color color) const ALWAYS_INLINE {
inline Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE {
// corrected = (uncorrected * max_brightness * local_brightness) ^ gamma
return Color(this->color_correct_red(color.red), this->color_correct_green(color.green),
this->color_correct_blue(color.blue), this->color_correct_white(color.white));
}
inline uint8_t color_correct_red(uint8_t red) const ALWAYS_INLINE {
inline uint8_t color_correct_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
uint8_t res = esp_scale8(esp_scale8(red, this->max_brightness_.red), this->local_brightness_);
return this->gamma_table_[res];
}
inline uint8_t color_correct_green(uint8_t green) const ALWAYS_INLINE {
inline uint8_t color_correct_green(uint8_t green) const ESPHOME_ALWAYS_INLINE {
uint8_t res = esp_scale8(esp_scale8(green, this->max_brightness_.green), this->local_brightness_);
return this->gamma_table_[res];
}
inline uint8_t color_correct_blue(uint8_t blue) const ALWAYS_INLINE {
inline uint8_t color_correct_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE {
uint8_t res = esp_scale8(esp_scale8(blue, this->max_brightness_.blue), this->local_brightness_);
return this->gamma_table_[res];
}
inline uint8_t color_correct_white(uint8_t white) const ALWAYS_INLINE {
inline uint8_t color_correct_white(uint8_t white) const ESPHOME_ALWAYS_INLINE {
uint8_t res = esp_scale8(esp_scale8(white, this->max_brightness_.white), this->local_brightness_);
return this->gamma_table_[res];
}
inline Color color_uncorrect(Color color) const ALWAYS_INLINE {
inline Color color_uncorrect(Color color) const ESPHOME_ALWAYS_INLINE {
// uncorrected = corrected^(1/gamma) / (max_brightness * local_brightness)
return Color(this->color_uncorrect_red(color.red), this->color_uncorrect_green(color.green),
this->color_uncorrect_blue(color.blue), this->color_uncorrect_white(color.white));
}
inline uint8_t color_uncorrect_red(uint8_t red) const ALWAYS_INLINE {
inline uint8_t color_uncorrect_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
if (this->max_brightness_.red == 0 || this->local_brightness_ == 0)
return 0;
uint16_t uncorrected = this->gamma_reverse_table_[red] * 255UL;
uint8_t res = ((uncorrected / this->max_brightness_.red) * 255UL) / this->local_brightness_;
return res;
}
inline uint8_t color_uncorrect_green(uint8_t green) const ALWAYS_INLINE {
inline uint8_t color_uncorrect_green(uint8_t green) const ESPHOME_ALWAYS_INLINE {
if (this->max_brightness_.green == 0 || this->local_brightness_ == 0)
return 0;
uint16_t uncorrected = this->gamma_reverse_table_[green] * 255UL;
uint8_t res = ((uncorrected / this->max_brightness_.green) * 255UL) / this->local_brightness_;
return res;
}
inline uint8_t color_uncorrect_blue(uint8_t blue) const ALWAYS_INLINE {
inline uint8_t color_uncorrect_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE {
if (this->max_brightness_.blue == 0 || this->local_brightness_ == 0)
return 0;
uint16_t uncorrected = this->gamma_reverse_table_[blue] * 255UL;
uint8_t res = ((uncorrected / this->max_brightness_.blue) * 255UL) / this->local_brightness_;
return res;
}
inline uint8_t color_uncorrect_white(uint8_t white) const ALWAYS_INLINE {
inline uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_ALWAYS_INLINE {
if (this->max_brightness_.white == 0 || this->local_brightness_ == 0)
return 0;
uint16_t uncorrected = this->gamma_reverse_table_[white] * 255UL;

View File

@ -24,9 +24,9 @@ struct ESPHSVColor {
};
uint8_t raw[3];
};
inline ESPHSVColor() ALWAYS_INLINE : h(0), s(0), v(0) { // NOLINT
inline ESPHSVColor() ESPHOME_ALWAYS_INLINE : h(0), s(0), v(0) { // NOLINT
}
inline ESPHSVColor(uint8_t hue, uint8_t saturation, uint8_t value) ALWAYS_INLINE : hue(hue),
inline ESPHSVColor(uint8_t hue, uint8_t saturation, uint8_t value) ESPHOME_ALWAYS_INLINE : hue(hue),
saturation(saturation),
value(value) {}
Color to_rgb() const;

View File

@ -40,7 +40,15 @@ from esphome.components.libretiny.const import (
COMPONENT_BK72XX,
COMPONENT_RTL87XX,
)
from esphome.components.nrf52.zephyr import zephyr_add_overlay, zephyr_add_prj_conf
from esphome.components.zephyr import zephyr_add_overlay, zephyr_add_prj_conf
from esphome.components.zephyr_uart import zephyr_add_cdc_acm
def AUTO_LOAD():
if CORE.using_zephyr:
return ["zephyr_uart"]
return []
CODEOWNERS = ["@esphome/core"]
logger_ns = cg.esphome_ns.namespace("logger")
@ -301,17 +309,7 @@ async def to_code(config):
zephyr_add_overlay("""&uart0 { status = "okay";};""")
if config[CONF_HARDWARE_UART] == USB_CDC:
zephyr_add_prj_conf("UART_LINE_CTRL", True)
zephyr_add_prj_conf("USB_DEVICE_STACK", True)
zephyr_add_prj_conf("USB_CDC_ACM", True)
zephyr_add_overlay(
"""
&zephyr_udc0 {
cdc_acm_uart0: cdc_acm_uart0 {
compatible = "zephyr,cdc-acm-uart";
};
};
"""
)
zephyr_add_cdc_acm(config)
# Register at end for safe mode
await cg.register_component(log, config)

View File

@ -16,7 +16,7 @@ from esphome.helpers import (
copy_file_if_changed,
)
from .zephyr import (
from esphome.components.zephyr import (
zephyr_copy_files,
zephyr_set_core_data,
zephyr_to_code,

View File

@ -6,10 +6,10 @@ from esphome.helpers import (
)
from .const import (
ZEPHYR_VARIANT_GENERIC,
ZEPHYR_VARIANT_NRF_SDK,
KEY_ZEPHYR,
KEY_PRJ_CONF,
KEY_OVERLAY,
ZEPHYR_VARIANT_NRF_SDK,
)
from esphome.const import (
CONF_VARIANT,
@ -75,16 +75,19 @@ def zephyr_to_code(conf):
# watchdog
zephyr_add_prj_conf("WATCHDOG", True)
zephyr_add_prj_conf("WDT_DISABLE_AT_BOOT", False)
# disable console
zephyr_add_prj_conf("UART_CONSOLE", False)
zephyr_add_prj_conf("CONSOLE", False)
# TODO debug only
# zephyr_add_prj_conf("DEBUG_THREAD_INFO", True)
zephyr_add_prj_conf("DEBUG_THREAD_INFO", True)
# zephyr_add_prj_conf("DEBUG", True)
###
# zephyr_add_prj_conf("USE_SEGGER_RTT", True)
# zephyr_add_prj_conf("RTT_CONSOLE", True)
# zephyr_add_prj_conf("UART_CONSOLE", False)
# zephyr_add_prj_conf("LOG", True)
# zephyr_add_prj_conf("MCUBOOT_UTIL_LOG_LEVEL_WRN", True)
# zephyr_add_prj_conf("BOOTLOADER_MCUBOOT", True)
# zephyr_add_prj_conf("USB_DEVICE_LOG_LEVEL_ERR", True)
# zephyr_add_prj_conf("USB_CDC_ACM_LOG_LEVEL_DBG", True)
def _format_prj_conf_val(value: PrjConfValueType) -> str:

View File

@ -0,0 +1,5 @@
ZEPHYR_VARIANT_GENERIC = "generic"
ZEPHYR_VARIANT_NRF_SDK = "nrf-sdk"
KEY_ZEPHYR = "zephyr"
KEY_PRJ_CONF = "prj_conf"
KEY_OVERLAY = "overlay"

View File

@ -3,7 +3,7 @@ import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
)
from esphome.components.nrf52.zephyr import zephyr_add_prj_conf
from esphome.components.zephyr import zephyr_add_prj_conf
zephyr_ble_server_ns = cg.esphome_ns.namespace("zephyr_ble_server")
BLEServer = zephyr_ble_server_ns.class_("BLEServer", cg.Component)
@ -22,4 +22,5 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
zephyr_add_prj_conf("BT", True)
zephyr_add_prj_conf("BT_PERIPHERAL", True)
# zephyr_add_prj_conf("BT_LL_SW_SPLIT", True)
await cg.register_component(var, config)

View File

@ -1,4 +1,4 @@
from esphome.components.nrf52.zephyr import zephyr_add_prj_conf
from esphome.components.zephyr import zephyr_add_prj_conf
async def to_code(config):

View File

@ -1,4 +1,4 @@
from esphome.components.nrf52.zephyr import zephyr_add_prj_conf
from esphome.components.zephyr import zephyr_add_prj_conf
DEPENDENCIES = ["zephyr_ble_server"]
AUTO_LOAD = ["zephyr_mcumgr"]

View File

@ -0,0 +1,17 @@
from esphome.components.zephyr import zephyr_add_prj_conf, zephyr_add_overlay
def zephyr_add_cdc_acm(config):
zephyr_add_prj_conf("USB_DEVICE_STACK", True)
zephyr_add_prj_conf("USB_CDC_ACM", True)
# prevent device to go to susspend
zephyr_add_prj_conf("USB_DEVICE_REMOTE_WAKEUP", False)
zephyr_add_overlay(
"""
&zephyr_udc0 {
cdc_acm_uart0: cdc_acm_uart0 {
compatible = "zephyr,cdc-acm-uart";
};
};
"""
)

View File

@ -0,0 +1,58 @@
#include "ring_buffer.h"
#include <zephyr/drivers/uart.h>
#ifndef CONFIG_UART_INTERRUPT_DRIVEN
#error "CONFIG_UART_INTERRUPT_DRIVEN is required"
#endif
// while (true) {
// uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
// if (dtr) {
// break;
// } else {
// /* Give CPU resources to low priority threads. */
// k_sleep(K_MSEC(100));
// }
// }
// }
namespace esphome {
namespace zephyr_uart {
void RingBuffer::setup(const device *dev, uint8_t *ring_buffer, uint32_t size) {
dev_ = dev;
ring_buf_init(&ringbuf_, size, ring_buffer);
uart_irq_callback_user_data_set(dev, interrupt_handler_, this);
}
uint32_t RingBuffer::fill(const char *data, uint32_t size) {
// TODO block if no space
uint32_t rb_len = ring_buf_put(&ringbuf_, reinterpret_cast<const uint8_t*>(data), size);
if (rb_len) {
uart_irq_tx_enable(dev_);
}
return rb_len;
}
void RingBuffer::interrupt_handler_(const device *dev, void *user_data) {
auto thiz = static_cast<RingBuffer *>(user_data);
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
if (uart_irq_tx_ready(dev)) {
uint8_t buffer[64];
int rb_len, send_len;
rb_len = ring_buf_get(&thiz->ringbuf_, buffer, sizeof(buffer));
if (!rb_len) {
uart_irq_tx_disable(dev);
continue;
}
uart_fifo_fill(dev, buffer, rb_len);
// TODO add blocking
}
}
}
} // namespace zephyr_uart
} // namespace esphome

View File

@ -0,0 +1,20 @@
#pragma once
#include <zephyr/sys/ring_buffer.h>
class device;
namespace esphome {
namespace zephyr_uart {
class RingBuffer {
public:
void setup(const device *dev, uint8_t *ring_buffer, uint32_t size);
uint32_t fill(const char* data, uint32_t size);
protected:
static void interrupt_handler_(const device *dev, void *user_data);
ring_buf ringbuf_;
const device *dev_ = {nullptr};
};
} // namespace zephyr_uart
} // namespace esphome

View File

@ -31,19 +31,19 @@ struct Color {
uint32_t raw_32;
};
inline Color() ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT
inline Color(uint8_t red, uint8_t green, uint8_t blue) ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {}
inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT
inline Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {}
inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ALWAYS_INLINE : r(red),
inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
g(green),
b(blue),
w(white) {}
inline explicit Color(uint32_t colorcode) ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
g((colorcode >> 8) & 0xFF),
b((colorcode >> 0) & 0xFF),
w((colorcode >> 24) & 0xFF) {}
inline bool is_on() ALWAYS_INLINE { return this->raw_32 != 0; }
inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; }
inline bool operator==(const Color &rhs) { // NOLINT
return this->raw_32 == rhs.raw_32;
@ -57,30 +57,30 @@ struct Color {
inline bool operator!=(uint32_t colorcode) { // NOLINT
return this->raw_32 != colorcode;
}
inline uint8_t &operator[](uint8_t x) ALWAYS_INLINE { return this->raw[x]; }
inline Color operator*(uint8_t scale) const ALWAYS_INLINE {
inline uint8_t &operator[](uint8_t x) ESPHOME_ALWAYS_INLINE { return this->raw[x]; }
inline Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE {
return Color(esp_scale8(this->red, scale), esp_scale8(this->green, scale), esp_scale8(this->blue, scale),
esp_scale8(this->white, scale));
}
inline Color &operator*=(uint8_t scale) ALWAYS_INLINE {
inline Color &operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE {
this->red = esp_scale8(this->red, scale);
this->green = esp_scale8(this->green, scale);
this->blue = esp_scale8(this->blue, scale);
this->white = esp_scale8(this->white, scale);
return *this;
}
inline Color operator*(const Color &scale) const ALWAYS_INLINE {
inline Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE {
return Color(esp_scale8(this->red, scale.red), esp_scale8(this->green, scale.green),
esp_scale8(this->blue, scale.blue), esp_scale8(this->white, scale.white));
}
inline Color &operator*=(const Color &scale) ALWAYS_INLINE {
inline Color &operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE {
this->red = esp_scale8(this->red, scale.red);
this->green = esp_scale8(this->green, scale.green);
this->blue = esp_scale8(this->blue, scale.blue);
this->white = esp_scale8(this->white, scale.white);
return *this;
}
inline Color operator+(const Color &add) const ALWAYS_INLINE {
inline Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE {
Color ret;
if (uint8_t(add.r + this->r) < this->r)
ret.r = 255;
@ -100,10 +100,10 @@ struct Color {
ret.w = this->w + add.w;
return ret;
}
inline Color &operator+=(const Color &add) ALWAYS_INLINE { return *this = (*this) + add; }
inline Color operator+(uint8_t add) const ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
inline Color &operator+=(uint8_t add) ALWAYS_INLINE { return *this = (*this) + add; }
inline Color operator-(const Color &subtract) const ALWAYS_INLINE {
inline Color &operator+=(const Color &add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
inline Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
inline Color &operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
inline Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE {
Color ret;
if (subtract.r > this->r)
ret.r = 0;
@ -123,11 +123,11 @@ struct Color {
ret.w = this->w - subtract.w;
return ret;
}
inline Color &operator-=(const Color &subtract) ALWAYS_INLINE { return *this = (*this) - subtract; }
inline Color operator-(uint8_t subtract) const ALWAYS_INLINE {
inline Color &operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
inline Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE {
return (*this) - Color(subtract, subtract, subtract, subtract);
}
inline Color &operator-=(uint8_t subtract) ALWAYS_INLINE { return *this = (*this) - subtract; }
inline Color &operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
static Color random_color() {
uint32_t rand = random_uint32();
uint8_t w = rand >> 24;

View File

@ -39,7 +39,7 @@ from esphome.const import (
)
from esphome.core import CORE, coroutine_with_priority
from esphome.helpers import copy_file_if_changed, walk_files
from esphome.components.nrf52.zephyr import zephyr_add_prj_conf
from esphome.components.zephyr import zephyr_add_prj_conf
_LOGGER = logging.getLogger(__name__)

View File

@ -29,7 +29,7 @@
#define HOT __attribute__((hot))
#define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg)))
#define ALWAYS_INLINE __attribute__((always_inline))
#define ESPHOME_ALWAYS_INLINE __attribute__((always_inline))
#define PACKED __attribute__((packed))
// Various functions can be constexpr in C++14, but not in C++11 (because their body isn't just a return statement).

View File

@ -12,9 +12,9 @@ esphome:
name: nrf52-test-nrf
logger:
# level: DEBUG
# logs:
# switch: NONE
level: DEBUG
logs:
switch: NONE
switch:
- platform: gpio