@@ -24,7 +24,6 @@ SOFTWARE.
2424AP3_PDM *ap3_pdm_handle = 0 ;
2525am_hal_pdm_transfer_t sTransfer ;
2626
27- // Temp
2827#define internalPDMDataBufferSize 4096 // Default is array of 4096 * 32bit
2928uint32_t internalPDMDataBuffer[internalPDMDataBufferSize];
3029
@@ -215,6 +214,8 @@ bool AP3_PDM::updateConfig(am_hal_pdm_config_t newConfiguration)
215214 _PDMconfig = newConfiguration;
216215 ap3_err_t retval = (ap3_err_t )am_hal_pdm_configure (_PDMhandle, &_PDMconfig);
217216
217+ am_hal_pdm_enable (_PDMhandle); // Reenable after changes
218+
218219 if (retval != AP3_OK)
219220 {
220221 return false ;
@@ -267,18 +268,23 @@ ap3_err_t ap3_pdm_pad_funcsel(ap3_pdm_pad_type_e type, ap3_gpio_pad_t pad, uint8
267268
268269// *****************************************************************************
269270//
270- // Start a transaction to get some number of bytes from the PDM interface.
271+ // Read PDM data from internal buffer
272+ // Returns number of bytes read.
271273//
272274// *****************************************************************************
273- void AP3_PDM::getData (uint32_t *externalBuffer, uint32_t bufferSize)
275+ uint32_t AP3_PDM::getData (uint32_t *externalBuffer, uint32_t bufferSize)
274276{
275277 if (_PDMdataReady)
276278 {
279+ if (bufferSize > internalPDMDataBufferSize)
280+ bufferSize = internalPDMDataBufferSize;
281+
277282 noInterrupts ();
278283
279284 // Move data from internal buffer to external caller
280285 for (int x = 0 ; x < bufferSize; x++)
281- externalBuffer[x] = internalPDMDataBuffer[x]; // Not clear, PDMDataBuffer is bufferSize * 2, do we need just the one left byte vs one right byte?
286+ externalBuffer[x] = internalPDMDataBuffer[x];
287+
282288 interrupts ();
283289 }
284290 _PDMdataReady = false ;
@@ -297,23 +303,16 @@ inline void AP3_PDM::pdm_isr(void)
297303 am_hal_pdm_interrupt_status_get (_PDMhandle, &ui32Status, true );
298304 am_hal_pdm_interrupt_clear (_PDMhandle, ui32Status);
299305
300- //
301- // Once our DMA transaction completes, we will disable the PDM and send a
302- // flag back down to the main routine. Disabling the PDM is only necessary
303- // because this example only implemented a single buffer for storing FFT
304- // data. More complex programs could use a system of multiple buffers to
305- // allow the CPU to run the FFT in one buffer while the DMA pulls PCM data
306- // into another buffer.
307- //
308306 if (ui32Status & AM_HAL_PDM_INT_DCMP)
309307 {
310308 if (_PDMdataReady == true )
311- Serial.println (" Buffer overrun!" );
312- else
313309 {
314- // New data has been loaded into internalPDMDataBuffer
315- _PDMdataReady = true ;
310+ // If flag has not previously been cleared, we're overrun
311+ // Serial.println("Buffer overrun!") ;
316312 }
313+
314+ // New data has been loaded into internalPDMDataBuffer
315+ _PDMdataReady = true ;
317316 }
318317}
319318
0 commit comments