/**********************************************************************
* $Id$		adc_interrupt_test.c			2010-07-16
*//**
* @file		adc_interrupt_test.c
* @brief	This example describes how to use ADC conversion in
* 			burst mode
* @version	2.0
* @date		16. July. 2010
* @author	NXP MCU SW Application Team/Micromint USA Support
*
* Copyright(C) 2010, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/
#include "lpc17xx_adc.h"
#include "lpc17xx_libcfg.h"
#include "lpc17xx_pinsel.h"
#include "debug_frmwrk.h"
#include "lpc17xx_gpio.h"
#include "lincoln_board.h"
/* Example group ----------------------------------------------------------- */
/** @defgroup ADC_Burst		Burst
 * @ingroup ADC_Examples
 * @{
 */

/************************** PRIVATE DEFINITIONS ***********************/
#define _ADC_INT	ADC_ADINTEN2
#define _ADC_CHANNEL	ADC_CHANNEL_2

#define _ADC_INT_3	ADC_ADINTEN3
#define _ADC_CHANNEL_3	ADC_CHANNEL_3

/** DMA size of transfer */
#define DMA_SIZE		8

/************************** PRIVATE VARIABLES *************************/
uint8_t menu1[] =
"********************************************************************************\n\r"
"Lincoln 60 \n\r"
" ADC burst demo \n\r"
"\t - MCU: LPC17xx \n\r"
"\t - Core: ARM CORTEX-M3 \n\r"
"\t - Communicate via: COM1 - 115200 bps \n\r"
" Use ADC with 12-bit resolution rate of 200KHz, running burst mode (single or multiple input)\n\r"
" Display ADC value via COM1\n\r"
" Adjust the voltage see how ADC value changes\n\r"
"********************************************************************************\n\r";

static BOOL_8 toggle=FALSE;

/************************** PRIVATE FUNCTION *************************/
void print_menu(void);


void EINT3_IRQHandler(void);


/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/

void EINT3_IRQHandler(void)
{
	  if (GPIO_GetIntStatus( USERPB_PORT, USERPB_PIN, 1))
	  {
		  GPIO_ClearInt(USERPB_PORT,(1<<USERPB_PIN));
		  toggle=~toggle;

		  if(toggle)
		  {
			  ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL_3,ENABLE);
			  GPIO_ClearValue(LED1_PORT, (1<<LED1_PIN));
		  }
		  else
		  {
			  ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL_3,DISABLE);			  
                          GPIO_SetValue(LED1_PORT, (1<<LED1_PIN));
		  }

	  }
}

/*-------------------------PRIVATE FUNCTIONS------------------------------*/
/*********************************************************************//**
 * @brief		Print menu
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void print_menu(void)
{
	_DBG(menu1);
}


/*-------------------------MAIN FUNCTION------------------------------*/
/*********************************************************************//**
 * @brief		c_entry: Main ADC program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	PINSEL_CFG_Type PinCfg;
	PINSEL_CFG_Type PinCfg1;
	uint32_t tmp;
	uint32_t adc_value;
	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	// print welcome screen
	print_menu();
	/*
	 * Init ADC pin connect
	 * AD0.2 on P0.25
	 */
	PinCfg.Funcnum = ADC2_FUNCTION;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = ADC2_PIN;
	PinCfg.Portnum = ADC2_PORT;
	PINSEL_ConfigPin(&PinCfg);

	/*
	 * Init ADC pin connect
	 * AD0.3 on P0.26
	 */
	PinCfg1.Funcnum = ADC2_FUNCTION;
	PinCfg1.OpenDrain = 0;
	PinCfg1.Pinmode = 0;
	PinCfg1.Pinnum = ADC3_PIN;
	PinCfg1.Portnum = ADC3_PORT;
	PINSEL_ConfigPin(&PinCfg1);


	/* Configuration for ADC:
	 *  ADC conversion rate = 200KHz
	 */
	ADC_Init(LPC_ADC, 200000);
	ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL,ENABLE);
	ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL_3,ENABLE);
        
	GPIO_SetDir(LED1_PORT, (1<<LED1_PIN), 1);  /* Configure as output */
	GPIO_ClearValue(LED1_PORT, (1<<LED1_PIN));
	
        // Enable GPIO interrupt P2.10
	GPIO_IntCmd(USERPB_PORT,(1<<USERPB_PIN),1);
	NVIC_EnableIRQ(EINT3_IRQn);

	//Start burst conversion
	ADC_BurstCmd(LPC_ADC,ENABLE);
	while(1)
	{
          adc_value =  ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_2);
	  _DBG("ADC value on channel 2: ");
	  _DBD32(adc_value);
	  _DBG_("");
	  adc_value =  ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_3);
	  _DBG("ADC value on channel 3: ");
	  _DBD32(adc_value);
	  _DBG_("");
	  // Wait for a while
	  for(tmp = 0; tmp < 1500000; tmp++);
	}
	ADC_DeInit(LPC_ADC);
	return (0);
}

/* Support required entry point for other toolchain */
int main (void)
{
	return c_entry();
}
#ifdef  DEBUG
/*******************************************************************************
* @brief		Reports the name of the source file and the source line number
* 				where the CHECK_PARAM error has occurred.
* @param[in]	file Pointer to the source file name
* @param[in]    line assert_param error line source number
* @return		None
*******************************************************************************/
void check_failed(uint8_t *file, uint32_t line)
{
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while(1);
}
#endif

/*
 * @}
 */
