ESP32C3 I2S Slave模式读取数据错误

henryli
Posts: 11
Joined: Wed Jun 23, 2021 6:53 am

ESP32C3 I2S Slave模式读取数据错误

Postby henryli » Sat Sep 11, 2021 5:52 am

i2s配置成slave ,tx/rx双工,具体参数如下,接收到的数据有错误。

I (43837) U_I2S: mode: 14
I (43837) U_I2S: sample_rate: 16000
I (43837) U_I2S: bits_per_sample: 16
I (43847) U_I2S: channel_format: 2
I (43847) U_I2S: communication_format: 1
I (43857) U_I2S: dma_buf_count: 4
I (43857) U_I2S: dma_buf_len: 1024
I (43857) U_I2S: use_apll: 1
I (43867) U_I2S: bck_io 6
I (43867) U_I2S: ws_io_num 5
I (43877) U_I2S: data_out_num 1
I (43877) U_I2S: data_in_num 0
I (43877) I2S: DMA Malloc info, datalen=blocksize=4092, dma_buf_count=4
I (43887) I2S: DMA Malloc info, datalen=blocksize=4092, dma_buf_count=4
I (43897) U_I2S: i2s_driver_install ret: 0
I (43897) I2S: set ws/bck io rx
I (43907) U_I2S: i2s_set_pin ret: 0
I (43907) U_I2S: i2s_set_sample_rates ret: 0
I (43907) U_I2S: u_i2s_init return 0

Codec 在左右channel上报固定数据 0x11,0x22, 0x33,0x44 和 0xaa 0xbb 0xcc 0xdd。app调用i2s_read读取数据,但是只有部分数据是正确的,绝大多少都是错误数据。

err = i2s_read(CONFIG_I2S_PORT, pbuf, in_len, &out_len, portMAX_DELAY);

测试了几种参数,无论是读取32/320/640字节,都是同样情况,只有头部部分数据正确。log如下

读取32字节
I (46547) AUDIO_RECORDER: 32: 1 80 22 11 bb aa dd cc
I (46547) AUDIO_RECORDER: 32: fc 7 c4 2 7 0 0 0
I (46557) AUDIO_RECORDER: 32: 0 0 0 0 0 0 0 0
I (46557) AUDIO_RECORDER: 32: 39 1 8 82 84 8b 96 24
I (46567) AUDIO_RECORDER: 64: 22 11 44 33 bb aa dd cc
I (46567) AUDIO_RECORDER: 64: fc 7 c4 2 7 0 0 0
I (46577) AUDIO_RECORDER: 64: 0 0 0 0 0 0 0 0
I (46577) AUDIO_RECORDER: 64: 39 1 8 82 84 8b 96 24
I (46587) AUDIO_RECORDER: 96: 22 11 44 33 bb aa dd cc
I (46587) AUDIO_RECORDER: 96: fc 7 c4 2 7 0 0 0
I (46597) AUDIO_RECORDER: 96: 0 0 0 0 0 0 0 0
I (46607) AUDIO_RECORDER: 96: 39 1 8 82 84 8b 96 24
I (46607) AUDIO_RECORDER: 128: 22 11 44 33 bb aa dd cc
I (46617) AUDIO_RECORDER: 128: fc 7 c4 2 7 0 0 0
I (46617) AUDIO_RECORDER: 128: 0 0 0 0 0 0 0 0
I (46627) AUDIO_RECORDER: 128: 39 1 8 82 84 8b 96 24
I (46627) AUDIO_RECORDER: 160: 22 11 44 33 bb aa dd cc
I (46637) AUDIO_RECORDER: 160: fc 7 c4 2 7 0 0 0
I (46637) AUDIO_RECORDER: 160: 0 0 0 0 0 0 0 0
I (46647) AUDIO_RECORDER: 160: 39 1 8 82 84 8b 96 24
I (46647) AUDIO_RECORDER: 192: 22 11 44 33 bb aa dd cc
I (46657) AUDIO_RECORDER: 192: fc 7 c4 2 7 0 0 0
I (46667) AUDIO_RECORDER: 192: 0 0 0 0 0 0 0 0
I (46667) AUDIO_RECORDER: 192: 39 1 8 82 84 8b 96 24
I (46677) AUDIO_RECORDER: 224: 22 11 44 33 bb aa dd cc
I (46677) AUDIO_RECORDER: 224: fc 7 c4 2 7 0 0 0
I (46687) AUDIO_RECORDER: 224: 0 0 0 0 0 0 0 0
I (46687) AUDIO_RECORDER: 224: 39 1 8 82 84 8b 96 24
I (46697) AUDIO_RECORDER: 256: 22 11 44 33 bb aa dd cc
I (46697) AUDIO_RECORDER: 256: fc 7 c4 2 7 0 0 0
I (46707) AUDIO_RECORDER: 256: 0 0 0 0 0 0 0 0


