ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tas58xx.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
8#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
10
11#ifdef USE_BINARY_SENSOR
13#endif
14#ifdef USE_SELECT
16#endif
17#ifdef USE_SWITCH
19#endif
20
21namespace esphome::tas58xx {
22
23enum DacMode : uint8_t {
24 DAC_MODE_BTL = 0, // Bridge tied load, two speakers
25 DAC_MODE_PBTL = 1, // Parallel bridge tied load, one speaker
26};
27
33
41
68
69// compile time check
70static_assert(
72 "enum FaultSensor altered: Update FAULT_SENSORS in binary_sensor.py and fault_sensor_bits in each model's cpp");
73
75static constexpr uint8_t NO_BIT = 0xFF;
77static constexpr uint8_t FAULT_OUTPUT_NONE = 0xFF;
78
84struct ModelInfo {
85 const LogString *(*name)();
91 uint8_t mixer_book;
92 uint8_t mixer_page;
102 const LogString *(*fault_name)(uint8_t index);
108 const LogString *(*fault_output_name)(uint8_t pin);
109};
110
111#ifdef USE_TAS58XX_TAS5805M
112extern const ModelInfo TAS5805M_MODEL;
113#endif
114#ifdef USE_TAS58XX_TAS5825M
115extern const ModelInfo TAS5825M_MODEL;
116#endif
117
119 public:
120 explicit TAS58xx(const ModelInfo *model) : model_(model) {}
121
122 void setup() override;
123 void dump_config() override;
124 float get_setup_priority() const override { return setup_priority::IO; }
125 void update() override;
126
129 void activate();
131 void deactivate();
132
133 bool set_mute_off() override { return this->set_mute_(false); }
134 bool set_mute_on() override { return this->set_mute_(true); }
135 bool set_volume(float volume) override;
136
137 bool is_muted() override { return this->is_muted_; }
138 float volume() override { return this->volume_; }
139
140 void on_audio_started() override;
141
142 void set_enable_pin(GPIOPin *enable_pin) { this->enable_pin_ = enable_pin; }
143 void set_analog_gain(float analog_gain_db) { this->analog_gain_db_ = analog_gain_db; }
144 void set_dac_mode(DacMode dac_mode) { this->dac_mode_ = dac_mode; }
146 void set_fault_output(uint8_t pin) { this->fault_output_pin_ = pin; }
147 void set_mixer_mode(MixerMode mixer_mode) { this->mixer_mode_ = mixer_mode; }
148 void set_modulation(Modulation modulation) { this->modulation_ = modulation; }
149 void set_volume_min_db(float volume_min_db) { this->volume_min_db_ = volume_min_db; }
150 void set_volume_max_db(float volume_max_db) { this->volume_max_db_ = volume_max_db; }
151
152#ifdef USE_SELECT
153 SUB_SELECT(mixer_mode)
155 void apply_mixer_mode(MixerMode mixer_mode);
156#endif
157
158#ifdef USE_BINARY_SENSOR
160 void set_fault_binary_sensor(FaultSensor fault, binary_sensor::BinarySensor *sensor) {
161 this->fault_binary_sensors_[fault] = sensor;
162 }
163#endif
164#ifdef USE_SWITCH
167 void set_active(bool active) {
168 if (active) {
169 this->activate();
170 } else {
171 this->deactivate();
172 }
173 }
174#endif
175
176 protected:
178 void publish_active_(bool active);
179 bool select_book_page_(uint8_t book, uint8_t page);
180 bool init_();
181 bool write_ctrl_state_(uint8_t state, bool muted);
182 bool set_mute_(bool muted);
183 bool write_volume_();
184 bool write_mixer_();
185 bool read_faults_();
187 bool read_power_state_(uint8_t &power_state);
189 void write_dsp_();
190#ifdef USE_SELECT
192 void publish_mixer_mode_();
193#endif
194
197#ifdef USE_BINARY_SENSOR
198 std::array<binary_sensor::BinarySensor *, FAULT_SENSOR_COUNT> fault_binary_sensors_{};
199#endif
200 float volume_{0};
201 float analog_gain_db_{-15.5f};
202 float volume_min_db_{-103.0f};
203 float volume_max_db_{24.0f};
204 uint32_t logged_faults_{0}; // Fault bits as last logged, packed like the fault masks
206 uint8_t fault_output_pin_{FAULT_OUTPUT_NONE}; // No fault output: the pin keeps its power on state
209 uint8_t ctrl_state_{0};
210 uint8_t power_state_{0xFF}; // Last POWER_STATE seen by update(), 0xFF until the first read
211 bool mixer_written_{false}; // False while mixer write is pending
212};
213
214} // namespace esphome::tas58xx
This class simplifies creating components that periodically check a state.
Definition component.h:532
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
binary_sensor::BinarySensor * sensor
Definition tas58xx.h:160
void set_analog_gain(float analog_gain_db)
Definition tas58xx.h:143
SUB_BINARY_SENSOR(have_fault) void set_fault_binary_sensor(FaultSensor fault
SUB_SWITCH(activate) void set_active(bool active)
The activate switch: on activates the amplifier (play), off puts it into deep sleep.
Definition tas58xx.h:165
SUB_SELECT(mixer_mode) void apply_mixer_mode(MixerMode mixer_mode)
Change input mixer; except if invalid for dac_mode. Written immediately if playing,...
void set_modulation(Modulation modulation)
Definition tas58xx.h:148
void set_dac_mode(DacMode dac_mode)
Definition tas58xx.h:144
void publish_active_(bool active)
Publish the activate switch state, if there is one.
Definition tas58xx.cpp:195
bool set_mute_(bool muted)
Definition tas58xx.cpp:338
void set_fault_output(uint8_t pin)
pin: 0 to 2 for GPIOn on TAS5825M, 0 for ADR on TAS5805M; leave unset for no fault output
Definition tas58xx.h:146
void set_volume_min_db(float volume_min_db)
Definition tas58xx.h:149
void set_mixer_mode(MixerMode mixer_mode)
Definition tas58xx.h:147
void write_dsp_()
Write DSP settings not written since last reset; call only while DSP is writable.
Definition tas58xx.cpp:275
float get_setup_priority() const override
Definition tas58xx.h:124
bool set_mute_off() override
Definition tas58xx.h:133
void set_enable_pin(GPIOPin *enable_pin)
Definition tas58xx.h:142
const ModelInfo * model_
Definition tas58xx.h:195
std::array< binary_sensor::BinarySensor *, FAULT_SENSOR_COUNT > fault_binary_sensors_
Definition tas58xx.h:198
void publish_mixer_mode_()
Show current mixer mode on mixer mode select.
Definition tas58xx.cpp:365
void setup() override
Definition tas58xx.cpp:82
void update() override
Definition tas58xx.cpp:281
TAS58xx(const ModelInfo *model)
Definition tas58xx.h:120
bool is_muted() override
Definition tas58xx.h:137
void dump_config() override
Definition tas58xx.cpp:296
void set_volume_max_db(float volume_max_db)
Definition tas58xx.h:150
bool set_volume(float volume) override
Definition tas58xx.cpp:345
bool read_power_state_(uint8_t &power_state)
Read POWER_STATE, masked to device state; false if read failed.
Definition tas58xx.cpp:267
bool write_ctrl_state_(uint8_t state, bool muted)
Definition tas58xx.cpp:201
void activate()
Clear faults, leave deep sleep and switch to play; the device waits in Hi-Z until an I2S clock is pre...
Definition tas58xx.cpp:154
void deactivate()
Switch to deep sleep, the lowest power state that keeps I2C and the DSP running.
Definition tas58xx.cpp:179
float volume() override
Definition tas58xx.h:138
void on_audio_started() override
Definition tas58xx.cpp:261
bool set_mute_on() override
Definition tas58xx.h:134
bool select_book_page_(uint8_t book, uint8_t page)
Definition tas58xx.cpp:101
bool state
Definition fan.h:2
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41
Modulation
DEVICE_CTRL_1 DAMP_MOD values, the same on all models.
Definition tas58xx.h:29
const ModelInfo TAS5805M_MODEL
const ModelInfo TAS5825M_MODEL
FaultSensor
Fault binary sensors that map to a single fault bit.
Definition tas58xx.h:44
@ FAULT_SENSOR_RIGHT_CHANNEL_OVER_CURRENT
Definition tas58xx.h:49
@ FAULT_SENSOR_LEFT_CHANNEL_CBC_OVER_CURRENT
Definition tas58xx.h:60
@ FAULT_SENSOR_BQ_WRITE_FAILED
Definition tas58xx.h:51
@ FAULT_SENSOR_RIGHT_CHANNEL_CBC_OVER_CURRENT_WARNING
Definition tas58xx.h:62
@ FAULT_SENSOR_OVER_TEMP_SHUTDOWN
Definition tas58xx.h:55
@ FAULT_SENSOR_LEFT_CHANNEL_DC_FAULT
Definition tas58xx.h:46
@ FAULT_SENSOR_RIGHT_CHANNEL_DC_FAULT
Definition tas58xx.h:47
@ FAULT_SENSOR_RIGHT_CHANNEL_CBC_OVER_CURRENT
Definition tas58xx.h:59
@ FAULT_SENSOR_OTP_CRC_CHECK
Definition tas58xx.h:50
@ FAULT_SENSOR_OVER_TEMP_WARNING
Definition tas58xx.h:56
@ FAULT_SENSOR_LEFT_CHANNEL_OVER_CURRENT
Definition tas58xx.h:48
@ FAULT_SENSOR_OVER_TEMP_146C_WARNING
Definition tas58xx.h:63
@ FAULT_SENSOR_PVDD_OVER_VOLTAGE
Definition tas58xx.h:53
@ FAULT_SENSOR_PVDD_UNDER_VOLTAGE
Definition tas58xx.h:54
@ FAULT_SENSOR_LOAD_EEPROM_ERROR
Definition tas58xx.h:58
@ FAULT_SENSOR_LEFT_CHANNEL_CBC_OVER_CURRENT_WARNING
Definition tas58xx.h:61
@ FAULT_SENSOR_CLOCK_FAULT
Definition tas58xx.h:52
@ MIXER_MODE_STEREO_INVERSE
Definition tas58xx.h:36
static void uint32_t
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18
Everything that differs between models of the family.
Definition tas58xx.h:84
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.
Definition tas58xx.h:104
uint32_t fault_output_off_mask
Faults that keep the output off until cleared by activate()
Definition tas58xx.h:99
uint32_t fault_warning_mask
Faults logged as warnings.
Definition tas58xx.h:97
const i2c::I2CRegValue * startup_sequence
Remainder of the startup sequence, run after the reset, as {register, value} pairs in PROGMEM.
Definition tas58xx.h:87
uint8_t mixer_book
Location of the four 9.23 fixed point input mixer coefficients: LEFT_TO_LEFT, RIGHT_TO_LEFT,...
Definition tas58xx.h:91
uint32_t fault_latched_mask
Faults that stay set after the condition is gone and are cleared after each read.
Definition tas58xx.h:101
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.
Definition tas58xx.h:106
uint32_t fault_error_mask
Faults logged as errors and reported by have_fault.
Definition tas58xx.h:95