/*****************************************************************************/
/*                                                                           */
/* FILENAME                                                                  */
/* 	 main.c                                                                  */
/*                                                                           */
/* DESCRIPTION                                                               */
/*   TMS320C5515 USB Stick Application 20 Spectrum Analyser.                 */
/*   Take microphone input and send to headphones with ring modulation.      */
/*                                                                           */
/* REVISION                                                                  */
/*   Revision: 1.00	                                                         */
/*   Author  : Richard Sikora                                                */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* HISTORY                                                                   */
/*   Revision: 1.00                                                          */
/*   15th September 2010. Created from TMS320C5505 USB Stick code.           */
/*                                                                           */
/*****************************************************************************/
/*
 * 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 "stdio.h"
#include "usbstk5515.h"
#include "usbstk5515_led.h"
#include "aic3204.h"
#include "PLL.h"
#include "oled.h"
#include "pushbuttons.h"
#include "stereo.h"
#include "dsplib.h"
#include "math.h"
#include "combfilters.h"
#include "sinewaves.h"

#include "SinVCO.h"

#include "IIR.h"

#include "IIR_filters_fourth_order.h"

#include "IIR_low_pass_filters.h"

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

Int16 iloczyn = 10000;
Int16 fpol = 100;
Int16 feedback = 10000;
Int16 vcoIn = 12000;
Int16 out = 1000;

int fo = 12000;


short int *faza = 0;

signed int wsp1[6] = {0, 0, 0, 0, 0, 0 };
signed int x1[3] = { 0, 0, 0 };  // próbki wejściowe
signed int y1[3] = { 0, 0, 0 };  // próbki wyjściowe

#define SAMPLES_PER_SECOND 48000
#define GAIN_IN_dB 0

//unsigned long int i = 0;

unsigned int Step = 0; 
unsigned int LastStep = 99;

int VcoSin, VcoCos;
double ALFA;
double omega3dB;
double f3dB = 3000;


 
/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  main( )                                                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
void main( void ) 
{
    /* Initialize BSL */
    USBSTK5515_init( );
	
	/* Initialize PLL */
	pll_frequency_setup(120);

    /* 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("\n\nLaboratorium FCiPS\n");
    printf( "Ćwiczenie Demodulatory z pętlą PLL\n" );

	/* Setup sampling frequency and  gain for microphone */
    set_sampling_frequency_and_gain(SAMPLES_PER_SECOND, GAIN_IN_dB);

    oled_display_message("Application 20     ", "Spectrum Analyser  ");

	/* New. Add descriptive text */ 
	
    printf("\n Wciśnij SW1 <-- , SW2 -->, SW1 + SW2 for reset\n");
    printf("1: Transmisja sygnału z wejścia na wyjście\n");
    printf("2: Pętla PLL fo = 12 kHz, filtr LP 50 Hz\n");
    printf("3: Pętla PLL fo = 12 kHz, filtr LP 100 Hz\n");
    printf("4: Pętla PLL - dzielnik częstotliwości 12/2 = 6 kHz\n");
    printf("5: Demodulator FSK\n\n");

      
	/* Default to XF LED off */
	asm(" bclr XF");
    
 	for ( ;; )
 	{

     aic3204_codec_read(&left_input, &right_input); // Configured for one interrupt per two channels.
   
     mono_input = stereo_to_mono(left_input, right_input);
     
     Step = pushbuttons_read(5);
         
	  if ( Step == 1 )
        {
         if ( Step != LastStep )
          {
           oled_display_message("patrz na konsole  ", "i wpisz parametry  ");           
           printf("Podaj częstotliwość środkową VCO [Hz] fo = ");
    	   scanf("%d", &fo);
           printf("1: Transmisja sygnału z wejścia na wyjście\n");
           LastStep = Step;
          }	
         left_output = left_input;      // Straight trough. No processing.
	     right_output = right_input;

	    } 
	  
	    if ( Step == 2)
        {
         if ( Step != LastStep )
          {
           oled_display_message("STEP2 PPL +        ", "12khz              ");
           printf("2: Pętla PLL fo = %d kHz, filtr LP 50 Hz\n", fo);
           printf("Wejście Kanał lewy -->\n");
           printf("Wyjście\n");
           printf("--> Kanał lewy: wejście(kanał lewy) - transmisja sygnału z wejścia na wyjście\n");
           printf("--> Kanał prawy: wyjście VCO \n");
           
           wsp_LP(50, SAMPLES_PER_SECOND, wsp1);
           LastStep = Step;
          }	
          
          iloczyn = ((long)left_input * feedback) >> 15;
          out = IIR_I_order(x1, y1, wsp1, iloczyn);
          vcoIn = _sadd(fo, out);         
          feedback = SinVCO(faza, vcoIn);
          
          left_output = left_input;
          right_output = feedback;

	    } 
	    if ( Step == 3)
        {
         if ( Step != LastStep )
          {
           oled_display_message("STEP3 PPL +        ", "12khz              ");
           printf("3: Pętla PLL fo = %d kHz, filtr LP 100 Hz\n", fo);
           printf("Wejście Kanał lewy -->\n");
           printf("Wyjście\n");
           printf("--> Kanał lewy: wejście(kanał lewy) - transmisja sygnału z wejścia na wyjście\n");
           printf("--> Kanał prawy: wyjście VCO \n");
           
           wsp_LP(100, SAMPLES_PER_SECOND, wsp1);
           
           LastStep = Step;
          }	
          
          iloczyn = ((long)left_input * feedback) >> 15;
          out = IIR_I_order(x1, y1, wsp1, iloczyn);
          vcoIn = _sadd(fo, out);         
          feedback = SinVCO(faza, vcoIn);
          
          left_output = left_input;
          right_output = feedback;

	    } 
	    if ( Step == 4)
        {
         if ( Step != LastStep )
          {
           oled_display_message("STEP4 PLL dzielnik ", "6 khz              ");
           printf("4: Pętla PLL - dzielnik częstotliwości %d/2 = %d kHz\n", fo, fo/2);
           printf("Wejście Kanał lewy -->\n");
           printf("Wyjście\n");
           printf("--> Kanał lewy: wyjście VCO\n");
           printf("--> Kanał prawy: wejście(kanał lewy) \n");
           
           wsp_LP(50, SAMPLES_PER_SECOND, wsp1);
           LastStep = Step;
          }	
          
          iloczyn = ((long)left_input * feedback) >> 15;
          out = IIR_I_order(x1, y1, wsp1, iloczyn); 
          fpol = SinVCO(faza, 6000 + (out));                    
          feedback = ((long)fpol * fpol) >> 15;
          feedback -= 1 << 14;
          
          left_output = fpol;         
          right_output = left_input;

	    } 	
	
	    if ( Step == 5)
        {
         if ( Step != LastStep )
          {
          //set_sampling_frequency_and_gain(8000, GAIN_IN_dB);          
           f3dB = 150;
    	   wsp_LP(f3dB, SAMPLES_PER_SECOND, wsp1);
    	      	   
    	   oled_display_message("STEP5 PLL - dem FSK", "sin 1000           ");
    	   printf("5: Demodulator FSK\n");
    	   printf("Wejście Kanał lewy -->\n");
           printf("Wyjście\n");
           printf("--> Kanał lewy: wyjście VCO\n");
           printf("--> Kanał prawy: napięcie sterujące VCO\n");
           LastStep = Step;
          }	
          
          iloczyn = ((long)left_input * feedback) >> 15;
          out = IIR_I_order(x1, y1, wsp1, iloczyn);         
          feedback = SinVCO(faza, 1500 + (out >> 5));
         
          left_output = feedback;
          right_output = out; 
	    }  	    	    
	    
     aic3204_codec_write(left_output, right_output);

 	}

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

    printf( "\n***Program has Terminated***\n" );
    
    oled_display_message("PROGRAM HAS        ", "TERMINATED        ");
    
    SW_BREAKPOINT;
}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  End of main.c                                                           *
 *                                                                          *
 * ------------------------------------------------------------------------ */
