I2S PDM Microphone: data is not correct

vibro_tech
Posts: 7
Joined: Wed May 27, 2020 2:07 pm

I2S PDM Microphone: data is not correct

Postby vibro_tech » Mon Jun 22, 2020 6:51 am

Hi,
I tried the linked codes (https://github.com/espressif/esp-idf/is ... -377944638) in order to get simple data from SPG08P4HM4H-1 (MEMS Digital Mic with Single Bit PDM Output) but I get all the same data.
I used @kewalmshah's code with small save and print addition as shown below (just to print data on terminal):

Code: Select all

/* I2S PDM example */
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2s.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "rom/spi_flash.h"
#include "esp_log.h"

#define SAMPLE_RATE 16000
#define BITS_PS 16
#define I2S_NUM 0
#define BUF_COUNT 4
#define BUF_LENGTH 250

/*

In PDM mode the clock is generated on WS line. Fwsclk = 64*(sample frequency)
Here sample frequuency is 8KHz, hence WS clk frequency will be 64*8k = 512KHz
Each sector of flash is 4096 bytes
*/
int16_t samples_data[SPI_FLASH_SEC_SIZE / 2] = {0}; //Since each sample is 2 bytes

#define I2S_size 8192

int32_t samples[I2S_size];
size_t bytes_read = 0;

static const char* TAG = "I2S:";

void app_main()
{
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM, //master, RX, PDM
.sample_rate = SAMPLE_RATE,
.bits_per_sample = BITS_PS,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_PCM ), //pcm data format
.dma_buf_count = BUF_COUNT,
.dma_buf_len = BUF_LENGTH,
.use_apll = 0, //apll disabled
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //interrupt level 1(lowest priority)
};

i2s_pin_config_t pin_config = {
    .ws_io_num = GPIO_NUM_19,
    .data_in_num = GPIO_NUM_35,
};

i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM, &pin_config);
vTaskDelay(1000/portTICK_PERIOD_MS);

while(1)
{
	ESP_LOGW(TAG,"Start Talking in 3 secs:");
	vTaskDelay(3000/portTICK_RATE_MS);
	i2s_read(I2S_NUM_0,(char *)samples,8192,&bytes_read,portMAX_DELAY);
	for(int k=1000; k<2000; k++)
	{
		ESP_LOGW(TAG,"s[%d] = %d", k, samples[k]);
	}
	vTaskDelay(20000/portTICK_RATE_MS);
}
}
But I only get below:

I (197) boot: Loaded app from partition at offset 0x10000
I (197) boot: Disabling RNG early entropy source...
I (198) cpu_start: Pro cpu up.
I (201) cpu_start: Application information:
I (206) cpu_start: Project name: esp32-i2s-driver-example
I (213) cpu_start: App version: 1
I (217) cpu_start: Compile time: Jun 11 2020 20:43:27
I (223) cpu_start: ELF file SHA256: 028c72052adb1165...
I (229) cpu_start: ESP-IDF: v4.1-beta1-317-g84b51781c
I (236) cpu_start: Starting app cpu, entry point is 0x40081044
I (0) cpu_start: App cpu up.
I (246) heap_init: Initializing. RAM available for dynamic allocation:
I (253) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (259) heap_init: At 3FFB49B8 len 0002B648 (173 KiB): DRAM
I (265) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (272) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (278) heap_init: At 40089E58 len 000161A8 (88 KiB): IRAM
I (284) cpu_start: Pro cpu start user code
I (302) spi_flash: detected chip: generic
I (303) spi_flash: flash io: dio
W (303) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (313) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (325) I2S: DMA Malloc info, datalen=blocksize=500, dma_buf_count=4
I (335) I2S: PLL_D2: Req RATE: 16000, real rate: 32258.000, BITS: 16, CLKM: 31, BCK: 5, MCLK: 31.250, SCLK: 1032256.000000, diva: 64, divb: 16
Start speaking
W (1345) I2S:: Start Talking in 3 secs:
W (4545) I2S:: s[1000] = -2027321559
W (4545) I2S:: s[1001] = -2027321559
W (4545) I2S:: s[1002] = -2027321559
W (4555) I2S:: s[1003] = -2027321559
W (4555) I2S:: s[1004] = -2027321559
W (4555) I2S:: s[1005] = -2027321559
W (4565) I2S:: s[1006] = -2027321559
W (4565) I2S:: s[1007] = -2027321559
W (4575) I2S:: s[1008] = -2027321559
W (4575) I2S:: s[1009] = -2027321559
W (4585) I2S:: s[1010] = -2027321559
W (4585) I2S:: s[1011] = -2027321559
W (4585) I2S:: s[1012] = -2027321559
W (4595) I2S:: s[1013] = -2027321559
W (4595) I2S:: s[1014] = -2027321559
W (4605) I2S:: s[1015] = -2027321559
W (4605) I2S:: s[1016] = -2027321559
W (4615) I2S:: s[1017] = -2027321559
W (4615) I2S:: s[1018] = -2027321559
W (4615) I2S:: s[1019] = -2027321559
W (4625) I2S:: s[1020] = -2027321559
W (4625) I2S:: s[1021] = -2027321559

Later I found that after 2000 samples it gives below data on terminal :

W (183265) I2S:: s[2042] = -2027321559
W (183265) I2S:: s[2043] = -2027321559
W (183275) I2S:: s[2044] = -2027321559
W (183275) I2S:: s[2045] = -2027321559
W (183285) I2S:: s[2046] = -2027321559
W (183285) I2S:: s[2047] = -2027321559
W (183295) I2S:: s[2048] = 0
W (183295) I2S:: s[2049] = 0
W (183295) I2S:: s[2050] = 0
W (183305) I2S:: s[2051] = 0
W (183305) I2S:: s[2052] = 0
W (183305) I2S:: s[2053] = 0
W (183315) I2S:: s[2054] = 0
W (183315) I2S:: s[2055] = 0
W (183325) I2S:: s[2056] = 0
W (183325) I2S:: s[2057] = 0
W (183325) I2S:: s[2058] = 0

Thanks in advance

vibro_tech
Posts: 7
Joined: Wed May 27, 2020 2:07 pm

Re: I2S PDM Microphone: data is not correct

Postby vibro_tech » Thu Jun 25, 2020 5:57 am

No one is dealing with I2S microphones nowadays?

Who is online

Users browsing this forum: No registered users and 255 guests