Skip to content

Commit 91f5462

Browse files
giulcioffipennam
authored andcommitted
Giga: Add PDM library
1 parent 57a7fbd commit 91f5462

File tree

3 files changed

+660
-0
lines changed

3 files changed

+660
-0
lines changed

libraries/PDM/src/giga/PDM.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
PDM.cpp - library to interface with STM32 PDM microphones
3+
Part of Arduino - http://www.arduino.cc/
4+
5+
Copyright (c) 2020 Arduino SA
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
#ifdef ARDUINO_GIGA
24+
25+
#include "PDM.h"
26+
#include "mbed.h"
27+
extern "C" {
28+
#include "audio.h"
29+
}
30+
31+
extern "C" uint16_t *g_pcmbuf;
32+
static PDMClass *_instance = NULL;
33+
34+
PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
35+
_dinPin(dinPin),
36+
_clkPin(clkPin),
37+
_pwrPin(pwrPin),
38+
_onReceive(NULL),
39+
_gain(-1),
40+
_channels(-1),
41+
_samplerate(-1),
42+
_init(-1)
43+
{
44+
_instance = this;
45+
}
46+
47+
PDMClass::~PDMClass()
48+
{
49+
_instance = NULL;
50+
}
51+
52+
int PDMClass::begin(int channels, int sampleRate) {
53+
if(_instance != this) {
54+
return 0;
55+
}
56+
57+
_channels = channels;
58+
_samplerate = sampleRate;
59+
60+
if (_gain == -1) {
61+
_gain = 3;
62+
}
63+
64+
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
65+
_doubleBuffer.swap(0);
66+
67+
if(py_audio_init(channels, sampleRate)) {
68+
if (py_audio_start_streaming()) {
69+
_init = 1;
70+
return 1;
71+
}
72+
}
73+
return 0;
74+
}
75+
76+
void PDMClass::end()
77+
{
78+
py_audio_stop_streaming();
79+
py_audio_deinit();
80+
}
81+
82+
int PDMClass::available()
83+
{
84+
size_t avail = _doubleBuffer.available();
85+
return avail;
86+
}
87+
88+
int PDMClass::read(void* buffer, size_t size)
89+
{
90+
int read = _doubleBuffer.read(buffer, size);
91+
return read;
92+
}
93+
94+
void PDMClass::onReceive(void(*function)(void))
95+
{
96+
_onReceive = function;
97+
if(_instance != this) {
98+
_instance = this;
99+
}
100+
}
101+
102+
void PDMClass::setGain(int gain)
103+
{
104+
_gain = gain;
105+
py_audio_gain_set(gain);
106+
}
107+
108+
void PDMClass::setBufferSize(int bufferSize)
109+
{
110+
_doubleBuffer.setSize(bufferSize);
111+
}
112+
113+
size_t PDMClass::getBufferSize()
114+
{
115+
return _doubleBuffer.getSize();
116+
}
117+
118+
#define HALF_TRANSFER_SIZE (256*_channels)
119+
static int g_pcmbuf_size=0;
120+
121+
void PDMClass::IrqHandler(bool halftranfer)
122+
{
123+
if (g_pcmbuf_size < _doubleBuffer.getSize()) {
124+
audio_pendsv_callback();
125+
g_pcmbuf += (HALF_TRANSFER_SIZE);
126+
g_pcmbuf_size += HALF_TRANSFER_SIZE*2;
127+
128+
if(g_pcmbuf_size == _doubleBuffer.getSize()) {
129+
_doubleBuffer.swap(g_pcmbuf_size);
130+
g_pcmbuf = (uint16_t*)_doubleBuffer.data();
131+
g_pcmbuf_size = 0;
132+
if (_onReceive) {
133+
_onReceive();
134+
}
135+
}
136+
}
137+
}
138+
139+
extern "C" {
140+
void PDMIrqHandler(bool halftranfer)
141+
{
142+
_instance->IrqHandler(halftranfer);
143+
}
144+
145+
void PDMsetBufferSize(int size) {
146+
_instance->setBufferSize(size);
147+
}
148+
149+
size_t PDMgetBufferSize() {
150+
return _instance->getBufferSize();
151+
}
152+
}
153+
154+
PDMClass PDM(0, 0, 0);
155+
156+
#endif

0 commit comments

Comments
 (0)