
/*****************************************************************************/
/*                                                                           */
/* PROJECT                                                                   */
/*   Low Power Operation                                                     */
/*                                                                           */
/* FILENAME                                                                  */
/* 	 main.c                                                                  */
/*                                                                           */
/* DESCRIPTION                                                               */
/*   TMS320C5515 USB Stick Application 20 Low Power Operation                */  
/*   Alter the power taken by the TMS320C5515 USB stick by changing the      */
/*   frequency of the phase lock loop (PLL).                                 */
/*   Brightness of XF reflects the power taken by the processor.             */
/*                                                                           */
/* REVISION                                                                  */
/*   Revision: 1.00	                                                         */
/*   Author  : Richard Sikora                                                */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* HISTORY                                                                   */
/*                                                                           */
/*   Revision 1.00                                                           */
/*   5th March 2010. Original template code from Spectrum Digital.           */
/*                                                                           */
/*****************************************************************************/
/*
 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/ 
 * 
 * 
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions 
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the   
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/




//#include "bargraph.h"
#include "stdio.h"
#include "usbstk5515.h"
#include "usbstk5515_led.h"
#include "aic3204.h"
#include "PLL.h"
#include "stereo.h"
#include "oled.h"
#include "pushbuttons.h"

#include "Dsplib.h"


#define N_MAX 400
ushort  N = 377;

DATA bufor1[N_MAX + 2];
DATA bufor_FIR[N_MAX + 2];
DATA H_1[N_MAX];

DATA *adres;

int para = 0;
DATA x[2];
DATA r[2];
DATA des[2];

DATA krok = 300;

//DATA bufor2[N_2 + 2];

LDATA wyciek = 32767L;

Int16 left_input;
Int16 right_input;
Int16 left_output;
Int16 right_output;

Int16 old_input;


#define SAMPLES_PER_SECOND 48000
#define GAIN_IN_dB         0

unsigned long int i = 0;

short i0;


unsigned int step = 0;
unsigned int last_step = 99; /* Some out of range value */

extern Int16 counter1; /* Counter from AIC3204 initialization */

