One more question is about dmaIsrHandler. I am not sure that it will work this way. Should i2s_read work inside the interrupt? If not, then is there another way to check sound loudness inside the interrupt?
I2S.cpp:
Code: Select all
#include "infra/I2S.h"
#include "config/config.h"
#define SAMPLE_RATE (16000)
#define PIN_I2S_BCLK (GPIO_NUM_26)
#define PIN_I2S_LRC (GPIO_NUM_25)
#define PIN_I2S_DIN (GPIO_NUM_3)
#define PIN_I2S_DOUT (GPIO_NUM_22)
#define PIN_I2S_MCK (GPIO_NUM_0)
SemaphoreHandle_t dmaInterruptSemaphore;
static intr_handle_t dmaInterruptRetHandle;
extern int loudnessFlag;
void IRAM_ATTR dmaIsrHandler(void* arg) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
int32_t buffer[128];
size_t bytes_read;
i2s_read(I2S_NUM_0, (char*)buffer, sizeof(buffer), &bytes_read, portMAX_DELAY);
if (*(int*)buffer > NOISE_THRESHOLD || *(int*)buffer < -NOISE_THRESHOLD) {
loudnessFlag = true;
}
xSemaphoreGiveFromISR(dmaInterruptSemaphore, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}
I2S::I2S() {
BITS_PER_SAMPLE = I2S_BITS_PER_SAMPLE_16BIT;// I2S_BITS_PER_SAMPLE_32BIT;
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = BITS_PER_SAMPLE,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 | ESP_INTR_FLAG_IRAM,
.dma_buf_count = 16,
.dma_buf_len = 60,
};
i2s_pin_config_t pin_config;
pin_config.bck_io_num = PIN_I2S_BCLK;
pin_config.ws_io_num = PIN_I2S_LRC;
pin_config.data_out_num = I2S_PIN_NO_CHANGE;
pin_config.data_in_num = PIN_I2S_DIN;
pin_config.mck_io_num = PIN_I2S_MCK;
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, BITS_PER_SAMPLE, I2S_CHANNEL_STEREO);
dmaInterruptSemaphore = xSemaphoreCreateBinary();
esp_err_t err = esp_intr_alloc(ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1 | ESP_INTR_FLAG_IRAM,
&dmaIsrHandler, NULL, &dmaInterruptRetHandle);
Serial.print("esp_intr_alloc: ");
Serial.println(err);
err = esp_intr_enable(dmaInterruptRetHandle);
Serial.print("esp_intr_enable: ");
Serial.println(err);
}
Code: Select all
#ifndef _I2S_H
#define _I2S_H
#include <Arduino.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2s.h"
#include "esp_system.h"
class I2S {
i2s_bits_per_sample_t BITS_PER_SAMPLE;
public:
I2S();
};
#endif // _I2S_H