ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pcm5122.cpp
Go to the documentation of this file.
1#include "pcm5122.h"
2
4#include "esphome/core/log.h"
5
6#include <cmath>
7
8namespace esphome::pcm5122 {
9
10ESPHOME_LOG_TAG(TAG, "pcm5122");
11
13 // Hold XSMT low (soft mute asserted) until init completes
14 setup_output_pin(this->enable_pin_, false);
15
16 // Select page 0 and verify chip presence via I2C ACK
17 if (!this->select_page_(0)) {
18 ESP_LOGE(TAG, "Write failed");
19 this->status_set_error(LOG_STR("Write failed"));
20 this->mark_failed();
21 return;
22 }
23
24 // Reset audio modules
25 this->reg(PCM5122_REG_RESET) = PCM5122_RESET_MODULES;
26 delay(20);
27 this->reg(PCM5122_REG_RESET) = 0x00;
28
29 // Ignore clock halt detection; enable clock divider autoset
30 optional<uint8_t> err_detect = this->read_byte(PCM5122_REG_ERROR_DETECT);
31 // Hand-written so clang-tidy sees the has_value() check before value()
32 if (!err_detect.has_value()) {
33 this->mark_failed(LOG_STR("Failed to read ERROR_DETECT"));
34 return;
35 }
36 uint8_t err_detect_val = err_detect.value();
37 err_detect_val |= PCM5122_ERROR_DETECT_IGNORE_CLKHALT;
38 err_detect_val &= ~PCM5122_ERROR_DETECT_DISABLE_DIV_AUTOSET;
39 this->reg(PCM5122_REG_ERROR_DETECT) = err_detect_val;
40
41 // I2S format with the configured word length
42 uint8_t alen;
43 switch (this->bits_per_sample_) {
45 alen = PCM5122_AUDIO_FORMAT_ALEN_16BIT;
46 break;
48 alen = PCM5122_AUDIO_FORMAT_ALEN_24BIT;
49 break;
51 default:
52 alen = PCM5122_AUDIO_FORMAT_ALEN_32BIT;
53 break;
54 }
55 this->reg(PCM5122_REG_AUDIO_FORMAT) = PCM5122_AUDIO_FORMAT_I2S | alen;
56
57 if (!this->setup_check(this->write_channel_mix_()))
58 return;
59
60 if (!this->setup_check(this->write_analog_gain_()))
61 return;
62
63 // PLL reference clock: BCK
64 if (!this->setup_check(this->select_page_(0), LOG_STR("Write failed")))
65 return;
66 optional<uint8_t> pll_ref = this->read_byte(PCM5122_REG_PLL_REF);
67 // Hand-written so clang-tidy sees the has_value() check before value()
68 if (!pll_ref.has_value()) {
69 this->mark_failed(LOG_STR("Failed to read PLL_REF"));
70 return;
71 }
72 uint8_t pll_ref_val = pll_ref.value();
73 pll_ref_val &= ~PCM5122_PLL_REF_MASK;
74 pll_ref_val |= PCM5122_PLL_REF_SOURCE_BCK;
75 this->reg(PCM5122_REG_PLL_REF) = pll_ref_val;
76
77 if (!this->set_mute_on() || !this->set_volume(this->volume_)) {
78 this->mark_failed();
79 return;
80 }
81
82 // Release XSMT (soft un-mute) now that init has completed
83 if (this->enable_pin_ != nullptr) {
84 this->enable_pin_->digital_write(true);
85 }
86}
87
89 const char *channel_mix_str;
90 switch (this->channel_mix_) {
92 channel_mix_str = "left only";
93 break;
95 channel_mix_str = "right only";
96 break;
98 channel_mix_str = "swapped";
99 break;
100 default:
101 channel_mix_str = "stereo";
102 break;
103 }
104 ESP_LOGCONFIG(TAG, "Audio DAC:");
105 LOG_I2C_DEVICE(this);
106 ESP_LOGCONFIG(TAG,
107 " Bits per sample: %u\n"
108 " Analog gain: %s\n"
109 " Channel mix: %s\n"
110 " Volume range: %.1f dB to %.1f dB\n"
111 " Muted: %s",
112 this->bits_per_sample_,
113 this->analog_gain_ == PCM5122_ANALOG_GAIN_0DB ? LOG_STR_LITERAL("0 dB") : LOG_STR_LITERAL("-6 dB"),
114 channel_mix_str, this->volume_min_db_, this->volume_max_db_, YESNO(this->is_muted_));
115 LOG_PIN(" Enable Pin: ", this->enable_pin_);
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 PCM5122::set_volume(float volume) {
129 this->volume_ = clamp<float>(volume, 0.0f, 1.0f);
130 return this->write_volume_();
131}
132
133bool PCM5122::is_muted() { return this->is_muted_; }
134
135float PCM5122::volume() { return this->volume_; }
136
137bool PCM5122::select_page_(uint8_t page) {
138 if (this->current_page_ == page)
139 return true;
140 if (!this->write_byte(PCM5122_REG_PAGE_SELECT, page)) {
141 this->current_page_ = -1;
142 return false;
143 }
144 this->current_page_ = page;
145 return true;
146}
147
149 uint8_t mute_byte = this->is_muted() ? 0x11 : 0x00;
150 if (!this->select_page_(0) || !this->write_byte(PCM5122_REG_MUTE, mute_byte)) {
151 ESP_LOGE(TAG, "Writing mute failed");
152 return false;
153 }
154 return true;
155}
156
158 // DVOL register: 0x00 = +24 dB, 0x30 = 0 dB, 0xFE = -103 dB, 0xFF = mute (-0.5 dB/step).
159 // Note: volume=0.0 maps to volume_min_db_, which is not true silence unless set to -103 dB.
160 // Use set_mute_on() for silence.
161 const uint8_t dvol_max_volume = static_cast<uint8_t>(lroundf(0x30 - this->volume_max_db_ * 2.0f));
162 const uint8_t dvol_min_volume = static_cast<uint8_t>(lroundf(0x30 - this->volume_min_db_ * 2.0f));
163
164 const uint8_t volume_byte =
165 dvol_max_volume + static_cast<uint8_t>(lroundf((1.0f - this->volume_) * (dvol_min_volume - dvol_max_volume)));
166
167 ESP_LOGV(TAG, "Setting volume to 0x%.2x", volume_byte);
168
169 if (!this->select_page_(0) || !this->write_byte(PCM5122_REG_DVOL_LEFT, volume_byte) ||
170 !this->write_byte(PCM5122_REG_DVOL_RIGHT, volume_byte)) {
171 ESP_LOGE(TAG, "Writing volume failed");
172 return false;
173 }
174 return true;
175}
176
178 uint8_t gain_byte = this->analog_gain_;
179 if (!this->select_page_(1) || !this->write_byte(PCM5122_REG_ANALOG_GAIN, gain_byte)) {
180 ESP_LOGE(TAG, "Writing analog gain failed");
181 return false;
182 }
183 return true;
184}
185
187 uint8_t channel_mix_byte = this->channel_mix_;
188 if (!this->select_page_(0) || !this->write_byte(PCM5122_REG_DAC_DATA_PATH, channel_mix_byte)) {
189 ESP_LOGE(TAG, "Writing channel mix failed");
190 return false;
191 }
192 return true;
193}
194
195bool PCM5122::set_standby(bool enable) {
196 bool prev_standby = this->standby_;
197 this->standby_ = enable;
198 if (!this->write_power_control_()) {
199 this->standby_ = prev_standby;
200 return false;
201 }
202 return true;
203}
204
205bool PCM5122::set_powerdown(bool enable) {
206 bool prev_powerdown = this->powerdown_;
207 this->powerdown_ = enable;
208 if (!this->write_power_control_()) {
209 this->powerdown_ = prev_powerdown;
210 return false;
211 }
212 return true;
213}
214
216 uint8_t power_byte =
217 (this->standby_ ? PCM5122_POWER_CONTROL_RQST : 0) | (this->powerdown_ ? PCM5122_POWER_CONTROL_RQPD : 0);
218 if (!this->select_page_(0) || !this->write_byte(PCM5122_REG_POWER_CONTROL, power_byte)) {
219 ESP_LOGE(TAG, "Writing power control failed");
220 return false;
221 }
222 return true;
223}
224
225} // namespace esphome::pcm5122
void mark_failed()
Mark this component as failed.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
virtual void digital_write(bool value)=0
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
bool set_standby(bool enable)
Definition pcm5122.cpp:195
void setup() override
Definition pcm5122.cpp:12
bool set_volume(float volume) override
Definition pcm5122.cpp:128
bool is_muted() override
Definition pcm5122.cpp:133
bool set_mute_on() override
Definition pcm5122.cpp:123
float volume() override
Definition pcm5122.cpp:135
bool set_mute_off() override
Definition pcm5122.cpp:118
void dump_config() override
Definition pcm5122.cpp:88
bool select_page_(uint8_t page)
Definition pcm5122.cpp:137
bool set_powerdown(bool enable)
Definition pcm5122.cpp:205
PCM5122AnalogGain analog_gain_
Definition pcm5122.h:113
PCM5122BitsPerSample bits_per_sample_
Definition pcm5122.h:112
PCM5122ChannelMix channel_mix_
Definition pcm5122.h:114
ESPHOME_LOG_TAG(TAG, "pcm5122")
@ PCM5122_ANALOG_GAIN_0DB
Definition pcm5122.h:59
@ PCM5122_CHANNEL_MIX_SWAPPED
Definition pcm5122.h:68
@ PCM5122_CHANNEL_MIX_LEFT_ONLY
Definition pcm5122.h:66
@ PCM5122_CHANNEL_MIX_RIGHT_ONLY
Definition pcm5122.h:67
@ PCM5122_BITS_PER_SAMPLE_16
Definition pcm5122.h:53
@ PCM5122_BITS_PER_SAMPLE_32
Definition pcm5122.h:55
@ PCM5122_BITS_PER_SAMPLE_24
Definition pcm5122.h:54
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
Definition gpio.cpp:44
void HOT delay(uint32_t ms)
Definition hal.cpp:78