Sunteți pe pagina 1din 3

#include <p33FJ256GP506.

h>
#include "..\h\WM8510CodecDrv.h"
#include "..\h\sask.h"
#include <dsp.h>
#include <libpic30.h>

_FGS(GWRP_OFF & GCP_OFF);


_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE);
_FWDT(FWDTEN_OFF);

#define LOG2_BLOCK_LENGTH 7
#define FFT_BLOCK_LENGTH 128
#define IFFT_BLOCK_LENGTH 128
#define FRAME_SIZE 128 /* Each audio frame will
have these many samples */

fractcomplex fft_input[FFT_BLOCK_LENGTH] __attribute__((space(ymemory),


aligned(FFT_BLOCK_LENGTH*2*2)));

fractional window[FFT_BLOCK_LENGTH] __attribute__((space(ymemory),


aligned(FFT_BLOCK_LENGTH*2)));

fractcomplex twiddleFactorsFFT[FFT_BLOCK_LENGTH/2] /* Declare Twiddle Factor


array in X-space*/
__attribute__ ((section (".xbss, bss, xmemory"), aligned (FFT_BLOCK_LENGTH*2)));

fractcomplex twiddleFactorsIFFT[IFFT_BLOCK_LENGTH/2] /* Declare Twiddle Factor


array in X-space*/
__attribute__ ((section (".xbss, bss, xmemory"), aligned (IFFT_BLOCK_LENGTH*2)));

/* Allocate memory for buffers and drivers */

int codecBuffer [WM8510DRV_DRV_BUFFER_SIZE];


int samples [FRAME_SIZE];

/* Instantiate the drivers */


WM8510Handle codec;

/* Create the driver handles */


WM8510Handle *codecHandle = &codec;

int main(void)
{
/* Configure Oscillator to operate the device at 40MHz.
* Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
* Fosc= 7.37M*40/(2*2)=80Mhz for 7.37M input clock */

PLLFBD=41; /* M=39 */
CLKDIVbits.PLLPOST=0; /* N1=2 */
CLKDIVbits.PLLPRE=0; /* N2=2 */
OSCTUN=0;

__builtin_write_OSCCONH(0x01); /* Initiate Clock Switch to FRC


with PLL*/
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0b01); /* Wait for Clock switch to occur
*/
while(!OSCCONbits.LOCK);

/* Intialize the board and the drivers */


SASKInit();
WM8510Init(codecHandle,codecBuffer);

/* Start Audio input and output function */


WM8510Start(codecHandle);

/* Configure codec for 8K operation */


WM8510SampleRate16KConfig(codecHandle);

TwidFactorInit (LOG2_BLOCK_LENGTH, &twiddleFactorsFFT[0], 0);


TwidFactorInit (LOG2_BLOCK_LENGTH, &twiddleFactorsIFFT[0], 1);

BlackmanInit( FFT_BLOCK_LENGTH, &window[128]);

while(1)
{
int i = 0;

/* Main processing loop. Executed for every input and


* output frame */

/* Obtaing the ADC samples */


while(WM8510IsReadBusy(codecHandle));
WM8510Read(codecHandle,samples,FRAME_SIZE);

for(i=0; i<FRAME_SIZE; i++)


{
fft_input[i].real = samples[i];
}

VectorWindow(FFT_BLOCK_LENGTH,&fft_input[FFT_BLOCK_LENGTH].real,&fft_input[FFT_BLOC
K_LENGTH].real,(fractional*)&window[0]);

for(i=0; i<FFT_BLOCK_LENGTH; i++)


{
fft_input[i].real = fft_input[i].real>>1;
fft_input[i].imag = 0x0000;
}

/* Perform FFT operation */

FFTComplexIP (LOG2_BLOCK_LENGTH, &fft_input[0], &twiddleFactorsFFT[0],


COEFFS_IN_DATA);

/* Store output samples in bit-reversed order of their addresses */


BitReverseComplex (LOG2_BLOCK_LENGTH, &fft_input[0]);

IFFTComplexIP (LOG2_BLOCK_LENGTH, &fft_input[0],


&twiddleFactorsIFFT[0], COEFFS_IN_DATA);

for(i=0; i<128; i++)


{
samples[i] = (fft_input[i].real<<8); //
}

/* Wait till the codec is available for a new frame */


while(WM8510IsWriteBusy(codecHandle));
/* Write the frame to the output */
WM8510Write (codecHandle,samples,FRAME_SIZE);