I am trying to interface the PDM mic (Link : https://www.adafruit.com/product/3492) with NRF52832 Devkit. I am using PIN 27 and 26 for Clock and Data Respectively.
I can see the PCM Data in the log. However, when I play the Audio, it generates only the chirping sound. Why so?
Code:-
#include "stdio.h"
#include <stdbool.h>
#include <stdint.h>
#include "nrf_drv_pdm.h"
#include "nrf_delay.h"
#include "app_error.h"
#include "boards.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "app_error.h"
#include <stdint.h>
#define PDM_CLK_PIN 27
#define PDM_DIN_PIN 26
#define BUFFER_SIZE 1024
static int16_t pdm_buffer[BUFFER_SIZE];
float calculate_volume_level(int16_t *buffer, size_t length) {
uint32_t sum = 0;
for (size_t i = 0; i < length; i++) {
sum += abs(buffer[i]);
}
float avg = (float)sum / length;
return avg;
}
void pdm_event_handler(nrfx_pdm_evt_t const *p_evt)
{
if (p_evt->buffer_requested)
{
APP_ERROR_CHECK(nrf_drv_pdm_buffer_set(pdm_buffer, BUFFER_SIZE));
}
if (p_evt->buffer_released != NULL)
{
for (int i = 0; i < BUFFER_SIZE; i++)
{
printf("%d\n", p_evt->buffer_released[i]);
}
}
}
void pdm_init(void)
{
nrf_drv_pdm_config_t pdm_cfg = NRF_DRV_PDM_DEFAULT_CONFIG(PDM_CLK_PIN, PDM_DIN_PIN);
pdm_cfg.mode = NRF_PDM_MODE_MONO;
pdm_cfg.edge = NRF_PDM_EDGE_LEFTFALLING;
pdm_cfg.clock_freq = NRF_PDM_FREQ_1032K;
pdm_cfg.gain_l = 10;
pdm_cfg.gain_r = 10;
APP_ERROR_CHECK(nrf_drv_pdm_init(&pdm_cfg, pdm_event_handler));
APP_ERROR_CHECK(nrf_drv_pdm_start());
}
int main(void)
{
bsp_board_init(BSP_INIT_LEDS);
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("PDM mic example started.");
pdm_init();
while (true)
{
NRF_LOG_FLUSH();
nrf_delay_ms(100);
}
}
Please help me out!!