读取640字节
I (44027) AUDIO_RECORDER: 640: 1 70 22 11 bb aa dd cc
I (44027) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44037) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44047) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44047) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44057) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44057) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44067) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44067) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44077) AUDIO_RECORDER: 640: 22 11 44 33 bb aa dd cc
I (44087) AUDIO_RECORDER: 640: e9 1c 1 0 14 0 cc 3f
I (44087) AUDIO_RECORDER: 640: 0 1 0 0 71 76 a0 2
I (44097) AUDIO_RECORDER: 640: 0 0 0 76 0 0 0 0
I (44097) AUDIO_RECORDER: 640: 61 72 32 2d e8 ae bf e5
I (44107) AUDIO_RECORDER: 640: 18 0 3d 16 b 7 4 0
I (44107) AUDIO_RECORDER: 640: 0 0 c0 5 0 0 0 c0
I (44117) AUDIO_RECORDER: 640: 0 0 0 0 0 dd 6 0
I (44117) AUDIO_RECORDER: 640: 3 a4 0 0 27 a4 0 0
I (44127) AUDIO_RECORDER: 640: 0 dd 9 0 3 7f 1 1
I (44127) AUDIO_RECORDER: 640: a0 f3 3b 98 c9 67 16 5
I (44137) AUDIO_RECORDER: 640: e2 c 1b a4 3c 94 9b 5d
I (44147) AUDIO_RECORDER: 640: e3 ff dd c af e2 4 7a
I (44147) AUDIO_RECORDER: 640: 84 7e 50 c6 fb e9 23 a1
I (44157) AUDIO_RECORDER: 640: 84 6a 5f c9 a1 52 20 8
I (44157) AUDIO_RECORDER: 640: ce 7d d4 84 bd 7b e2 bf
I (44167) AUDIO_RECORDER: 640: 1e 2f 0 61 17 fe 5e 23
I (44167) AUDIO_RECORDER: 640: f7 82 86 26 e2 6c 38 17
I (44177) AUDIO_RECORDER: 640: d2 4e 38 fb a7 89 53 d8
I (44187) AUDIO_RECORDER: 640: 1e 50 ca 0 14 65 88 7d
I (44187) AUDIO_RECORDER: 640: c7 19 b0 bf 45 e4 3 7c
I (44197) AUDIO_RECORDER: 640: 7e 24 9a 85 19 9f 68 91
I (44197) AUDIO_RECORDER: 640: d1 20 2b dc 25 7c 7e e
I (44207) AUDIO_RECORDER: 640: a5 e 66 3c e5 80 d 64
I (44207) AUDIO_RECORDER: 640: f 3b e0 97 67 40 9d 5d
I (44217) AUDIO_RECORDER: 640: 5 93 69 0 8e 50 b3 9e
I (44227) AUDIO_RECORDER: 640: d aa a0 df 95 a8 31 4c
I (44227) AUDIO_RECORDER: 640: 44 78 9 b6 c2 e8 c1 aa
I (44237) AUDIO_RECORDER: 640: ed c9 61 59 c0 de 80 6f
I (44237) AUDIO_RECORDER: 640: b 8d bd 2d e4 a9 8a d0
I (44247) AUDIO_RECORDER: 640: 17 e1 14 14 13 2a 2d df
I (44257) AUDIO_RECORDER: 640: 1e 2a 4b be 98 d7 9c 7d
I (44257) AUDIO_RECORDER: 640: e5 6c 8 b6 6e 5d 25 2d
I (44267) AUDIO_RECORDER: 640: 9d 5d 11 ea 28 de 93 15
I (44267) AUDIO_RECORDER: 640: af 4 e0 69 bf c9 43 8e
I (44277) AUDIO_RECORDER: 640: bd c5 ff cc f4 a7 15 5b
I (44277) AUDIO_RECORDER: 640: a3 15 64 72 7b eb 7a b5
I (44287) AUDIO_RECORDER: 640: 34 56 ac df e8 5d 4c 5e
I (44297) AUDIO_RECORDER: 640: e6 d4 b3 4 3c 5b ab 75
I (44297) AUDIO_RECORDER: 640: d0 61 ee b9 79 4 de 34
I (44307) AUDIO_RECORDER: 640: b9 7f 44 f3 b5 49 49 c
I (44307) AUDIO_RECORDER: 640: 6c c2 a 38 b8 fc 9b 2e
I (44317) AUDIO_RECORDER: 640: 7e dd c3 86 71 89 e1 3e
I (44317) AUDIO_RECORDER: 640: 3c d1 90 f0 62 6 fe 53
I (44327) AUDIO_RECORDER: 640: e0 5 1f d1 2a 7 2f 2b
I (44337) AUDIO_RECORDER: 640: 3b 88 69 96 f4 a3 6a dc
I (44337) AUDIO_RECORDER: 640: 26 fd 52 29 a0 2b a9 e0
I (44347) AUDIO_RECORDER: 640: d 6 cd d6 65 4a f4 2e
I (44347) AUDIO_RECORDER: 640: 51 14 92 5b df 81 1c 20
I (44357) AUDIO_RECORDER: 640: 1c 33 c2 c6 2d f7 c5 a4
I (44357) AUDIO_RECORDER: 640: 15 f6 7b e0 2f bb 1b e6
I (44367) AUDIO_RECORDER: 640: 5d ae 76 7f c9 d6 c9 6
I (44377) AUDIO_RECORDER: 640: 97 20 c3 ff e7 80 40 32
I (44377) AUDIO_RECORDER: 640: 1 45 29 20 b2 45 8 8
I (44387) AUDIO_RECORDER: 640: b1 47 1 47 8d 46 5 46
I (44387) AUDIO_RECORDER: 640: e7 a 3 a7 c7 a b7 6
I (44397) AUDIO_RECORDER: 640: 2d 2a 31 26 21 26 e1 22
I (44397) AUDIO_RECORDER: 640: 89 e 97 20 c3 ff e7 80
I (44407) AUDIO_RECORDER: 640: 8 41 13 7 0 10 85 66
I (44417) AUDIO_RECORDER: 640: 85 c3 ef 20 20 21 37 76
I (44417) AUDIO_RECORDER: 640: f2 40 62 44 26 85 42 49
I (44427) AUDIO_RECORDER: 640: 97 20 c3 ff e7 80 40 e1
I (44427) AUDIO_RECORDER: 640: 37 7 e8 ff 7d 17 79 8d
I (44437) AUDIO_RECORDER: 640: e7 80 c0 d2 b1 47 e3 19
I (44437) AUDIO_RECORDER: 640: 5 0 17 23 c3 ff 67 0
I (44447) AUDIO_RECORDER: 640: 7d 55 b2 40 41 1 82 80
I (44457) AUDIO_RECORDER: 640: ef 30 70 3a 81 45 a 85
I (44457) AUDIO_RECORDER: 640: c3 ff e7 80 e0 4b 2a 86
I (44467) AUDIO_RECORDER: 640: f2 40 62 44 5 61 82 80
I (44467) AUDIO_RECORDER: 640: 1 46 81 45 13 5 f0 9
I (44477) AUDIO_RECORDER: 640: 6 3a b7 7 0 a 73 90
I (44477) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44487) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44497) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44497) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44507) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44507) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44517) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44527) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44527) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44537) AUDIO_RECORDER: 1280: 22 11 44 33 bb aa dd cc
I (44537) AUDIO_RECORDER: 1280: e9 1c 1 0 14 0 cc 3f
I (44547) AUDIO_RECORDER: 1280: 0 1 0 0 e8 ae a6 2
I (44547) AUDIO_RECORDER: 1280: 0 0 10 7f 0 0 0 0
I (44557) AUDIO_RECORDER: 1280: 61 72 32 2d e8 ae bf e5
I (44567) AUDIO_RECORDER: 1280: 18 0 3d 16 b 7 4 0
I (44567) AUDIO_RECORDER: 1280: 0 0 c0 5 0 0 0 c0
I (44577) AUDIO_RECORDER: 1280: 0 0 0 0 0 dd 6 0
I (44577) AUDIO_RECORDER: 1280: 0 0 27 a4 0 0 42 43
I (44587) AUDIO_RECORDER: 1280: 0 dd 9 0 3 7f 1 1
I (44587) AUDIO_RECORDER: 1280: a0 f3 3b 98 c9 67 16 5
I (44597) AUDIO_RECORDER: 1280: e2 c 1b a4 3c 94 9b 5d
I (44597) AUDIO_RECORDER: 1280: e3 ff dd c af e2 4 7a
I (44607) AUDIO_RECORDER: 1280: 84 7e 50 c6 fb e9 23 a1
I (44617) AUDIO_RECORDER: 1280: 84 6a 5f c9 a1 52 20 8
I (44617) AUDIO_RECORDER: 1280: ce 7d d4 84 bd 7b e2 bf
I (44627) AUDIO_RECORDER: 1280: 1e 2f 0 61 17 fe 5e 23
I (44627) AUDIO_RECORDER: 1280: f7 82 86 26 e2 6c 38 17
I (44637) AUDIO_RECORDER: 1280: d2 4e 38 fb a7 89 53 d8
I (44647) AUDIO_RECORDER: 1280: 1e 50 ca 0 14 65 88 7d
I (44647) AUDIO_RECORDER: 1280: c7 19 b0 bf 45 e4 3 7c
I (44657) AUDIO_RECORDER: 1280: 7e 24 9a 85 19 9f 68 91
I (44657) AUDIO_RECORDER: 1280: d1 20 2b dc 25 7c 7e e
I (44667) AUDIO_RECORDER: 1280: a5 e 66 3c e5 80 d 64
I (44667) AUDIO_RECORDER: 1280: f 3b e0 97 67 40 9d 5d
I (44677) AUDIO_RECORDER: 1280: 5 93 69 0 8e 50 b3 9e
I (44687) AUDIO_RECORDER: 1280: d aa a0 df 95 a8 31 4c
I (44687) AUDIO_RECORDER: 1280: 44 78 9 b6 c2 e8 c1 aa
I (44697) AUDIO_RECORDER: 1280: ed c9 61 59 c0 de 80 6f
I (44697) AUDIO_RECORDER: 1280: b 8d bd 2d e4 a9 8a d0
I (44707) AUDIO_RECORDER: 1280: 17 e1 14 14 13 2a 2d df
I (44717) AUDIO_RECORDER: 1280: 1e 2a 4b be 98 d7 9c 7d

