/***************************************************************

This is a basic STR7x/REva example.

It uses the potentiometer (ADC), the serial port (UART0)
 and 6 of the 8 LEDs (GPIO1).
It calls the STR7x and smallprintf libraries
 (see the linker options).

Make sure that the correct derivative is selected in the target options
 (the code contains #if's that will change it according to the selected target)

Make sure that the jumpers for enabling the peripherals are set:
	TX for the serial port
	POT for the potentiometer
	D2 to D7

Also make sure that the boot mode selected on the board matches
 the mode selected in the target options.

Finally, you must connect the serial port of the REva to the PC
 and open a terminal program on the PC. choose a baudrate of 9600.

The example endlessly reads the value on the potentiometer and
 sends it on the UART and LEDs.

***************************************************************

This program will work with the STR71x and the STR3x devices.
Just make sure that the correct derivative is selected
 in the target options.

***************************************************************/


//==============================================================
//FL050315 first draft
//VC050408 cleanup
//VC050512 add LEDS
//==============================================================
#if _STR73x_
#include "73x_lib.h"
#else
#include "71x_lib.h"
#endif

#include "UART0_stdio.h"


//==============================================================
// main
//==============================================================
int init_uart(void);
int init_adc(void);
int measure_pot (void);	
void myputs(char * str);
	
int counter;

#if ( _STR73x_ )
#define MAX_ADC_VAL (0x400)
#else
#define MAX_ADC_VAL (0xA40)
#endif

//this is the main function, called just after the startup
int main ( void )
	{
	int valread;
	char buffer[26];
	unsigned short ToLEDs;
	
#if ( _STR73x_ )
   unsigned char delay = 0 ;

	//tell io_putchar the board's crystal freq   
   __io_SetMainOscFreq(8000000);

	//configure REva LEDs D2 to D7 as output
   /* GPIO2 Clock Enable */
   CFG->PCGR0 |= 0x0001 << 20;
   for (delay = 8 ; delay != 0 ; delay--); /* Peripheral Stabilization */
   //select CPIO2 for the rest of the demo
#define myGPIO GPIO2
#endif

#if ( _STR71x_ )
	//tell io_putchar the board's crystal freq   
   __io_SetMainOscFreq(4000000);

   //select CPIO1 for the rest of the demo
#define myGPIO GPIO1
#endif
	
	//configure REva LEDs D2 to D7 as output
	myGPIO->PC0=0x0000;
	myGPIO->PC1=0x0000;
	myGPIO->PC2=0xFFFF;
	
	init_adc();
	
	
#if ( _STR73x_ )
	printf ( "\nADC_UART STR73x demo");
#else
	printf ( "\nADC_UART STR71x demo");
#endif
	
	while ( 1 )
		{
		valread = 	measure_pot ()	;
		printf ( "\n\rADC=0x%X (%d)", valread, valread);

		valread = (valread * 7) / MAX_ADC_VAL;
		
#if ( _STR73x_ )
		valread = 0x00FF>>valread;
		myGPIO->PD&=(~0x00FF);
		myGPIO->PD|=valread;
#else
		valread = (0x07E0<<valread);
		ToLEDs = (valread&0x0700) + ((valread&0x00E0)>>1);
		myGPIO->PD&=(~0x0770);
		myGPIO->PD|=ToLEDs;
#endif
		}
	}

#if ( _STR73x_ )
#else

//this is needed for letting time to the ADC to perform some actions
void dummy_fct()
	{	;	}
void delay ( int n )
	{
	int i;
	
	while (n--)
		{
		for ( i = 0 ; i < 10000 ; i++ )
			dummy_fct();
		}
	}

#define referencemv 2500
#define offset 0x9ab   // digital value for the 0 volt
#define vref 0x5f1  // digital value for the 2.5 volt

// using calibration offer the possibility to have the value of the tension conversted.
u32 ADC12_Calib(u16 Conv_Res)
{
  if ((Conv_Res>offset)&&(Conv_Res<=0xfff))
    return referencemv*(Conv_Res-offset)/(0xfff-offset+vref);
  else
  if (Conv_Res<0x7ff)
    return referencemv*(0xfff+Conv_Res-offset)/(0xfff-offset+vref);
  else return 0;
}
#endif



//initialize the ADC
int init_adc( void ) 
	{
#if ( _STR73x_ )

	GPIO_InitTypeDef GPIOx_InitStructure;
	ADC_InitTypeDef  ADC_InitStructure;
	
/*-----------------------------------GPIO3 ----------------------------------*/
  /* GPIO3 Clock Enable */
  CFG_PeripheralClockConfig(CFG_CLK_GPIO3, ENABLE);
  /* GPIO3 Configuration */
  GPIOx_InitStructure.GPIO_Mode = GPIO_Mode_HI_AIN_TRI;
  GPIOx_InitStructure.GPIO_Pins = GPIO_PIN_3 ;
  GPIO_Init (GPIO3, &GPIOx_InitStructure);

/*---------------------------------ADC---------------------------------------*/
  /* ADC Clock Enable */
  CFG_PeripheralClockConfig(CFG_CLK_ADC, ENABLE);

  /* ADC Configuration */
  ADC_StructInit(&ADC_InitStructure);
  ADC_InitStructure.ADC_Calibration = ADC_Calibration_ON;
  ADC_InitStructure.ADC_CalibAverage = ADC_CalibAverage_Enable;
  ADC_InitStructure.ADC_AutoClockOff = ADC_AutoClockOff_Disable ;
  ADC_InitStructure.ADC_ConversionMode = ADC_ConversionMode_Scan ;
  ADC_InitStructure.ADC_SamplingPrescaler = 0x7;
  ADC_InitStructure.ADC_ConversionPrescaler = 0x0;
  ADC_InitStructure.ADC_FirstChannel = ADC_CHANNEL3 ;
  ADC_InitStructure.ADC_ChannelNumber = 1;
  ADC_Init (&ADC_InitStructure);

  /* Enable ADC */
  ADC_Cmd(ENABLE);
  /* Start ADC conversion */
  ADC_ConversionCmd (ADC_ConversionStart);
  
#else
  // Configure the used analog input to HI_AIN_
  GPIO_Config (GPIO1, 0x0004, GPIO_HI_AIN_TRI);

  // Initialize the conveter register.
  ADC12_Init();
  delay ( 1 );

  // Configure the prescaler register using the configured PCLK with
  // a sampling frequency=500Hz
  ADC12_PrescalerConfig(50);

  // Select the conversion mode=single channel
  ADC12_ModeConfig (ADC12_SINGLE);

  // Select the channel to be converted
  ADC12_ChannelSelect(ADC12_CHANNEL2);
  delay ( 1 );
#endif
	}

//perform one measure on the ADC
int measure_pot (void)
	{
	u16 valpot=0;
#if ( _STR73x_ )

	//we're in scan mode, so no need for polling...
	// see adc_init() above
   valpot=ADC_GetConversionValue(ADC_CHANNEL3);

#else
  	u16 Conv_Res;

  	// Start the Converter
	ADC12_ConversionStart();
  
	delay ( 1 );	//Why??
	
  	// Wait until the availabiliy of data in the specific flags
  	while (ADC12_FlagStatus (ADC12_DA2)==RESET);

  	// Get the conversion result from the correponding Data register
	Conv_Res = ADC12_ConversionValue(ADC12_CHANNEL2);
	valpot=ADC12_Calib(Conv_Res);

  	// Disable the converter
  	ADC12_ConversionStop();
  
#endif

  	return valpot;
	}



