Skip to content

Commit 504ffc6

Browse files
committed
Interim stash: Implementing circular buffer
1 parent d1172d8 commit 504ffc6

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

libraries/PDM/src/PDM.cpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ SOFTWARE.
2424
AP3_PDM *ap3_pdm_handle = 0;
2525
am_hal_pdm_transfer_t sTransfer;
2626

27-
#define internalPDMDataBufferSize 4096 //Default is array of 4096 * 32bit
28-
uint32_t internalPDMDataBuffer[internalPDMDataBufferSize];
29-
3027
bool AP3_PDM::begin(ap3_gpio_pin_t pinPDMData, ap3_gpio_pin_t pinPDMClock)
3128
{
3229
_PDMhandle = NULL;
@@ -43,7 +40,16 @@ bool AP3_PDM::begin(ap3_gpio_pin_t pinPDMData, ap3_gpio_pin_t pinPDMClock)
4340

4441
bool AP3_PDM::available(void)
4542
{
46-
return (_PDMdataReady);
43+
if (_head != _tail)
44+
return (true);
45+
return (false);
46+
}
47+
48+
bool AP3_PDM::isOverrun(void)
49+
{
50+
if (_overrun == true)
51+
return (true);
52+
return (false);
4753
}
4854

4955
ap3_err_t AP3_PDM::_begin(void)
@@ -274,45 +280,45 @@ ap3_err_t ap3_pdm_pad_funcsel(ap3_pdm_pad_type_e type, ap3_gpio_pad_t pad, uint8
274280
//*****************************************************************************
275281
uint32_t AP3_PDM::getData(uint32_t *externalBuffer, uint32_t bufferSize)
276282
{
277-
if (_PDMdataReady)
278-
{
279-
if (bufferSize > internalPDMDataBufferSize)
280-
bufferSize = internalPDMDataBufferSize;
281-
282-
noInterrupts();
283+
if (bufferSize > circularBufferSize)
284+
bufferSize = circularBufferSize;
283285

284-
//Move data from internal buffer to external caller
285-
for (int x = 0; x < bufferSize; x++)
286-
externalBuffer[x] = internalPDMDataBuffer[x];
286+
noInterrupts();
287287

288-
interrupts();
289-
}
290-
_PDMdataReady = false;
288+
//Move data from internal buffer to external caller
289+
for (int x = 0; x < bufferSize; x++)
290+
externalBuffer[x] = _pdmCircularBuffer[x];
291291

292-
//Start next conversion
293-
am_hal_pdm_dma_start(_PDMhandle, &sTransfer);
292+
interrupts();
293+
}
294294
}
295295

296296
inline void AP3_PDM::pdm_isr(void)
297297
{
298298
uint32_t ui32Status;
299299

300-
//
301300
// Read the interrupt status.
302-
//
303301
am_hal_pdm_interrupt_status_get(_PDMhandle, &ui32Status, true);
304302
am_hal_pdm_interrupt_clear(_PDMhandle, ui32Status);
305303

306304
if (ui32Status & AM_HAL_PDM_INT_DCMP)
307305
{
308-
if (_PDMdataReady == true)
306+
//Move current DMA to circular buffer
307+
for (int x = 0; x < pdmDataBufferSize; x++)
308+
{
309+
_pdmCircularBuffer[head++] = _pdmDataBuffer[x];
310+
if (_head++ == circularBufferSize)
311+
_head = 0;
312+
}
313+
314+
if (_head == _tail)
309315
{
310-
//If flag has not previously been cleared, we're overrun
311-
//Serial.println("Buffer overrun!");
316+
Serial.println("Buffer overrun!");
317+
_overrun = true;
312318
}
313319

314-
//New data has been loaded into internalPDMDataBuffer
315-
_PDMdataReady = true;
320+
//Start next conversion
321+
am_hal_pdm_dma_start(_PDMhandle, &sTransfer);
316322
}
317323
}
318324

libraries/PDM/src/PDM.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class AP3_PDM
9696
{
9797
public:
9898
bool begin(ap3_gpio_pin_t pinPDMData = MIC_DATA, ap3_gpio_pin_t pinPDMClock = MIC_CLOCK);
99-
bool available(void); //Goes true once an interrupt has occured
99+
bool available(void); //Goes true if circular buffer is not empty
100+
bool isOverrun(void); //Goes true if head crosses tail
100101

101102
bool setClockSpeed(am_hal_pdm_clkspd_e clockSpeed);
102103
am_hal_pdm_clkspd_e getClockSpeed();
@@ -126,7 +127,17 @@ class AP3_PDM
126127

127128
ap3_err_t _begin(void);
128129

129-
volatile bool _PDMdataReady = false;
130+
//volatile bool _PDMdataReady = false;
131+
132+
volatile bool _head = 0;
133+
volatile bool _tail = 0;
134+
volatile bool _overrun = false;
135+
136+
const int pdmDataBufferSize = 512; //Default is array of 4096 * 32bit
137+
volatile uint32_t _pdmDataBuffer[pdmDataBufferSize]; //This has been filled previous to ISR being called
138+
139+
const int circularBufferSize = 4096;
140+
volatile uint32_t _pdmCircularBuffer[circularBufferSize]; //This is filled by ISR and read by getData
130141
};
131142

132143
#endif //_PDM_H_

0 commit comments

Comments
 (0)