ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
i2s_audio.cpp
Go to the documentation of this file.
1#include "i2s_audio.h"
2
3#if defined(USE_ESP32) && defined(USE_I2S_AUDIO_FULL_DUPLEX)
4
5#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "i2s_audio");
10
12 if (!this->is_full_duplex())
13 return;
14
15 // ESP-IDF only shares the bit and word clocks between channels allocated in the same call, and deleting either
16 // channel ends the pairing. Allocate both once here and keep them for the lifetime of the device.
17 i2s_chan_config_t chan_cfg{};
18 i2s_std_config_t tx_cfg{};
19 i2s_std_config_t rx_cfg{};
20 this->audio_out_->build_full_duplex_config(chan_cfg, tx_cfg);
22
23 this->dma_desc_num_ = chan_cfg.dma_desc_num;
24 esp_err_t err = i2s_new_channel(&chan_cfg, &this->tx_handle_, &this->rx_handle_);
25 if (err == ESP_OK) {
26 // The first initialized channel drives the shared clocks. Use RX so the microphone runs on its own.
27 err = i2s_channel_init_std_mode(this->rx_handle_, &rx_cfg);
28 }
29 if (err == ESP_OK) {
30 err = i2s_channel_init_std_mode(this->tx_handle_, &tx_cfg);
31 }
32 if (err != ESP_OK) {
33 ESP_LOGE(TAG, "Full duplex channel setup failed: %s", esp_err_to_name(err));
34 if (this->tx_handle_ != nullptr) {
35 i2s_del_channel(this->tx_handle_);
36 this->tx_handle_ = nullptr;
37 }
38 if (this->rx_handle_ != nullptr) {
39 i2s_del_channel(this->rx_handle_);
40 this->rx_handle_ = nullptr;
41 }
42 this->mark_failed();
43 }
44}
45
46i2s_chan_handle_t I2SAudioComponent::acquire_(bool &in_use, i2s_chan_handle_t handle) {
47 if (handle == nullptr)
48 return nullptr;
49 in_use = true;
50 if (!this->update_rx_channel_()) {
51 in_use = false;
52 return nullptr;
53 }
54 return handle;
55}
56
57i2s_chan_handle_t I2SAudioComponent::acquire_rx_channel() { return this->acquire_(this->rx_in_use_, this->rx_handle_); }
58
60 this->rx_in_use_ = false;
61 this->update_rx_channel_();
62}
63
65 // Speakers sharing the bus take turns; a second one must not get the channel while the first is playing
66 if (this->tx_in_use_)
67 return nullptr;
68 return this->acquire_(this->tx_in_use_, this->tx_handle_);
69}
70
72 // The speaker task may have left the channel disabled already, so an invalid state error is expected here
73 i2s_channel_disable(this->tx_handle_);
74 this->tx_in_use_ = false;
75 this->update_rx_channel_();
76}
77
79 const bool needed = this->rx_in_use_ || this->tx_in_use_;
80 if (needed == this->rx_enabled_)
81 return true;
82
83 if (needed) {
85 }
86 esp_err_t err = needed ? i2s_channel_enable(this->rx_handle_) : i2s_channel_disable(this->rx_handle_);
87 if (err != ESP_OK) {
88 ESP_LOGE(TAG, "Failed to %s RX channel: %s", needed ? LOG_STR_LITERAL("enable") : LOG_STR_LITERAL("disable"),
89 esp_err_to_name(err));
90 return false;
91 }
92 this->rx_enabled_ = needed;
93 return true;
94}
95
96} // namespace esphome::i2s_audio
97
98#endif // USE_ESP32 && USE_I2S_AUDIO_FULL_DUPLEX
void mark_failed()
Mark this component as failed.
i2s_chan_handle_t acquire_rx_channel()
Enables the full duplex RX channel.
Definition i2s_audio.cpp:57
bool is_full_duplex() const
True when a microphone and a speaker share this bus at the same time.
Definition i2s_audio.h:75
i2s_chan_handle_t acquire_(bool &in_use, i2s_chan_handle_t handle)
Marks one side active and brings the shared clocks up; undoes the mark if that fails.
Definition i2s_audio.cpp:46
bool update_rx_channel_()
Enables the RX channel while either side is active, since it drives the shared clocks.
Definition i2s_audio.cpp:78
void release_rx_channel()
Releases the RX channel; it keeps running while the TX side needs its clocks. Main loop only.
Definition i2s_audio.cpp:59
i2s_chan_handle_t acquire_tx_channel()
Starts the shared clocks and hands over the full duplex TX channel, still disabled,...
Definition i2s_audio.cpp:64
void release_tx_channel()
Disables the TX channel and stops the shared clocks if the RX side is idle.
Definition i2s_audio.cpp:71
virtual void full_duplex_rx_enabling(i2s_chan_handle_t rx_handle, uint32_t dma_desc_num)=0
Called by the parent on the main loop just before it enables the full duplex RX channel,...
virtual void build_full_duplex_config(i2s_std_config_t &std_cfg)=0
Builds the RX configuration the parent uses to set up a full duplex channel pair.
virtual void build_full_duplex_config(i2s_chan_config_t &chan_cfg, i2s_std_config_t &std_cfg)
Builds the TX configuration the parent uses to set up a full duplex channel pair.
Definition i2s_audio.h:62
ESPHOME_LOG_TAG(TAG, "i2s_audio")
spi_device_handle_t handle