/***************************************************************************//**
 *   @file   Main.c
 *   @brief  Implementation of the program's main function.
 *   @author Bancisor Mihai
********************************************************************************
 * Copyright 2012(c) Analog Devices, Inc.
 *
 * All rights reserved.
 *
 * 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 Analog Devices, Inc. nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *  - The use of this software may or may not infringe the patent rights
 *    of one or more patent holders.  This license does not release you
 *    from the requirement that you obtain separate licenses from these
 *    patent holders to use this software.
 *  - Use of the software either in source or binary form, must be run
 *    on or directly connected to an Analog Devices Inc. component.
 *
 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, 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.
 *
********************************************************************************
 *   SVN Revision: 381
*******************************************************************************/

/******************************************************************************/
/* Include Files                                                              */
/******************************************************************************/
#include "YRDKRX62N.h"	// YRDKRX62N definitions.
#include "ST7579.h"		// ST7579 definitions.
#include "AD7793.h"		// ADF4360 definitions.

/******************************************************************************/
/* Variables Declarations                                                     */
/******************************************************************************/
const unsigned char adiLogo [2 * 19] =
	{ 0xFF, 0xFF, 0xFF, 0x03, 0x03, 0x03, 0x07, 0x07,
	  0x0F, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F,
	  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0xC0,
	  0xC0, 0xE0, 0xE0, 0xF0, 0xF0, 0xF8, 0xF8, 0xFC,
	  0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF };
unsigned char 	periodEnd	= 0;	// Indicates when the OneShot period ends.
unsigned long 	value 		= 0;	// value read from the ADC

/******************************************************************************/
/* Functions Prototypes                                                       */
/******************************************************************************/
void DelaySeconds(float seconds);
void OneShotCallback(void);

/***************************************************************************//**
 * @brief Main function.
 *
 * @param None.
 *
 * @return None.
*******************************************************************************/
void main(void)
{
	YRDKRX62N_Init();
	ST7579_Init();
    ST7579_Image(2,0,adiLogo,19,16);
    ST7579_String(2,21,"ANALOG");
    ST7579_String(3,21,"DEVICES");
    ST7579_String(4,0,"wiki.analog.com");
	DelaySeconds(2);
	ST7579_Clear();
	ST7579_Image(0,0,adiLogo,19,16);	
	ST7579_String(0,22,"ST7579 OK");

	AD7793_Reset();		// Reset AD7793 to bring the SPI interface in a  known state
	if(AD7793_Init())
	{
		ST7579_String(1,22,"AD7793 OK");
	}
	else
	{
		ST7579_String(1,22,"AD7793 Err");		
	}
	/* AD7792 setup. */
    AD7793_SetGain(AD7793_GAIN_1);                // set gain to 1
    AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M);     // use AIN1(+) - AIN1(-) 
    AD7793_SetReference(AD7793_REFSEL_INT);       // select internal 1.17V reference
    /* Print the value of the configuration register after setup */
    ST7579_String(3,0,"Conf Reg:");
	ST7579_HexNumber(3,70,AD7793_GetRegisterValue( AD7793_REG_CONF,2),2);
	while(1)
	{	
        if(AD7793_Ready())
        {
           	ST7579_String(5,2,"AIN1(+)-AIN1(-)");
            value = AD7793_GetRegisterValue( AD7793_REG_DATA,3);
            ST7579_String(4,0,"Data:");
            ST7579_HexNumber(4,75,value,4);
            if(value > 0x800000)
            {
                value -= 0x800000;
                value = ((value * 1170) >> 23);
                ST7579_String(6,10,"+");
            }
            else
            {
                value = 0x800000 - value;
                value = ((value * 1170) >> 23);
                ST7579_String(6,10,"-");
            }
            ST7579_Number(6,55,value);
            ST7579_String(6,65,"mV");
		}
		 
	}
}

/***************************************************************************//**
 * @brief Creates a delay of seconds.
 *
 * @param None.
 *
 * @return None.
*******************************************************************************/
void DelaySeconds(float seconds)
{
	periodEnd = 0;
	R_TMR_CreateOneShot(PDL_TMR_UNIT0,
						PDL_TMR_OUTPUT_OFF,
						seconds,
						OneShotCallback,
						4);
	while(periodEnd == 0);
}

/***************************************************************************//**
 * @brief OneShot callback function.
 *
 * @param None.
 *
 * @return None.
*******************************************************************************/
void OneShotCallback(void)
{
	periodEnd = 1;
}
