#include "dsk5510_dual3006cfg.h"
#include "dsk5510.h"
#include "swi_process.h"
#include "dsplib.h"

#define N 1024
#define logN 10
#include "window.h"

/* comment the next line to use DSPLIB fft */
#define C_FFT

#ifdef C_FFT /* Use C FFT */
	
   /* function defined in lab4fft.c */
   void fft(void);

   /* FFT data buffers */
   int real[N]; /* Real part of data      */
   int imag[N]; /* Imaginary part of data */
	#include "lab4fft.c"
#else        /* Use DSPLIB FFT */

   /* Function defined by c_fft_given.asm */            
  	void bit_rev(void);

   /* FFT data buffers (declared in c_fft_given.asm) */
   extern int bit_rev_data[N*2]; /* Data output for bit-reverse function */
   extern int fft_data[N*2];     /* In-place FFT & Output array         */
 
#endif       /* C_FFT */


// all data processing should be done in SWI_ProcessBuffer

void SWI_ProcessBuffer()
{
	static unsigned int mbox_value = 0;
	short *psrc, *pdest;
	unsigned int i;

	mbox_value |= SWI_getmbox();

	// buffers are only processed when both transmit and receive are ready
	if((mbox_value & DMA_RECEIVE_DONE) && (mbox_value & DMA_TRANSMIT_DONE)) {
 		mbox_value = 0;

		// get buffer pointers
		psrc = receive_buffer[receive_buffer_to_process_index];
		pdest = transmit_buffer[transmit_buffer_to_fill_index];

		// samples are interleaved in input buffer 3-4-1-2
		// output buffer is organized 3-4-1-2-3-4-1-2
		// The following code would copy input from each input channel to the
		// respective output channel:
		/*
		for (i = 0; i < 1024; i++)
		{
			pdest[4*i] = psrc[4*i];			//channel 3 output is channel 3 input
			pdest[4*i+1] = psrc[4*i+1];		//channel 4 output is channel 4 input
			pdest[4*i+2] = psrc[4*i+2];		//channel 1 output is channel 1 input
			pdest[4*i+3] = psrc[4*i+3];		//channel 2 output is channel 2 input
		}
		*/
		
#ifdef C_FFT /* Use C FFT */
    /* I n s e r t   c o d e   t o   f i l l   */
	/* C   F F T   b u f f e r s               */

#else        /* Use DSPLIB FFT */
    /* I n s e r t   c o d e   t o   f i l l   */
	/* a s s e m b l y   F F T   b u f f e r s */

#endif       /* C_FFT */
		


#ifdef C_FFT /* Use C FFT */

    /* Multiply the input signal by the Hamming window.       */
	/* . . . i n s e r t   C / a s m   code . . .             */

    /* Bit-reverse and compute FFT in C                       */
    	fft();

    /* Now, take absolute value squared of FFT                */
    /* . . . i n s e r t   C / a s m   code . . .             */
	
	/* Last, set the DC coefficient to -1 for a trigger pulse */
	/* . . . i n s e r t   C / a s m   code . . .             */

    /* done, wait for next time around!                       */

#else        /* Use DSPLIB FFT */

    /* Multiply the input signal by the Hamming window.       */
	/* . . . i n s e r t   C / a s m   code . . .             */

	/* Compute FFT using DSPLIB function                */
		cfft((DATA *)fft_data,N, SCALE);
	
	/* Bit reverse using assembly function			*/
    	bit_rev();

    /* Now, take absolute value squared of FFT                */
  	/* . . . i n s e r t   C / a s m   code . . .             */

    /* Last, set the DC coefficient to -1 for a trigger pulse */
    /* . . . i n s e r t   C / a s m   code . . .             */

    /* done, wait for next time around!                       */


#endif       /* C_FFT */
		
		receive_buffer_processed = 1; // flag receive buffer as processed
		transmit_buffer_filled = 1; // flag output buffer as full
	}
}

