Skip to content

PDM breakout #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libraries/PDM/src/PDM.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PDMClass
//NANO 33 BLE SENSe min 0 max 80
void setGain(int gain);
void setBufferSize(int bufferSize);
size_t getBufferSize();

// private:
void IrqHandler(bool halftranfer);
Expand All @@ -58,7 +59,7 @@ class PDMClass
int _init;

PDMDoubleBuffer _doubleBuffer;

void (*_onReceive)(void);
};

Expand Down
50 changes: 40 additions & 10 deletions libraries/PDM/src/stm32/PDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
}

extern "C" uint16_t *g_pcmbuf;
static PDMClass *_instance = NULL;

PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
_dinPin(dinPin),
Expand All @@ -40,10 +41,12 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
_samplerate(-1),
_init(-1)
{
_instance = this;
}

PDMClass::~PDMClass()
{
_instance = NULL;
}

int PDMClass::begin(int channels, int sampleRate) {
Expand All @@ -63,14 +66,21 @@ int PDMClass::begin(int channels, int sampleRate) {
i2c.write(8 << 1, data, sizeof(data));
}

if(_instance != this) {
return 0;
}

_channels = channels;
_samplerate = sampleRate;

if (_gain == -1) {
_gain = 24;
}

if(py_audio_init(channels, sampleRate, _gain, 0.9883f)) {
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
_doubleBuffer.swap(0);

if(py_audio_init(channels, sampleRate, gain_db, 0.9883f)) {
py_audio_start_streaming();
_init = 1;
return 1;
Expand Down Expand Up @@ -99,6 +109,9 @@ int PDMClass::read(void* buffer, size_t size)
void PDMClass::onReceive(void(*function)(void))
{
_onReceive = function;
if(_instance != this) {
_instance = this;
}
}

void PDMClass::setGain(int gain)
Expand All @@ -114,27 +127,44 @@ void PDMClass::setBufferSize(int bufferSize)
_doubleBuffer.setSize(bufferSize);
}

size_t PDMClass::getBufferSize()
{
return _doubleBuffer.getSize();
}

#define HALF_TRANSFER_SIZE (64*_channels)
static int g_pcmbuf_size=0;

void PDMClass::IrqHandler(bool halftranfer)
{
if (_doubleBuffer.available() == 0) {
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
if (g_pcmbuf_size < _doubleBuffer.getSize()) {
audio_pendsv_callback();
_doubleBuffer.swap(_doubleBuffer.availableForWrite());
}

if (_onReceive) {
_onReceive();
g_pcmbuf += (HALF_TRANSFER_SIZE/2);
g_pcmbuf_size += HALF_TRANSFER_SIZE;

if(g_pcmbuf_size == _doubleBuffer.getSize()) {
_doubleBuffer.swap(g_pcmbuf_size);
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
g_pcmbuf_size = 0;
if (_onReceive) {
_onReceive();
}
}
}
}

extern "C" {
void PDMIrqHandler(bool halftranfer)
{
PDM.IrqHandler(halftranfer);
_instance->IrqHandler(halftranfer);
}

void PDMsetBufferSize(int size) {
PDM.setBufferSize(size);
_instance->setBufferSize(size);
}

size_t PDMgetBufferSize() {
return _instance.getBufferSize();
}
}

Expand Down
7 changes: 5 additions & 2 deletions libraries/PDM/src/stm32/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,11 @@ int py_audio_init(size_t channels, uint32_t frequency, int gain_db, float highpa
PDM_Filter_setConfig(&PDM_FilterHandler[i], &PDM_FilterConfig[i]);
}

PDMsetBufferSize(samples_per_channel * g_o_channels * sizeof(int16_t));
//g_pcmbuf = malloc(samples_per_channel * g_channels * sizeof(int16_t));
uint32_t min_buff_size = samples_per_channel * g_o_channels * sizeof(int16_t);
uint32_t buff_size = PDMgetBufferSize();
if(buff_size < min_buff_size) {
PDMsetBufferSize(min_buff_size);
}

return 1;
}
Expand Down
5 changes: 5 additions & 0 deletions libraries/PDM/src/utility/PDMDoubleBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ void PDMDoubleBuffer::setSize(int size)
reset();
}

size_t PDMDoubleBuffer::getSize()
{
return _size;
}

void PDMDoubleBuffer::reset()
{
_buffer[0] = (uint8_t*)realloc(_buffer[0], _size);
Expand Down
1 change: 1 addition & 0 deletions libraries/PDM/src/utility/PDMDoubleBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PDMDoubleBuffer
virtual ~PDMDoubleBuffer();

void setSize(int size);
size_t getSize();

void reset();

Expand Down