The read I2S buffer will only be loaded with half of the data

maykol-rey
Posts: 2
Joined: Wed Feb 14, 2024 12:39 am

The read I2S buffer will only be loaded with half of the data

Postby maykol-rey » Wed Feb 14, 2024 2:24 am

I am trying to read an I2S buffer of 1024 elements, which will be 512 per channel, but instead of receiving 512 I receive only 128 elements with information per channel.

Image

I have read the data sent with a logic analyzer and the information is complete (a little ugly due to noise issues, but complete)

Image

Image

This is the information read with the logic analyzer.

The code I am using is the following

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <math.h>
#include "i2s_driver.h"
#define	BUFF_SIZE_I2S	1024
static	i2s_chan_handle_t	rx_chan;	// I2S rx channel handler

uint16_t *r_buf;
size_t r_bytes=0;
int16_t *chan_1=NULL;
int16_t *chan_2=NULL;

void i2s_configure_channel(){

	r_buf=(uint16_t *)calloc(2,BUFF_SIZE_I2S);
	i2s_chan_config_t chan_cfg ={
		.id=I2S_NUM_AUTO,
		.role = I2S_ROLE_MASTER,
		.dma_desc_num=16*2,
		.dma_frame_num = 2048*2,
    	.auto_clear = false
	};
	ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg,NULL, &rx_chan));
 
    i2s_std_config_t std_cfg = {
    	.clk_cfg  = {
		    .sample_rate_hz = 44100,
		    .clk_src = I2S_CLK_SRC_DEFAULT,//I2S_CLK_SRC_DEFAULT,I2S_CLK_SRC_PLL_160M,I2S_CLK_SRC_APLL
		    .mclk_multiple = I2S_MCLK_MULTIPLE_256,
    	},
    	.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
    	.gpio_cfg = {
        	.mclk = GPIO_NUM_0,
        	.bclk = GPIO_NUM_19,
        	.ws   = GPIO_NUM_22,
        	.dout = GPIO_NUM_21,
        	.din  = GPIO_NUM_17, // In duplex mode, bind output and input to a same gpio can loopback internally
        	.invert_flags = {
            	.mclk_inv = false,
            	.bclk_inv = false,
            	.ws_inv   = false,
        	},
    	},
    };
    ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_chan, &std_cfg));
}
uint16_t* i2s_raw(){

	i2s_channel_enable(rx_chan);
	if (i2s_channel_read(rx_chan, r_buf, BUFF_SIZE_I2S, &r_bytes, 2000)!= ESP_OK){
		printf("Read Task: i2s read failed\n");
	}
	i2s_channel_disable(rx_chan);
	return r_buf;
}
If I read the r_bytes variable I actually get 1024, but only 512 elements have information.

I don't know what to do anymore, it's for my thesis! :( :( :( :(

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: The read I2S buffer will only be loaded with half of the data

Postby ESP_Sprite » Wed Feb 14, 2024 5:31 am

Decently sure the 'size' argument to i2s_channel_read is in bytes, while you're feeding it the number of words you want to read.

maykol-rey
Posts: 2
Joined: Wed Feb 14, 2024 12:39 am

Re: The read I2S buffer will only be loaded with half of the data

Postby maykol-rey » Wed Feb 14, 2024 10:55 am

ESP_Sprite wrote:
Wed Feb 14, 2024 5:31 am
Decently sure the 'size' argument to i2s_channel_read is in bytes, while you're feeding it the number of words you want to read.
Thank you very much, it worked, I made the following change:

Code: Select all

i2s_channel_read(rx_chan,r_buf, sizeof(r_buf), &r_bytes, portMAX_DELAY)!= ESP_OK)
Instead of setting the size of the buffer directly, I used the sizeof() function and in this way I obtained the correct size.

I owe you a coffee! :roll:

Who is online

Users browsing this forum: No registered users and 85 guests