This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PDM working??

Hello, 

I've been posting PDM questions for a while now and I think I finally have something, but I'm not sure.

/*  
		C Program to run a pulse density modulation (PDM) Microphone on the nRF9160 Dev Kit using the Nordic SDK
*/

#include <nrf9160.h>
#include <zephyr.h>
#include <misc/printk.h>
#include <string.h>
#include <stdlib.h>
#include <nrfx_pdm.h>
#include <stdint.h>
#include <gpio.h>



#define PDM_BUF_SIZE 3072  //length of buffer in 16 bit words

int16_t pdm_buf[PDM_BUF_SIZE];

void nrfx_pdm_event_handler(nrfx_pdm_evt_t const *const p_evt)
{
	if (p_evt->buffer_requested) {
		nrfx_pdm_buffer_set(pdm_buf, PDM_BUF_SIZE);
	}
	if (p_evt->buffer_released != 0) {
		printk("Out: %4x @ 0x%x\r\n %4x @ 0x%x\r\n",
		       (uint16_t)pdm_buf[0], &pdm_buf[0],
		       (uint16_t)pdm_buf[1], &pdm_buf[1]);
	}
}

static void pdm_init(void)
{
	nrfx_pdm_config_t pdm_config = NRFX_PDM_DEFAULT_CONFIG(
		10, 11); /*configures CLK to pin 10 and Din to pin 11*/
	nrfx_pdm_init(&pdm_config, nrfx_pdm_event_handler);
}

/*ISR workaround given by Nordic*/
ISR_DIRECT_DECLARE(pdm_isr_handler)
{
	nrfx_pdm_irq_handler();
	ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency
			  */

	return 1; /* We should check if scheduling decision should be made */
}

void main(void)
{
	IRQ_DIRECT_CONNECT(PDM_IRQn, 0, pdm_isr_handler, 0);  //workaround line 
	printk("Starting PDM program!\n");
	printk("PDM Buffer size: %d in 16 bit words\n", PDM_BUF_SIZE);

	/*
	bool enabled = nrfx_pdm_enable_check();
	printk(enabled ? "true/n" : "false\n");
	*/

	pdm_init();
	printk("%4x is the starting address\n", &pdm_buf);
	printk("%4x is current value at %x\n", pdm_buf[0], &pdm_buf);
	printk("The PDM will start reading NOW...\n");
      /*  for (int i = 0; i < 500; i++) {
          int k = 5;
          for (volatile int j = 0; j < 50; j++) {
                k = !k;
		}
	}*/
	nrfx_pdm_start();
	for (int i = 0; i < 100; i++) {
          int k = 5;
          for (volatile int j = 0; j < 100; j++) {
                k = !k;
		}
	}
	nrfx_pdm_stop();

	printk("%4x is current value at %x\n", pdm_buf[0], &pdm_buf);
}

When I run my code w/ the PDM Mic connected I get this output on the monitor.

However when I go through the debugger and set up these breakpoints

These are my outputs after the last breakpoint for RAM, pdm_buf [ ], and on the monitor.

Alternatively, when I disconnect the mic (removing the CLK and DATA lines to the DK)

I get this for outputs

I'm rather confused as to why my output looks the same when it's connected to the DK and when it's not (at least on the monitor side) if I just run with no breakpoints and why my output is so very different when I step through the code. 

Any help is appreciated. Thank you

  • Hi Octavio,

    Have you checked the signals you are getting from the microphone if they are even valid?

  • Hi Octavio,

    Have you checked the signals you are getting from the microphone if they are even valid?

  • You can check with a logic analyzer or something similar that you get some data from the microphone via the correct signals.

  • output data signalinput pulse

    Hi Martin,

    I have this problem, too. I use nrf52840 with SDK 17.1 libraries, I use MP34DT05A ST microphone with PDM protocol. upload my input pulse and output data pictures that microphone receive and returned to us

    #include <stdio.h>
    #include "nrf_delay.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_drv_pdm.h"
    #include "nrf_pdm.h"
    
    #define CONFIG_PDM_BUFFER_SIZE_SAMPLES 320
    
    // <o> PDM Decimation Filter Gain <0x00-0x50>
    // <i> For details on the PDM decimation filter, see the 'Decimation filter' section in the nRF52 Product Specification document.
    
    
    #define CONFIG_PDM_GAIN 0x28
    
    
    /**@brief Audio buffer handler. */
    //typedef void (*drv_audio_buffer_handler_t)(nrfx_pdm_evt_t const *p_buffer, uint16_t samples);
    
    
    static int16_t m_pdm_buff[2][CONFIG_PDM_BUFFER_SIZE_SAMPLES];
    
    //static drv_audio_buffer_handler_t m_buffer_handler;
    
    
    static void m_audio_buffer_handler(int16_t *p_buffer, uint16_t samples)
    {
        // Audio processing
    }
    
    static void drv_audio_pdm_event_handler(nrfx_pdm_evt_t const *p_buffer, uint16_t length)
    {
    	if (p_buffer->buffer_requested) {
    		nrfx_pdm_buffer_set(m_pdm_buff, 3027);
    	}
    	if (p_buffer->buffer_released != 0) {
    		printf("Out: %4x @ 0x%x\r\n %4x @ 0x%x\r\n",
    		       (uint16_t)m_pdm_buff[0], &m_pdm_buff[0],
    		       (uint16_t)m_pdm_buff[1], &m_pdm_buff[1]);
    	}
            //m_buffer_handler((int16_t *)p_buffer, length);
    }
    
    
    
    uint32_t drv_audio_init( )
    {
        nrf_drv_pdm_config_t pdm_cfg = NRF_DRV_PDM_DEFAULT_CONFIG(MIC_CLK,MIC_DOUT);
        return nrf_drv_pdm_init(&pdm_cfg, drv_audio_pdm_event_handler);
    }
    
    
    int main(void)
    {
        uint32_t err_code = NRF_SUCCESS;
    
    
    
        err_code = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("PDM  example started.\n\t");
        
        err_code =nrfx_pdm_enable_check();
        APP_ERROR_CHECK(err_code);
    
    
        // Initialize PDM and start receiving PDM data    //
        nrfx_pdm_buffer_set(&m_pdm_buff[0],CONFIG_PDM_BUFFER_SIZE_SAMPLES);
    
        err_code = drv_audio_init();
        APP_ERROR_CHECK(err_code);
        
        err_code =  nrf_drv_pdm_start();
        APP_ERROR_CHECK(err_code);
     
    	for (int i = 0; i < 300; i++) {
              int k = 5;
              for (volatile int j = 0; j < 100; j++) {
                    k = !k;
    		}
    	}
             printf("%4x is current value at %x\n", m_pdm_buff[0], &m_pdm_buff);
    
                
    
    
    }
    this buffer after start function is run.

Related