ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pcm1863.cpp
Go to the documentation of this file.
1#include "pcm1863.h"
2#include "pcm1863_const.h"
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
6#include <cmath>
7
8namespace esphome::pcm1863 {
9
10ESPHOME_LOG_TAG(TAG, "pcm1863");
11
12// Indexed by PCM1863BitsPerSample
13PROGMEM_STRING_TABLE(BitsPerSampleStrings, "32", "24", "20", "16");
14PROGMEM_STRING_TABLE(InputStrings, "NONE", "VIN1", "VIN2", "VIN3", "VIN4", "DIFF_1", "DIFF_2");
15
16// Page select and power up: bits 2:0 clear is the run state, and the reserved bits 7:3
17// must be written as 0b01110 or the analog section stops
18static constexpr i2c::I2CRegValue PCM1863_INIT[] PROGMEM = {
19 {PCM1863_REG_PAGE, PCM1863_PAGE_0},
20 {PCM1863_REG_POWER_CTRL, PCM1863_PWR_CTRL_RSV},
21};
22
23void PCM1863::dump_config() {
24 ESP_LOGCONFIG(TAG,
25 "PCM1863 audio ADC:\n"
26 " Bits Per Sample: %s\n"
27 " Input: %s\n"
28 " Mic Gain: %.1f dB",
29 LOG_STR_ARG(BitsPerSampleStrings::get_log_str(this->bits_per_sample_, 0)),
30 LOG_STR_ARG(InputStrings::get_log_str(this->input_, 0)), this->mic_gain_);
31#ifdef USE_PCM1863_SELECT
32 LOG_SELECT(" ", "Input", this->input_select_);
33#endif
34}
35
36void PCM1863::setup() {
37 // Writing PCM1863_RESET to the page selector restores every register to its default
38 if (!this->setup_check(this->write_byte(PCM1863_REG_PAGE, PCM1863_RESET)))
39 return;
40 delay(2);
41 // The datasheet does not say which page the reset leaves selected, so select page 0 first
42 if (!this->setup_check(this->write_registers(PCM1863_INIT)))
43 return;
44
45 if (!this->setup_check(this->configure_i2s_format_() && this->configure_input_() && this->configure_mic_gain_()))
46 return;
47
48#ifdef USE_PCM1863_SELECT
49 // Show the input the ADC booted with, so the select does not start out blank
50 PUBLISH_STATE(this->input_select_, this->input_);
51#endif
52}
53
54bool PCM1863::set_mic_gain(float mic_gain) {
55 // Nothing reaches the chip after a failed setup, so report the change as not applied
56 if (this->is_failed())
57 return false;
58 this->mic_gain_ = clamp<float>(mic_gain, PCM1863_MIC_GAIN_MIN, PCM1863_MIC_GAIN_MAX);
59 if (!this->is_ready())
60 return true;
61 return this->status_comm_result(this->configure_mic_gain_());
62}
63
65 // The PGA gain registers take a two's complement value in 0.5 dB steps
66 const auto reg_val = static_cast<uint8_t>(lroundf(this->mic_gain_ / PCM1863_MIC_GAIN_STEP));
67 return this->write_byte(PCM1863_REG_PGA_VAL_CH1_L, reg_val) && this->write_byte(PCM1863_REG_PGA_VAL_CH1_R, reg_val);
68}
69
70bool PCM1863::set_input(PCM1863Input input) {
71 if (this->is_failed())
72 return false;
73 this->input_ = input;
74 if (!this->is_ready())
75 return true;
76 return this->status_comm_result(this->configure_input_());
77}
78
80 // Bit 6 of both input select registers is reserved and must always be written as 1
81 const uint8_t mux = this->input_ == PCM1863_INPUT_NONE ? 0 : 1 << (this->input_ - 1);
82 const uint8_t reg_val = PCM1863_INPUT_SEL_RSV | mux;
83 return this->write_byte(PCM1863_REG_ADC1_INPUT_SEL_L, reg_val) &&
84 this->write_byte(PCM1863_REG_ADC1_INPUT_SEL_R, reg_val);
85}
86
88 const uint8_t wlen = this->bits_per_sample_;
89 const uint8_t reg_val =
90 (wlen << PCM1863_PCM_CFG_RX_WLEN_SHIFT) | (wlen << PCM1863_PCM_CFG_TX_WLEN_SHIFT) | PCM1863_PCM_CFG_FMT_I2S;
91 return this->write_byte(PCM1863_REG_PCM_CFG, reg_val);
92}
93
94} // namespace esphome::pcm1863
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool is_failed() const
Definition component.h:288
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool is_ready() const
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
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:89
PCM1863Input input_
Definition pcm1863.h:60
PCM1863BitsPerSample bits_per_sample_
Definition pcm1863.h:59
PROGMEM_STRING_TABLE(BitsPerSampleStrings, "32", "24", "20", "16")
ESPHOME_LOG_TAG(TAG, "pcm1863")
void HOT delay(uint32_t ms)
Definition hal.cpp:78
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18