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

Parents
  • I get constant values from the start and its always 0x00000001 and once in a while it displays 0x20001EA8. It's the same with every parameter I change. Here is the modified code for the PDM part of the program:

    int BLE_TEXT(uint8_t bletext[90])
    {
        if(connected)
        {
            uint16_t length=strlen(bletext);ble_nus_data_send(&m_nus, bletext, &length, m_conn_handle);
            nrf_gpio_pin_clear(ledPin);
        }
        else
        {
            nrf_gpio_pin_set(ledPin);
        }
    }
    
    
    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;
                 sprintf(lcdtext,"\n\rBuffer is released");BLE_TEXT(lcdtext);
                 
              }
           }
    
           if(p_buffer)
           {
              //NRF_LOG_DEBUG("Buffer Request: 0x%08X%s",
              //               p_buffer,
              //               (p_buffer == p_evt->buffer_released) ?"(reusing released buffer)":"");
              
              sprintf(lcdtext,"\n\rBuffer Request: 0x%08X %s",
                             p_buffer,
                             (p_buffer == p_evt->buffer_released) ?"(reusing released buffer)":"");
              BLE_TEXT(lcdtext);
              APP_ERROR_CHECK(nrf_drv_pdm_buffer_set(p_buffer, CONFIG_PDM_BUFFER_SIZE_SAMPLES));
           }
    
       }
    
       if(p_evt->buffer_released)
       {
            //NRF_LOG_DEBUG("Buffer Release: 0x%08X", p_buffer_released);
            sprintf(lcdtext,"\n\rBuffer Release: 0x%08X", p_buffer_released);BLE_TEXT(lcdtext);
            sprintf(lcdtext,"\n\rBuffer Release: 0x%08X", p_evt->buffer_released);BLE_TEXT(lcdtext);
           
       }
    	
    }
    
    
    
    
    ret_code_t drv_audio_init(/*nrf_balloc_t const *p_buffer_pool, drv_audio_buffer_handler_t buffer_handler*/)
    {
        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.mode        = 1;
        pdm_cfg.gain_l      = CONFIG_PDM_GAIN;
        pdm_cfg.gain_r      = CONFIG_PDM_GAIN;
        pdm_cfg.edge        = NRF_PDM_EDGE_LEFTFALLING;
        pdm_cfg.clock_freq  = 134217728; //1 MHz
    
        //Initialize PDM driver
        return nrf_drv_pdm_init(&pdm_cfg, drv_audio_pdm_event_handler);
           
    }
    
    
    
    /**@brief Application main function.
     */
    int main(void)
    {//1
    
        int count=1;
        nrf_gpio_pin_dir_set(ledPin,NRF_GPIO_PIN_DIR_OUTPUT);
        nrf_gpio_pin_dir_set(lrsel,NRF_GPIO_PIN_DIR_OUTPUT);
        nrf_gpio_pin_dir_set(clk_audio,NRF_GPIO_PIN_DIR_OUTPUT);
        nrf_gpio_pin_dir_set(dout,NRF_GPIO_PIN_DIR_INPUT);
        nrf_gpio_pin_clear(lrsel);
        
        log_init();
        timers_init();
     
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        advertising_start();
    
        APP_ERROR_CHECK(drv_audio_init());
        
    
        nrf_delay_ms(100);
        sprintf(lcdtext,"\n\n\tStarting application...\n");BLE_TEXT(lcdtext);
        
               
            
      uint8_t bletext[100];
      while(1)
      {
        if((connected==false)&(advertising_initiated==false))
             {//3
               advertising_start();
               advertising_initiated=true;
             }//3
        sprintf(bletext,"\n\rReading %d",count);
        count++;
        BLE_TEXT(bletext);
        drv_audio_enable();
        drv_audio_disable();
        nrf_delay_ms(1000);
      }
       
       
    }

  • Hi Raj,

    Could you provide minimal sample project and the steps which I can follow to test it?

    Note that if you have any private information which you don't want to be publicly visible there is a possibility to make the case private.

    Best regards,
    Dejan

Reply Children
No Data
Related