Here's some sample code that demonstrates how to interface two SPH8878LR5H-1 MEMS omnidirectional microphones audio evaluation boards (BOB-19389) with the ESP-WROOM-32 using the ADC with DMA, circular buffers, and two cores/threads.
Please note that this is just a starting point and may need to be modified to fit your specific application.
#include <driver/adc.h>
#include <driver/dma.h>
#include <soc/dma_reg.h>
#include <soc/sens_reg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#define ADC_RESOLUTION ADC_WIDTH_BIT_12
#define ADC_SAMPLE_RATE (16000)
#define ADC_BUFFER_SIZE (1024)
#define ADC_CHANNEL (GPIO_NUM_34)
#define DMA_CHANNEL (DMA_CHANNEL_1)
#define DMA_BUFFER_SIZE (1024)
#define NUM_MICROPHONES (2)
static uint16_t adc_buffer[NUM_MICROPHONES][ADC_BUFFER_SIZE];
static dma_channel_t dma_channel[NUM_MICROPHONES];
static SemaphoreHandle_t dma_semaphore[NUM_MICROPHONES];
static TaskHandle_t task_handle[NUM_MICROPHONES];
void dma_isr_handler(void* arg) {
uint32_t channel = (uint32_t)arg;
dma_channel_stop(dma_channel[channel]);
dma_channel_clear_interrupt(dma_channel[channel], DMA_INT_OUT_EOF);
xSemaphoreGiveFromISR(dma_semaphore[channel], NULL);
}
void microphone_task(void* arg) {
uint32_t mic_num = (uint32_t)arg;
uint32_t read_index = 0;
uint32_t write_index = 0;
while (1) {
xSemaphoreTake(dma_semaphore[mic_num], portMAX_DELAY);
for (int i = 0; i < ADC_BUFFER_SIZE; i++) {
// Process the audio data in the buffer here
// For example, you can send it over Wi-Fi or store it in flash
// uint16_t sample = adc_buffer[mic_num][i];
}
write_index = (write_index + 1) % ADC_BUFFER_SIZE;
dma_channel_config_t config = {
.dst_addr = &adc_buffer[mic_num][write_index],
.src_addr = (void*)ADC_LL_REG_TO_APB_REG(ADC_CHANNEL_DATA_OUT_REG(ADC_CHANNEL)),
.transfer_count = sizeof(uint16_t),
.src_width = DMA_SRC_WIDTH_BIT_12,
.dst_width = DMA_DEST_WIDTH_BIT_16,
.descriptor_num = ADC_BUFFER_SIZE,
.tx_desc_addr = (uint32_t)&dma_desc_t[mic_num][write_index],
.cycle_mode = false,
.repeat_mode = true,
.stop_mode = false,
.start_mode = DMA_START_TRIGGERED,
.interrupt_enable = true,
.end_callback = NULL,
};
dma_channel_configure(dma_channel[mic_num], &config, 0);
dma_channel_start(dma_channel[mic_num]);
}
}
void app_main() {
for (int i = 0; i < NUM_MICROPHONES; i++) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC_CHANNEL, ADC_ATTEN_DB_11);
dma_semaphore[i] = xSemaphoreCreateBinary();
if (dma_semaphore[i] == NULL) {
printf("Failed to create DMA semaphore\n");
abort();
}
dma_channel_config_t config = {
.dst_addr = &adc_buffer[i][0],
.src_addr = (void*)ADC_LL_REG_TO_APB_REG(ADC_CHANNEL_DATA_OUT_REG(ADC_CHANNEL)),
.transfer_count = sizeof(uint16_t),
.src_width = DMA_SRC_WIDTH_BIT_12,
.dst_width = DMA_DEST_WIDTH_BIT_16,
.descriptor_num = ADC_BUFFER_SIZE,
.tx_desc_addr = (uint32_t)&dma_desc_t[i][0],
.cycle_mode = false,
.repeat_mode = true,
.stop_mode = false,
.start_mode = DMA_START_TRIGGERED,
.interrupt_enable = true,
.end_callback = NULL,
};
dma_channel_t channel = dma_channel_alloc(DMA_CHANNEL_AUTO, &config, DMA_ALLOC_FROM_POOL);
if (channel == NULL) {
printf("Failed to allocate DMA channel\n");
abort();
}
dma_channel[i] = channel;
dma_desc_t* desc = dma_channel_get_descriptor(channel);
for (int j = 0; j < ADC_BUFFER_SIZE; j++) {
desc[j].src_addr = (uint32_t)ADC_LL_REG_TO_APB_REG(ADC_CHANNEL_DATA_OUT_REG(ADC_CHANNEL));
desc[j].dst_addr = (uint32_t)&adc_buffer[i][j];
desc[j].transfer_count = sizeof(uint16_t);
desc[j].next_desc_addr = (uint32_t)&desc[(j + 1) % ADC_BUFFER_SIZE];
}
desc[ADC_BUFFER_SIZE - 1].next_desc_addr = (uint32_t)&desc[0];
ESP_ERROR_CHECK(dma_channel_start(channel));
ESP_ERROR_CHECK(dma_set_channel_priority(channel, DMA_PRIORITY_HIGH));
ESP_ERROR_CHECK(dma_set_channel_interrupt_level(channel, ESP_INTR_FLAG_LEVEL1));
ESP_ERROR_CHECK(dma_register_isr(channel, dma_isr_handler, (void*)i, ESP_INTR_FLAG_IRAM, NULL));
xTaskCreatePinnedToCore(µphone_task, "microphone_task", 4096, (void*)i, 5, &task_handle[i], i);
}
}
This code uses two circular buffers for each microphone, one for reading and one for writing. The DMA controller is configured to transfer data from the ADC channel to the write buffer, and triggers an interrupt when it reaches the end of the buffer. The interrupt handler signals a semaphore to indicate that the buffer is ready to be processed.
In the microphone_task function, we wait for the semaphore to be signaled, process the audio data