|
3 | 3 | * |
4 | 4 | * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> |
5 | 5 | * |
6 | | - * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, |
7 | | - * it is free of charge, to any person obtaining a copy of this software and associated |
8 | | - * documentation files (the "Software"), to deal in the Software without restriction, including |
9 | | - * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 | | - * and/or sell copies of the Software, and to permit persons to whom the Software is furnished |
11 | | - * to do so, subject to the following conditions: |
| 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: |
12 | 13 | * |
13 | | - * The above copyright notice and this permission notice shall be included in all copies or |
14 | | - * substantial portions of the Software. |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
15 | 16 | * |
16 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
18 | | - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
19 | | - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
20 | | - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
21 | | - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 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. |
22 | 24 | * |
23 | 25 | */ |
24 | 26 |
|
25 | | -#include <string.h> |
26 | 27 | #include "es7243.h" |
27 | | -#include "Utils/etc.h" |
| 28 | + |
| 29 | +#include <string.h> |
| 30 | + |
28 | 31 | #include "Utils/etc.h" |
29 | 32 |
|
30 | | -#define MCLK_PULSES_NUMBER (20) |
| 33 | +#define MCLK_PULSES_NUMBER (20) |
31 | 34 | #define ES_ASSERT(a, format, b, ...) \ |
32 | | - if ((a) != 0) { \ |
33 | | - AD_LOGE( format, ##__VA_ARGS__); \ |
34 | | - return b;\ |
35 | | - } |
| 35 | + if ((a) != 0) { \ |
| 36 | + AD_LOGE(format, ##__VA_ARGS__); \ |
| 37 | + return b; \ |
| 38 | + } |
36 | 39 |
|
37 | 40 | static i2c_bus_handle_t i2c_handle = NULL; |
38 | | -static int es7243_addr = 0x13; // 0x26>>1; |
| 41 | +static int es7243_addr = 0x13; // 0x26>>1; |
39 | 42 | static int mclk_gpio = 0; |
40 | 43 | static int actual_volume = 0; |
41 | 44 | static uint8_t audio_format = 0x0C; |
42 | 45 |
|
43 | | -void es7243_mclk_gpio(int gpio){ |
44 | | - mclk_gpio = gpio; |
45 | | -} |
| 46 | +void es7243_mclk_gpio(int gpio) { mclk_gpio = gpio; } |
46 | 47 |
|
47 | | -static error_t es7243_write_reg(uint8_t reg_add, uint8_t data) |
48 | | -{ |
49 | | - return i2c_bus_write_bytes(i2c_handle, es7243_addr, ®_add, sizeof(reg_add), &data, sizeof(data)); |
| 48 | +static error_t es7243_write_reg(uint8_t reg_add, uint8_t data) { |
| 49 | + return i2c_bus_write_bytes(i2c_handle, es7243_addr, ®_add, sizeof(reg_add), |
| 50 | + &data, sizeof(data)); |
50 | 51 | } |
51 | 52 |
|
52 | | - |
53 | | -error_t es7243_adc_set_addr(int addr) |
54 | | -{ |
55 | | - es7243_addr = addr; |
56 | | - return RESULT_OK; |
| 53 | +error_t es7243_adc_set_addr(int addr) { |
| 54 | + es7243_addr = addr; |
| 55 | + return RESULT_OK; |
57 | 56 | } |
58 | 57 |
|
59 | | - |
60 | | -static error_t es7243_mclk_active(uint8_t mclk_gpio) |
61 | | -{ |
62 | | - /* |
63 | | - Before initializing es7243, it is necessary to output |
64 | | - mclk to es7243 to activate the I2C configuration. |
65 | | - So give some clocks to active es7243. |
66 | | - */ |
67 | | - pinMode(mclk_gpio, OUTPUT); |
68 | | - for (int i = 0; i < MCLK_PULSES_NUMBER; ++i) { |
69 | | - digitalWrite(mclk_gpio, 0); |
70 | | - delay(1); |
71 | | - digitalWrite(mclk_gpio, 1); |
72 | | - delay(1); |
73 | | - } |
74 | | - return RESULT_OK; |
| 58 | +static error_t es7243_mclk_active(uint8_t mclk_gpio) { |
| 59 | + /* |
| 60 | + Before initializing es7243, it is necessary to output |
| 61 | + mclk to es7243 to activate the I2C configuration. |
| 62 | + So give some clocks to active es7243. |
| 63 | + */ |
| 64 | + pinMode(mclk_gpio, OUTPUT); |
| 65 | + for (int i = 0; i < MCLK_PULSES_NUMBER; ++i) { |
| 66 | + digitalWrite(mclk_gpio, 0); |
| 67 | + delay(1); |
| 68 | + digitalWrite(mclk_gpio, 1); |
| 69 | + delay(1); |
| 70 | + } |
| 71 | + return RESULT_OK; |
75 | 72 | } |
76 | 73 |
|
77 | | -error_t es7243_adc_init(codec_config_t *codec_cfg, i2c_bus_handle_t handle) |
78 | | -{ |
79 | | - error_t ret = RESULT_OK; |
80 | | - i2c_handle = handle; |
81 | | - es7243_mclk_active(mclk_gpio); |
82 | | - ret |= es7243_write_reg(0x00, 0x01); // slave mode, software mode |
83 | | - ret |= es7243_write_reg(0x06, 0x00); |
84 | | - ret |= es7243_write_reg(0x05, 0x1B); // Mute ADC |
85 | | - ret |= es7243_write_reg(0x01, audio_format); // i2s -16bit |
86 | | - ret |= es7243_write_reg(0x08, 0x43); // enable AIN, PGA GAIN = 27DB |
87 | | - ret |= es7243_write_reg(0x05, 0x13); // un Mute ADC |
88 | | - if (ret) { |
89 | | - AD_LOGE( "Es7243 initialize failed!"); |
90 | | - return RESULT_FAIL; |
91 | | - } |
92 | | - return ret; |
| 74 | +error_t es7243_adc_init(codec_config_t *codec_cfg, i2c_bus_handle_t handle) { |
| 75 | + error_t ret = RESULT_OK; |
| 76 | + i2c_handle = handle; |
| 77 | + es7243_mclk_active(mclk_gpio); |
| 78 | + ret |= es7243_write_reg(0x00, 0x01); // slave mode, software mode |
| 79 | + ret |= es7243_write_reg(0x06, 0x00); |
| 80 | + ret |= es7243_write_reg(0x05, 0x1B); // Mute ADC |
| 81 | + ret |= es7243_write_reg(0x01, audio_format); // i2s -16bit |
| 82 | + ret |= es7243_write_reg(0x08, 0x43); // enable AIN, PGA GAIN = 27DB |
| 83 | + ret |= es7243_write_reg(0x05, 0x13); // un Mute ADC |
| 84 | + if (ret) { |
| 85 | + AD_LOGE("Es7243 initialize failed!"); |
| 86 | + return RESULT_FAIL; |
| 87 | + } |
| 88 | + return ret; |
93 | 89 | } |
94 | 90 |
|
95 | | -error_t es7243_adc_deinit(void) |
96 | | -{ |
97 | | - return es7243_adc_set_voice_mute(true); |
98 | | -} |
| 91 | +error_t es7243_adc_deinit(void) { return es7243_adc_set_voice_mute(true); } |
99 | 92 |
|
100 | | -error_t es7243_adc_ctrl_state_active(codec_mode_t mode, bool ctrl_state_active) |
101 | | -{ |
102 | | - return es7243_adc_set_voice_mute(!ctrl_state_active); |
| 93 | +error_t es7243_adc_ctrl_state_active(codec_mode_t mode, |
| 94 | + bool ctrl_state_active) { |
| 95 | + return es7243_adc_set_voice_mute(!ctrl_state_active); |
103 | 96 | } |
104 | 97 |
|
105 | | -error_t es7243_adc_config_i2s(codec_mode_t mode, I2SDefinition *iface) |
106 | | -{ |
107 | | - // master mode not supported |
108 | | - if (iface->mode == MODE_MASTER){ |
109 | | - return RESULT_FAIL; |
110 | | - } |
111 | | - |
112 | | - // set bits |
113 | | - switch(iface->bits){ |
114 | | - case 16: |
115 | | - audio_format |= 0b011 < 2; |
116 | | - break; |
117 | | - case 24: |
118 | | - audio_format |= 0b000 < 2; |
119 | | - break; |
120 | | - case 32: |
121 | | - audio_format |= 0b100 < 2; |
122 | | - break; |
123 | | - default: |
124 | | - return RESULT_FAIL; |
125 | | - } |
126 | | - |
127 | | - switch(iface->fmt){ |
128 | | - case I2S_RIGHT: |
129 | | - case I2S_NORMAL: |
130 | | - audio_format |= 0b00; |
131 | | - break; |
132 | | - case I2S_LEFT: |
133 | | - audio_format |= 0b01; |
134 | | - break; |
135 | | - case I2S_DSP: |
136 | | - audio_format |= 0b11; |
137 | | - break; |
138 | | - default: |
139 | | - return RESULT_FAIL; |
140 | | - } |
141 | | - |
142 | | - error_t ret = es7243_write_reg(0x01, audio_format); |
143 | | - return ret; |
| 98 | +error_t es7243_adc_config_i2s(codec_mode_t mode, I2SDefinition *iface) { |
| 99 | + // master mode not supported |
| 100 | + if (iface->mode == MODE_MASTER) { |
| 101 | + AD_LOGE("es7243_adc_config_i2s: Mode must be slave"); |
| 102 | + return RESULT_FAIL; |
| 103 | + } |
| 104 | + |
| 105 | + // set bits |
| 106 | + switch (iface->bits) { |
| 107 | + case BIT_LENGTH_16BITS: |
| 108 | + audio_format |= 0b011 < 2; |
| 109 | + break; |
| 110 | + case BIT_LENGTH_24BITS: |
| 111 | + audio_format |= 0b000 < 2; |
| 112 | + break; |
| 113 | + case BIT_LENGTH_32BITS: |
| 114 | + audio_format |= 0b100 < 2; |
| 115 | + break; |
| 116 | + default: |
| 117 | + AD_LOGE("es7243_adc_config_i2s: bits not supported %d", iface->bits); |
| 118 | + return RESULT_FAIL; |
| 119 | + } |
| 120 | + |
| 121 | + switch (iface->fmt) { |
| 122 | + case I2S_RIGHT: |
| 123 | + case I2S_NORMAL: |
| 124 | + audio_format |= 0b00; |
| 125 | + break; |
| 126 | + case I2S_LEFT: |
| 127 | + audio_format |= 0b01; |
| 128 | + break; |
| 129 | + case I2S_DSP: |
| 130 | + audio_format |= 0b11; |
| 131 | + break; |
| 132 | + default: |
| 133 | + AD_LOGE("es7243_adc_config_i2s: mft not supported %d", iface->fmt); |
| 134 | + return RESULT_FAIL; |
| 135 | + } |
| 136 | + |
| 137 | + error_t ret = es7243_write_reg(0x01, audio_format); |
| 138 | + return ret; |
144 | 139 | } |
145 | 140 |
|
146 | | -error_t es7243_adc_set_voice_mute(bool mute) |
147 | | -{ |
148 | | - AD_LOGI( "Enter into es7243_mute(), mute = %d", mute); |
149 | | - if (mute) { |
150 | | - es7243_write_reg(0x05, 0x1B); |
151 | | - } else { |
152 | | - es7243_write_reg(0x05, 0x13); |
153 | | - } |
154 | | - return RESULT_OK; |
| 141 | +error_t es7243_adc_set_voice_mute(bool mute) { |
| 142 | + AD_LOGI("es7243_adc_set_voice_mute es7243_mute(), mute = %d", mute); |
| 143 | + if (mute) { |
| 144 | + es7243_write_reg(0x05, 0x1B); |
| 145 | + } else { |
| 146 | + es7243_write_reg(0x05, 0x13); |
| 147 | + } |
| 148 | + return RESULT_OK; |
155 | 149 | } |
156 | 150 |
|
157 | | -error_t es7243_adc_set_voice_volume(int volume) |
158 | | -{ |
159 | | - error_t ret = RESULT_OK; |
160 | | - if (volume > 100) { |
161 | | - volume = 100; |
162 | | - } |
163 | | - if (volume < 0) { |
164 | | - volume = 0; |
165 | | - } |
166 | | - actual_volume = volume; |
167 | | - switch (volume) { |
168 | | - case 0 ... 12: |
169 | | - ret |= es7243_write_reg(0x08, 0x11); // 1db |
170 | | - break; |
171 | | - case 13 ... 25: |
172 | | - ret |= es7243_write_reg(0x08, 0x13); //3.5db |
173 | | - break; |
174 | | - case 26 ... 38: |
175 | | - ret |= es7243_write_reg(0x08, 0x21); //18db |
176 | | - break; |
177 | | - case 39 ... 51: |
178 | | - ret |= es7243_write_reg(0x08, 0x23); //20.5db |
179 | | - break; |
180 | | - case 52 ... 65: |
181 | | - ret |= es7243_write_reg(0x08, 0x05); //22.5db |
182 | | - break; |
183 | | - case 66 ... 80: |
184 | | - ret |= es7243_write_reg(0x08, 0x41); //24.5db |
185 | | - break; |
186 | | - case 81 ... 90: |
187 | | - ret |= es7243_write_reg(0x08, 0x07); //25db |
188 | | - break; |
189 | | - case 91 ... 100: |
190 | | - ret |= es7243_write_reg(0x08, 0x43); //27db |
191 | | - break; |
192 | | - default: |
193 | | - break; |
194 | | - } |
195 | | - return RESULT_OK; |
| 151 | +error_t es7243_adc_set_voice_volume(int volume) { |
| 152 | + error_t ret = RESULT_OK; |
| 153 | + if (volume > 100) { |
| 154 | + volume = 100; |
| 155 | + } |
| 156 | + if (volume < 0) { |
| 157 | + volume = 0; |
| 158 | + } |
| 159 | + actual_volume = volume; |
| 160 | + switch (volume) { |
| 161 | + case 0 ... 12: |
| 162 | + ret |= es7243_write_reg(0x08, 0x11); // 1db |
| 163 | + break; |
| 164 | + case 13 ... 25: |
| 165 | + ret |= es7243_write_reg(0x08, 0x13); // 3.5db |
| 166 | + break; |
| 167 | + case 26 ... 38: |
| 168 | + ret |= es7243_write_reg(0x08, 0x21); // 18db |
| 169 | + break; |
| 170 | + case 39 ... 51: |
| 171 | + ret |= es7243_write_reg(0x08, 0x23); // 20.5db |
| 172 | + break; |
| 173 | + case 52 ... 65: |
| 174 | + ret |= es7243_write_reg(0x08, 0x05); // 22.5db |
| 175 | + break; |
| 176 | + case 66 ... 80: |
| 177 | + ret |= es7243_write_reg(0x08, 0x41); // 24.5db |
| 178 | + break; |
| 179 | + case 81 ... 90: |
| 180 | + ret |= es7243_write_reg(0x08, 0x07); // 25db |
| 181 | + break; |
| 182 | + case 91 ... 100: |
| 183 | + ret |= es7243_write_reg(0x08, 0x43); // 27db |
| 184 | + break; |
| 185 | + default: |
| 186 | + break; |
| 187 | + } |
| 188 | + return RESULT_OK; |
196 | 189 | } |
197 | 190 |
|
198 | | -error_t es7243_adc_get_voice_volume(int *volume) |
199 | | -{ |
200 | | - *volume = actual_volume; |
201 | | - return RESULT_OK; |
| 191 | +error_t es7243_adc_get_voice_volume(int *volume) { |
| 192 | + *volume = actual_volume; |
| 193 | + return RESULT_OK; |
202 | 194 | } |
0 commit comments