Code for measuring the signal with a specified length and sampling rate, and saving it on a SD card.

Dependencies:   EALib I2S mbed

Committer:
msamadani
Date:
Thu Oct 05 17:44:39 2017 +0000
Revision:
2:8c5b6522139f
Parent:
0:c05b00be2229
A version of the code that initializes the SD RAM, activates the codec, measures the signal and saves the signal onto the SD Card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
msamadani 0:c05b00be2229 1
msamadani 0:c05b00be2229 2 #include "mbed.h"
msamadani 0:c05b00be2229 3 #include "measureSignal.h"
msamadani 0:c05b00be2229 4 #include "WM8731.h"
msamadani 0:c05b00be2229 5 #include "I2S.h"
msamadani 0:c05b00be2229 6
msamadani 0:c05b00be2229 7
msamadani 0:c05b00be2229 8 /* Define codec and I2S instances */
msamadani 0:c05b00be2229 9 WM8731 _codec(P0_27, P0_28);
msamadani 0:c05b00be2229 10 I2S _i2sTx(I2S_TRANSMIT, p11, p12, p13);
msamadani 0:c05b00be2229 11 I2S _i2sRx(I2S_RECEIVE, p14, p33, p34);
msamadani 0:c05b00be2229 12
msamadani 0:c05b00be2229 13
msamadani 0:c05b00be2229 14
msamadani 0:c05b00be2229 15 void measureSignal(int signal[],int fs,int N)
msamadani 0:c05b00be2229 16 {
msamadani 0:c05b00be2229 17
msamadani 0:c05b00be2229 18 printf("Sampling data....\n");
msamadani 0:c05b00be2229 19 int _rxBuf[16];
msamadani 0:c05b00be2229 20 int signalPtr=0;
msamadani 0:c05b00be2229 21
msamadani 0:c05b00be2229 22
msamadani 0:c05b00be2229 23 _codec.power(true);
msamadani 0:c05b00be2229 24 _codec.frequency(fs);
msamadani 0:c05b00be2229 25 _codec.wordsize(32);
msamadani 0:c05b00be2229 26 _codec.master(true);
msamadani 0:c05b00be2229 27 _codec.input_select(WM8731_MIC); //select mic as input.
msamadani 0:c05b00be2229 28 //_codec.input_power(true);
msamadani 0:c05b00be2229 29 //_codec.input_mute(false);
msamadani 0:c05b00be2229 30 //_codec.microphone_boost(true);
msamadani 0:c05b00be2229 31 _codec.start();
msamadani 0:c05b00be2229 32
msamadani 0:c05b00be2229 33 _i2sRx.frequency(fs);
msamadani 0:c05b00be2229 34 _i2sRx.wordsize(32);
msamadani 0:c05b00be2229 35 _i2sRx.stereomono(I2S_STEREO);
msamadani 0:c05b00be2229 36 _i2sRx.masterslave(I2S_SLAVE);
msamadani 0:c05b00be2229 37 //_i2sRx.set_interrupt_fifo_level(7);
msamadani 0:c05b00be2229 38 _i2sRx.start();
msamadani 0:c05b00be2229 39
msamadani 0:c05b00be2229 40 wait(0.5);
msamadani 0:c05b00be2229 41
msamadani 0:c05b00be2229 42 while (signalPtr<N) {
msamadani 0:c05b00be2229 43 int to_read = _i2sRx.fifo_points(); //Find out how many stereo samples to read
msamadani 0:c05b00be2229 44 _i2sRx.read(_rxBuf, to_read);
msamadani 0:c05b00be2229 45
msamadani 0:c05b00be2229 46 //Loop through all stereo samples
msamadani 0:c05b00be2229 47 for(int i = 0; i <to_read; i+=2) {
msamadani 0:c05b00be2229 48 //Only fill echo buffer with right side samples, i.e., ignore rxBuf[i+1] content
msamadani 0:c05b00be2229 49 signal[signalPtr] = _rxBuf[i];
msamadani 0:c05b00be2229 50
msamadani 0:c05b00be2229 51 //Increment echo buffer write pointer and check for wrap-around
msamadani 0:c05b00be2229 52 signalPtr++;
msamadani 0:c05b00be2229 53 }
msamadani 0:c05b00be2229 54 }
msamadani 0:c05b00be2229 55 _codec.stop();
msamadani 0:c05b00be2229 56 _i2sRx.stop();
msamadani 0:c05b00be2229 57 printf("Signal sampled.\n\n");
msamadani 0:c05b00be2229 58 }