L-KAYA
Posts: 22
Joined: Thu Aug 12, 2021 3:44 am

Re: ESP32C3 I2S Slave模式读取数据错误

Postby L-KAYA » Mon Sep 13, 2021 5:40 am

试试更新到最新版本的IDF,修复了一些I2S的问题。
另外也可以试试两块C3相互用I2S通信,看数据是否正确。
测试代码如下,将SLAVE_MODE设为1和设为0分别烧录到两个C3中,连好信号线观察输出数据是否正确。
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "driver/i2s.h"
  6. #include "driver/gpio.h"
  7. #include "esp_system.h"
  8. #include "esp_log.h"
  9. #include <math.h>
  10. #include "hal/i2s_hal.h"
  11.  
  12.  
  13. #define SAMPLE_RATE     (16000)
  14. #define BITS_PER_SAMPLE I2S_BITS_PER_SAMPLE_16BIT
  15. // #define BITS_PER_CHAN   I2S_BITS_PER_CHAN_16BIT
  16. #define I2S_BUFF_SIZE   (2560)
  17. #define SLAVE_MODE      1
  18. #define USE_I2S         0
  19.  
  20. static const char *TAG = "i2s_loop";
  21.  
  22. static void i2s_send(void *args)
  23. {
  24.     size_t i2s_bytes_write = 0;
  25.     uint16_t *samples_data = malloc(I2S_BUFF_SIZE * sizeof(uint16_t));
  26.     if (!samples_data) {
  27.         ESP_LOGE(TAG, "No memory for write data buffer");
  28.     }
  29.     // memset(samples_data, 0xba, I2S_BUFF_SIZE);
  30.     for (int i = 0; i < I2S_BUFF_SIZE; i += 4) {
  31.         samples_data[i] = 0x1234;
  32.         samples_data[i + 1] = 0x5678;
  33.         samples_data[i + 2] = 0x9abc;
  34.         samples_data[i + 3] = 0xdef0;
  35.     }
  36.     while (1) {
  37.         // i2s send
  38.         if (i2s_write(USE_I2S, samples_data, I2S_BUFF_SIZE, &i2s_bytes_write, 100) != ESP_OK) {
  39.             printf("i2s write failed.\n");
  40.         } else {
  41.             printf("i2s write success, %d bytes written.\n", i2s_bytes_write);
  42.         }
  43.         vTaskDelay(500 / portTICK_RATE_MS);
  44.     }
  45. }
  46.  
  47. static void i2s_recv(void *args)
  48. {
  49.     uint16_t *read_data = malloc(I2S_BUFF_SIZE);
  50.     if (!read_data) {
  51.         ESP_LOGE(TAG, "No memory for read data buffer");
  52.     }
  53.     size_t i2s_bytes_read = 0;
  54.     while (1) {
  55.         memset(read_data, 0, I2S_BUFF_SIZE);
  56.         esp_err_t ret = i2s_read(USE_I2S, read_data, I2S_BUFF_SIZE, &i2s_bytes_read, portMAX_DELAY);
  57.         if (ret != ESP_OK || i2s_bytes_read == 0) {
  58.             printf("i2s read failed. read status %d, read bytes %d\n", ret, i2s_bytes_read);
  59.         } else {
  60.             printf("\n\ni2s read success, %d bytes read.\n---------------------------------------------------\n", i2s_bytes_read);
  61.             printf("[0]: %x [1]: %x [2]: %x [3]: %x [4]: %x\n", read_data[100], read_data[101], read_data[102], read_data[103],  read_data[104]);
  62.         }
  63.         vTaskDelay(500 / portTICK_RATE_MS);
  64.     }
  65. }
  66.  
  67. void app_main(void)
  68. {
  69.     i2s_config_t i2s0_config = {
  70. #if SLAVE_MODE
  71.         .mode = I2S_MODE_SLAVE | I2S_MODE_TX | I2S_MODE_RX,
  72. #else
  73.         .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_TX,
  74. #endif
  75.         .sample_rate = SAMPLE_RATE,
  76.         .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  77.         .bits_per_sample = BITS_PER_SAMPLE,
  78.         // .bits_per_chan = I2S_BITS_PER_CHAN_16BIT,
  79.         .channel_format = I2S_CHANNEL_FMT_ALL_LEFT,
  80.         // .channel_format = I2S_CHANNEL_FMT_MULTIPLE,
  81. #if SOC_I2S_SUPPORTS_TDM    // TDM is only available on s3 and c3
  82.         .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 | I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3,
  83.         .total_chan = 4,
  84.         .big_edin = 1,
  85. #endif
  86.         .dma_buf_count = 4,
  87.         .dma_buf_len = 1024,
  88.         .use_apll = 1,
  89.         .fixed_mclk = 0,
  90.     };
  91.  
  92.     i2s_pin_config_t i2s0_pin = {
  93.         // .mck_io_num = GPIO_NUM_0,
  94.         .bck_io_num = GPIO_NUM_4,
  95.         .ws_io_num = GPIO_NUM_5,
  96.         .data_out_num = GPIO_NUM_18,
  97.         .data_in_num = GPIO_NUM_19
  98.     };
  99.     i2s_driver_install(USE_I2S, &i2s0_config, 0, NULL);
  100.     i2s_set_pin(USE_I2S, &i2s0_pin);
  101.  
  102.     xTaskCreate(i2s_send, "i2s_send", 4096, NULL, 5, NULL);
  103.     xTaskCreate(i2s_recv, "i2s_recv", 4096, NULL, 5, NULL);
  104. }

henryli
Posts: 11
Joined: Wed Jun 23, 2021 6:53 am

Re: ESP32C3 I2S Slave模式读取数据错误

Postby henryli » Sat Sep 18, 2021 6:09 am

升级到最新的IDF代码,问题解决了!

shenbk
Posts: 12
Joined: Thu Jun 10, 2021 2:45 am

Re: ESP32C3 I2S Slave模式读取数据错误

Postby shenbk » Fri Dec 31, 2021 12:39 pm

你好楼主,请问你IDF升级到哪个版本解决了这个问题?

Who is online

Users browsing this forum: Google [Bot] and 46 guests