ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
es8156.cpp
Go to the documentation of this file.
1#include "es8156.h"
2#include "es8156_const.h"
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6#include <cinttypes>
7
8namespace esphome::es8156 {
9
10ESPHOME_LOG_TAG(TAG, "es8156");
11
12static constexpr i2c::I2CRegValue ES8156_INIT[] PROGMEM = {
13 // REG02 MODE CONFIG 1: Enable software mode for I2C control of volume/mute
14 // Bit 2: SOFT_MODE_SEL=1 (software mode enabled)
15 {ES8156_REG02_SCLK_MODE, 0x04},
16 // Analog system configuration (active-low power down bits, active-high enables)
17 // REG20 ANALOG SYSTEM: Configure analog signal path
18 {ES8156_REG20_ANALOG_SYS1, 0x2A},
19 // REG21 ANALOG SYSTEM: VSEL=0x1C (bias level ~120%), normal VREF ramp speed
20 {ES8156_REG21_ANALOG_SYS2, 0x3C},
21 // REG22 ANALOG SYSTEM: Line out mode (HPSW=0), OUT_MUTE=0 (not muted)
22 {ES8156_REG22_ANALOG_SYS3, 0x00},
23 // REG24 ANALOG SYSTEM: Low power mode for VREFBUF, HPCOM, DACVRP; DAC normal power
24 // Bits 2:0 = 0x07: LPVREFBUF=1, LPHPCOM=1, LPDACVRP=1, LPDAC=0
25 {ES8156_REG24_ANALOG_LP, 0x07},
26 // REG23 ANALOG SYSTEM: Lowest bias (IBIAS_SW=0), VMIDLVL=VDDA/2, normal impedance
27 {ES8156_REG23_ANALOG_SYS4, 0x00},
28 // Timing and interface configuration
29 // REG0A/0B TIME CONTROL: Fast state machine transitions
30 {ES8156_REG0A_TIME_CONTROL1, 0x01},
31 {ES8156_REG0B_TIME_CONTROL2, 0x01},
32 // REG11 SDP INTERFACE CONFIG: Default I2S format (24-bit, I2S mode)
33 {ES8156_REG11_DAC_SDP, 0x00},
34 // REG19 EQ CONTROL 1: EQ disabled (EQ_ON=0), EQ_BAND_NUM=2
35 {ES8156_REG19_EQ_CONTROL1, 0x20},
36 // REG0D P2S CONTROL: Parallel-to-serial converter settings
37 {ES8156_REG0D_P2S_CONTROL, 0x14},
38 // REG09 MISC CONTROL 2: Default settings
39 {ES8156_REG09_MISC_CONTROL2, 0x00},
40 // REG18 MISC CONTROL 3: Stereo channel routing, no inversion
41 // Bits 5:4 CHN_CROSS: 0=L→L/R→R, 1=L to both, 2=R to both, 3=swap L/R
42 // Bits 3:2: LCH_INV/RCH_INV channel inversion
43 {ES8156_REG18_MISC_CONTROL3, 0x00},
44 // REG08 CLOCK OFF: Enable all internal clocks (0x3F = all clock gates open)
45 {ES8156_REG08_CLOCK_ON_OFF, 0x3F},
46 // REG00 RESET CONTROL: Reset sequence
47 // First: RST_DIG=1 (assert digital reset)
48 {ES8156_REG00_RESET, 0x02},
49 // Then: CSM_ON=1 (enable chip state machine), RST_DIG=1
50 {ES8156_REG00_RESET, 0x03},
51 // REG25 ANALOG SYSTEM: Power up analog blocks
52 // VMIDSEL=2 (normal VMID operation), PDN_ANA=0, ENREFR=0, ENHPCOM=0
53 // PDN_DACVREFGEN=0, PDN_VREFBUF=0, PDN_DAC=0 (all enabled)
54 {ES8156_REG25_ANALOG_SYS5, 0x20},
55};
56
58 if (!this->write_registers(ES8156_INIT))
59 this->mark_failed(LOG_STR("Failed to initialize"));
60}
61
62void ES8156::dump_config() { ESP_LOGCONFIG(TAG, "ES8156 Audio Codec:"); }
63
64bool ES8156::set_volume(float volume) {
65 volume = clamp(volume, 0.0f, 1.0f);
66 uint8_t reg = remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 255);
67 ESP_LOGV(TAG, "Setting ES8156_REG14_VOLUME_CONTROL to %u (volume: %f)", reg, volume);
68 return this->write_byte(ES8156_REG14_VOLUME_CONTROL, reg);
69}
70
72 uint8_t reg;
73 this->read_byte(ES8156_REG14_VOLUME_CONTROL, &reg);
74 return remap<float, uint8_t>(reg, 0, 255, 0.0f, 1.0f);
75}
76
77bool ES8156::set_mute_state_(bool mute_state) {
78 this->is_muted_ = mute_state;
79 const uint8_t mask = BIT(1) | BIT(2);
80 return this->update_register(ES8156_REG13_DAC_MUTE, mask, mute_state ? mask : 0);
81}
82
83} // namespace esphome::es8156
void mark_failed()
Mark this component as failed.
float volume() override
Gets the current volume out from the DAC.
Definition es8156.cpp:71
void dump_config() override
Definition es8156.cpp:62
void setup() override
Definition es8156.cpp:57
bool set_mute_state_(bool mute_state)
Mutes or unmutes the DAC audio out.
Definition es8156.cpp:77
bool set_volume(float volume) override
Writes the volume out to the DAC.
Definition es8156.cpp:64
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 update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
Definition i2c.cpp:73
ESPHOME_LOG_TAG(TAG, "es8156")
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
Definition helpers.h:932
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18