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