/**
 * @file	: ssp_dac_test.c
 * @purpose	: An example for the optional 12-bit SPI DAC
 * @version	: 1.0
 * @date	:1. June. 2011
 * @author	: Micromint USA Support
 *----------------------------------------------------------------------------
 * 
 **********************************************************************/
#include "lpc17xx_timer.h"
#include "lpc17xx_uart.h"
#include "lpc17xx_libcfg.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_nvic.h"
#include "debug_frmwrk.h"

#include "ssp_dac.h"

/************************** PRIVATE TYPES *************************/

/************************** PRIVATE VARIABLES *************************/
uint8_t menu1[] =
"********************************************************************************\n\r"
"Hello Lincoln 60 \n\r"
"Optional 12-bit SSP DAC demo\n\r"
"\t - MCU: LPC17xx \n\r"
"\t - Core: ARM Cortex-M3 \n\r"
"\t - Communicate via: UART0 -  115200 bps \n\r"
"Channel 0 = triangle wave generated by the timer interrupt\n\r"
"Channel 1 = 1.0 V\n\r"
"Channel 2 = 2.0 V\n\r"
"Channel 3 = 3.0 V\n\r"
"********************************************************************************\n\r";
// UART Configuration structure variable
UART_CFG_Type UARTConfigStruct;

//timer init
TIM_TIMERCFG_Type TIM_ConfigStruct;
TIM_MATCHCFG_Type TIM_MatchConfigStruct ;
//Global Variable
static unsigned long digital_count = 0; // Holds the digital count for the DAC CH0
static char chrCount = 1;     // Tells which direction to increment the count
/************************** PRIVATE FUNCTION *************************/
void print_menu(void);
void TIMER0_IRQHandler(void);
// Print Menu 1
void print_menu(void)
{
   _DBG(menu1);
}

void TIMER0_IRQHandler(void)

{ 
  if (TIM_GetIntStatus(LPC_TIM0,0))
  {
    if (digital_count>DAC_RES) SSP_Dac_Write(DAC0, 4095); 
    else SSP_Dac_Write(DAC0, digital_count);
    
    if (chrCount)   digital_count = digital_count + 200;
    else digital_count = digital_count - 200;
    if (digital_count>=DAC_RES)
    {
       chrCount = 0;
    }
    if (digital_count==0)
    {
      chrCount = 1; 
    }
    TIM_ClearIntPending(LPC_TIM0,0);
   }
}

int c_entry(void)
{
        PINSEL_CFG_Type PinCfg;
	// DeInit NVIC and SCBNVIC
	NVIC_DeInit();
	NVIC_SCBDeInit();

	/* Configure the NVIC Preemption Priority Bits:
	 * two (2) bits of preemption priority, six (6) bits of sub-priority.
	 * Since the Number of Bits used for Priority Levels is five (5), so the
	 * actual bit number of sub-priority is three (3)
	 */
	NVIC_SetPriorityGrouping(0x05);

	//  Set Vector table offset value
#if (__RAM_MODE__==1)
	NVIC_SetVTOR(0x10000000);
#else
	NVIC_SetVTOR(0x00000000);
#endif

	/* Init debug */
	debug_frmwrk_init();
        
        /* Init DAC */
        Init_SSP_Dac();
        SSP_Dac_Write(DAC0, DAC_VOLT(0.0)); /*Set channel 1 to 1.0V */
        SSP_Dac_Write(DAC1, DAC_VOLT(1.0)); /*Set channel 1 to 1.0V */
        SSP_Dac_Write(DAC2, DAC_VOLT(2.0)); /*Set channel 2 to 2.0V */
        SSP_Dac_Write(DAC3, DAC_VOLT(3.0)); /*Set channel 3 to 3.0V */

	// print welcome screen
	print_menu();
        
	// Conifg P1.28 as MAT0.0
	PinCfg.Funcnum = 3;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 1;
	PinCfg.Pinnum = 28;
	PINSEL_ConfigPin(&PinCfg);

	// Initialize timer 0, prescale count time of 1uS
	TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
	TIM_ConfigStruct.PrescaleValue	= 1;
	// use channel 0, MR0
	TIM_MatchConfigStruct.MatchChannel = 0;
	// Enable interrupt when MR0 matches the value in TC register
	TIM_MatchConfigStruct.IntOnMatch   = TRUE;
	//Enable reset on MR0: TIMER will reset if MR0 matches it
	TIM_MatchConfigStruct.ResetOnMatch = TRUE;
	//Stop on MR0 if MR0 matches it
	TIM_MatchConfigStruct.StopOnMatch  = FALSE;
	//Toggle MR0.0 pin if MR0 matches it
	TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE;
	// Set Match value, count value of 25 (25 * 1uS = 25uS --> 40 kHz)
	TIM_MatchConfigStruct.MatchValue   = 25;

	// Set configuration for Tim_config and Tim_MatchConfig
	TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);
	TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct);

	/* preemption = 1, sub-priority = 1 */
	NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));
	/* Enable interrupt for timer 0 */
	NVIC_EnableIRQ(TIMER0_IRQn);
	// To start timer 0
	TIM_Cmd(LPC_TIM0,ENABLE);

	while (1)
        {
  
        }
	return 1;

}

/* Support required entry point for other toolchain */
int main (void)
{
	return c_entry();
}
