ESP32-S3 reading INMP441 micorphone data by i2s incorrect

MarsONTU
Posts: 7
Joined: Tue May 03, 2022 4:14 am

ESP32-S3 reading INMP441 micorphone data by i2s incorrect

Postby MarsONTU » Fri Jul 19, 2024 8:01 pm

The IDF version I am currently using is 5.1.2. The inmP441 microphone is a MEMS microphone that can directly output audio data in i2s format. Therefore, I plan to set up ESP32s3 as the host to obtain i2s data from the microphone. The microphone supports 24 bit bit wide data output, and the data I read is liked following

[id:244]0f96fe00
[id:245]0fb52800
[id:246]0fd4b5e0
[id:247]0ff489e0
[id:248]10109800
[id:249]10301e00
[id:250]104eae00
[id:251]106d5be0
[id:252]108d99e0
[id:253]10abbde0
[id:254]10c9afe0

but it should output 24 bits data like fff3cd00, instead of 1ad8e9e0,here is my code about i2s setting:

#include <stdint.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_check.h"
#include "driver/i2s_std.h"
#include "sdkconfig.h"
#include "string.h"

// #include "driver/i2s.h"

#define I2S_NUM (I2S_NUM_0)

#define I2S_BCK_IO (GPIO_NUM_42)
#define I2S_WS_IO (GPIO_NUM_40)
#define I2S_DI_IO (GPIO_NUM_41)

#define SAMPLE_RATE (44100)

uint32_t I2S_BUFF[512];

i2s_chan_handle_t rx_handle;

void i2s_receive(void *pvParameters)
{
size_t r_bytes = 0;
bzero(I2S_BUFF, sizeof(I2S_BUFF));

ESP_ERROR_CHECK(i2s_channel_enable(rx_handle));

while (1)
{
if (i2s_channel_read(rx_handle, I2S_BUFF, sizeof(I2S_BUFF), &r_bytes, portMAX_DELAY) == ESP_OK)
{
for (int i = 0; i < 512; i++)
{
printf("[id:%d]%08lx\n", i, I2S_BUFF);
}
}
vTaskDelete(NULL);
}
ESP_ERROR_CHECK(i2s_channel_disable(rx_handle));
ESP_ERROR_CHECK(i2s_del_channel(rx_handle));
vTaskDelete(NULL);
}

/**
* 麦克风初始化
*/
void i2s_init(void)
{
i2s_chan_config_t chan_cfg = {
.dma_desc_num = 12,
.dma_frame_num = 240,
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.auto_clear = false,
};

ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, NULL, &rx_handle));

i2s_std_config_t std_cfg = {
.clk_cfg.clk_src = I2S_CLK_SRC_DEFAULT,
.clk_cfg.sample_rate_hz = SAMPLE_RATE,
.clk_cfg.mclk_multiple = 384,

.slot_cfg = {
.bit_shift = true, // 飞利浦模式下启用bit shift
.data_bit_width = I2S_SLOT_BIT_WIDTH_32BIT, // 数据位宽
.slot_bit_width = I2S_SLOT_BIT_WIDTH_32BIT, // 声道位宽
.slot_mode = I2S_SLOT_MODE_STEREO, // 声道模式
.slot_mask = I2S_STD_SLOT_LEFT, // 声道掩码
.ws_width = 32, // ws信号为高的bclk滴答数
.ws_pol = false, // ws极性,true为高电平优先
},

.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = I2S_BCK_IO,
.ws = I2S_WS_IO,
.dout = I2S_GPIO_UNUSED,
.din = I2S_DI_IO,
.invert_flags = {
.mclk_inv = false, // 反转输出
.bclk_inv = false,
.ws_inv = false,
},
},
};

i2s_channel_init_std_mode(rx_handle, &std_cfg);
}

void app_main(void)
{
i2s_init();
xTaskCreatePinnedToCore(i2s_receive, "i2s_receive", 4096, NULL, 5, NULL, 0);

while (1)
{
vTaskDelay(pdTICKS_TO_MS(1000));
}
}

Thank you for reading. I'll be appriciate if you could to point out my questions

Who is online

Users browsing this forum: PhilipK and 83 guests