Changing ESP32 I2S_SAMPLE_RATE during execution

Does anybody know how to change the I2S sample rate dynamically ?

Is this forum the right place for questions about high level ESP32 programming ?

Have you checked the documentation?
https://docs.espressif.com/projects/arduino-esp32/en/latest/api/i2s.html#arduino-esp32-i2s-api

Or, perhaps in the Programming Guide:
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2s.html#functional-overview

Yes I checked every page at espressif.com site. The chinese documentation is too poor and very confuse. What I need to know is not there !!!

Then you should provide more detail about what you want to do and provide the code that you have.

Bellow the code and the problem:

#include <driver/i2s.h>

#define I2S_SAMPLE_RATE (1250000)
#define ADC_INPUT (ADC1_CHANNEL_4)    //pin 32
#define I2S_DMA_BUF_LEN (720)

size_t bytes_read;
uint16_t buffer[I2S_DMA_BUF_LEN] = {0};

void i2sInit() {
  i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
    .sample_rate =  I2S_SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 2,
    .dma_buf_len = I2S_DMA_BUF_LEN,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
  };

  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT);
  i2s_adc_enable(I2S_NUM_0);
  adc1_config_channel_atten(ADC_INPUT, ADC_ATTEN_DB_11);
}

void setup(void) {
  i2sInit();
}

void loop() {
  
  i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, 0);
  
  //at this point the buffer is full and it was filled at 1250000Hz rate
  //according to the constant I2S_SAMPLE_RATE
  //Under certain circustances I want to change the sample rate to other
  //values. I have tryied to define uint32_t newrate and replace
  //.sample_rate = I2S_SAMPLE_RATE by .sample_rate = newrate, on i2sInit()
  //under this change all I get are successives reset loop like this:
    
  //rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  //configsip: 0, SPIWP:0xee
  //clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  //mode:DIO, clock div:1
  //load:0x3fff0030,len:1344
  //load:0x40078000,len:13864
  //load:0x40080400,len:3608
  //entry 0x400805f0
  //ets Jun  8 2016 00:22:57
  //
}

What compile error do you get?

And, if you get a compile error, how can you be getting "successives reset loop" as that's a run time problem?

Oooops... Sorry. There is no compile error. It was a mistake when I wrote that post. I have already edit and fixed it.

Please post the exact code that causes the problem rather than just saying what you tried.

Bellow the exactly code that results in successives resets.
Recall that if I defined samplerate as a constant like
#define samplerate (1250000) the program works well !!!
Thanks

#include <driver/i2s.h>

#define ADC_INPUT (ADC1_CHANNEL_4)    //pin 32
#define I2S_DMA_BUF_LEN (720)

size_t bytes_read;
uint16_t buffer[I2S_DMA_BUF_LEN] = {0};
uint32_t samplerate=1250000;

void i2sInit() {
  i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
    .sample_rate =  samplerate,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 2,
    .dma_buf_len = I2S_DMA_BUF_LEN,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
  };

  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT);
  i2s_adc_enable(I2S_NUM_0);
  adc1_config_channel_atten(ADC_INPUT, ADC_ATTEN_DB_11);
}

void setup(void) {
  Serial.begin(115200);
  i2sInit();
}

void loop() {
  
  i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, 0);
  Serial.println("buffer full.. Ready to be processed...");
}

I did not get crashes / resets with that code. Using ESP32 Board Package 2.0.8 and board "ESP32 Dev Module".

This is a very good news. My board is a different clone. I will by the model you have tested. Thanks a lot

My Chinese is too bad to read the tons of documentation, kudos to you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.