forked from derkling/rtos2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Microphone.cpp
332 lines (277 loc) · 9.59 KB
/
Microphone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**************************************************************************
* Copyright (C) 2014 Riccardo Binetti, Guido Gerosa, Alessandro Mariani *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#include <cstdio>
#include "miosix.h"
#include "miosix/kernel/buffer_queue.h"
#include "miosix/kernel/scheduler/scheduler.h"
#include "util/software_i2c.h"
#include "Microphone.h"
using namespace std;
using namespace miosix;
typedef Gpio<GPIOB_BASE,10> clk;
typedef Gpio<GPIOC_BASE,3> dout;
static const int bufferSize=512; //Buffer RAM is 4*bufferSize bytes
static const int bufNum = 2;
static Thread *waiting;
static BufferQueue<unsigned short,bufferSize,bufNum> *bq;
static bool enobuf=true;
static const char filterOrder = 4;
static const short oversample = 16;
static unsigned short intReg[filterOrder] = {0,0,0,0};
static unsigned short combReg[filterOrder] = {0,0,0,0};
static signed char pdmLUT[] = {-1, 1};
/**
* Configure the DMA to do another transfer
*/
static void IRQdmaRefill()
{
unsigned short *buffer;
if(bq->IRQgetWritableBuffer(buffer)==false)
{
enobuf=true;
return;
}
DMA1_Stream3->CR=0;
DMA1_Stream3->PAR=reinterpret_cast<unsigned int>(&SPI2->DR);
DMA1_Stream3->M0AR=reinterpret_cast<unsigned int>(buffer);
DMA1_Stream3->NDTR=bufferSize;
DMA1_Stream3->CR=DMA_SxCR_PL_1 | //High priority DMA stream
DMA_SxCR_MSIZE_0 | //Write 16bit at a time to RAM
DMA_SxCR_PSIZE_0 | //Read 16bit at a time from SPI
DMA_SxCR_MINC | //Increment RAM pointer
DMA_SxCR_TCIE | //Interrupt on completion
DMA_SxCR_EN; //Start the DMA
}
static void dmaRefill()
{
FastInterruptDisableLock dLock;
IRQdmaRefill();
}
/**
* DMA end of transfer interrupt
*/
void __attribute__((naked)) DMA1_Stream3_IRQHandler()
{
saveContext();
asm volatile("bl _Z17I2SdmaHandlerImplv");
restoreContext();
}
/**
* DMA end of transfer interrupt actual implementation
*/
void __attribute__((used)) I2SdmaHandlerImpl()
{
DMA1->LIFCR=DMA_LIFCR_CTCIF3 |
DMA_LIFCR_CTEIF3 |
DMA_LIFCR_CDMEIF3 |
DMA_LIFCR_CFEIF3;
bq->IRQbufferFilled(bufferSize);
IRQdmaRefill();
waiting->IRQwakeup();
if(waiting->IRQgetPriority()>Thread::IRQgetCurrentThread()->IRQgetPriority())
Scheduler::IRQfindNextThread();
}
/**
* This function allows to atomically check if a variable equals a specific
* value and if not, put the thread in wait state until said condition is
* satisfied. To wake the thread, another thread or an interrupt routine
* should first set the variable to the desired value, and then call
* wakeup() (or IRQwakeup()) on the sleeping thread.
* \param T type of the variable to test
* \param variable the variable to test
* \param value the value that will cause this function to return.
*/
template<typename T>
static void atomicTestAndWaitUntil(volatile T& variable, T value)
{
FastInterruptDisableLock dLock;
while(variable!=value)
{
Thread::IRQgetCurrentThread()->IRQwait();
{
FastInterruptEnableLock eLock(dLock);
Thread::yield();
}
}
}
/**
* Helper function that waits until a buffer is available for reading
* \return a readable buffer from bq
*/
static const unsigned short *getReadableBuffer()
{
FastInterruptDisableLock dLock;
const unsigned short *result;
unsigned int size;
while(bq->IRQgetReadableBuffer(result, size)==false)
{
waiting->IRQwait();
{
FastInterruptEnableLock eLock(dLock);
Thread::yield();
}
}
return result;
}
static void bufferEmptied()
{
FastInterruptDisableLock dLock;
bq->IRQbufferEmptied();
}
Microphone& Microphone::instance()
{
static Microphone singleton;
return singleton;
}
Microphone::Microphone() {
}
void Microphone::init(function<void (unsigned short*, unsigned int)> cback, unsigned int bufsize){
callback = cback;
PCMsize = bufsize;
}
void Microphone::start(){
recording = true;
readyBuffer = (unsigned short*) malloc(sizeof(unsigned short) * PCMsize);
processingBuffer = (unsigned short*) malloc(sizeof(unsigned short) * PCMsize);
{
FastInterruptDisableLock dLock;
//Enable DMA1 and SPI2/I2S2 and GPIOB and GPIOC
RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN;
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN;
//Configure GPIOs
clk::mode(Mode::ALTERNATE);
clk::alternateFunction(5);
clk::speed(Speed::_50MHz);
dout::mode(Mode::ALTERNATE);
dout::alternateFunction(5);
dout::speed(Speed::_50MHz);
// I2S PLL Clock Frequency: 135.5 Mhz
RCC->PLLI2SCFGR=(2<<28) | (271<<6);
RCC->CR |= RCC_CR_PLLI2SON;
}
//Wait for PLL to lock
while((RCC->CR & RCC_CR_PLLI2SRDY)==0) ;
// RX buffer not empty interrupt enable
SPI2->CR2 = SPI_CR2_RXDMAEN;
SPI2->I2SPR= SPI_I2SPR_MCKOE | 12;
//Configure SPI
SPI2->I2SCFGR = SPI_I2SCFGR_I2SMOD | SPI_I2SCFGR_I2SCFG_0 | SPI_I2SCFGR_I2SCFG_1 | SPI_I2SCFGR_I2SE;
NVIC_SetPriority(DMA1_Stream3_IRQn,2);//High priority for DMA
delayMs(10); //waits for the microphone to enable
pthread_create(&mainLoopThread,NULL,mainLoopLauncher,reinterpret_cast<void*>(this));
}
void* Microphone::mainLoopLauncher(void* arg){
reinterpret_cast<Microphone*>(arg)->mainLoop();
}
void Microphone::mainLoop(){
waiting = Thread::getCurrentThread();
pthread_t cback;
bool first=true;
bq=new BufferQueue<unsigned short,bufferSize,bufNum>();
NVIC_EnableIRQ(DMA1_Stream3_IRQn);
while(recording){
PCMindex = 0;
// process any new chunk of PDM samples
for (;;){
if(enobuf){
enobuf = false;
dmaRefill();
}
if(processPDM(getReadableBuffer(),bufferSize) == true){
// transcode until the specified number of PCM samples
break;
}
bufferEmptied();
}
// swaps the ready and the processing buffer: allows double buffering
//on the callback side
unsigned short* tmp;
tmp = readyBuffer;
readyBuffer = processingBuffer;
processingBuffer = tmp;
if (!first){
// if the previous callback is still running, wait for it to end.
// this implies that some samples may be lost
pthread_join(cback,NULL);
} else {
first = false;
}
pthread_create(&cback,NULL,callbackLauncher,reinterpret_cast<void*>(this));
}
}
void* Microphone::callbackLauncher(void* arg){
reinterpret_cast<Microphone*>(arg)->execCallback();
}
void Microphone::execCallback() {
callback(readyBuffer,PCMsize);
}
bool Microphone::processPDM(const unsigned short *pdmbuffer, int size) {
int remaining = PCMsize - PCMindex;
int length = std::min(remaining, size);
// convert couples 16 pdm one-bit samples in one 16-bit PCM sample
for (int i=0; i < length; i++){
processingBuffer[PCMindex++] = PDMFilter(pdmbuffer, i);
}
if (PCMindex < PCMsize) //if produced PCM sample are not enough
return false;
return true;
}
/*
* This function takes care of the transcoding from 16 PDM bit to 1 PCM sample
* via CIC filtering. Decimator rate: 16:1, CIC stages: 4.
*/
unsigned short Microphone::PDMFilter(const unsigned short* PDMBuffer, unsigned int index) {
short combInput, combRes;
// perform integration on the first word of the PDM chunk to be filtered
for (short i=0; i < 16; i++){
//integrate each single bit
intReg[0] += pdmLUT[(PDMBuffer[index] >> (15-i)) & 1];
for (short j=1; j < filterOrder; j++){
intReg[j] += intReg[j-1];
}
}
// the last cell of intReg contains the output of the integrator stage
combInput = intReg[filterOrder-1];
//apply the comb filter (with delay 1):
for (short i=0; i < filterOrder; i++){
combRes = combInput - combReg[i];
combReg[i] = combInput;
combInput = combRes;
}
return combRes;
}
Microphone::Microphone(const Microphone& orig) {
}
void Microphone::stop() {
// stop the software
recording = false;
// waits for the last PCM processing to end
pthread_join(mainLoopThread, NULL);
// reset the configuration registers to stop the hardware
NVIC_DisableIRQ(DMA1_Stream3_IRQn);
delete bq;
SPI2->I2SCFGR=0;
{
FastInterruptDisableLock dLock;
RCC->CR &= ~RCC_CR_PLLI2SON;
}
free(readyBuffer);
free(processingBuffer);
}
Microphone::~Microphone() {
}