1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string.h>
16 #include <stdbool.h>
17 #include <math.h>
18 #include <esp_types.h>
19
20 #include "esp_osal/esp_osal.h"
21 #include "esp_osal/queue.h"
22 #include "esp_osal/semphr.h"
23
24 #include "soc/lldesc.h"
25 #include "driver/gpio.h"
26 #include "driver/i2s.h"
27 #include "hal/gpio_hal.h"
28 #if SOC_I2S_SUPPORTS_ADC_DAC
29 #include "driver/dac.h"
30 #include "hal/i2s_hal.h"
31 #include "adc1_private.h"
32 #endif
33
34 #include "soc/rtc.h"
35
36 #include "esp_intr_alloc.h"
37 #include "esp_err.h"
38 #include "esp_attr.h"
39 #include "esp_log.h"
40 #include "esp_pm.h"
41 #include "esp_efuse.h"
42 #include "esp_rom_gpio.h"
43
44 #include "sdkconfig.h"
45
46 static const char* I2S_TAG = "I2S";
47
48 #define I2S_CHECK(a, str, ret) if (!(a)) { \
49 ESP_LOGE(I2S_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
50 return (ret); \
51 }
52
53 #define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
54 #define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
55 #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
56 #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
57 #define I2S_FULL_DUPLEX_SLAVE_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_SLAVE)
58 #define I2S_FULL_DUPLEX_MASTER_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_MASTER)
59
60 //TODO: Refactor to put this logic into LL
61 #define I2S_AD_BCK_FACTOR (2)
62 #define I2S_PDM_BCK_FACTOR (64)
63 #define I2S_BASE_CLK (2*APB_CLK_FREQ)
64
65 /**
66 * @brief DMA buffer object
67 *
68 */
69 typedef struct {
70 char **buf;
71 int buf_size;
72 int rw_pos;
73 void *curr_ptr;
74 SemaphoreHandle_t mux;
75 xQueueHandle queue;
76 lldesc_t **desc;
77 } i2s_dma_t;
78
79 /**
80 * @brief I2S object instance
81 *
82 */
83 typedef struct {
84 i2s_port_t i2s_num; /*!< I2S port number*/
85 int queue_size; /*!< I2S event queue size*/
86 QueueHandle_t i2s_queue; /*!< I2S queue handler*/
87 int dma_buf_count; /*!< DMA buffer count, number of buffer*/
88 int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
89 i2s_dma_t *rx; /*!< DMA Tx buffer*/
90 i2s_dma_t *tx; /*!< DMA Rx buffer*/
91 i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
92 int channel_num; /*!< Number of channels*/
93 int bytes_per_sample; /*!< Bytes per sample*/
94 int bits_per_sample; /*!< Bits per sample*/
95 i2s_mode_t mode; /*!< I2S Working mode*/
96 uint32_t sample_rate; /*!< I2S sample rate */
97 bool use_apll; /*!< I2S use APLL clock */
98 bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
99 int fixed_mclk; /*!< I2S fixed MLCK clock */
100 double real_rate;
101 #ifdef CONFIG_PM_ENABLE
102 esp_pm_lock_handle_t pm_lock;
103 #endif
104 i2s_hal_context_t hal; /*!< I2S hal context*/
105 } i2s_obj_t;
106
107 static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
108
109 static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
110 #if SOC_I2S_SUPPORTS_ADC_DAC
111 static int _i2s_adc_unit = -1;
112 static int _i2s_adc_channel = -1;
113 #endif
114
115 static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
116 static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
117
gpio_matrix_out_check(int gpio,uint32_t signal_idx,bool out_inv,bool oen_inv)118 static inline void gpio_matrix_out_check(int gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
119 {
120 //if pin = -1, do not need to configure
121 if (gpio != -1) {
122 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
123 gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
124 esp_rom_gpio_connect_out_signal(gpio, signal_idx, out_inv, oen_inv);
125 }
126 }
127
gpio_matrix_in_check(int gpio,uint32_t signal_idx,bool inv)128 static inline void gpio_matrix_in_check(int gpio, uint32_t signal_idx, bool inv)
129 {
130 if (gpio != -1) {
131 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
132 //Set direction, for some GPIOs, the input function are not enabled as default.
133 gpio_set_direction(gpio, GPIO_MODE_INPUT);
134 esp_rom_gpio_connect_in_signal(gpio, signal_idx, inv);
135 }
136 }
137
i2s_clear_intr_status(i2s_port_t i2s_num,uint32_t clr_mask)138 esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
139 {
140 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
141 i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
142 return ESP_OK;
143 }
144
i2s_enable_rx_intr(i2s_port_t i2s_num)145 esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
146 {
147
148 I2S_ENTER_CRITICAL();
149 i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
150 I2S_EXIT_CRITICAL();
151 return ESP_OK;
152 }
153
i2s_disable_rx_intr(i2s_port_t i2s_num)154 esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
155 {
156 I2S_ENTER_CRITICAL();
157 i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
158 I2S_EXIT_CRITICAL();
159 return ESP_OK;
160 }
161
i2s_disable_tx_intr(i2s_port_t i2s_num)162 esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
163 {
164 I2S_ENTER_CRITICAL();
165 i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
166 I2S_EXIT_CRITICAL();
167 return ESP_OK;
168 }
169
i2s_enable_tx_intr(i2s_port_t i2s_num)170 esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
171 {
172 I2S_ENTER_CRITICAL();
173 i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
174 I2S_EXIT_CRITICAL();
175 return ESP_OK;
176 }
177
i2s_get_clk(i2s_port_t i2s_num)178 float i2s_get_clk(i2s_port_t i2s_num)
179 {
180 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
181 return p_i2s_obj[i2s_num]->real_rate;
182 }
183
i2s_isr_register(i2s_port_t i2s_num,int intr_alloc_flags,void (* fn)(void *),void * arg,i2s_isr_handle_t * handle)184 static esp_err_t i2s_isr_register(i2s_port_t i2s_num, int intr_alloc_flags, void (*fn)(void*), void * arg, i2s_isr_handle_t *handle)
185 {
186 return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
187 }
188
i2s_apll_get_fi2s(int bits_per_sample,int sdm0,int sdm1,int sdm2,int odir)189 static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
190 {
191 int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
192
193 #if CONFIG_IDF_TARGET_ESP32
194 /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
195 if (esp_efuse_get_chip_ver() == 0) {
196 sdm0 = 0;
197 sdm1 = 0;
198 }
199 #endif
200 float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
201 if (fout < SOC_I2S_APLL_MIN_FREQ || fout > SOC_I2S_APLL_MAX_FREQ) {
202 return SOC_I2S_APLL_MAX_FREQ;
203 }
204 float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
205 return fpll/2;
206 }
207
208 /**
209 * @brief APLL calculate function, was described by following:
210 * APLL Output frequency is given by the formula:
211 *
212 * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
213 * apll_freq = fout / ((o_div + 2) * 2)
214 *
215 * The dividend in this expression should be in the range of 240 - 600 MHz.
216 * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
217 * * sdm0 frequency adjustment parameter, 0..255
218 * * sdm1 frequency adjustment parameter, 0..255
219 * * sdm2 frequency adjustment parameter, 0..63
220 * * o_div frequency divider, 0..31
221 *
222 * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
223 * then apply the above formula, finding the closest frequency to the desired one.
224 * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
225 * 1. We will choose the parameters with the highest level of change,
226 * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
227 * Take average frequency close to the desired frequency, and select sdm2
228 * 2. Next, we look for sequences of less influential and more detailed parameters,
229 * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
230 * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
231 *
232 * @param[in] rate The I2S Frequency (MCLK)
233 * @param[in] bits_per_sample The bits per sample
234 * @param[out] sdm0 The sdm 0
235 * @param[out] sdm1 The sdm 1
236 * @param[out] sdm2 The sdm 2
237 * @param[out] odir The odir
238 *
239 * @return ESP_ERR_INVALID_ARG or ESP_OK
240 */
241
i2s_apll_calculate_fi2s(int rate,int bits_per_sample,int * sdm0,int * sdm1,int * sdm2,int * odir)242 static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
243 {
244 int _odir, _sdm0, _sdm1, _sdm2;
245 float avg;
246 float min_rate, max_rate, min_diff;
247 if (rate/bits_per_sample/2/8 < SOC_I2S_APLL_MIN_RATE) {
248 return ESP_ERR_INVALID_ARG;
249 }
250
251 *sdm0 = 0;
252 *sdm1 = 0;
253 *sdm2 = 0;
254 *odir = 0;
255 min_diff = SOC_I2S_APLL_MAX_FREQ;
256
257 for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
258 max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
259 min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
260 avg = (max_rate + min_rate)/2;
261 if (abs(avg - rate) < min_diff) {
262 min_diff = abs(avg - rate);
263 *sdm2 = _sdm2;
264 }
265 }
266 min_diff = SOC_I2S_APLL_MAX_FREQ;
267 for (_odir = 0; _odir < 32; _odir ++) {
268 max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
269 min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
270 avg = (max_rate + min_rate)/2;
271 if (abs(avg - rate) < min_diff) {
272 min_diff = abs(avg - rate);
273 *odir = _odir;
274 }
275 }
276 min_diff = SOC_I2S_APLL_MAX_FREQ;
277 for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
278 max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
279 min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
280 avg = (max_rate + min_rate)/2;
281 if (abs(avg - rate) < min_diff) {
282 min_diff = abs(avg - rate);
283 *sdm2 = _sdm2;
284 }
285 }
286
287 min_diff = SOC_I2S_APLL_MAX_FREQ;
288 for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
289 max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
290 min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
291 avg = (max_rate + min_rate)/2;
292 if (abs(avg - rate) < min_diff) {
293 min_diff = abs(avg - rate);
294 *sdm1 = _sdm1;
295 }
296 }
297
298 min_diff = SOC_I2S_APLL_MAX_FREQ;
299 for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
300 avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
301 if (abs(avg - rate) < min_diff) {
302 min_diff = abs(avg - rate);
303 *sdm0 = _sdm0;
304 }
305 }
306
307 return ESP_OK;
308 }
309
i2s_set_clk(i2s_port_t i2s_num,uint32_t rate,i2s_bits_per_sample_t bits,i2s_channel_t ch)310 esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
311 {
312 int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
313 int clkmInteger, clkmDecimals, bck = 0;
314 double denom = (double)1 / 64;
315 int channel = 2;
316 i2s_dma_t *save_tx = NULL, *save_rx = NULL;
317
318 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
319
320 if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
321 ESP_LOGE(I2S_TAG, "Invalid bits per sample");
322 return ESP_ERR_INVALID_ARG;
323 }
324
325 if (p_i2s_obj[i2s_num] == NULL) {
326 ESP_LOGE(I2S_TAG, "Not initialized yet");
327 return ESP_ERR_INVALID_ARG;
328 }
329 p_i2s_obj[i2s_num]->sample_rate = rate;
330 double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
331
332 if (clkmdiv > 256) {
333 ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
334 return ESP_ERR_INVALID_ARG;
335 }
336
337 // wait all on-going writing finish
338 if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
339 xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
340 }
341 if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
342 xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
343 }
344
345 i2s_stop(i2s_num);
346 #if SOC_I2S_SUPPORTS_ADC_DAC
347 /* I2S-ADC only support single channel format. */
348 if (!(p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN)) {
349 i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
350 }
351 #else
352 i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
353 #endif
354 i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
355
356 if (p_i2s_obj[i2s_num]->channel_num != (int)ch) {
357 p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
358 }
359
360 if ((int)bits != p_i2s_obj[i2s_num]->bits_per_sample) {
361 p_i2s_obj[i2s_num]->bits_per_sample = bits;
362
363 // Round bytes_per_sample up to next multiple of 16 bits
364 int halfwords_per_sample = (bits + 15) / 16;
365 p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
366
367 // Because limited of DMA buffer is 4092 bytes
368 if (p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num > 4092) {
369 p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
370 }
371 // Re-create TX DMA buffer
372 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
373
374 save_tx = p_i2s_obj[i2s_num]->tx;
375
376 p_i2s_obj[i2s_num]->tx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
377 if (p_i2s_obj[i2s_num]->tx == NULL) {
378 ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
379 i2s_driver_uninstall(i2s_num);
380 return ESP_ERR_NO_MEM;
381 }
382 i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
383
384 //destroy old tx dma if exist
385 if (save_tx) {
386 i2s_destroy_dma_queue(i2s_num, save_tx);
387 }
388 }
389 // Re-create RX DMA buffer
390 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
391
392 save_rx = p_i2s_obj[i2s_num]->rx;
393
394 p_i2s_obj[i2s_num]->rx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
395 if (p_i2s_obj[i2s_num]->rx == NULL){
396 ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
397 i2s_driver_uninstall(i2s_num);
398 return ESP_ERR_NO_MEM;
399 }
400 i2s_hal_set_in_link(&(p_i2s_obj[i2s_num]->hal), p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample, (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0]);
401 //destroy old rx dma if exist
402 if (save_rx) {
403 i2s_destroy_dma_queue(i2s_num, save_rx);
404 }
405 }
406
407 }
408
409 double mclk;
410 int sdm0, sdm1, sdm2, odir, m_scale = 8;
411 int fi2s_clk = rate*channel*bits*m_scale;
412 #if SOC_I2S_SUPPORTS_ADC_DAC
413 if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
414
415 //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
416 //Rate as given to this function is the intended sample rate;
417 //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
418 uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
419 fi2s_clk /= I2S_AD_BCK_FACTOR;
420 int factor2 = 60;
421 mclk = b_clk * factor2;
422 clkmdiv = ((double) I2S_BASE_CLK) / mclk;
423 clkmInteger = clkmdiv;
424 clkmDecimals = (clkmdiv - clkmInteger) / denom;
425 bck = mclk / b_clk;
426 #endif
427 #if SOC_I2S_SUPPORTS_PDM
428 } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
429 uint32_t b_clk = 0;
430 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
431 uint32_t fp, fs;
432 i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
433 // Recommended set `fp = 960, fs = sample_rate / 100`
434 fs = rate / 100;
435 i2s_hal_tx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), fp, fs);
436 b_clk = rate * I2S_PDM_BCK_FACTOR * fp / fs;
437
438 } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
439 uint32_t dsr;
440 i2s_hal_get_rx_pdm(&(p_i2s_obj[i2s_num]->hal), &dsr);
441 b_clk = rate * I2S_PDM_BCK_FACTOR * (dsr ? 2 : 1);
442 }
443 fi2s_clk = b_clk * m_scale;
444 int factor2 = 5 ;
445 mclk = b_clk * factor2;
446 clkmdiv = ((double) I2S_BASE_CLK) / mclk;
447 clkmInteger = clkmdiv;
448 clkmDecimals = (clkmdiv - clkmInteger) / denom;
449 bck = mclk / b_clk;
450 } else
451 #endif
452 {
453 clkmInteger = clkmdiv;
454 clkmDecimals = (clkmdiv - clkmInteger) / denom;
455 mclk = clkmInteger + denom * clkmDecimals;
456 bck = factor/(bits * channel);
457 }
458
459 if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
460 fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
461 m_scale = fi2s_clk/bits/rate/channel;
462 }
463 if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
464 ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
465 rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
466 i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
467 i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
468 double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
469 p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
470 ESP_LOGI(I2S_TAG, "APLL: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK_M: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
471 rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
472 } else {
473 i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
474 i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
475 double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
476 p_i2s_obj[i2s_num]->real_rate = real_rate;
477 ESP_LOGI(I2S_TAG, "PLL_D2: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
478 rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
479 }
480 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
481 p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
482 p_i2s_obj[i2s_num]->tx->rw_pos = 0;
483 }
484 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
485 p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
486 p_i2s_obj[i2s_num]->rx->rw_pos = 0;
487 }
488
489 i2s_hal_set_tx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
490 i2s_hal_set_rx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
491
492 // wait all writing on-going finish
493 if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
494 xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
495 }
496 if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
497 xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
498 }
499 i2s_start(i2s_num);
500 return ESP_OK;
501 }
502
i2s_intr_handler_default(void * arg)503 static void IRAM_ATTR i2s_intr_handler_default(void *arg)
504 {
505 i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
506 uint32_t status;
507 i2s_hal_get_intr_status(&(p_i2s->hal), &status);
508 if(status == 0) {
509 //Avoid spurious interrupt
510 return;
511 }
512
513 i2s_event_t i2s_event;
514 int dummy;
515
516 portBASE_TYPE high_priority_task_awoken = 0;
517
518 lldesc_t *finish_desc = NULL;
519
520 if ((status & I2S_INTR_OUT_DSCR_ERR) || (status & I2S_INTR_IN_DSCR_ERR)) {
521 ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", status);
522 if (p_i2s->i2s_queue) {
523 i2s_event.type = I2S_EVENT_DMA_ERROR;
524 if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
525 xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
526 }
527 xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
528 }
529 }
530
531 if ((status & I2S_INTR_OUT_EOF) && p_i2s->tx) {
532 i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
533 // All buffers are empty. This means we have an underflow on our hands.
534 if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
535 xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
536 // See if tx descriptor needs to be auto cleared:
537 // This will avoid any kind of noise that may get introduced due to transmission
538 // of previous data from tx descriptor on I2S line.
539 if (p_i2s->tx_desc_auto_clear == true) {
540 memset((void *) dummy, 0, p_i2s->tx->buf_size);
541 }
542 }
543 xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
544 if (p_i2s->i2s_queue) {
545 i2s_event.type = I2S_EVENT_TX_DONE;
546 if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
547 xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
548 }
549 xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
550 }
551 }
552
553 if ((status & I2S_INTR_IN_SUC_EOF) && p_i2s->rx) {
554 // All buffers are full. This means we have an overflow.
555 i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
556 if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
557 xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
558 }
559 xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
560 if (p_i2s->i2s_queue) {
561 i2s_event.type = I2S_EVENT_RX_DONE;
562 if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
563 xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
564 }
565 xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
566 }
567 }
568 i2s_hal_clear_intr_status(&(p_i2s->hal), status);
569
570 if (high_priority_task_awoken == pdTRUE) {
571 portYIELD_FROM_ISR();
572 }
573 }
574
i2s_destroy_dma_queue(i2s_port_t i2s_num,i2s_dma_t * dma)575 static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
576 {
577 int bux_idx;
578 if (p_i2s_obj[i2s_num] == NULL) {
579 ESP_LOGE(I2S_TAG, "Not initialized yet");
580 return ESP_ERR_INVALID_ARG;
581 }
582 if (dma == NULL) {
583 ESP_LOGE(I2S_TAG, "dma is NULL");
584 return ESP_ERR_INVALID_ARG;
585 }
586 for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
587 if (dma->desc && dma->desc[bux_idx]) {
588 free(dma->desc[bux_idx]);
589 }
590 if (dma->buf && dma->buf[bux_idx]) {
591 free(dma->buf[bux_idx]);
592 }
593 }
594 if (dma->buf) {
595 free(dma->buf);
596 }
597 if (dma->desc) {
598 free(dma->desc);
599 }
600 vQueueDelete(dma->queue);
601 vSemaphoreDelete(dma->mux);
602 free(dma);
603 return ESP_OK;
604 }
605
i2s_create_dma_queue(i2s_port_t i2s_num,int dma_buf_count,int dma_buf_len)606 static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
607 {
608 int bux_idx;
609 int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
610 i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
611 if (dma == NULL) {
612 ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
613 return NULL;
614 }
615 memset(dma, 0, sizeof(i2s_dma_t));
616
617 dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
618 if (dma->buf == NULL) {
619 ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
620 free(dma);
621 return NULL;
622 }
623 memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
624
625 for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
626 dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
627 if (dma->buf[bux_idx] == NULL) {
628 ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
629 i2s_destroy_dma_queue(i2s_num, dma);
630 return NULL;
631 }
632 ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
633 }
634
635 dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
636 if (dma->desc == NULL) {
637 ESP_LOGE(I2S_TAG, "Error malloc dma description");
638 i2s_destroy_dma_queue(i2s_num, dma);
639 return NULL;
640 }
641 for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
642 dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
643 if (dma->desc[bux_idx] == NULL) {
644 ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
645 i2s_destroy_dma_queue(i2s_num, dma);
646 return NULL;
647 }
648 }
649
650 for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
651 dma->desc[bux_idx]->owner = 1;
652 dma->desc[bux_idx]->eof = 1;
653 dma->desc[bux_idx]->sosf = 0;
654 dma->desc[bux_idx]->length = dma_buf_len * sample_size;
655 dma->desc[bux_idx]->size = dma_buf_len * sample_size;
656 dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
657 dma->desc[bux_idx]->offset = 0;
658 dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
659 }
660 dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
661 dma->mux = xSemaphoreCreateMutex();
662 dma->buf_size = dma_buf_len * sample_size;
663 ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
664 return dma;
665 }
666
i2s_start(i2s_port_t i2s_num)667 esp_err_t i2s_start(i2s_port_t i2s_num)
668 {
669 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
670 //start DMA link
671 I2S_ENTER_CRITICAL();
672 i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
673
674 esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
675 i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
676 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
677 i2s_enable_tx_intr(i2s_num);
678 i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
679 }
680 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
681 i2s_enable_rx_intr(i2s_num);
682 i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
683 }
684 esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
685 I2S_EXIT_CRITICAL();
686 return ESP_OK;
687 }
688
i2s_stop(i2s_port_t i2s_num)689 esp_err_t i2s_stop(i2s_port_t i2s_num)
690 {
691 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
692 I2S_ENTER_CRITICAL();
693 esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
694 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
695 i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
696 i2s_disable_tx_intr(i2s_num);
697 }
698 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
699 i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
700 i2s_disable_rx_intr(i2s_num);
701 }
702 uint32_t mask;
703 i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
704 i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
705 I2S_EXIT_CRITICAL();
706 return ESP_OK;
707 }
708
709 #if SOC_I2S_SUPPORTS_ADC_DAC
i2s_set_dac_mode(i2s_dac_mode_t dac_mode)710 esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
711 {
712 I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
713 if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
714 dac_output_disable(DAC_CHANNEL_1);
715 dac_output_disable(DAC_CHANNEL_2);
716 dac_i2s_disable();
717 } else {
718 dac_i2s_enable();
719 }
720
721 if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
722 //DAC1, right channel
723 dac_output_enable(DAC_CHANNEL_1);
724 }
725 if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
726 //DAC2, left channel
727 dac_output_enable(DAC_CHANNEL_2);
728 }
729 return ESP_OK;
730 }
731
_i2s_adc_mode_recover(void)732 static esp_err_t _i2s_adc_mode_recover(void)
733 {
734 I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
735 return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
736 }
737
i2s_set_adc_mode(adc_unit_t adc_unit,adc1_channel_t adc_channel)738 esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
739 {
740 I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
741 // For now, we only support SAR ADC1.
742 _i2s_adc_unit = adc_unit;
743 _i2s_adc_channel = adc_channel;
744 return adc_i2s_mode_init(adc_unit, adc_channel);
745 }
746 #endif
747
i2s_set_pin(i2s_port_t i2s_num,const i2s_pin_config_t * pin)748 esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
749 {
750 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
751 if (pin == NULL) {
752 #if SOC_I2S_SUPPORTS_ADC_DAC
753 return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
754 #else
755 return ESP_ERR_INVALID_ARG;
756 #endif
757 }
758
759 if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
760 ESP_LOGE(I2S_TAG, "bck_io_num error");
761 return ESP_FAIL;
762 }
763 if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
764 ESP_LOGE(I2S_TAG, "ws_io_num error");
765 return ESP_FAIL;
766 }
767 if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
768 ESP_LOGE(I2S_TAG, "data_out_num error");
769 return ESP_FAIL;
770 }
771 if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
772 ESP_LOGE(I2S_TAG, "data_in_num error");
773 return ESP_FAIL;
774 }
775
776 int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
777 //Each IIS hw module has a RX and TX unit.
778 //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
779 //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
780 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
781 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
782 bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
783 ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
784 data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
785 } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
786 bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
787 ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
788 data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
789 }
790 }
791 //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
792 //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
793 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
794 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
795 bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
796 ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
797 data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
798 } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
799 bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
800 ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
801 data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
802 }
803 }
804 //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
805 //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
806 if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
807 bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
808 ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
809 } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
810 bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
811 ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
812 }
813 gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
814 gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
815 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
816 gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
817 gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
818 } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
819 gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
820 gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
821 }
822 ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
823
824 return ESP_OK;
825 }
826
i2s_set_sample_rates(i2s_port_t i2s_num,uint32_t rate)827 esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
828 {
829 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
830 I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
831 return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
832 }
833
834 #if SOC_I2S_SUPPORTS_PDM
i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num,i2s_pdm_dsr_t dsr)835 esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
836 {
837 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
838 i2s_hal_rx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), dsr);
839 return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
840 }
841 #endif
842
i2s_check_cfg_static(i2s_port_t i2s_num,const i2s_config_t * cfg)843 static esp_err_t i2s_check_cfg_static(i2s_port_t i2s_num, const i2s_config_t *cfg)
844 {
845 #if SOC_I2S_SUPPORTS_ADC_DAC
846 //We only check if the I2S number is invalid when set to build in ADC and DAC mode.
847 I2S_CHECK(!((cfg->mode & I2S_MODE_ADC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S ADC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
848 I2S_CHECK(!((cfg->mode & I2S_MODE_DAC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S DAC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
849 return ESP_OK;
850 #endif
851 #if SOC_I2S_SUPPORTS_PDM
852 //We only check if the I2S number is invalid when set to PDM mode.
853 I2S_CHECK(!((cfg->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
854 return ESP_OK;
855 #endif
856
857 I2S_CHECK(cfg->communication_format && (cfg->communication_format < I2S_COMM_FORMAT_STAND_MAX), "invalid communication formats", ESP_ERR_INVALID_ARG);
858 I2S_CHECK(!((cfg->communication_format & I2S_COMM_FORMAT_STAND_MSB) && (cfg->communication_format & I2S_COMM_FORMAT_STAND_PCM_LONG)), "multiple communication formats specified", ESP_ERR_INVALID_ARG);
859 return ESP_OK;
860 }
861
i2s_param_config(i2s_port_t i2s_num,const i2s_config_t * i2s_config)862 static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
863 {
864 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
865 I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
866 I2S_CHECK((i2s_check_cfg_static(i2s_num, i2s_config) == ESP_OK), "param check error", ESP_ERR_INVALID_ARG);
867
868 #if SOC_I2S_SUPPORTS_ADC_DAC
869 if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
870 //in ADC built-in mode, we need to call i2s_set_adc_mode to
871 //initialize the specific ADC channel.
872 //in the current stage, we only support ADC1 and single channel mode.
873 //In default data mode, the ADC data is in 12-bit resolution mode.
874 adc_power_acquire();
875 }
876 #endif
877 // configure I2S data port interface.
878 i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
879 if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
880 i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
881 if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
882 i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
883 } else {
884 i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
885 }
886 }
887
888 p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
889 p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
890 p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
891 return ESP_OK;
892 }
893
i2s_zero_dma_buffer(i2s_port_t i2s_num)894 esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
895 {
896 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
897 if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
898 for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
899 memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
900 }
901 }
902 if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
903 int bytes_left = 0;
904 bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
905 if (bytes_left) {
906 size_t zero_bytes = 0, bytes_written;
907 i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
908 }
909 for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
910 memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
911 }
912 }
913 return ESP_OK;
914 }
915
i2s_driver_install(i2s_port_t i2s_num,const i2s_config_t * i2s_config,int queue_size,void * i2s_queue)916 esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
917 {
918 esp_err_t err;
919 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
920 I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
921 I2S_CHECK((i2s_config->dma_buf_count >= 2 && i2s_config->dma_buf_count <= 128), "I2S buffer count less than 128 and more than 2", ESP_ERR_INVALID_ARG);
922 I2S_CHECK((i2s_config->dma_buf_len >= 8 && i2s_config->dma_buf_len <= 1024), "I2S buffer length at most 1024 and more than 8", ESP_ERR_INVALID_ARG);
923 if (p_i2s_obj[i2s_num] == NULL) {
924 p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
925 if (p_i2s_obj[i2s_num] == NULL) {
926 ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
927 return ESP_ERR_NO_MEM;
928 }
929 memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
930
931 portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
932 for (int x = 0; x < I2S_NUM_MAX; x++) {
933 i2s_spinlock[x] = i2s_spinlock_unlocked[0];
934 }
935 //To make sure hardware is enabled before any hardware register operations.
936 periph_module_enable(i2s_periph_signal[i2s_num].module);
937 i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
938
939 p_i2s_obj[i2s_num]->i2s_num = i2s_num;
940 p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
941 p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
942 p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
943 p_i2s_obj[i2s_num]->mode = i2s_config->mode;
944
945 p_i2s_obj[i2s_num]->bits_per_sample = 0;
946 p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
947 p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
948
949 #ifdef CONFIG_PM_ENABLE
950 if (i2s_config->use_apll) {
951 err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
952 } else {
953 err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
954 }
955 if (err != ESP_OK) {
956 free(p_i2s_obj[i2s_num]);
957 p_i2s_obj[i2s_num] = NULL;
958 ESP_LOGE(I2S_TAG, "I2S pm lock error");
959 return err;
960 }
961 #endif //CONFIG_PM_ENABLE
962
963 //initial interrupt
964 err = i2s_isr_register(i2s_num, i2s_config->intr_alloc_flags, i2s_intr_handler_default, p_i2s_obj[i2s_num], &p_i2s_obj[i2s_num]->i2s_isr_handle);
965 if (err != ESP_OK) {
966 #ifdef CONFIG_PM_ENABLE
967 if (p_i2s_obj[i2s_num]->pm_lock) {
968 esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
969 }
970 #endif
971 free(p_i2s_obj[i2s_num]);
972 p_i2s_obj[i2s_num] = NULL;
973 ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
974 return err;
975 }
976 i2s_stop(i2s_num);
977 err = i2s_param_config(i2s_num, i2s_config);
978 if (err != ESP_OK) {
979 i2s_driver_uninstall(i2s_num);
980 ESP_LOGE(I2S_TAG, "I2S param configure error");
981 return err;
982 }
983
984 if (i2s_queue) {
985 p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
986 *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
987 ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
988 } else {
989 p_i2s_obj[i2s_num]->i2s_queue = NULL;
990 }
991 //set clock and start
992 return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
993 }
994
995 ESP_LOGW(I2S_TAG, "I2S driver already installed");
996 return ESP_OK;
997 }
998
i2s_driver_uninstall(i2s_port_t i2s_num)999 esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
1000 {
1001 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
1002 if (p_i2s_obj[i2s_num] == NULL) {
1003 ESP_LOGI(I2S_TAG, "already uninstalled");
1004 return ESP_OK;
1005 }
1006 i2s_stop(i2s_num);
1007 esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
1008
1009 if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
1010 i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
1011 p_i2s_obj[i2s_num]->tx = NULL;
1012 }
1013 if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
1014 i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
1015 p_i2s_obj[i2s_num]->rx = NULL;
1016 }
1017
1018 if (p_i2s_obj[i2s_num]->i2s_queue) {
1019 vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
1020 p_i2s_obj[i2s_num]->i2s_queue = NULL;
1021 }
1022
1023 if(p_i2s_obj[i2s_num]->use_apll) {
1024 // switch back to PLL clock source
1025 i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
1026 rtc_clk_apll_enable(0, 0, 0, 0, 0);
1027 }
1028 #ifdef CONFIG_PM_ENABLE
1029 if (p_i2s_obj[i2s_num]->pm_lock) {
1030 esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
1031 }
1032 #endif
1033
1034 free(p_i2s_obj[i2s_num]);
1035 p_i2s_obj[i2s_num] = NULL;
1036 periph_module_disable(i2s_periph_signal[i2s_num].module);
1037
1038 return ESP_OK;
1039 }
1040
i2s_write(i2s_port_t i2s_num,const void * src,size_t size,size_t * bytes_written,TickType_t ticks_to_wait)1041 esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait)
1042 {
1043 char *data_ptr, *src_byte;
1044 size_t bytes_can_write;
1045 *bytes_written = 0;
1046 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
1047 I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
1048 I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
1049 xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
1050 #ifdef CONFIG_PM_ENABLE
1051 esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
1052 #endif
1053 src_byte = (char *)src;
1054 while (size > 0) {
1055 if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
1056 if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
1057 break;
1058 }
1059 p_i2s_obj[i2s_num]->tx->rw_pos = 0;
1060 }
1061 ESP_LOGD(I2S_TAG, "size: %d, rw_pos: %d, buf_size: %d, curr_ptr: %d", size, p_i2s_obj[i2s_num]->tx->rw_pos, p_i2s_obj[i2s_num]->tx->buf_size, (int)p_i2s_obj[i2s_num]->tx->curr_ptr);
1062 data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
1063 data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
1064 bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
1065 if (bytes_can_write > size) {
1066 bytes_can_write = size;
1067 }
1068 memcpy(data_ptr, src_byte, bytes_can_write);
1069 size -= bytes_can_write;
1070 src_byte += bytes_can_write;
1071 p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
1072 (*bytes_written) += bytes_can_write;
1073 }
1074 #ifdef CONFIG_PM_ENABLE
1075 esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
1076 #endif
1077
1078 xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
1079 return ESP_OK;
1080 }
1081
1082 #if SOC_I2S_SUPPORTS_ADC_DAC
i2s_adc_enable(i2s_port_t i2s_num)1083 esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
1084 {
1085 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
1086 I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
1087 I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
1088
1089 adc1_dma_mode_acquire();
1090 _i2s_adc_mode_recover();
1091 i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
1092 i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
1093 return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
1094 }
1095
i2s_adc_disable(i2s_port_t i2s_num)1096 esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
1097 {
1098 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
1099 I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
1100 I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
1101
1102 i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
1103 adc1_lock_release();
1104 return ESP_OK;
1105 }
1106 #endif
1107
i2s_write_expand(i2s_port_t i2s_num,const void * src,size_t size,size_t src_bits,size_t aim_bits,size_t * bytes_written,TickType_t ticks_to_wait)1108 esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, size_t src_bits, size_t aim_bits, size_t *bytes_written, TickType_t ticks_to_wait)
1109 {
1110 char *data_ptr;
1111 int bytes_can_write, tail;
1112 int src_bytes, aim_bytes, zero_bytes;
1113 *bytes_written = 0;
1114 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
1115 I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
1116 I2S_CHECK((aim_bits * size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
1117 I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
1118 I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
1119 if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
1120 ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
1121 return ESP_ERR_INVALID_ARG;
1122 }
1123 if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
1124 ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
1125 return ESP_ERR_INVALID_ARG;
1126 }
1127 if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
1128 ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
1129 return ESP_ERR_INVALID_ARG;
1130 }
1131 if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
1132 ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
1133 return ESP_ERR_INVALID_ARG;
1134 }
1135
1136 src_bytes = src_bits / 8;
1137 aim_bytes = aim_bits / 8;
1138 zero_bytes = aim_bytes - src_bytes;
1139 xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
1140 size = size * aim_bytes / src_bytes;
1141 ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
1142 while (size > 0) {
1143 if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
1144 if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
1145 break;
1146 }
1147 p_i2s_obj[i2s_num]->tx->rw_pos = 0;
1148 }
1149 data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
1150 data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
1151 bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
1152 if (bytes_can_write > (int)size) {
1153 bytes_can_write = size;
1154 }
1155 tail = bytes_can_write % aim_bytes;
1156 bytes_can_write = bytes_can_write - tail;
1157
1158 memset(data_ptr, 0, bytes_can_write);
1159 for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
1160 j += zero_bytes;
1161 memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
1162 (*bytes_written) += (aim_bytes - zero_bytes);
1163 }
1164 size -= bytes_can_write;
1165 p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
1166 }
1167 xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
1168 return ESP_OK;
1169 }
1170
i2s_read(i2s_port_t i2s_num,void * dest,size_t size,size_t * bytes_read,TickType_t ticks_to_wait)1171 esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
1172 {
1173 char *data_ptr, *dest_byte;
1174 int bytes_can_read;
1175 *bytes_read = 0;
1176 dest_byte = (char *)dest;
1177 I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
1178 I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
1179 I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
1180 xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
1181 #ifdef CONFIG_PM_ENABLE
1182 esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
1183 #endif
1184 while (size > 0) {
1185 if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) {
1186 if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
1187 break;
1188 }
1189 p_i2s_obj[i2s_num]->rx->rw_pos = 0;
1190 }
1191 data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
1192 data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
1193 bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
1194 if (bytes_can_read > (int)size) {
1195 bytes_can_read = size;
1196 }
1197 memcpy(dest_byte, data_ptr, bytes_can_read);
1198 size -= bytes_can_read;
1199 dest_byte += bytes_can_read;
1200 p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
1201 (*bytes_read) += bytes_can_read;
1202 }
1203 #ifdef CONFIG_PM_ENABLE
1204 esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
1205 #endif
1206 xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
1207 return ESP_OK;
1208 }
1209