/******************************************************************************
   
    ssp_dac.h - Digital to Analog Extended  declarations for the 
              Micromint Lincoln (Cortex-M3)
              by Micromint Support <support@micromint.com>    June-2011

******************************************************************************/
#ifndef _SSP_DAC_H
#define _SSP_DAC_H

#include "lpc17xx_libcfg.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_ssp.h"
#include "lpc17xx_gpio.h"
/******************************************************************************

 Defines for the SSP_DAC

*******************************************************************************/
#define SSP_DAC_SYNC_PIN                 8      /* DAC Sync GPIO */
#define SSP_DAC_SYNC_PIN_FUNCTION        0      /* DAC Sync Function for GPIO */
#define SSP_DAC_SYNC_PIN_PORT            2      /* DAC Sync Port number */
#define SSP_DAC_SYNC_PIN_MASK            0x100  /* DAC Sync Pin mask */
/* SYNC pin direction */
#define SET_SSP_DAC_SYNC_DIR             GPIO_SetDir(SSP_DAC_SYNC_PIN_PORT, SSP_DAC_SYNC_PIN_MASK, 1);
/* SYNC pin is logic high to disable the DAC */
#define SSP_DAC_DISABLE                  GPIO_SetValue(SSP_DAC_SYNC_PIN_PORT, SSP_DAC_SYNC_PIN_MASK);
#define SSP_DAC_ENABLE                   GPIO_ClearValue(SSP_DAC_SYNC_PIN_PORT, SSP_DAC_SYNC_PIN_MASK);
/* SSP port */
#define SSP_DAC_PIN_FUNCTION     2      /* Function number for SSP */
#define SSP_DAC_GPIO_PORT        0      /* Port number for SSP */
#define SSP_DAC_CLK_PIN          15      /* Clock GPIO for SSP DAC */
#define SSP_DAC_TX_PIN           18      /* TX GPIO for SSP DAC */
#define SSP_DAC_PORT             LPC_SSP0  
#define SSP_DAC_DATABIT          SSP_DATABIT_16
#define SSP_DAC_CPHA             SSP_CPHA_FIRST
#define SSP_DAC_CPOL             SSP_CPOL_LO
#define SSP_DAC_MODE             SSP_MASTER_MODE
#define SSP_DAC_FRAME            SSP_FRAME_SPI
#define SSP_DAC_CLOCK_RATE       25000000          /* Clock the DAC at 25 MHz */


/*Input Shift Register Difinition for DAC
A1 A0 OP1 OP0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
A1, A0 - for addressing 00 - DAC A, 01 - DAC B, 10 - DAC C, 11 - DAC D
OP1, OP0 - Output Instructions
00 - Write to specified register but do not update outputs
01 - Write to specified register and update outputs
10 - Write to all registers and update outputs
11 - Power down outputs 
For further information on Power Down refer to the DAC104085 data sheet 
D2 - D11 are for the 10-bit conversion count. D0 - D1 are used for 12-bit

Channel and Output Instruction Declarations */
#define DAC0               0x00
#define DAC1               0x4000
#define DAC2               0x8000
#define DAC3               0xC000 
/* Write to specified register and update outputs */
#define WR_REG_UPDATE      0x1000

#ifdef DAC_10BIT
   #define DAC_RES     1024
#else
   #define DAC_RES     4096
#endif
#define DAC_RANGE   3.3
#define DAC_OFF     0        /* Offset for the DAC */
#define DAC_VOLT(volts) (int)((DAC_RES*(volts+DAC_OFF))/DAC_RANGE)  

/*******************************************************************************

 Prototypes

*******************************************************************************/
void Init_SSP_Dac(void);
unsigned long SSP_Dac_Write(unsigned long,unsigned long);
#endif /* _SSP_DAC_H */
