
/*****************************************************************************/
/*                                                                           */
/* 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 "combfilters.h"
#include "math.h"

//#include "IIR_filters_fourth_order.h"

#include "IIR.h"

#include "Dsplib.h"
#include "FIR_LP_8.h"
#include "FIR_HP_8.h"


unsigned long int k = 0;

#define SAMPLES_PER_SECOND 8000
#define GAIN_IN_dB         10
#define N_Avg 80						// SAMPLES_PER_SECOND / 100

DATA H_Avg[N_Avg];
DATA buforAvg[N_Avg + 2];

// Bufory dla filtrów LP i HP
DATA buforHP[N_HP + 2];
DATA buforLP[N_LP + 2];

Int16 left_input;
Int16 right_input;
Int16 left_output;
Int16 right_output;
//Int16 mono_input;

Int16 input;

Int16 LP_out;
Int16 HP_out;

Int16 LP_out2;
Int16 HP_out2;

Int16 HP_minus_LP;

Int16 demAM1;
Int16 prog1;

Int16 demAM2;
Int16 prog2;

Int16 AvgOut;

double    f3dB = 50;
double    omega3dB;		
double    ALFA;

unsigned long int i = 0;
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


signed int wsp_BP[6] = {0, 0, 0, 0, 0, 0 };
signed int x_BP[3] = { 0, 0, 0 };  // próbki wejściowe
signed int y_BP[3] = { 0, 0, 0 };  // próbki wyjściowe

signed int wsp_LP_out[6] = {0, 0, 0, 0, 0, 0 };
signed int x_LP[3] = { 0, 0, 0 };  // próbki wejściowe
signed int y_LP[3] = { 0, 0, 0 };  // próbki wyjściowe

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  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("Demodulacja FSK\n");
    printf("częstotliwość nosnej = 1500Hz, dewiacja = 85Hz \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("Application 20    ", "      FSK         ");

    puts("\n Press SW1 for DOWN, SW2 for UP, SW1 + SW2 for reset\n");
	puts("1: Sygnał FSK przed i po filtracji BP(1500 Hz)");
	puts("2: Sygnał FSK po dyskryminacji filtrem HP i LP");
	puts("3: Wynik dyskryminacji podniesiony do kwadratu");
	puts("4: Róznica kwadratów przed o po filtracji filtrem LP(50Hz)");
	puts("5: Filtr LP (50 Hz) + progowanie");
	puts("6: Filtr uśredniający + progowanie");
	puts("7: Porównanie wyników progowania 1 i 2\n");

    asm(" bclr XF");
    // ---------------------------------- projekty filtrów --------------------------------							
    // Projekt filtru pasmowego   
    wsp_SOS_BP_Q14(1500, 300, SAMPLES_PER_SECOND, wsp_BP);    
       
    // projekt filtru LP 50Hz
    f3dB = 50;
    wsp_LP(f3dB, SAMPLES_PER_SECOND, wsp_LP_out); 
    
    // projekt filtru dopasowanego
    for(k = 0; k < N_Avg; k++)
    	H_Avg[k]=60000 / N_Avg;
      
 	for(;;)
 	{
     aic3204_codec_read(&left_input, &right_input); // Configured for one interrupt per two channels.

     input = SOS_Q14(x_BP, y_BP, wsp_BP, left_input);
     
     // DEMODULACJA FSK ------------------------------------------------------ początek demodulacji FSK 
     
     fir(&input, H_HP, &HP_out, buforHP, 1, N_HP); 	//dyskryminatory
     fir(&input, H_LP, &LP_out, buforLP, 1, N_LP);
             
     HP_out2 = _smpy(HP_out, HP_out);		        //do kawdratu   
     LP_out2 = _smpy(LP_out, LP_out);

     HP_minus_LP = _ssub(HP_out2, LP_out2); 		//odejmowanie
         
     demAM1 = IIR_I_order(x_LP, y_LP, wsp_LP_out, HP_minus_LP);            // filtr IIR LP 50 Hz
         
     if(demAM1 > 0) prog1 =  26000;					// progowanie
         else       prog1 = -26000;
         
     // Koniec demodulacji FSK  ---------------------------------------------- Koniec demodulacji FSK
     
     // II wariant filtru LP - filtr dopasowany do sygnału
     fir(&HP_minus_LP, H_Avg, &AvgOut, buforAvg, 1, N_Avg);  // filtr dopasowany
     
     if(AvgOut > 0) prog2 =  26000;						// progowanie
         else       prog2 = -26000;    

     step = pushbuttons_read(7); // 7 kroków
     
     switch (step)
      {      
       case 1:  
          if ( step != last_step)
          {
            oled_display_message("1: sygnal wejsciowy", " FSK - FSK + szum  ");
            puts("1: L -> Sygnał FSK, P -> Sygnał FSK po filtracji BP(1500 Hz)");
          }
		 
		 left_output = left_input;		 
		 right_output =  input;

       break; 
       
       case 2:
       	  if ( step != last_step)
          {
            oled_display_message("2: dyskryminacja  ", "LP_out  -  HP_out ");
            puts("2: Sygnał FSK po dyskryminacji filtrem HP i LP");
          }

         left_output = HP_out;   
         right_output = LP_out; 
       break;
       
       case 3:
         if ( step != last_step)
          {
            oled_display_message("3: do kwadratu     ", "LP_out  -  HP_out ");
            puts("3: Wynik dyskryminacji podniesiony do kwadratu");
          }
          
          left_output = HP_out2;   
          right_output = LP_out2;  
       break;       
       
       case 4:
          if ( step != last_step)
          {
            oled_display_message("4:  demodulacja AM ", "roznica - filtr LP ");
            puts("4: Róznica kwadratów przed o po filtracji filtrem LP(50Hz)");
          } 
         
         left_output = HP_out2 - LP_out2;
	     right_output = demAM1;                                   
       break;  
       
       case 5:
		  if ( step != last_step)
          {
            oled_display_message("5:  progowanie 1   ", "  przed i po       ");
            puts("5: Wynik filtracji LP(50 Hz) przed i po progowaniu");
          }
		 		 
		 left_output =  demAM1;
		 right_output = prog1;		 
		 
       break; 
       
       case 6:
		  if ( step != last_step)
          {
            oled_display_message("6:  progowanie 2   ", "  przed i po       ");
            puts("6: Filtr uśredniający + progowanie");
          }
          
		 left_output =  AvgOut;		 
		 right_output = prog2;

       break;
       
       case 7:
		  if ( step != last_step)
          {
            oled_display_message("7: progowanie 1 i 2", "porownanie wynikow ");
            puts("7: Porównanie wyników progowania dla różnych filtrów\nL -> LP (50 Hz)\nP -> filtr dopasowany (uśredniający)");
          }
          
		 left_output =  prog1;		 
		 right_output = prog2;		 

       break;
      }
    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                                                          *
 *                                                                          *
 * ------------------------------------------------------------------------ */

