arduino-audio-driver
Loading...
Searching...
No Matches
ES7243.h
Go to the documentation of this file.
1/*
2 * ESPRESSIF MIT License
3 *
4 * Copyright (c) 2019 <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
27#pragma once
28
29#include <string.h>
30
32#include "DriverCommon.h"
33#include "Platforms/API_Delay.h"
34#include "Platforms/API_I2C.h"
35#include "Platforms/GPIO.h"
36#include "stdbool.h"
37
38
39namespace audio_driver {
40
46class ES7243 {
47 public:
48 static constexpr int MCLK_PULSES_NUMBER = 20;
49 ES7243() = default;
50
52 void setWire(i2c_bus_handle_t handle) { i2c_handle = handle; }
53
55 void setAddress(int addr) { i2c_addr = addr; }
56
58 void setMclkGpio(GpioPin gpio) { mclk_gpio = gpio; }
59
62 error_t ret = RESULT_OK;
64 ret |= writeReg(0x00, 0x01); // slave mode, software mode
65 ret |= writeReg(0x06, 0x00);
66 ret |= writeReg(0x05, 0x1B); // Mute ADC
67 ret |= writeReg(0x01, audio_format); // i2s -16bit
68 ret |= writeReg(0x08, 0x43); // enable AIN, PGA GAIN = 27DB
69 ret |= writeReg(0x05, 0x13); // un Mute ADC
70 if (ret) {
71 AD_LOGE("Es7243 initialize failed!");
72 return RESULT_FAIL;
73 }
74 return ret;
75 }
76
78 error_t deinit(void) { return setVoiceMute(true); }
79
81 error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active) {
82 return setVoiceMute(!ctrl_state_active);
83 }
84
87 // master mode not supported
88 if (iface->mode == MODE_MASTER) {
89 AD_LOGE("configI2S: Mode must be slave");
90 return RESULT_FAIL;
91 }
92
93 // set bits
94 switch (iface->bits) {
96 audio_format |= 0b011 << 2;
97 break;
99 audio_format |= 0b000 << 2;
100 break;
102 audio_format |= 0b100 << 2;
103 break;
104 default:
105 AD_LOGE("configI2S: bits not supported %d", iface->bits);
106 return RESULT_FAIL;
107 }
108
109 switch (iface->fmt) {
110 case I2S_RIGHT:
111 case I2S_NORMAL:
112 audio_format |= 0b00;
113 break;
114 case I2S_LEFT:
115 audio_format |= 0b01;
116 break;
117 case I2S_DSP:
118 audio_format |= 0b11;
119 break;
120 default:
121 AD_LOGE("configI2S: fmt not supported %d", iface->fmt);
122 return RESULT_FAIL;
123 }
124
125 return writeReg(0x01, audio_format);
126 }
127
130 AD_LOGI("setVoiceMute: mute = %d", mute);
131 error_t ret = RESULT_OK;
132 if (mute) {
133 ret = writeReg(0x05, 0x1B);
134 } else {
135 ret = writeReg(0x05, 0x13);
136 }
137 return ret;
138 }
139
142 AD_LOGI("setVoiceVolume: %d", volume);
143 error_t ret = RESULT_OK;
144 if (volume > 100) {
145 volume = 100;
146 }
147 if (volume < 0) {
148 volume = 0;
149 }
150 actual_volume = volume;
151 switch (volume) {
152 case 0 ... 12:
153 ret |= writeReg(0x08, 0x11); // 1db
154 break;
155 case 13 ... 25:
156 ret |= writeReg(0x08, 0x13); // 3.5db
157 break;
158 case 26 ... 38:
159 ret |= writeReg(0x08, 0x21); // 18db
160 break;
161 case 39 ... 51:
162 ret |= writeReg(0x08, 0x23); // 20.5db
163 break;
164 case 52 ... 65:
165 ret |= writeReg(0x08, 0x05); // 22.5db
166 break;
167 case 66 ... 80:
168 ret |= writeReg(0x08, 0x41); // 24.5db
169 break;
170 case 81 ... 90:
171 ret |= writeReg(0x08, 0x07); // 25db
172 break;
173 case 91 ... 100:
174 ret |= writeReg(0x08, 0x43); // 27db
175 break;
176 default:
177 break;
178 }
179 return ret;
180 }
181
183 error_t getVoiceVolume(int* volume) {
184 *volume = actual_volume;
185 return RESULT_OK;
186 }
187 error_t writeReg(uint8_t reg_add, uint8_t data) {
188 return i2c_bus_write_bytes(i2c_handle, i2c_addr, &reg_add, sizeof(reg_add),
189 &data, sizeof(data));
190 }
191
192 /*
193 Before initializing es7243, it is necessary to output
194 mclk to es7243 to activate the I2C configuration.
195 So give some clocks to active es7243.
196 */
198 GPIO gpio;
199 gpio.pinMode(mclk_gpio, OUTPUT);
200 for (int i = 0; i < MCLK_PULSES_NUMBER; ++i) {
201 gpio.digitalWrite(mclk_gpio, 0);
202 delayMs(1);
203 gpio.digitalWrite(mclk_gpio, 1);
204 delayMs(1);
205 }
206 return RESULT_OK;
207 }
208
209 protected:
211 int i2c_addr = 0x13; // 0x26>>1;
214 uint8_t audio_format = 0x0C;
215};
216
217} // namespace audio_driver
#define RESULT_OK
Definition DriverCommon.h:74
#define GPIO_UNDEFINED
Definition DriverCommon.h:131
#define OUTPUT
Definition DriverCommon.h:44
#define RESULT_FAIL
Definition DriverCommon.h:75
Simple GPIO abstraction for digital input/output.
#define AD_LOGI(...)
Definition Logger.h:13
#define AD_LOGE(...)
Definition Logger.h:15
Header-only driver class for the ES7243 ADC chip.
Definition ES7243.h:46
error_t writeReg(uint8_t reg_add, uint8_t data)
Definition ES7243.h:187
error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active)
Control ES7243 adc chip.
Definition ES7243.h:81
void setMclkGpio(GpioPin gpio)
Defines the GPIO pin used to provide the MCLK signal.
Definition ES7243.h:58
void setAddress(int addr)
Defines the I2C device address.
Definition ES7243.h:55
error_t deinit(void)
Deinitialize ES7243 adc chip.
Definition ES7243.h:78
GpioPin mclk_gpio
Definition ES7243.h:212
int i2c_addr
Definition ES7243.h:211
error_t configI2S(codec_mode_t mode, I2SDefinition *iface)
Configure ES7243 adc mode and I2S interface.
Definition ES7243.h:86
error_t setVoiceMute(bool mute)
Set mute.
Definition ES7243.h:129
void setWire(i2c_bus_handle_t handle)
Defines the I2C bus instance to be used.
Definition ES7243.h:52
int actual_volume
Definition ES7243.h:213
i2c_bus_handle_t i2c_handle
Definition ES7243.h:210
error_t mclkActive(GpioPin mclk_gpio)
Definition ES7243.h:197
uint8_t audio_format
Definition ES7243.h:214
static constexpr int MCLK_PULSES_NUMBER
Definition ES7243.h:48
error_t setVoiceVolume(int volume)
Set adc gain (value of gain 0~100)
Definition ES7243.h:141
error_t getVoiceVolume(int *volume)
Get adc gain (value of gain 0~100)
Definition ES7243.h:183
error_t init(codec_config_t *codec_cfg)
Initialize ES7243 adc chip.
Definition ES7243.h:61
Definition GPIO_Zephyr.h:20
bool digitalWrite(GpioPin pin, bool value)
Definition GPIO_Zephyr.h:59
void pinMode(GpioPin pin, int mode)
Definition GPIO_Zephyr.h:28
Namespace for audio driver components.
Definition AudioBoard.h:5
int16_t GpioPin
Definition DriverCommon.h:153
void delayMs(unsigned long ms)
Definition API_Delay.h:19
@ BIT_LENGTH_24BITS
Definition DriverCommon.h:240
@ BIT_LENGTH_32BITS
Definition DriverCommon.h:241
@ BIT_LENGTH_16BITS
Definition DriverCommon.h:237
codec_mode_t
Definition DriverCommon.h:290
int error_t
Definition DriverCommon.h:150
@ I2S_RIGHT
Definition DriverCommon.h:253
@ I2S_DSP
Definition DriverCommon.h:254
@ I2S_LEFT
Definition DriverCommon.h:252
@ I2S_NORMAL
Definition DriverCommon.h:251
@ MODE_MASTER
Definition DriverCommon.h:205
void * i2c_bus_handle_t
Definition DriverCommon.h:155
I2S interface parameters for the audio codec.
Definition DriverCommon.h:384
i2s_format_t fmt
Definition DriverCommon.h:386
i2s_master_slave_t mode
Definition DriverCommon.h:385
sample_bits_t bits
Definition DriverCommon.h:388
Top-level codec configuration passed to the audio driver at init.
Definition DriverCommon.h:397