Problem about simple I2S data read code for ESP32-S2 and INMP441 microphone

detan386
Posts: 1
Joined: Sun Jul 11, 2021 6:33 pm

Problem about simple I2S data read code for ESP32-S2 and INMP441 microphone

Postby detan386 » Thu Jan 27, 2022 1:00 pm

Hi! I'm trying to write a basic i2s code to take microphone data, and I don't have much ESP or C knowledge. I am using ESP32-S2-MINI1 (devkit) and INMP441. I cannot make it work, so I would be very grateful if you help.
  1. #include <math.h>
  2. #include <sys/unistd.h>
  3. #include <sys/stat.h>
  4. #include "esp_log.h"
  5. #include "esp_err.h"
  6. #include "esp_system.h"
  7. #include "esp_vfs_fat.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "driver/i2s.h"
  11. #include "driver/gpio.h"
  12. #include "driver/spi_common.h"
  13. #include "sdmmc_cmd.h"
  14. #include "sdkconfig.h"
  15.  
  16. #define I2S_NUM         (0)
  17. #define I2S_WS 15
  18. #define I2S_SD 13
  19. #define I2S_SCK 2
  20.  
  21. #define SAMPLE_RATE     (44100)
  22. #define WAVE_FREQ_HZ    (441)
  23. #define SAMPLE_PER_CYCLE (SAMPLE_RATE/WAVE_FREQ_HZ)
  24. #define SAMPLE_SIZE     (1024)
  25.  
  26.  
  27. void app_main(void)
  28. {
  29.     i2s_config_t i2s_config = {
  30.         .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  31.         .sample_rate = 44100,
  32.         .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  33.         .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  34.         .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  35.         .use_apll = false,
  36.         .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Interrupt level 1, default 0
  37.         .dma_buf_count = 8,
  38.         .dma_buf_len = 1024,
  39.         .tx_desc_auto_clear = true,
  40.         .fixed_mclk = -1  
  41.     };
  42.  
  43.     i2s_pin_config_t pin_config = {
  44.         .bck_io_num = I2S_SCK,
  45.         .ws_io_num = I2S_WS,
  46.         .data_out_num = I2S_PIN_NO_CHANGE,
  47.         .data_in_num = I2S_SD                                              
  48.     };
  49.    
  50.     i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  51.     i2s_set_pin(I2S_NUM, &pin_config);
  52.     i2s_start(I2S_NUM);
  53.  
  54.     printf("I2S is ready!\n");
  55.  
  56.     int16_t *i2s_output = (int16_t *)malloc(sizeof(uint16_t) * SAMPLE_SIZE);
  57.     int bits = 8;
  58.     size_t bytes_read = 0;
  59.  
  60.     while (1) {
  61.         i2s_read(I2S_NUM, (char *)i2s_output, ((bits+8)/16)*SAMPLE_PER_CYCLE*4, &bytes_read, portMAX_DELAY);
  62.         printf("data: %hn\n", i2s_output);
  63.         vTaskDelay(1000 / portTICK_RATE_MS);
  64.     }
  65.  
  66. }

Who is online

Users browsing this forum: ESP_rrtandler and 121 guests