ESPHome 2026.6.2
Loading...
Searching...
No Matches
pcm5122.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/hal.h"
7
8namespace esphome::pcm5122 {
9
10// Page 0 register addresses
11static const uint8_t PCM5122_REG_PAGE_SELECT = 0x00;
12static const uint8_t PCM5122_REG_RESET = 0x01;
13static const uint8_t PCM5122_REG_MUTE = 0x03;
14static const uint8_t PCM5122_REG_GPIO_ENABLE = 0x08;
15static const uint8_t PCM5122_REG_PLL_REF = 0x0D;
16static const uint8_t PCM5122_REG_ERROR_DETECT = 0x25;
17static const uint8_t PCM5122_REG_AUDIO_FORMAT = 0x28;
18static const uint8_t PCM5122_REG_DVOL_LEFT = 0x3D;
19static const uint8_t PCM5122_REG_DVOL_RIGHT = 0x3E;
20static const uint8_t PCM5122_REG_GPIO_OUTPUT_SELECT = 0x50; // Base address; GPIO n uses offset n-1
21static const uint8_t PCM5122_GPIO_OUTPUT_SELECT_REGISTER = 0x02; // GPIO driven by GPIO_OUTPUT register (reg 0x56)
22static const uint8_t PCM5122_REG_GPIO_OUTPUT = 0x56;
23static const uint8_t PCM5122_REG_GPIO_INVERT = 0x57;
24static const uint8_t PCM5122_REG_GPIO_INPUT = 0x77;
25
26// Register values for init sequence
27static const uint8_t PCM5122_RESET_MODULES = 0x10; // RSTM: reset audio modules
28static const uint8_t PCM5122_AUDIO_FORMAT_I2S = 0x00; // AFMT = I2S (bits [5:4] = 00)
29// ALEN (word length) occupies bits [1:0] of the audio format register
30static const uint8_t PCM5122_AUDIO_FORMAT_ALEN_16BIT = 0x00;
31static const uint8_t PCM5122_AUDIO_FORMAT_ALEN_24BIT = 0x02;
32static const uint8_t PCM5122_AUDIO_FORMAT_ALEN_32BIT = 0x03;
33static const uint8_t PCM5122_ERROR_DETECT_IGNORE_CLKHALT = (1 << 3);
34static const uint8_t PCM5122_ERROR_DETECT_DISABLE_DIV_AUTOSET = (1 << 1);
35static const uint8_t PCM5122_PLL_REF_MASK = (7 << 4); // SREF bits [6:4]
36static const uint8_t PCM5122_PLL_REF_SOURCE_BCK = (1 << 4); // SREF = 001 (BCK)
37
43
44class PCM5122 : public audio_dac::AudioDac, public Component, public i2c::I2CDevice {
45 public:
46 void setup() override;
47 void dump_config() override;
48 float get_setup_priority() const override { return setup_priority::IO; }
49
50 void set_bits_per_sample(PCM5122BitsPerSample bits_per_sample) { this->bits_per_sample_ = bits_per_sample; }
51
52 bool set_mute_off() override;
53 bool set_mute_on() override;
54 bool set_volume(float volume) override;
55
56 bool is_muted() override;
57 float volume() override;
58
59 friend class PCM5122GPIOPin;
60
61 protected:
62 bool select_page_(uint8_t page);
63 bool write_mute_();
64 bool write_volume_();
65
66 float volume_{1.0f}; // Matches chip post-reset DVOL default (0x30 = 0 dB)
67 int16_t current_page_{-1}; // -1 = unknown; cached to skip redundant page-select writes
68 bool is_muted_{false};
70};
71
72} // namespace esphome::pcm5122
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
void setup() override
Definition pcm5122.cpp:12
bool set_volume(float volume) override
Definition pcm5122.cpp:91
bool is_muted() override
Definition pcm5122.cpp:96
bool set_mute_on() override
Definition pcm5122.cpp:86
float volume() override
Definition pcm5122.cpp:98
bool set_mute_off() override
Definition pcm5122.cpp:81
void dump_config() override
Definition pcm5122.cpp:72
bool select_page_(uint8_t page)
Definition pcm5122.cpp:100
float get_setup_priority() const override
Definition pcm5122.h:48
void set_bits_per_sample(PCM5122BitsPerSample bits_per_sample)
Definition pcm5122.h:50
PCM5122BitsPerSample bits_per_sample_
Definition pcm5122.h:69
@ PCM5122_BITS_PER_SAMPLE_16
Definition pcm5122.h:39
@ PCM5122_BITS_PER_SAMPLE_32
Definition pcm5122.h:41
@ PCM5122_BITS_PER_SAMPLE_24
Definition pcm5122.h:40
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:39