/************************************************************************//***
*   @file   AD5781.h
*   @brief  Header file for AD5781 Driver
*   @author ATofan (alexandru.tofan@analog.com)
*
******************************************************************************
* Copyright 2013(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: $WCREV$
******************************************************************************/
#ifndef __AD5781_H__
#define __AD5781_H__

/*****************************************************************************/
/***************************** Include Files *********************************/
/*****************************************************************************/
#include "xparameters.h"

/*****************************************************************************/
/********************** Macros and Constant Definitions **********************/
/*****************************************************************************/
///DAC5781 Registers Definition
#define DAC_REG 	0x1 /* Offset of DAC Register's Address */
#define DAC_CNT 	0x2 /* Offset of Control Register's Address */
#define DAC_CC 		0x3 /* Offset of Clearcode Register's Address */
#define DAC_SW	 	0x4 /* Offset of Software Control Register's Address */

///Control Register's bits
#define LIN_COMP 	0x3C0 /* Linearity error compensation for varying reference input span */
#define SDODIS		0x020 /* SDO pin enable/disable control */
#define BIN2SC		0x010 /* DAC register coding select */
#define DACTRI		0x008 /* DAC tristate control */
#define OPGND		0x004 /* Output ground clamp control */
#define RBUF		0x002 /* Output amplifier configuration control */

///Software Control Register's bits
#define RST			0x4 /* Software control for Reset */
#define CLR			0x2 /* Software control for Clear */
#define LDAC		0x1 /* Software control for Load DAC */

///User Defined Macros
#define REGWR(x) ((0x0 << 23) | ((DAC_REG << 20) | ((x) << 2))) /* Generate packet which writes the arguments value to the DAC Register */
#define REGRD(x) ((0x1 << 23) | ((DAC_REG << 20) | ((x) << 2))) /* Generate packet which reads the arguments value from the DAC Register */
#define UPDATE_DAC_REG ((DAC_SW << 20) | LDAC) 					/* Macro to assert the LDAC line from software */

///User Defined Constants
static const unsigned char sinetable[256] = {
		 0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
		 0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
		 0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
		 0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
		 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
		 0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4,
		 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB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0x37,
		 0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D,
		 0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64,
		 0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D
		 }; 
static const unsigned char sawtable[256] = {
		 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
		 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
		 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
		 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
		 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
		 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
		 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
		 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
		 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
		 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
		 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
		 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
		 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
		 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
		 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
		 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
		 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
		 0x98, 0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,
		 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10,
		 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00
		 };

static const unsigned char squaretable[256] = {
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
		 };

void AD5781_DisplayMainMenu(void); 	/*!< Display Main Menu*/
void AD5781_DisplayMenu(void); 		/*!< Display Menu*/
void AD5781_DisplayWaveMenu(void);	/*!< Display Wave Menu*/
void AD5781_SendData(int txData);	/*!< Sending data to DAC*/
void AD5781_FixedValue(void);		/*!< Display FixedValue Menu, and set the DAC Register*/
void AD5781_WaveGen(void);			/*!< Generate one of the waveform from the Wave Menu's list */
void AD5781_ProgRamp(void);			/*!< Runs Programmable Ramp Generation Mode Main Menu*/
void AD5781_InitDAC(void);			/*!< Initial COnfiguration of the DAC5781*/
void DemoProgram(void);

#endif /* __AD5781_H__ */
