/******************************************************************************
 *
 * Copyright:
 *    (C) 2000 - 2005 Embedded Artists AB
 *
 * Description:
 *    Christmas tree
 *
 *****************************************************************************/


#include <general.h>
#include <printf_P.h>
#include <lpc246x.h>
#include <config.h>
#include "adc.h"

void delayMs(tU16 delayInMs);

/******************************************************************************
 * Defines and typedefs
 *****************************************************************************/
#define AD0CR          (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x00))
#define AD0GDR         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x04))
#define AD0INTEN       (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x0C))
#define AD0DR0         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x10))
#define AD0DR1         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x14))
#define AD0DR2         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x18))
#define AD0DR3         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x1C))
#define AD0DR4         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x20))
#define AD0DR5         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x24))
#define AD0DR6         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x28))
#define AD0DR7         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x2C))
#define AD0STAT        (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x30))


/*****************************************************************************
 *
 * Description:
 *    Start a conversion of one selected analogue input and return
 *    10-bit result.
 *
 * Params:
 *    [in] channel - analogue input channel to convert.
 *
 * Return:
 *    10-bit conversion result
 *
 ****************************************************************************/
tU16
getAnalogueInput(tU8 channel)
{
//  volatile tU32 cpsrReg;
  tU16 returnResult;

  //disable IRQ
//  cpsrReg = disIrq();

	//start conversion now (for selected channel)
	AD0CR = (AD0CR & 0xFFFFFF00) | (1 << channel) | (1 << 24);
	
	//wait til done
	while((AD0GDR & 0x80000000) == 0)
	  ;

	//get result and adjust to 10-bit integer
	returnResult = (AD0GDR>>6) & 0x3FF;

  //enable IRQ
//  restoreIrq(cpsrReg);
  
  return returnResult;
}

/*****************************************************************************
 *
 * Description:
 *
 ****************************************************************************/
void
initAdc(void)
{
	volatile tU32 integerResult;

	//
  //Initialize ADC: AIN0 = P0.23, AIN1 = P0.24, AIN2 = P0.25
  //
  PINSEL1 &= ~0x000FC000;
  PINSEL1 |=  0x00054000;
  
  PCONP |= (1 << 12); //Enable ADC
  
  //initialize ADC
 AD0CR = (1 << 0)                             |  //SEL = 1, dummy channel #1
         ((Fpclk / 4500000) - 1) << 8 |  //set clock division factor, so ADC clock is 4.5MHz
         (0 << 16)                            |  //BURST = 0, conversions are SW controlled
         (0 << 17)                            |  //CLKS  = 0, 11 clocks = 10-bit result
         (1 << 21)                            |  //PDN   = 1, ADC is active
         (1 << 24)                            |  //START = 1, start a conversion now
         (0 << 27);							                 //EDGE  = 0, not relevant when start=1

  //short delay and dummy read
  delayMs(1);
  integerResult = AD0GDR;
}
