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