feat(i2s): support to select PDM data format

This commit is contained in:
laokaiyao
2024-08-15 19:02:42 +08:00
parent 390745c999
commit 9b779d8b3c
37 changed files with 863 additions and 254 deletions

View File

@@ -15,6 +15,7 @@
#include "sdkconfig.h"
#include "i2s_pdm_example.h"
#include "i2s_example_pins.h"
#include "esp_log.h"
#define EXAMPLE_PDM_RX_CLK_IO EXAMPLE_I2S_BCLK_IO1 // I2S PDM RX clock io number
#define EXAMPLE_PDM_RX_DIN_IO EXAMPLE_I2S_DIN_IO1 // I2S PDM RX data in io number
@@ -24,10 +25,21 @@
#define EXAMPLE_PDM_RX_DIN3_IO EXAMPLE_I2S_DIN3_IO1 // I2S PDM RX data line3 in io number
#endif
#define EXAMPLE_PDM_RX_FREQ_HZ 16000 // I2S PDM RX frequency
#if SOC_I2S_SUPPORTS_PDM2PCM
#define EXAMPLE_PDM_RX_FREQ_HZ 16000 // I2S PDM RX frequency in PCM format
#else
#define EXAMPLE_PDM_RX_FREQ_HZ 2048000 // I2S PDM RX over sample frequency in raw PDM format
#endif
static const char *TAG = "i2s_pdm_rx";
static i2s_chan_handle_t i2s_example_init_pdm_rx(void)
{
#if SOC_I2S_SUPPORTS_PDM2PCM
ESP_LOGI(TAG, "I2S PDM RX example (receiving data in PCM format)");
#else
ESP_LOGI(TAG, "I2S PDM RX example (receiving data in raw PDM format)");
#endif // SOC_I2S_SUPPORTS_PDM2PCM
i2s_chan_handle_t rx_chan; // I2S rx channel handler
/* Setp 1: Determine the I2S channel configuration and allocate RX channel only
* The default configuration can be generated by the helper macro,
@@ -42,7 +54,12 @@ static i2s_chan_handle_t i2s_example_init_pdm_rx(void)
i2s_pdm_rx_config_t pdm_rx_cfg = {
.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(EXAMPLE_PDM_RX_FREQ_HZ),
/* The data bit-width of PDM mode is fixed to 16 */
.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
#if SOC_I2S_SUPPORTS_PDM2PCM
.slot_cfg = I2S_PDM_RX_SLOT_PCM_FMT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
#else
// For the target that not support PDM-to-PCM format, we can only receive RAW PDM data format
.slot_cfg = I2S_PDM_RX_SLOT_RAW_FMT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
#endif // SOC_I2S_SUPPORTS_PDM2PCM
.gpio_cfg = {
.clk = EXAMPLE_PDM_RX_CLK_IO,
#if SOC_I2S_PDM_MAX_RX_LINES == 4

View File

@@ -32,9 +32,11 @@ menu "Example Configuration"
config EXAMPLE_SAMPLE_RATE
int "Audio Sample Rate"
default 44100
default 44100 if SOC_I2S_SUPPORTS_PDM2PCM
default 5644800
help
Set the audio sample rate frequency. Usually 16000 or 44100 Hz.
Set the audio sample rate frequency. Usually 16000 or 44100 Hz if PCM data format supported.
Oversample rate usually can be 2048000 or 5644800 Hz if only raw PDM data format supported.
config EXAMPLE_BIT_SAMPLE
int "Audio Bit Sample"

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -22,6 +22,7 @@
#include "driver/spi_common.h"
#include "sdmmc_cmd.h"
#include "format_wav.h"
#include "esp_log.h"
static const char *TAG = "pdm_rec_example";
@@ -144,13 +145,22 @@ void record_wav(uint32_t rec_time)
void init_microphone(void)
{
#if SOC_I2S_SUPPORTS_PDM2PCM
ESP_LOGI(TAG, "Receive PDM microphone data in PCM format");
#else
ESP_LOGI(TAG, "Receive PDM microphone data in raw PDM format");
#endif // SOC_I2S_SUPPORTS_PDM2PCM
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, NULL, &rx_handle));
i2s_pdm_rx_config_t pdm_rx_cfg = {
.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(CONFIG_EXAMPLE_SAMPLE_RATE),
/* The default mono slot is the left slot (whose 'select pin' of the PDM microphone is pulled down) */
.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
#if SOC_I2S_SUPPORTS_PDM2PCM
.slot_cfg = I2S_PDM_RX_SLOT_PCM_FMT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
#else
.slot_cfg = I2S_PDM_RX_SLOT_RAW_FMT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
#endif
.gpio_cfg = {
.clk = CONFIG_EXAMPLE_I2S_CLK_GPIO,
.din = CONFIG_EXAMPLE_I2S_DATA_GPIO,