#define MAX_COUNT 64

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  main( )                                                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
void main( void )
{
    /* Initialize BSL */
    USBSTK5515_init( );
    
    pll_frequency_setup(100); //40, 12, 2
    
    /* Initialise hardware interface and I2C for code */
    aic3204_hardware_init();
    
    /* Initialise the AIC3204 codec */
	aic3204_init(); 
	
	/* Turn off the 4 coloured LEDs */
	USBSTK5515_ULED_init();
	
	/* Initialise the OLED LCD display */
	oled_init();
	SAR_init();
	/* Flush display buffer */
    oled_display_message("                 ", "                   ");


    printf("Filtry adaptacyjne\n");

	/* Set sampling frequency in Hz and ADC gain in dB */
    set_sampling_frequency_and_gain(SAMPLES_PER_SECOND, GAIN_IN_dB);

    oled_display_message("Laboratorium FCiPS ", "Filtry adaptacyjne ");

	/* New. Add descriptive text */ 

    //puts("\n Press SW1 for DOWN, SW2 for UP, SW1 + SW2 for reset\n");
	puts("1: Zwarcie do zerowania");
	puts("2: Filtr adaptacyjny");
	puts("3: Predykcja liniowa");
	puts("4: Predykcja liniowa");
	puts("5: Predykcja liniowa z regulowanym krokiem\n");

    asm(" bclr XF"); 
   
 	//for ( i = 0  ; i < SAMPLES_PER_SECOND * 600L  ;i++  )
 	
 	           
 	
 	
 	
 	
 	for(;;)
 	{

     aic3204_codec_read(&left_input, &right_input); // Configured for one interrupt per two channels.

     step = pushbuttons_read(5); // 4 Different PLL settings

     switch (step)
      {      
       case 1:
		 /* Zwarcie */ 
		 
		  if ( step != last_step)
          {
            oled_display_message("1: zwarcie         ", " do zerowania      ");
            puts("1: - > Zwarcie do zerowania");
            
          }
		 
		 right_output = right_input;		 
		 left_output =  left_input;

       break; 
       
       case 2:
         /* Straight through. No processing. 120 MHz clock */ 

         if ( step != last_step)
          {
            oled_display_message("L: FIR 1            ", "P: FIR 2            ");
            puts("2: -Adaptacyjny 1");
            
            para = 0;            
            for (i0 = 0; i0 < N+2; i0++) bufor1[i0] = 0; // clear delay buffer (a must)
            for (i0 = 0; i0 < N; i0++) H_1[i0] = 0;

          }			
		
		if(para == 0)
		{
			x[0]   = left_input;
			des[0] = right_input;
			right_output = right_input;
			left_output = r[0];
			para = 1;
		}
		else
		//if(para == 1)
		{
			x[1]   = left_input;
			des[1] = right_input;
			right_output = right_input;
			left_output = r[1];
			para = 0;
			dlms(x,  H_1, r, des,  bufor1, 1000, N, 2 );	
		}
					
 
       break;
       
       case 3:
       if ( step != last_step)
         {
            oled_display_message("L: FIR 1            ", "P: FIR 2            ");
            puts("2: -Adaptacyjny 2");
            
            para = 0;            
            for (i0 = 0; i0 < N+2; i0++) bufor1[i0] = 0; // clear delay buffer (a must)
            for (i0 = 0; i0 < N; i0++) H_1[i0] = 0;

          }			
		
		if(para == 0)
		{
			x[0]   = left_input;
			des[0] = right_input;
			right_output = des[1];
			left_output = r[1];
			para = 1;
		}
		else
		//if(para == 1)
		{
			x[1]   = left_input;
			des[1] = right_input;
			
			dlms(x,  H_1, r, des,  bufor1, 1000, N, 2 );
						
			right_output = des[0];
			left_output = r[0];
			para = 0;
				
		}
                                                   
       break;  
       
       case 4:
       if ( step != last_step)
         {
            oled_display_message("L: FIR 1            ", "P: FIR 2            ");
            puts("4: Predykcja liniowa z regulowanym krokiem");
            puts("   x -> left input");
            puts("   left output -> y,  right output -> x \n");
            
            para = 0;            
            for (i0 = 0; i0 < N+2; i0++) bufor1[i0] = 0; // clear delay buffer (a must)
            for (i0 = 0; i0 < N; i0++) H_1[i0] = 0;
            
            printf("Krok korekcji = ");
    		scanf("%d", &krok);
    		
    		printf("Rząd filtru N = ");
    		scanf("%d", &N);

          }	
           		
		// filtr predykcyjny w kanale lewym
		// filtr FIR w kanale prawym
		if(para == 0)
		{
			x[0]   = old_input;
			des[0] = left_input;
			left_output = r[1];
			para = 1;
			
			//for (i0 = 0; i0 < N; i0++)			
			//	H_1[i0] = _smpy(H_1[i0], 32767);
			
		}
		else
		//if(para == 1)
		{
			x[1]   = old_input;
			des[1] = left_input;
			right_output = des[0];
			
			dlms(x,  H_1, r, des,  bufor1, krok, N, 2 );
						
			
			left_output = r[0];
			para = 0;
				
		}
		
		old_input = left_input; 
		
		fir(&right_input, H_1, &right_output, bufor_FIR, 1, N);
		
		                                
       break;  
       
       case 5:
		 if ( step != last_step)
         {
            oled_display_message("L: FIR 1            ", "P: FIR 2            ");
            puts("5: Predykcja liniowa z regulowanym krokiem i FIR");
            puts("   x -> left input");
            puts("   left output -> y,  right output -> x \n");
            
            para = 0;            
            for (i0 = 0; i0 < N+2; i0++) bufor1[i0] = 0; // clear delay buffer (a must)
            for (i0 = 0; i0 < N; i0++) H_1[i0] = 0;
            
            printf("Krok korekcji = ");
    		scanf("%d", &krok);
    		
    		printf("Rząd filtru N = ");
    		scanf("%d", &N);

          }	
           		
		
		if(para == 0)
		{
			x[0]   = old_input;
			des[0] = left_input;
			left_output = r[1];
			para = 1;
			adres = &H_1[0];

		}
		else
		//if(para == 1)
		{
			x[1]   = old_input;
			des[1] = left_input;
			
			dlms(x,  H_1, r, des,  bufor1, krok, N, 2 );
						
			
			left_output = r[0];
			para = 0;
				
		}
		
		old_input = left_input;
		
		fir(&right_input, H_1, &right_output, bufor_FIR, 1, N);
         
       break; 
       
      }
     
   //  LED_PWM(); /* LED on bright for 120 MHz PLL. Dull for 1 MHz PLL. */

     last_step = step; 
      
     aic3204_codec_write(left_output, right_output);
 	}

   /* Disable I2S and put codec into reset */ 
    aic3204_disable();

    printf( "\n***Program has Terminated***\n" );
    SW_BREAKPOINT;
}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  End of main1.c                                                          *
 *                                                                          *
 * ------------------------------------------------------------------------ */

