
/*****************************************************************************/
/*                                                                           */
/* 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"


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

#define SAMPLES_PER_SECOND 8000
#define GAIN_IN_dB         30

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

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  LED_PWM( ). PWM LED brightness based on PLL speed.                      *
 *                                                                          *
 * ------------------------------------------------------------------------ */


static void LED_PWM(void)
{
 static int counter = 0;
 
  if ( counter == 0)
   {
    asm ( " bset XF"); /* Minimum PWM rate */
   } 
  else if (counter >= MAX_COUNT)
   {
    asm ( " bset XF"); /* Maximum PWM rate 100% on */
   } 
  else if ( counter < counter1/10)
   {
    asm ( " bset XF");
   } 
  else
   {
    asm ( " bclr XF");
   } 
  
  counter++;
  
  if ( counter >= MAX_COUNT)
    {
      counter = 0;
    }  

}


/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  main( )                                                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
void main( void ) 
{
    /* Initialize BSL */
    USBSTK5515_init( );
    
    /* 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("Running Low Power Operation Project\n");
    printf("<-> Audio Loopback from Stereo Line IN --> to HP/Lineout\n\n" );
	printf("Updates settings once very 20 seconds\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     ", "Low Power Operation");

	/* New. Add descriptive text */ 
	puts("\n Bargraph at 6dB intervals");
    puts("\n Press SW1 for DOWN, SW2 for UP, SW1 + SW2 for reset\n");
	puts("Step 1    = PLL 120 MHz");
	puts("Step 2    = PLL 40 MHz");
	puts("Step 3    = PLL 12 MHz");
	puts("Step 4    = PLL 2 MHz");

    asm(" bclr XF");

   
 	for ( i = 0  ; i < SAMPLES_PER_SECOND * 600L  ;i++  )
 	{

     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(4); // 4 Different PLL settings
     
     switch (step)
      {
       case 1:
         /* Straight through. No processing. 120 MHz clock */ 

         if ( step != last_step)
          {
            pll_frequency_setup(100); /* Step has just changed. Update frequency */
            oled_display_message("Step 1 Fast PLL    ", "PLL = 120 MHz      ");
          }
          
         left_output = mono_input; 
         right_output = mono_input;
       break;
       
       case 2:
         /* Straight through. No processing. 40 MHz clock */ 

         if ( step != last_step)
          {
            pll_frequency_setup(40); /* Step has just changed. Update frequency */
            oled_display_message("Step 2 Medium PLL  ", " PLL = 40 MHz      ");
          }
          
         left_output = mono_input; 
         right_output = mono_input;
       break;       
       
       case 3:
         /* Straight through. No processing. 12 MHz clock */ 

          if ( step != last_step)
          {
            pll_frequency_setup(12); /* Step has just changed. Update frequency */
            oled_display_message("Step 3 Slow PLL    ", "PLL = 12 MHz       ");
          } 
         
         right_output = mono_input;
	     left_output = mono_input;                                   
       break;  
       
       case 4:
		 /* Straight through. No processing. 2 MHz clock */ 
		 
		  if ( step != last_step)
          {
            pll_frequency_setup(2); /* Step has just changed. Update frequency */
            oled_display_message("Step4 Very slow PLL", " PLL = 2 MHz       ");
          }
		   
         left_output = mono_input;
		 right_output = mono_input;  
       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                                                          *
 *                                                                          *
 * ------------------------------------------------------------------------ */

