Storing and accessing PDM data from microphone input

Hi

I'm new to programming and recently started working on nrf52832.

I'm working on a wearable device that monitors patient's vital. I'm using MP34DB02 digital microphone to record patient breathing to detect difficulty in breathing. I can't read input from the mic and creating and accessing pdm data stored in buffer. This is the code I have so far:

ret_code_t drv_audio_enable(void)
    {
        nrf_delay_ms(100);
        
        //Start audio capture
        return nrf_drv_pdm_start();
    }

ret_code_t drv_audio_disable(void)
    {
        ret_code_t status;

        //Stop audio capture
        status = nrf_drv_pdm_stop();
        if(status!= NRF_SUCCESS)
        {
          return status;
        }
        
        return NRF_SUCCESS;
    }


static void drv_audio_pdm_event_handler(nrf_drv_pdm_evt_t const * const p_evt)
{
    int16_t *p_buffer_released = p_evt->buffer_released;

    
    
    if(p_evt->buffer_requested)
    {
       int16_t *p_buffer;
       
       
       if(!p_buffer)
       {
          NRF_LOG_WARNING("%s(): WARNING: Cannot allocate audio buffer!", __func__);
          //We have to provide fresh buffer to keep PDM running.
          //Since pool is empty, our sole option is to reuse the released buffer.

          if(p_buffer_released)
          {
             p_buffer          = p_buffer_released;
             p_buffer_released = NULL;
             
          }
       }

       if(p_buffer)
       {
          NRF_LOG_DEBUG("Buffer Request: 0x%08X%s",
                         p_buffer,
                         (p_buffer == p_evt->buffer_released) ?"(reusing released buffer)":"");

          APP_ERROR_CHECK(nrf_drv_pdm_buffer_set(p_buffer, CONFIG_PDM_BUFFER_SIZE_SAMPLES));
       }

   }

   if(p_buffer_released)
   {
        NRF_LOG_DEBUG("Buffer Release: 0x%08X", p_buffer_released);

   }

}


ret_code_t drv_audio_init(void)
{
    nrf_drv_pdm_config_t pdm_cfg = NRF_DRV_PDM_DEFAULT_CONFIG(clk_audio,
                                                              dout);
                                                              
    ret_code_t err_code; // a variable to hold error code value
    pdm_cfg.gain_l      = CONFIG_PDM_GAIN;
    pdm_cfg.gain_r      = CONFIG_PDM_GAIN;
    pdm_cfg.edge = NRF_PDM_EDGE_LEFTFALLING;

    nrf_gpio_cfg_output(lrsel);
    nrf_gpio_pin_clear(lrsel);

    NRF_LOG_FLUSH(); // flushing is important, if you set the deffered to 1, if deffered is set to 0 then we don't need to flush the log buffer. 
    
    //Initialize PDM driver
    return nrf_drv_pdm_init(&pdm_cfg, drv_audio_pdm_event_handler);

}



int main(void)
{

// initialize the Logger so that we can print msgs on the logger
  APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); 
  NRF_LOG_DEFAULT_BACKENDS_INIT();
  
  
  NRF_LOG_INFO("Application Started");
  
  
  NRF_LOG_FLUSH(); // flushing is necessary if deferred is set to 1(check this video tutorial to know it better)

  
  APP_ERROR_CHECK(drv_audio_init());
  drv_audio_enable();
  //nrf_delay_ms(100);
  drv_audio_disable();



}

What am I missing?

Regards

Raj

Related