13static constexpr uint8_t TAS58XX_PAGE_SELECT = 0x00;
14static constexpr uint8_t TAS58XX_BOOK_SELECT = 0x7F;
15static constexpr uint8_t TAS58XX_BOOK_CONTROL = 0x00;
16static constexpr uint8_t TAS58XX_PAGE_0 = 0x00;
19static constexpr uint8_t TAS58XX_RESET_CTRL = 0x01;
20static constexpr uint8_t TAS58XX_RESET_CTRL_ALL = 0x11;
21static constexpr uint8_t TAS58XX_DEVICE_CTRL_1 = 0x02;
22static constexpr uint8_t TAS58XX_DEVICE_CTRL_1_PBTL = (1 << 2);
23static constexpr uint8_t TAS58XX_DEVICE_CTRL_1_1SPW = 0x01;
24static constexpr uint8_t TAS58XX_DEVICE_CTRL_2 = 0x03;
25static constexpr uint8_t TAS58XX_DEVICE_CTRL_2_MUTE = (1 << 3);
26static constexpr uint8_t TAS58XX_CTRL_STATE_MASK = 0x03;
27static constexpr uint8_t TAS58XX_CTRL_STATE_DEEP_SLEEP = 0x00;
28static constexpr uint8_t TAS58XX_CTRL_STATE_SLEEP = 0x01;
29static constexpr uint8_t TAS58XX_CTRL_STATE_HIZ = 0x02;
30static constexpr uint8_t TAS58XX_CTRL_STATE_PLAY = 0x03;
31static constexpr uint8_t TAS58XX_CTRL_STATE_UNKNOWN = 0xFF;
32static constexpr uint8_t TAS58XX_DIG_VOL = 0x4C;
33static constexpr uint8_t TAS58XX_DIG_VOL_0DB = 0x30;
34static constexpr uint8_t TAS58XX_DIG_VOL_MINUS_103DB = 0xFE;
35static constexpr uint8_t TAS58XX_AGAIN = 0x54;
36static constexpr uint8_t TAS58XX_AGAIN_MINUS_15_5DB = 0x1F;
37static constexpr uint8_t TAS58XX_GPIO_CTRL = 0x60;
38static constexpr uint8_t TAS58XX_GPIO0_SEL = 0x61;
39static constexpr uint8_t TAS58XX_GPIO_SEL_FAULTZ = 0x0B;
40static constexpr uint8_t TAS58XX_POWER_STATE = 0x68;
41static constexpr uint8_t TAS58XX_CHAN_FAULT = 0x70;
42static constexpr uint8_t TAS58XX_FAULT_REGISTER_COUNT = 4;
43static constexpr uint8_t TAS58XX_FAULT_CLEAR = 0x78;
44static constexpr uint8_t TAS58XX_FAULT_CLEAR_ANALOG = 0x80;
47static constexpr uint8_t TAS58XX_MIXER_COEFFICIENT_SIZE = 4;
49static constexpr uint8_t TAS58XX_MIXER_MUTE = 0x00;
50static constexpr uint8_t TAS58XX_MIXER_MINUS_6DB = 0x40;
51static constexpr uint8_t TAS58XX_MIXER_0DB = 0x80;
53static constexpr uint32_t PDN_LOW_MS = 1;
54static constexpr uint32_t PDN_TO_I2C_MS = 5;
55static constexpr uint32_t RESET_SETTLE_MS = 5;
57static const LogString *power_state_name(uint8_t
state) {
58 if (
state == TAS58XX_CTRL_STATE_DEEP_SLEEP)
59 return LOG_STR(
"Deep sleep");
60 if (
state == TAS58XX_CTRL_STATE_SLEEP)
61 return LOG_STR(
"Sleep");
62 if (
state == TAS58XX_CTRL_STATE_HIZ)
63 return LOG_STR(
"Hi-Z");
64 return LOG_STR(
"Play");
68static bool dsp_writable(uint8_t power_state) {
return power_state == TAS58XX_CTRL_STATE_PLAY; }
70static const LogString *mixer_mode_name(
MixerMode mixer_mode) {
72 return LOG_STR(
"Stereo inverse");
74 return LOG_STR(
"Mono");
76 return LOG_STR(
"Left");
78 return LOG_STR(
"Right");
79 return LOG_STR(
"Stereo");
103 return this->
write_byte(TAS58XX_PAGE_SELECT, TAS58XX_PAGE_0) && this->
write_byte(TAS58XX_BOOK_SELECT, book) &&
111 !this->
write_byte(TAS58XX_DEVICE_CTRL_2, TAS58XX_CTRL_STATE_HIZ) ||
112 !this->
write_byte(TAS58XX_RESET_CTRL, TAS58XX_RESET_CTRL_ALL) ||
113 !this->
write_byte(TAS58XX_DEVICE_CTRL_2, TAS58XX_CTRL_STATE_HIZ)) {
114 ESP_LOGE(TAG,
"I2C write failed during reset");
117 delay(RESET_SETTLE_MS);
120 ESP_LOGE(TAG,
"I2C write failed during init");
132 ESP_LOGE(TAG,
"I2C write failed during init");
138 const uint8_t again =
139 static_cast<uint8_t
>(lroundf(clamp(-this->
analog_gain_db_ * 2.0f, 0.0f,
float{TAS58XX_AGAIN_MINUS_15_5DB})));
142 if (!this->
write_byte(TAS58XX_DEVICE_CTRL_1, device_ctrl_1) || !this->
write_byte(TAS58XX_AGAIN, again) ||
144 !this->
write_byte(TAS58XX_FAULT_CLEAR, TAS58XX_FAULT_CLEAR_ANALOG)) {
145 ESP_LOGE(TAG,
"I2C write failed during init");
157 ESP_LOGD(TAG,
"[0x%02X] Activating", this->
address_);
160 ESP_LOGE(TAG,
"Failed to select the control port");
164 if (!this->
write_byte(TAS58XX_FAULT_CLEAR, TAS58XX_FAULT_CLEAR_ANALOG)) {
165 ESP_LOGW(TAG,
"Failed to clear faults");
169 if (this->
ctrl_state_ == TAS58XX_CTRL_STATE_DEEP_SLEEP &&
182 ESP_LOGD(TAG,
"[0x%02X] Deactivating", this->
address_);
184 ESP_LOGE(TAG,
"Failed to select the control port");
197 PUBLISH_STATE(this->activate_switch_, active);
202 if (!this->
write_byte(TAS58XX_DEVICE_CTRL_2,
state | (muted ? TAS58XX_DEVICE_CTRL_2_MUTE : 0))) {
203 ESP_LOGE(TAG,
"Failed to write DEVICE_CTRL_2");
212 uint8_t fault_registers[TAS58XX_FAULT_REGISTER_COUNT];
213 if (!this->
read_bytes(TAS58XX_CHAN_FAULT, fault_registers,
sizeof(fault_registers)))
216 for (uint8_t
reg = 0;
reg < TAS58XX_FAULT_REGISTER_COUNT;
reg++)
223 for (uint8_t index = 0; index < 32; index++) {
225 if (!(changed & mask))
227 const LogString *name = model.
fault_name(index);
228 if (!(active & mask)) {
229 ESP_LOGI(TAG,
"[0x%02X] %s cleared", this->
address_, LOG_STR_ARG(name));
231 ESP_LOGE(TAG,
"[0x%02X] %s", this->
address_, LOG_STR_ARG(name));
233 ESP_LOGW(TAG,
"[0x%02X] %s", this->
address_, LOG_STR_ARG(name));
237 ESP_LOGW(TAG,
"[0x%02X] Output stays off: fix the cause, then call tas58xx.activate or power cycle the amplifier",
240 this->logged_faults_ = active;
242#ifdef USE_BINARY_SENSOR
243 PUBLISH_STATE(this->have_fault_binary_sensor_, (active & model.
fault_error_mask) != 0);
255 !this->write_byte(TAS58XX_FAULT_CLEAR, TAS58XX_FAULT_CLEAR_ANALOG)) {
256 ESP_LOGW(TAG,
"Failed to clear faults");
268 if (!this->
read_byte(TAS58XX_POWER_STATE, &power_state))
270 power_state &= TAS58XX_CTRL_STATE_MASK;
277 ESP_LOGW(TAG,
"Failed to write mixer");
282 uint8_t power_state{0};
284 LOG_STR(
"Failed to read status")))
288 ESP_LOGD(TAG,
"[0x%02X] Power state: %s", this->
address_, LOG_STR_ARG(power_state_name(power_state)));
292 if (dsp_writable(power_state))
297 const LogString *fault_output;
299 fault_output = LOG_STR(
"None");
301 fault_output = LOG_STR(
"Invalid");
309 LOG_I2C_DEVICE(
this);
311 LOG_UPDATE_INTERVAL(
this);
313 " Analog Gain: %.1f dB\n"
317 " Volume Range: %.1f dB to %.1f dB\n"
323 LOG_STR_ARG(fault_output));
324#ifdef USE_BINARY_SENSOR
325 LOG_BINARY_SENSOR(
" ",
"Any Fault", this->have_fault_binary_sensor_);
327 LOG_BINARY_SENSOR(
" ",
"Fault",
sensor);
331 LOG_SWITCH(
" ",
"Activate", this->activate_switch_);
334 LOG_SELECT(
" ",
"Mixer Mode", this->mixer_mode_select_);
346 float previous = this->
volume_;
358 const uint8_t dig_vol =
static_cast<uint8_t
>(
359 lroundf(clamp(TAS58XX_DIG_VOL_0DB - volume_db * 2.0f, 0.0f,
float{TAS58XX_DIG_VOL_MINUS_103DB})));
360 ESP_LOGV(TAG,
"Setting volume to 0x%02X", dig_vol);
361 return this->
write_byte(TAS58XX_DIG_VOL, dig_vol);
366 if (this->mixer_mode_select_ !=
nullptr) {
369 this->mixer_mode_select_->publish_state(this->
mixer_mode_ - first_mode);
373void TAS58xx::apply_mixer_mode(
MixerMode mixer_mode) {
378 ESP_LOGW(TAG,
"[0x%02X] Mixer mode %s is not valid for PBTL", this->
address_,
379 LOG_STR_ARG(mixer_mode_name(mixer_mode)));
392 uint8_t left_to_left = TAS58XX_MIXER_0DB;
393 uint8_t right_to_left = TAS58XX_MIXER_MUTE;
394 uint8_t left_to_right = TAS58XX_MIXER_MUTE;
395 uint8_t right_to_right = TAS58XX_MIXER_0DB;
397 left_to_left = TAS58XX_MIXER_MUTE;
398 right_to_left = TAS58XX_MIXER_0DB;
399 left_to_right = TAS58XX_MIXER_0DB;
400 right_to_right = TAS58XX_MIXER_MUTE;
402 left_to_left = TAS58XX_MIXER_MINUS_6DB;
403 right_to_left = TAS58XX_MIXER_MINUS_6DB;
404 left_to_right = TAS58XX_MIXER_MINUS_6DB;
405 right_to_right = TAS58XX_MIXER_MINUS_6DB;
407 left_to_right = TAS58XX_MIXER_0DB;
408 right_to_right = TAS58XX_MIXER_MUTE;
410 left_to_left = TAS58XX_MIXER_MUTE;
411 right_to_left = TAS58XX_MIXER_0DB;
413 const uint8_t coefficients[4 * TAS58XX_MIXER_COEFFICIENT_SIZE] = {0, left_to_left, 0, 0, 0, right_to_left, 0, 0,
414 0, left_to_right, 0, 0, 0, right_to_right, 0, 0};
421 ESP_LOGD(TAG,
"[0x%02X] Mixer mode written: %s", this->
address_, LOG_STR_ARG(mixer_mode_name(this->
mixer_mode_)));
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
virtual void digital_write(bool value)=0
uint8_t address_
store the address of the device on the bus
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
binary_sensor::BinarySensor * sensor
void publish_active_(bool active)
Publish the activate switch state, if there is one.
bool set_mute_(bool muted)
void write_dsp_()
Write DSP settings not written since last reset; call only while DSP is writable.
std::array< binary_sensor::BinarySensor *, FAULT_SENSOR_COUNT > fault_binary_sensors_
void publish_mixer_mode_()
Show current mixer mode on mixer mode select.
void dump_config() override
bool set_volume(float volume) override
bool read_power_state_(uint8_t &power_state)
Read POWER_STATE, masked to device state; false if read failed.
bool write_ctrl_state_(uint8_t state, bool muted)
void activate()
Clear faults, leave deep sleep and switch to play; the device waits in Hi-Z until an I2S clock is pre...
void deactivate()
Switch to deep sleep, the lowest power state that keeps I2C and the DSP running.
void on_audio_started() override
uint8_t fault_output_pin_
bool select_book_page_(uint8_t book, uint8_t page)
ESPHOME_LOG_TAG(TAG, "tas58xx")
@ MIXER_MODE_STEREO_INVERSE
void HOT delay(uint32_t ms)
Everything that differs between models of the family.
uint8_t fault_sensor_bits[FAULT_SENSOR_COUNT]
Bit index for each FaultSensor, NO_BIT used where the model does not have that fault bit.
uint32_t fault_output_off_mask
Faults that keep the output off until cleared by activate()
const LogString *(* fault_name)(uint8_t index)
uint32_t fault_warning_mask
Faults logged as warnings.
const i2c::I2CRegValue * startup_sequence
Remainder of the startup sequence, run after the reset, as {register, value} pairs in PROGMEM.
uint8_t mixer_book
Location of the four 9.23 fixed point input mixer coefficients: LEFT_TO_LEFT, RIGHT_TO_LEFT,...
const LogString *(* name)()
uint32_t fault_latched_mask
Faults that stay set after the condition is gone and are cleared after each read.
uint8_t fault_output_pins
Number of pins that can report FAULTZ: pin n is selected at 0x61 + n and enabled by bit n of 0x60.
uint32_t fault_error_mask
Faults logged as errors and reported by have_fault.
const LogString *(* fault_output_name)(uint8_t pin)
Name of FAULTZ pin n, for dump_config.