Skip to content

Commit 11b12f1

Browse files
authored
Merge pull request #138 from facchinm/pdm_revamp
PDM: cleanup and fix
2 parents 66f1f03 + 8ed5830 commit 11b12f1

File tree

9 files changed

+350
-3351
lines changed

9 files changed

+350
-3351
lines changed

libraries/PDM/src/stm32/PDM.cpp

+36-69
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
#ifdef TARGET_STM
2424

2525
#include "PDM.h"
26-
#include "audio.h"
2726
#include "mbed.h"
27+
extern "C" {
28+
#include "audio.h"
29+
}
2830

29-
#define AUDIO_FREQUENCY BSP_AUDIO_FREQUENCY_16K
30-
#define AUDIO_IN_PDM_BUFFER_SIZE (uint32_t)(128)
31-
32-
//ALIGN_32BYTES (uint16_t recordPDMBuf[AUDIO_IN_PDM_BUFFER_SIZE]) __attribute__((section(".OPEN_AMP_SHMEM")));
33-
// FIXME: need to add an entry for RAM_D3 to linker script
34-
uint16_t* recordPDMBuf = (uint16_t*)0x38000000;
31+
extern "C" uint16_t *g_pcmbuf;
3532

3633
PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
3734
_dinPin(dinPin),
@@ -45,14 +42,10 @@ PDMClass::~PDMClass()
4542
{
4643
}
4744

48-
int PDMClass::begin(int channels, long sampleRate) {
49-
50-
_channels = channels;
51-
52-
// fixme: only works in stereo mode
53-
channels = 2;
45+
static int gain_db = -1;
46+
static int _samplerate = -1;
5447

55-
setBufferSize(AUDIO_IN_PDM_BUFFER_SIZE / 4 * channels);
48+
int PDMClass::begin(int channels, long sampleRate) {
5649

5750
if (isBoardRev2()) {
5851
mbed::I2C i2c(PB_7, PB_6);
@@ -69,43 +62,36 @@ int PDMClass::begin(int channels, long sampleRate) {
6962
i2c.write(8 << 1, data, sizeof(data));
7063
}
7164

72-
BSP_AUDIO_IN_SelectInterface(AUDIO_IN_INTERFACE_PDM);
65+
_channels = channels;
66+
_samplerate = sampleRate;
7367

74-
/* Initialize audio IN at REC_FREQ*/
75-
if (BSP_AUDIO_IN_InitEx(INPUT_DEVICE_DIGITAL_MIC, AUDIO_FREQUENCY, DEFAULT_AUDIO_IN_BIT_RESOLUTION, channels) != AUDIO_OK)
76-
{
77-
return 0;
68+
if (gain_db == -1) {
69+
gain_db = 24;
7870
}
7971

80-
/* Start the record */
81-
BSP_AUDIO_IN_Record((uint16_t*)recordPDMBuf, AUDIO_IN_PDM_BUFFER_SIZE * channels);
82-
return 1;
72+
//g_pcmbuf = (uint16_t*)_doubleBuffer.data();
73+
74+
if(py_audio_init(channels, sampleRate, gain_db, 0.9883f)) {
75+
py_audio_start_streaming();
76+
return 1;
77+
}
78+
return 0;
8379
}
8480

8581
void PDMClass::end()
8682
{
83+
py_audio_stop_streaming();
84+
py_audio_deinit();
8785
}
8886

8987
int PDMClass::available()
9088
{
9189
size_t avail = _doubleBuffer.available();
92-
if (_channels == 1) {
93-
return avail/2;
94-
} else {
95-
return avail;
96-
}
90+
return avail;
9791
}
9892

9993
int PDMClass::read(void* buffer, size_t size)
10094
{
101-
if (_channels == 1) {
102-
uint16_t temp[size*2];
103-
int read = _doubleBuffer.read(temp, size*2);
104-
for (int i = 0; i < size; i++) {
105-
((uint16_t*)buffer)[i] = temp[i*2];
106-
}
107-
return read;
108-
}
10995
int read = _doubleBuffer.read(buffer, size);
11096
return read;
11197
}
@@ -117,7 +103,9 @@ void PDMClass::onReceive(void(*function)(void))
117103

118104
void PDMClass::setGain(int gain)
119105
{
120-
106+
gain_db = gain;
107+
//end();
108+
//begin(_channels, _samplerate);
121109
}
122110

123111
void PDMClass::setBufferSize(int bufferSize)
@@ -127,49 +115,28 @@ void PDMClass::setBufferSize(int bufferSize)
127115

128116
void PDMClass::IrqHandler(bool halftranfer)
129117
{
130-
131-
int start = halftranfer ? 0 : AUDIO_IN_PDM_BUFFER_SIZE;
132-
133-
if (BSP_AUDIO_IN_GetInterface() == AUDIO_IN_INTERFACE_PDM && _doubleBuffer.available() == 0) {
134-
135-
/* Invalidate Data Cache to get the updated content of the SRAM*/
136-
SCB_InvalidateDCache_by_Addr((uint32_t *)&recordPDMBuf[start], AUDIO_IN_PDM_BUFFER_SIZE * 2);
137-
138-
//memcpy((uint16_t*)_doubleBuffer.data(), (uint16_t*)&recordPDMBuf[start], AUDIO_IN_PDM_BUFFER_SIZE/2);
139-
BSP_AUDIO_IN_PDMToPCM((uint16_t*)&recordPDMBuf[start], (uint16_t*)_doubleBuffer.data());
140-
141-
/* Clean Data Cache to update the content of the SRAM */
142-
SCB_CleanDCache_by_Addr((uint32_t*)_doubleBuffer.data(), AUDIO_IN_PDM_BUFFER_SIZE * 2);
143-
118+
if (_doubleBuffer.available() == 0) {
119+
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
120+
audio_pendsv_callback();
144121
_doubleBuffer.swap(_doubleBuffer.availableForWrite());
145122
}
123+
146124
if (_onReceive) {
147125
_onReceive();
148126
}
149127
}
150128

151129
extern "C" {
152-
/**
153-
@brief Calculates the remaining file size and new position of the pointer.
154-
@param None
155-
@retval None
156-
*/
157-
__attribute__((__used__)) void BSP_AUDIO_IN_TransferComplete_CallBack(void)
158-
{
159-
PDM.IrqHandler(false);
160-
}
130+
void PDMIrqHandler(bool halftranfer)
131+
{
132+
PDM.IrqHandler(halftranfer);
133+
}
161134

162-
/**
163-
@brief Manages the DMA Half Transfer complete interrupt.
164-
@param None
165-
@retval None
166-
*/
167-
__attribute__((__used__)) void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
168-
{
169-
PDM.IrqHandler(true);
170-
}
135+
void PDMsetBufferSize(int size) {
136+
PDM.setBufferSize(size);
137+
}
171138
}
172139

173140
PDMClass PDM(0, 0, 0);
174141

175-
#endif
142+
#endif

0 commit comments

Comments
 (0)