Question about PDM library source code

Question/Issue:
Which one of PDMDoubleBuffer does pdm_data_ready_inference_callback really accesses?

Project ID:
Projects reference to Arduino PDM library.

Context/Use case:
I am using PlatformIO (on VSCode) to develope a audio recognition project on NANO 33 BLE.
I just have a look into the PDM library source code(under framework-arduino-mbed@3.5.4) and found that somewhere of the begin and IrqHandler function is beyond my understanding.

In begin, nrf_pdm_enable is called but SAMPLE.PTR/SAMPLE.MAXCOUNT are not well set, will this leads to an error that data incoming from PDM interface be fed to an unexpected place?

In IrqHandler, while STARTED_EVENT is received, SAMPLE.PTR is set to the other buffer and index is swapped, but why the comment is “// make the current one available for reading”?

And, _onReceive(=pdm_data_ready_inference_callback) is called after swapping, in such situation, is PDM.read actually copying data from one of the double-buffer which the PCM data is updating by PDM interface to sampleBuffer but not from the other one of the double-buffer which just filled?

  • In this file ei_microphone.cpp
  • variable inference.buf_select is toggled at line 156
    • (see inference.buf_select ^= 1).
  • This then controls what part of the double buffer is returned
    • (see inference.buffers[inference.buf_select])
1 Like

Hi @chihshan,

We currently using v4.0.2 of mbed_nano there the initial buffer set is commented out:

// set the buffer for transfer
  // nrf_pdm_buffer_set((uint32_t*)_doubleBuffer.data(), _doubleBuffer.availableForWrite() / (sizeof(int16_t) * _channels));
  // _doubleBuffer.swap();

I’m assuming NRF_PDM_EVENT_STARTED is called at start of DMA transmission where it first sets the buffer and starts streaming audio to that buffer. At start we always ignore the first buffers, because it contains no audio or distorted audio.

In the IRQ routine the nrf_pdm_buffer_set is first called to set the pointer to the current buffer. the __doubleBuffer.swap is called to switch to the other buffer for reading.

1 Like