arduino-audio-driver
Loading...
Searching...
No Matches
ES8156.h
Go to the documentation of this file.
1/*
2 * ESPRESSIF MIT License
3 *
4 * Copyright (c) 2021 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD>
5 *
6 * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in
7 * which case, it is free of charge, to any person obtaining a copy of this
8 * software and associated documentation files (the "Software"), to deal in the
9 * Software without restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do
12 * so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26#pragma once
27
29#include "DriverCommon.h"
30#include "Platforms/API_I2C.h"
31#include "stdbool.h"
32#include <math.h>
33#include "string.h"
34
35
36namespace audio_driver {
37
43class ES8156 {
44 public:
45 static constexpr int ES8156_ADDR = 0x8;
46 static constexpr uint8_t ES8156_RESET_REG00 = 0x00;
47 static constexpr uint8_t ES8156_MAINCLOCK_CTL_REG01 = 0x01;
48 static constexpr uint8_t ES8156_SCLK_MODE_REG02 = 0x02;
49 static constexpr uint8_t ES8156_LRCLK_DIV_H_REG03 = 0x03;
50 static constexpr uint8_t ES8156_LRCLK_DIV_L_REG04 = 0x04;
51 static constexpr uint8_t ES8156_SCLK_DIV_REG05 = 0x05;
52 static constexpr uint8_t ES8156_NFS_CONFIG_REG06 = 0x06;
53 static constexpr uint8_t ES8156_MISC_CONTROL1_REG07 = 0x07;
54 static constexpr uint8_t ES8156_CLOCK_ON_OFF_REG08 = 0x08;
55 static constexpr uint8_t ES8156_MISC_CONTROL2_REG09 = 0x09;
56 static constexpr uint8_t ES8156_TIME_CONTROL1_REG0A = 0x0a;
57 static constexpr uint8_t ES8156_TIME_CONTROL2_REG0B = 0x0b;
58 static constexpr uint8_t ES8156_CHIP_STATUS_REG0C = 0x0c;
59 static constexpr uint8_t ES8156_P2S_CONTROL_REG0D = 0x0d;
60 static constexpr uint8_t ES8156_DAC_OSR_COUNTER_REG10 = 0x10;
61 static constexpr uint8_t ES8156_DAC_SDP_REG11 = 0x11;
62 static constexpr uint8_t ES8156_AUTOMUTE_SET_REG12 = 0x12;
63 static constexpr uint8_t ES8156_DAC_MUTE_REG13 = 0x13;
64 static constexpr uint8_t ES8156_VOLUME_CONTROL_REG14 = 0x14;
65 static constexpr uint8_t ES8156_ALC_CONFIG1_REG15 = 0x15;
66 static constexpr uint8_t ES8156_ALC_CONFIG2_REG16 = 0x16;
67 static constexpr uint8_t ES8156_ALC_CONFIG3_REG17 = 0x17;
68 static constexpr uint8_t ES8156_MISC_CONTROL3_REG18 = 0x18;
69 static constexpr uint8_t ES8156_EQ_CONTROL1_REG19 = 0x19;
70 static constexpr uint8_t ES8156_EQ_CONTROL2_REG1A = 0x1a;
71 static constexpr uint8_t ES8156_ANALOG_SYS1_REG20 = 0x20;
72 static constexpr uint8_t ES8156_ANALOG_SYS2_REG21 = 0x21;
73 static constexpr uint8_t ES8156_ANALOG_SYS3_REG22 = 0x22;
74 static constexpr uint8_t ES8156_ANALOG_SYS4_REG23 = 0x23;
75 static constexpr uint8_t ES8156_ANALOG_LP_REG24 = 0x24;
76 static constexpr uint8_t ES8156_ANALOG_SYS5_REG25 = 0x25;
77 static constexpr uint8_t ES8156_I2C_PAGESEL_REGFC = 0xFC;
78 static constexpr uint8_t ES8156_CHIPID1_REGFD = 0xFD;
79 static constexpr uint8_t ES8156_CHIPID0_REGFE = 0xFE;
80 static constexpr uint8_t ES8156_CHIP_VERSION_REGFF = 0xFF;
81 ES8156() = default;
82
84 void setWire(i2c_bus_handle_t handle) { i2c_handle = handle; }
85
87 void setAddress(int addr) { i2c_addr = addr; }
88
91 if (initialized) {
92 AD_LOGW("The es8156 DAC has been already initialized");
93 return RESULT_OK;
94 }
95 initialized = true;
96
97 writeReg(0x02, 0x04);
98 writeReg(0x20, 0x2A);
99 writeReg(0x21, 0x3C);
100 writeReg(0x22, 0x00);
101 writeReg(0x24, 0x07);
102 writeReg(0x23, 0x00);
103
104 writeReg(0x0A, 0x01);
105 writeReg(0x0B, 0x01);
106 writeReg(0x11, 0x00);
107 writeReg(0x14, 179); // volume 70%
108
109 writeReg(0x0D, 0x14);
110 writeReg(0x18, 0x00);
111 writeReg(0x08, 0x3F);
112 writeReg(0x00, 0x02);
113 writeReg(0x00, 0x03);
114 writeReg(0x25, 0x20);
115
116 // codec_dac_volume_config_t vol_cfg = ES8156_DAC_VOL_CFG_DEFAULT();
117 // dac_vol_handle = audio_codec_volume_init(&vol_cfg);
118 return RESULT_OK;
119 }
120
123 initialized = false;
124 // audio_codec_volume_deinit(dac_vol_handle);
125 return RESULT_OK;
126 }
127
129 error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active) {
130 error_t ret = RESULT_OK;
131 if (ctrl_state_active) {
132 ret = resume();
133 } else {
134 AD_LOGW("The codec going to stop");
135 ret = standby();
136 }
137 return ret;
138 }
139
142 return RESULT_OK;
143 }
144
146 error_t setVoiceMute(bool enable) {
147 int regv = readReg(ES8156_DAC_MUTE_REG13);
148 if (enable) {
149 regv = regv | bitVal(1) | bitVal(2);
150 } else {
151 regv = regv & (~(bitVal(1) | bitVal(2)));
152 }
154 return RESULT_OK;
155 }
156
164 int ret = 0;
165 // TODO
166 // uint8_t reg = 0;
167 // reg = audio_codec_get_dac_reg_value(dac_vol_handle, volume);
168 // ret = writeReg(ES8156_VOLUME_CONTROL_REG14, reg);
169 // AD_LOGD( "Set volume:%.2d reg_value:0x%.2x dB:%.1f",
170 // dac_vol_handle->user_volume, reg,
171 // audio_codec_cal_dac_volume(dac_vol_handle));
172 return ret;
173 }
174
176 error_t getVoiceVolume(int* volume) {
177 int ret = 0;
178 int regv = 0;
179 *volume = 0;
181 if (regv == RESULT_FAIL) {
182 ret = RESULT_FAIL;
183 } else {
184 if (regv == dac_vol_handle->reg_value) {
185 *volume = dac_vol_handle->user_volume;
186 } else {
187 *volume = 0;
188 ret = RESULT_FAIL;
189 }
190 }
191 AD_LOGD("Get volume:%.2d reg_value:0x%.2x", *volume, regv);
192 return ret;
193 }
194 static constexpr int bitVal(int nr) { return 1 << nr; }
195
196 error_t writeReg(uint8_t reg_addr, uint8_t data) {
197 return i2c_bus_write_bytes(i2c_handle, i2c_addr, &reg_addr,
198 sizeof(reg_addr), &data, sizeof(data));
199 }
200
201 int readReg(uint8_t reg_addr) {
202 uint8_t data;
203 i2c_bus_read_bytes(i2c_handle, i2c_addr, &reg_addr, sizeof(reg_addr),
204 &data, sizeof(data));
205 return (int)data;
206 }
207
209 error_t ret = 0;
210 ret = writeReg(0x14, 0x00);
211 ret |= writeReg(0x19, 0x02);
212 ret |= writeReg(0x21, 0x1F);
213 ret |= writeReg(0x22, 0x02);
214 ret |= writeReg(0x25, 0x21);
215 ret |= writeReg(0x25, 0xA1);
216 ret |= writeReg(0x18, 0x01);
217 ret |= writeReg(0x09, 0x02);
218 ret |= writeReg(0x09, 0x01);
219 ret |= writeReg(0x08, 0x00);
220 return ret;
221 }
222
224 error_t ret = 0;
225 ret |= writeReg(0x08, 0x3F);
226 ret |= writeReg(0x09, 0x00);
227 ret |= writeReg(0x18, 0x00);
228
229 ret |= writeReg(0x25, 0x20);
230 ret |= writeReg(0x22, 0x00);
231 ret |= writeReg(0x21, 0x3C);
232 ret |= writeReg(0x19, 0x20);
233 ret |= writeReg(0x14, 179);
234 return ret;
235 }
236
237 protected:
240 bool initialized = false;
242};
243
244} // namespace audio_driver
#define RESULT_OK
Definition DriverCommon.h:74
#define RESULT_FAIL
Definition DriverCommon.h:75
#define AD_LOGD(...)
Definition Logger.h:12
#define AD_LOGW(...)
Definition Logger.h:14
Header-only driver class for the ES8156 DAC chip.
Definition ES8156.h:43
static constexpr uint8_t ES8156_EQ_CONTROL2_REG1A
Definition ES8156.h:70
static constexpr uint8_t ES8156_ANALOG_SYS3_REG22
Definition ES8156.h:73
static constexpr uint8_t ES8156_CLOCK_ON_OFF_REG08
Definition ES8156.h:54
codec_dac_volume_config_t * dac_vol_handle
Definition ES8156.h:241
error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active)
Control ES8156 codec chip.
Definition ES8156.h:129
static constexpr uint8_t ES8156_CHIP_STATUS_REG0C
Definition ES8156.h:58
static constexpr uint8_t ES8156_AUTOMUTE_SET_REG12
Definition ES8156.h:62
error_t resume(void)
Definition ES8156.h:223
static constexpr uint8_t ES8156_ALC_CONFIG1_REG15
Definition ES8156.h:65
static constexpr uint8_t ES8156_ANALOG_SYS2_REG21
Definition ES8156.h:72
static constexpr uint8_t ES8156_TIME_CONTROL1_REG0A
Definition ES8156.h:56
void setAddress(int addr)
Defines the I2C device address.
Definition ES8156.h:87
error_t init(codec_config_t *cfg)
Initialize ES8156 codec chip.
Definition ES8156.h:90
static constexpr uint8_t ES8156_SCLK_DIV_REG05
Definition ES8156.h:51
error_t deinit(void)
Deinitialize ES8156 codec chip.
Definition ES8156.h:122
static constexpr uint8_t ES8156_DAC_MUTE_REG13
Definition ES8156.h:63
static constexpr uint8_t ES8156_MAINCLOCK_CTL_REG01
Definition ES8156.h:47
static constexpr uint8_t ES8156_ANALOG_SYS4_REG23
Definition ES8156.h:74
error_t setVoiceMute(bool enable)
Configure ES8156 DAC mute or not.
Definition ES8156.h:146
static constexpr uint8_t ES8156_ANALOG_SYS5_REG25
Definition ES8156.h:76
static constexpr uint8_t ES8156_ALC_CONFIG2_REG16
Definition ES8156.h:66
static constexpr uint8_t ES8156_NFS_CONFIG_REG06
Definition ES8156.h:52
int readReg(uint8_t reg_addr)
Definition ES8156.h:201
int i2c_addr
Definition ES8156.h:239
static constexpr uint8_t ES8156_DAC_SDP_REG11
Definition ES8156.h:61
static constexpr int bitVal(int nr)
Definition ES8156.h:194
error_t configI2S(codec_mode_t mode, I2SDefinition *iface)
Configure ES8156 codec mode and I2S interface.
Definition ES8156.h:141
static constexpr uint8_t ES8156_LRCLK_DIV_L_REG04
Definition ES8156.h:50
static constexpr uint8_t ES8156_ALC_CONFIG3_REG17
Definition ES8156.h:67
static constexpr uint8_t ES8156_EQ_CONTROL1_REG19
Definition ES8156.h:69
static constexpr uint8_t ES8156_I2C_PAGESEL_REGFC
Definition ES8156.h:77
void setWire(i2c_bus_handle_t handle)
Defines the I2C bus instance to be used.
Definition ES8156.h:84
static constexpr uint8_t ES8156_TIME_CONTROL2_REG0B
Definition ES8156.h:57
static constexpr uint8_t ES8156_SCLK_MODE_REG02
Definition ES8156.h:48
static constexpr uint8_t ES8156_CHIPID1_REGFD
Definition ES8156.h:78
static constexpr uint8_t ES8156_ANALOG_LP_REG24
Definition ES8156.h:75
error_t writeReg(uint8_t reg_addr, uint8_t data)
Definition ES8156.h:196
i2c_bus_handle_t i2c_handle
Definition ES8156.h:238
static constexpr uint8_t ES8156_CHIP_VERSION_REGFF
Definition ES8156.h:80
static constexpr uint8_t ES8156_LRCLK_DIV_H_REG03
Definition ES8156.h:49
static constexpr int ES8156_ADDR
Definition ES8156.h:45
static constexpr uint8_t ES8156_DAC_OSR_COUNTER_REG10
Definition ES8156.h:60
static constexpr uint8_t ES8156_P2S_CONTROL_REG0D
Definition ES8156.h:59
error_t setVoiceVolume(int volume)
Set voice volume.
Definition ES8156.h:163
static constexpr uint8_t ES8156_MISC_CONTROL1_REG07
Definition ES8156.h:53
error_t standby(void)
Definition ES8156.h:208
static constexpr uint8_t ES8156_ANALOG_SYS1_REG20
Definition ES8156.h:71
static constexpr uint8_t ES8156_VOLUME_CONTROL_REG14
Definition ES8156.h:64
static constexpr uint8_t ES8156_RESET_REG00
Definition ES8156.h:46
error_t getVoiceVolume(int *volume)
Get voice volume (0~100)
Definition ES8156.h:176
static constexpr uint8_t ES8156_MISC_CONTROL3_REG18
Definition ES8156.h:68
static constexpr uint8_t ES8156_CHIPID0_REGFE
Definition ES8156.h:79
bool initialized
Definition ES8156.h:240
static constexpr uint8_t ES8156_MISC_CONTROL2_REG09
Definition ES8156.h:55
Namespace for audio driver components.
Definition AudioBoard.h:5
codec_mode_t
Definition DriverCommon.h:290
int error_t
Definition DriverCommon.h:150
void * i2c_bus_handle_t
Definition DriverCommon.h:155
I2S interface parameters for the audio codec.
Definition DriverCommon.h:384
Top-level codec configuration passed to the audio driver at init.
Definition DriverCommon.h:397
Codec dac volume configurations.
Definition CodecConstants.h:88
uint8_t reg_value
Definition CodecConstants.h:95
int user_volume
Definition CodecConstants.h:96