3#if defined(USE_ESP32) && SOC_I2S_SUPPORTS_TDM
10#include <driver/i2s_tdm.h>
49 : internal_accessor_{nullptr, nullptr, channel_colors, &this->correction_},
50 color_data_bytes_(num_leds * channel_colors.bytes_per_led()),
56 "I2S Clockless LED Strip:\n"
59 char channel_colors[5];
61 " Channel colors: %s\n"
62 " Number of LEDs: %" PRIu16
"\n"
63 " DMA buffer: %" PRIuPTR
" samples (%" PRIu32
" us)",
76 i2s_chan_config_t chan_config = {
78 .role = I2S_ROLE_MASTER,
81 .auto_clear_after_cb =
false,
82 .auto_clear_before_cb =
false,
86 esp_err_t err = i2s_new_channel(&chan_config, &this->
tx_handle_, NULL);
88 ESP_LOGE(TAG,
"Error in i2s_new_channel: %s", esp_err_to_name(err));
90 i2s_tdm_config_t tdm_config = {
94 .clk_src = I2S_CLK_SRC_DEFAULT,
95 .mclk_multiple = I2S_MCLK_MULTIPLE_384,
99 .data_bit_width = I2S_DATA_BIT_WIDTH_24BIT,
100 .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
101 .slot_mode = I2S_SLOT_MODE_MONO,
102 .slot_mask = I2S_TDM_SLOT0,
105 .total_slot = I2S_TDM_AUTO_SLOT_NUM,
109 .mclk = I2S_GPIO_UNUSED,
110 .bclk = I2S_GPIO_UNUSED,
111 .ws = I2S_GPIO_UNUSED,
112 .dout = gpio_num_t(this->
pin_),
113 .din = I2S_GPIO_UNUSED,
122 err = i2s_channel_init_tdm_mode(this->
tx_handle_, &tdm_config);
124 ESP_LOGE(TAG,
"Error in i2s_channel_init_tdm_mode: %s", esp_err_to_name(err));
126 i2s_event_callbacks_t event_callbacks = {
129 err = i2s_channel_register_event_callback(this->
tx_handle_, &event_callbacks,
this);
131 ESP_LOGE(TAG,
"Error in i2s_channel_register_event_callback: %s", esp_err_to_name(err));
138 ESP_LOGE(TAG,
"Error in i2s_channel_enable: %s", esp_err_to_name(err));
154 memset(this->
i2s_data_, 0, i2s_data_bytes);
176 for (
size_t i = 0; i < color_data_bytes; i++) {
177 const uint8_t color_byte = *(color_data++);
178 *(i2s_data++) = 0b10010010 | ((color_byte & 0x80) >> 1) | ((color_byte & 0x40) >> 3) | ((color_byte & 0x20) >> 5);
179 *(i2s_data++) = 0b01001001 | ((color_byte & 0x10) << 1) | ((color_byte & 0x08) >> 1);
180 *(i2s_data++) = 0b00100100 | ((color_byte & 0x04) << 5) | ((color_byte & 0x02) << 3) | ((color_byte & 0x01) << 1);
192 auto *
const dma_buf =
static_cast<uint8_t *
>(
event->dma_buf);
193 const size_t dma_buf_size =
event->
size;
195 bool dma_buf_filled =
false;
196 if (self->i2s_data_ready_.load(std::memory_order_acquire)) {
198 if (self->i2s_data_sent_ < i2s_data_bytes) {
199 const size_t i2s_data_remaining = i2s_data_bytes - self->i2s_data_sent_;
200 if (i2s_data_remaining >= dma_buf_size) {
201 memcpy(dma_buf, self->i2s_data_ + self->i2s_data_sent_, dma_buf_size);
203 memcpy(dma_buf, self->i2s_data_ + self->i2s_data_sent_, i2s_data_remaining);
204 memset(dma_buf + i2s_data_remaining, 0, dma_buf_size - i2s_data_remaining);
206 dma_buf_filled =
true;
208 self->i2s_data_sent_ += dma_buf_size;
210 self->i2s_data_ready_.store(
false, std::memory_order_release);
216 if (!dma_buf_filled && dma_buf[0] != 0) {
217 memset(dma_buf, 0, dma_buf_size);
void mark_failed()
Mark this component as failed.
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
std::atomic< bool > i2s_data_ready_
light::CorrectedBufferColorAccessor internal_accessor_
int32_t size() const override
void dump_config() override
i2s_chan_handle_t tx_handle_
void write_state(light::LightState *state) override
const size_t color_data_bytes_
float get_setup_priority() const override
I2SClocklessLedStrip(uint8_t pin, uint16_t num_leds, light::ChannelColors channel_colors)
static bool i2s_on_sent_callback(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
ESPColorAccessor * accessor_
ChannelColors channel_colors
bool allocate(RAMAllocator< uint8_t > &allocator, size_t num_leds, size_t extra_bytes=0)
Allocate the color and effect data for num_leds LEDs as one zeroed block, followed by extra_bytes mor...
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
ESPHOME_LOG_TAG(TAG, "i2s_clockless_led_strip")
constexpr size_t I2S_RESET_SAMPLES
constexpr uint32_t I2S_BUFFER_DURATION_US_ACTUAL
constexpr uint32_t num_i2s_samples_to_duration_us(size_t samples)
constexpr uint32_t I2S_SAMPLE_RATE_HZ
constexpr size_t I2S_BYTES_PER_SAMPLE
constexpr uint32_t I2S_BUFFER_DURATION_US_IDEAL
constexpr size_t I2S_RESET_BYTES
constexpr const char *const ERROR_I2S
constexpr size_t num_i2s_samples_from_duration_us(uint32_t duration_us)
constexpr uint32_t I2S_RESET_DURATION_US
constexpr size_t I2S_BUFFER_SAMPLES
constexpr float IO
For components that represent GPIO pins like PCF8573.
Which byte of an addressable LED's data carries each colour.
const char * to_string(char *buf) const
Write the order back out as text, e.g.
spi_device_handle_t handle