ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
aic3204.cpp
Go to the documentation of this file.
1#include "aic3204.h"
2
5#include "esphome/core/log.h"
6
7namespace esphome::aic3204 {
8
9static const char *const TAG = "aic3204";
10
11#define ERROR_CHECK(err, msg) \
12 if (!(err)) { \
13 ESP_LOGE(TAG, msg); \
14 this->mark_failed(); \
15 return; \
16 }
17
19 // Set register page to 0
20 ERROR_CHECK(this->write_byte(AIC3204_PAGE_CTRL, 0x00), "Set page 0 failed");
21 // Initiate SW reset (PLL is powered off as part of reset)
22 ERROR_CHECK(this->write_byte(AIC3204_SW_RST, 0x01), "Software reset failed");
23 // *** Program clock settings ***
24 // Default is CODEC_CLKIN is from MCLK pin. Don't need to change this.
25 // MDAC*NDAC*FOSR*48Khz = mClk (24.576 MHz when the XMOS is expecting 48kHz audio)
26 // (See page 51 of https://www.ti.com/lit/ml/slaa557/slaa557.pdf)
27 // We do need MDAC*DOSR/32 >= the resource compute level for the processing block
28 // So here 2*128/32 = 8, which is equal to processing block 1 's resource compute
29 // See page 5 of https://www.ti.com/lit/an/slaa404c/slaa404c.pdf for the workflow
30 // for determining these settings.
31
32 // Power up NDAC and set to 2
33 ERROR_CHECK(this->write_byte(AIC3204_NDAC, 0x82), "Set NDAC failed");
34 // Power up MDAC and set to 2
35 ERROR_CHECK(this->write_byte(AIC3204_MDAC, 0x82), "Set MDAC failed");
36 // Program DOSR = 128
37 ERROR_CHECK(this->write_byte(AIC3204_DOSR, 0x80), "Set DOSR failed");
38 // Set Audio Interface Config: I2S, 32 bits, DOUT always driving
39 ERROR_CHECK(this->write_byte(AIC3204_CODEC_IF, 0x30), "Set CODEC_IF failed");
40 // For I2S Firmware only, set SCLK/MFP3 pin as Audio Data In
41 ERROR_CHECK(this->write_byte(AIC3204_SCLK_MFP3, 0x02), "Set SCLK/MFP3 failed");
42 ERROR_CHECK(this->write_byte(AIC3204_AUDIO_IF_4, 0x01), "Set AUDIO_IF_4 failed");
43 ERROR_CHECK(this->write_byte(AIC3204_AUDIO_IF_5, 0x01), "Set AUDIO_IF_5 failed");
44 // Program the DAC processing block to be used - PRB_P1
45 ERROR_CHECK(this->write_byte(AIC3204_DAC_SIG_PROC, 0x01), "Set DAC_SIG_PROC failed");
46
47 // *** Select Page 1 ***
48 ERROR_CHECK(this->write_byte(AIC3204_PAGE_CTRL, 0x01), "Set page 1 failed");
49 // Enable the internal AVDD_LDO:
50 ERROR_CHECK(this->write_byte(AIC3204_LDO_CTRL, 0x09), "Set LDO_CTRL failed");
51 // *** Program Analog Blocks ***
52 // Disable Internal Crude AVdd in presence of external AVdd supply or before powering up internal AVdd LDO
53 ERROR_CHECK(this->write_byte(AIC3204_PWR_CFG, 0x08), "Set PWR_CFG failed");
54 // Enable Master Analog Power Control
55 ERROR_CHECK(this->write_byte(AIC3204_LDO_CTRL, 0x01), "Set LDO_CTRL failed");
56 // Page 125: Common mode control register, set d6 to 1 to make the full chip common mode = 0.75 v
57 // We are using the internal AVdd regulator with a nominal output of 1.72 V (see LDO_CTRL_REGISTER on page 123)
58 // Page 86 says to only set the common mode voltage to 0.9 v if AVdd >= 1.8... but it isn't on our hardware
59 // We also adjust the HPL and HPR gains to -2dB gian later in this config flow compensate (see page 47)
60 // (All pages refer to the TLV320AIC3204 Application Reference Guide)
61 ERROR_CHECK(this->write_byte(AIC3204_CM_CTRL, 0x40), "Set CM_CTRL failed");
62 // *** Set PowerTune Modes ***
63 // Set the Left & Right DAC PowerTune mode to PTM_P3/4. Use Class-AB driver.
64 ERROR_CHECK(this->write_byte(AIC3204_PLAY_CFG1, 0x00), "Set PLAY_CFG1 failed");
65 ERROR_CHECK(this->write_byte(AIC3204_PLAY_CFG2, 0x00), "Set PLAY_CFG2 failed");
66 // Set the REF charging time to 40ms
67 ERROR_CHECK(this->write_byte(AIC3204_REF_STARTUP, 0x01), "Set REF_STARTUP failed");
68 // HP soft stepping settings for optimal pop performance at power up
69 // Rpop used is 6k with N = 6 and soft step = 20usec. This should work with 47uF coupling
70 // capacitor. Can try N=5,6 or 7 time constants as well. Trade-off delay vs “pop” sound.
71 ERROR_CHECK(this->write_byte(AIC3204_HP_START, 0x25), "Set HP_START failed");
72 // Route Left DAC to HPL
73 ERROR_CHECK(this->write_byte(AIC3204_HPL_ROUTE, 0x08), "Set HPL_ROUTE failed");
74 // Route Right DAC to HPR
75 ERROR_CHECK(this->write_byte(AIC3204_HPR_ROUTE, 0x08), "Set HPR_ROUTE failed");
76 // Route Left DAC to LOL
77 ERROR_CHECK(this->write_byte(AIC3204_LOL_ROUTE, 0x08), "Set LOL_ROUTE failed");
78 // Route Right DAC to LOR
79 ERROR_CHECK(this->write_byte(AIC3204_LOR_ROUTE, 0x08), "Set LOR_ROUTE failed");
80
81 // Unmute HPL and set gain to -2dB (see comment before configuring the AIC3204_CM_CTRL register)
82 ERROR_CHECK(this->write_byte(AIC3204_HPL_GAIN, 0x3e), "Set HPL_GAIN failed");
83 // Unmute HPR and set gain to -2dB (see comment before configuring the AIC3204_CM_CTRL register)
84 ERROR_CHECK(this->write_byte(AIC3204_HPR_GAIN, 0x3e), "Set HPR_GAIN failed");
85 // Unmute LOL and set gain to 0dB
86 ERROR_CHECK(this->write_byte(AIC3204_LOL_DRV_GAIN, 0x00), "Set LOL_DRV_GAIN failed");
87 // Unmute LOR and set gain to 0dB
88 ERROR_CHECK(this->write_byte(AIC3204_LOR_DRV_GAIN, 0x00), "Set LOR_DRV_GAIN failed");
89
90 // Power up HPL and HPR, LOL and LOR drivers
91 ERROR_CHECK(this->write_byte(AIC3204_OP_PWR_CTRL, 0x3C), "Set OP_PWR_CTRL failed");
92
93 // Wait for 2.5 sec for soft stepping to take effect before attempting power-up
94 this->set_timeout(2500, [this]() {
95 // *** Power Up DAC ***
96 // Select Page 0
97 ERROR_CHECK(this->write_byte(AIC3204_PAGE_CTRL, 0x00), "Set PAGE_CTRL failed");
98 // Power up the Left and Right DAC Channels. Route Left data to Left DAC and Right data to Right DAC.
99 // DAC Vol control soft step 1 step per DAC word clock.
100 ERROR_CHECK(this->write_byte(AIC3204_DAC_CH_SET1, 0xd4), "Set DAC_CH_SET1 failed");
101 // Set left and right DAC digital volume control
102 ERROR_CHECK(this->write_volume_(), "Set volume failed");
103 // Unmute left and right channels
104 ERROR_CHECK(this->write_mute_(), "Set mute failed");
105 });
106}
107
109 ESP_LOGCONFIG(TAG, "AIC3204:");
110 LOG_I2C_DEVICE(this);
111
112 if (this->is_failed()) {
113 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
114 }
115}
116
118 this->is_muted_ = false;
119 return this->write_mute_();
120}
121
123 this->is_muted_ = true;
124 return this->write_mute_();
125}
126
127bool AIC3204::set_auto_mute_mode(uint8_t auto_mute_mode) {
128 this->auto_mute_mode_ = auto_mute_mode & 0x07;
129 ESP_LOGVV(TAG, "Setting auto_mute_mode to 0x%.2x", this->auto_mute_mode_);
130 return this->write_mute_();
131}
132
133bool AIC3204::set_volume(float volume) {
134 this->volume_ = clamp<float>(volume, 0.0, 1.0);
135 return this->write_volume_();
136}
137
138bool AIC3204::is_muted() { return this->is_muted_; }
139
140float AIC3204::volume() { return this->volume_; }
141
143 uint8_t mute_mode_byte = this->auto_mute_mode_ << 4; // auto-mute control is bits 4-6
144 mute_mode_byte |= this->is_muted_ ? 0x0c : 0x00; // mute bits are 2-3
145 if (!this->write_byte(AIC3204_PAGE_CTRL, 0x00) || !this->write_byte(AIC3204_DAC_CH_SET2, mute_mode_byte)) {
146 ESP_LOGE(TAG, "Writing mute modes failed");
147 return false;
148 }
149 return true;
150}
151
153 const int8_t dvc_min_byte = -127;
154 const int8_t dvc_max_byte = 48;
155
156 int8_t volume_byte = dvc_min_byte + (this->volume_ * (dvc_max_byte - dvc_min_byte));
157 volume_byte = clamp<int8_t>(volume_byte, dvc_min_byte, dvc_max_byte);
158
159 ESP_LOGVV(TAG, "Setting volume to 0x%.2x", volume_byte & 0xFF);
160
161 if ((!this->write_byte(AIC3204_PAGE_CTRL, 0x00)) || (!this->write_byte(AIC3204_DACL_VOL_D, volume_byte)) ||
162 (!this->write_byte(AIC3204_DACR_VOL_D, volume_byte))) {
163 ESP_LOGE(TAG, "Writing volume failed");
164 return false;
165 }
166 return true;
167}
168
169} // namespace esphome::aic3204
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
bool set_volume(float volume) override
Definition aic3204.cpp:133
bool is_muted() override
Definition aic3204.cpp:138
void dump_config() override
Definition aic3204.cpp:108
bool set_auto_mute_mode(uint8_t auto_mute_mode)
Definition aic3204.cpp:127
bool set_mute_off() override
Definition aic3204.cpp:117
bool set_mute_on() override
Definition aic3204.cpp:122
float volume() override
Definition aic3204.cpp:140
void setup() override
Definition aic3204.cpp:18
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265