Hi,
I am trying to use the nrfx_pdm driver with a microphone connected to the NRF52840DK. Currently, it is working but I cannot quite understand how handlers are functioning.
Can you please elaborate on this?
Here is the code example I did:
ISR_DIRECT_DECLARE(pdm_isr_handler)
{
nrfx_pdm_irq_handler();
ISR_DIRECT_PM(); /* PM done after servicing interrupt for best latency
*/
//printk("pdm_isr_handler\n");
return 1; /* We should check if scheduling decision should be made */
}
static void data_handler(nrfx_pdm_evt_t const * p_evt)
{
nrfx_err_t err = 0;
printk("data_handler error: %d, %d ", p_evt->error, p_evt->buffer_requested);
if(true == p_evt->buffer_requested){
err = nrfx_pdm_buffer_set(pdm_buffer, PDM_DATA_BLOCK_WORDS);
if(err != NRFX_SUCCESS)
{
printk("PDM buffer init error: %d\n", err);
}
else
{
printk("PDM buffer init OK\n");
}
}
if(p_evt->buffer_released != 0){
printk("buffer_released\n");
data_ready_flag = true;
}
}
void print_sound_data(void)
{
/* print PCM stream */
while(!data_ready_flag){
k_sleep(K_MSEC(1));
}
nrfx_pdm_stop();
memcpy(pdm_buffer, pdm_buffer_temp, sizeof(pdm_buffer));
data_ready_flag = false;
printk("PINRT DATA\n");
}
void main(void)
{
nrfx_pdm_config_t config_pdm = NRFX_PDM_DEFAULT_CONFIG(PDM_CLK_PIN, PDM_DIN_PIN);
//set buffer to zero
memset(pdm_buffer_temp, 0x0000, PDM_DATA_BLOCK_WORDS);
IRQ_DIRECT_CONNECT(PDM_IRQn, 0,
pdm_isr_handler, 0);
nrfx_err_t err = nrfx_pdm_init(&config_pdm, data_handler);
if(err != NRFX_SUCCESS){
printk("PDM init error: %d\n", err);
}
else{
printk("PDM init OK\n");
}
err = nrfx_pdm_start();
if(err != NRFX_SUCCESS){
printk("PDM start error: %d\n", err);
}
else{
printk("PDM start OK\n");
}
while (1)
{
//k_sleep(K_SECONDS(2));
printk("Hello World! %s\n", CONFIG_BOARD);
print_sound_data();
}
}
It is not clear how data is collected and using the handler.
cc Håkon Alseth
Best regards,
Vojislav