ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
pcm5122.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/gpio.h"
7#include "esphome/core/hal.h"
8
9namespace esphome::pcm5122 {
10
11// Page 0 register addresses
12static const uint8_t PCM5122_REG_PAGE_SELECT = 0x00;
13static const uint8_t PCM5122_REG_RESET = 0x01;
14static const uint8_t PCM5122_REG_POWER_CONTROL = 0x02;
15static const uint8_t PCM5122_REG_MUTE = 0x03;
16static const uint8_t PCM5122_REG_GPIO_ENABLE = 0x08;
17static const uint8_t PCM5122_REG_PLL_REF = 0x0D;
18static const uint8_t PCM5122_REG_ERROR_DETECT = 0x25;
19static const uint8_t PCM5122_REG_AUDIO_FORMAT = 0x28;
20static const uint8_t PCM5122_REG_DAC_DATA_PATH = 0x2A;
21static const uint8_t PCM5122_REG_DVOL_LEFT = 0x3D;
22static const uint8_t PCM5122_REG_DVOL_RIGHT = 0x3E;
23static const uint8_t PCM5122_REG_GPIO_OUTPUT_SELECT = 0x50; // Base address; GPIO n uses offset n-1
24static const uint8_t PCM5122_GPIO_OUTPUT_SELECT_REGISTER = 0x02; // GPIO driven by GPIO_OUTPUT register (reg 0x56)
25static const uint8_t PCM5122_REG_GPIO_OUTPUT = 0x56;
26static const uint8_t PCM5122_REG_GPIO_INVERT = 0x57;
27static const uint8_t PCM5122_REG_GPIO_INPUT = 0x77;
28
29// Page 1 register addresses
30static const uint8_t PCM5122_REG_ANALOG_GAIN = 0x02;
31
32// Register values for init sequence
33static const uint8_t PCM5122_RESET_MODULES = 0x10; // RSTM: reset audio modules
34static const uint8_t PCM5122_AUDIO_FORMAT_I2S = 0x00; // AFMT = I2S (bits [5:4] = 00)
35// ALEN (word length) occupies bits [1:0] of the audio format register
36static const uint8_t PCM5122_AUDIO_FORMAT_ALEN_16BIT = 0x00;
37static const uint8_t PCM5122_AUDIO_FORMAT_ALEN_24BIT = 0x02;
38static const uint8_t PCM5122_AUDIO_FORMAT_ALEN_32BIT = 0x03;
39static const uint8_t PCM5122_ERROR_DETECT_IGNORE_CLKHALT = (1 << 3);
40static const uint8_t PCM5122_ERROR_DETECT_DISABLE_DIV_AUTOSET = (1 << 1);
41static const uint8_t PCM5122_PLL_REF_MASK = (7 << 4); // SREF bits [6:4]
42static const uint8_t PCM5122_PLL_REF_SOURCE_BCK = (1 << 4); // SREF = 001 (BCK)
43
44// Page 0, Register 2 (Power Control): RQST = standby request, RQPD = powerdown request (§10.5.3)
45static const uint8_t PCM5122_POWER_CONTROL_RQST = (1 << 4);
46static const uint8_t PCM5122_POWER_CONTROL_RQPD = (1 << 0);
47
48// Page 1, Register 2 (Analog Gain Control): LAGN/RAGN select 0 dB or -6 dB analog gain (§8.3.5.5)
49static const uint8_t PCM5122_ANALOG_GAIN_LAGN = (1 << 4);
50static const uint8_t PCM5122_ANALOG_GAIN_RAGN = (1 << 0);
51
57
58enum PCM5122AnalogGain : uint8_t {
60 PCM5122_ANALOG_GAIN_MINUS_6DB = PCM5122_ANALOG_GAIN_LAGN | PCM5122_ANALOG_GAIN_RAGN,
61};
62
63// Page 0, Register 0x2A (DAC Data Path): AUPL/AUPR select which channel's data feeds each output (§7.4.2.42)
64enum PCM5122ChannelMix : uint8_t {
65 PCM5122_CHANNEL_MIX_STEREO = 0x11, // Left data -> left out, right data -> right out
66 PCM5122_CHANNEL_MIX_LEFT_ONLY = 0x12, // Left data -> both outputs
67 PCM5122_CHANNEL_MIX_RIGHT_ONLY = 0x21, // Right data -> both outputs
68 PCM5122_CHANNEL_MIX_SWAPPED = 0x22, // Left/right outputs swapped
69};
70
71class PCM5122 final : public audio_dac::AudioDac, public Component, public i2c::I2CDevice {
72 public:
73 void setup() override;
74 void dump_config() override;
75 float get_setup_priority() const override { return setup_priority::IO; }
76
77 void set_bits_per_sample(PCM5122BitsPerSample bits_per_sample) { this->bits_per_sample_ = bits_per_sample; }
78 void set_analog_gain(PCM5122AnalogGain analog_gain) { this->analog_gain_ = analog_gain; }
79 void set_channel_mix(PCM5122ChannelMix channel_mix) { this->channel_mix_ = channel_mix; }
80 void set_volume_min_db(float volume_min_db) { this->volume_min_db_ = volume_min_db; }
81 void set_volume_max_db(float volume_max_db) { this->volume_max_db_ = volume_max_db; }
82 void set_enable_pin(GPIOPin *enable_pin) { this->enable_pin_ = enable_pin; }
83
84 bool set_mute_off() override;
85 bool set_mute_on() override;
86 bool set_volume(float volume) override;
87
88 bool is_muted() override;
89 float volume() override;
90
91 bool set_standby(bool enable);
92 bool set_powerdown(bool enable);
93
94 friend class PCM5122GPIOPin;
95
96 protected:
97 bool select_page_(uint8_t page);
98 bool write_mute_();
99 bool write_volume_();
100 bool write_analog_gain_();
101 bool write_channel_mix_();
103
105 float volume_{1.0f}; // Matches chip post-reset DVOL default (0x30 = 0 dB)
106 float volume_min_db_{-52.5f}; // Matches the previous hardcoded minimum (0x99)
107 float volume_max_db_{0.0f}; // Matches the previous hardcoded maximum (0x30)
108 int16_t current_page_{-1}; // -1 = unknown; cached to skip redundant page-select writes
109 bool is_muted_{false};
110 bool standby_{false};
111 bool powerdown_{false};
115};
116
117} // namespace esphome::pcm5122
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
bool set_standby(bool enable)
Definition pcm5122.cpp:204
void setup() override
Definition pcm5122.cpp:12
bool set_volume(float volume) override
Definition pcm5122.cpp:137
bool is_muted() override
Definition pcm5122.cpp:142
bool set_mute_on() override
Definition pcm5122.cpp:132
float volume() override
Definition pcm5122.cpp:144
bool set_mute_off() override
Definition pcm5122.cpp:127
void dump_config() override
Definition pcm5122.cpp:98
void set_volume_max_db(float volume_max_db)
Definition pcm5122.h:81
bool select_page_(uint8_t page)
Definition pcm5122.cpp:146
void set_volume_min_db(float volume_min_db)
Definition pcm5122.h:80
void set_enable_pin(GPIOPin *enable_pin)
Definition pcm5122.h:82
void set_analog_gain(PCM5122AnalogGain analog_gain)
Definition pcm5122.h:78
bool set_powerdown(bool enable)
Definition pcm5122.cpp:214
PCM5122AnalogGain analog_gain_
Definition pcm5122.h:113
float get_setup_priority() const override
Definition pcm5122.h:75
void set_bits_per_sample(PCM5122BitsPerSample bits_per_sample)
Definition pcm5122.h:77
PCM5122BitsPerSample bits_per_sample_
Definition pcm5122.h:112
void set_channel_mix(PCM5122ChannelMix channel_mix)
Definition pcm5122.h:79
PCM5122ChannelMix channel_mix_
Definition pcm5122.h:114
@ PCM5122_ANALOG_GAIN_0DB
Definition pcm5122.h:59
@ PCM5122_ANALOG_GAIN_MINUS_6DB
Definition pcm5122.h:60
@ 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_CHANNEL_MIX_STEREO
Definition pcm5122.h:65
@ 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
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41