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");
95 return this->
write_byte(TAS58XX_PAGE_SELECT, TAS58XX_PAGE_0) && this->
write_byte(TAS58XX_BOOK_SELECT, book) &&
103 !this->
write_byte(TAS58XX_DEVICE_CTRL_2, TAS58XX_CTRL_STATE_HIZ) ||
104 !this->
write_byte(TAS58XX_RESET_CTRL, TAS58XX_RESET_CTRL_ALL) ||
105 !this->
write_byte(TAS58XX_DEVICE_CTRL_2, TAS58XX_CTRL_STATE_HIZ)) {
106 ESP_LOGE(TAG,
"I2C write failed during reset");
109 delay(RESET_SETTLE_MS);
112 ESP_LOGE(TAG,
"I2C write failed during init");
124 ESP_LOGE(TAG,
"I2C write failed during init");
130 const uint8_t again =
131 static_cast<uint8_t
>(lroundf(clamp(-this->
analog_gain_db_ * 2.0f, 0.0f,
float{TAS58XX_AGAIN_MINUS_15_5DB})));
134 if (!this->
write_byte(TAS58XX_DEVICE_CTRL_1, device_ctrl_1) || !this->
write_byte(TAS58XX_AGAIN, again) ||
136 !this->
write_byte(TAS58XX_FAULT_CLEAR, TAS58XX_FAULT_CLEAR_ANALOG)) {
137 ESP_LOGE(TAG,
"I2C write failed during init");
149 ESP_LOGD(TAG,
"[0x%02X] Activating", this->
address_);
152 ESP_LOGE(TAG,
"Failed to select the control port");
156 if (!this->
write_byte(TAS58XX_FAULT_CLEAR, TAS58XX_FAULT_CLEAR_ANALOG)) {
157 ESP_LOGW(TAG,
"Failed to clear faults");
161 if (this->
ctrl_state_ == TAS58XX_CTRL_STATE_DEEP_SLEEP &&
174 ESP_LOGD(TAG,
"[0x%02X] Deactivating", this->
address_);
176 ESP_LOGE(TAG,
"Failed to select the control port");
189 PUBLISH_STATE(this->activate_switch_, active);
194 if (!this->
write_byte(TAS58XX_DEVICE_CTRL_2,
state | (muted ? TAS58XX_DEVICE_CTRL_2_MUTE : 0))) {
195 ESP_LOGE(TAG,
"Failed to write DEVICE_CTRL_2");
204 uint8_t fault_registers[TAS58XX_FAULT_REGISTER_COUNT];
205 if (!this->
read_bytes(TAS58XX_CHAN_FAULT, fault_registers,
sizeof(fault_registers)))
208 for (uint8_t
reg = 0;
reg < TAS58XX_FAULT_REGISTER_COUNT;
reg++)
215 for (uint8_t index = 0; index < 32; index++) {
217 if (!(changed & mask))
219 const LogString *name = model.
fault_name(index);
220 if (!(active & mask)) {
221 ESP_LOGI(TAG,
"[0x%02X] %s cleared", this->
address_, LOG_STR_ARG(name));
223 ESP_LOGE(TAG,
"[0x%02X] %s", this->
address_, LOG_STR_ARG(name));
225 ESP_LOGW(TAG,
"[0x%02X] %s", this->
address_, LOG_STR_ARG(name));
229 ESP_LOGW(TAG,
"[0x%02X] Output stays off: fix the cause, then call tas58xx.activate or power cycle the amplifier",
232 this->logged_faults_ = active;
234#ifdef USE_BINARY_SENSOR
235 PUBLISH_STATE(this->have_fault_binary_sensor_, (active & model.
fault_error_mask) != 0);
247 !this->write_byte(TAS58XX_FAULT_CLEAR, TAS58XX_FAULT_CLEAR_ANALOG)) {
248 ESP_LOGW(TAG,
"Failed to clear faults");
260 if (!this->
read_byte(TAS58XX_POWER_STATE, &power_state))
262 power_state &= TAS58XX_CTRL_STATE_MASK;
269 ESP_LOGW(TAG,
"Failed to write mixer");
274 uint8_t power_state{0};
276 LOG_STR(
"Failed to read status")))
280 ESP_LOGD(TAG,
"[0x%02X] Power state: %s", this->
address_, LOG_STR_ARG(power_state_name(power_state)));
284 if (dsp_writable(power_state))
289 const LogString *fault_output;
291 fault_output = LOG_STR(
"None");
293 fault_output = LOG_STR(
"Invalid");
301 LOG_I2C_DEVICE(
this);
303 LOG_UPDATE_INTERVAL(
this);
305 " Analog Gain: %.1f dB\n"
309 " Volume Range: %.1f dB to %.1f dB\n"
315 LOG_STR_ARG(fault_output));
316#ifdef USE_BINARY_SENSOR
317 LOG_BINARY_SENSOR(
" ",
"Any Fault", this->have_fault_binary_sensor_);
319 LOG_BINARY_SENSOR(
" ",
"Fault",
sensor);
323 LOG_SWITCH(
" ",
"Activate", this->activate_switch_);
326 LOG_SELECT(
" ",
"Mixer Mode", this->mixer_mode_select_);
338 float previous = this->
volume_;
350 const uint8_t dig_vol =
static_cast<uint8_t
>(
351 lroundf(clamp(TAS58XX_DIG_VOL_0DB - volume_db * 2.0f, 0.0f,
float{TAS58XX_DIG_VOL_MINUS_103DB})));
352 ESP_LOGV(TAG,
"Setting volume to 0x%02X", dig_vol);
353 return this->
write_byte(TAS58XX_DIG_VOL, dig_vol);
358 if (this->mixer_mode_select_ !=
nullptr) {
361 this->mixer_mode_select_->publish_state(this->
mixer_mode_ - first_mode);
365void TAS58xx::apply_mixer_mode(
MixerMode mixer_mode) {
370 ESP_LOGW(TAG,
"[0x%02X] Mixer mode %s is not valid for PBTL", this->
address_,
371 LOG_STR_ARG(mixer_mode_name(mixer_mode)));
384 uint8_t left_to_left = TAS58XX_MIXER_0DB;
385 uint8_t right_to_left = TAS58XX_MIXER_MUTE;
386 uint8_t left_to_right = TAS58XX_MIXER_MUTE;
387 uint8_t right_to_right = TAS58XX_MIXER_0DB;
389 left_to_left = TAS58XX_MIXER_MUTE;
390 right_to_left = TAS58XX_MIXER_0DB;
391 left_to_right = TAS58XX_MIXER_0DB;
392 right_to_right = TAS58XX_MIXER_MUTE;
394 left_to_left = TAS58XX_MIXER_MINUS_6DB;
395 right_to_left = TAS58XX_MIXER_MINUS_6DB;
396 left_to_right = TAS58XX_MIXER_MINUS_6DB;
397 right_to_right = TAS58XX_MIXER_MINUS_6DB;
399 left_to_right = TAS58XX_MIXER_0DB;
400 right_to_right = TAS58XX_MIXER_MUTE;
402 left_to_left = TAS58XX_MIXER_MUTE;
403 right_to_left = TAS58XX_MIXER_0DB;
405 const uint8_t coefficients[4 * TAS58XX_MIXER_COEFFICIENT_SIZE] = {0, left_to_left, 0, 0, 0, right_to_left, 0, 0,
406 0, left_to_right, 0, 0, 0, right_to_right, 0, 0};
413 ESP_LOGD(TAG,
"[0x%02X] Mixer mode written: %s", this->
address_, LOG_STR_ARG(mixer_mode_name(this->
mixer_mode_)));
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
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)
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
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.