#include "adc.h"
#include "dwarfcore.h"
#include "cpld.h"
long int val;
long int temp;

void initAdc(void)
{
	
	P6SEL=0xff;	//disable I/O on all port 6 pins (see note on ADC port pg17-5 in users manual)
	P6DIR=0;	//Set all port 6 pins to be inputs
	
	//set ADC control register 0
	ADC12CTL1=SHP+ADC12SSEL_3+CONSEQ_1; //use SMCLK as source, pulse mode sampling, sequence of conversions mode
	ADC12MCTL0=SREF_1+INCH_0;
	ADC12MCTL1=SREF_1+INCH_1;
	ADC12MCTL2=SREF_1+INCH_2;
	ADC12MCTL3=SREF_1+INCH_3;
	ADC12MCTL4=SREF_1+INCH_4;
	ADC12MCTL5=SREF_1+INCH_5; 
	ADC12MCTL6=SREF_1+INCH_6;
	ADC12MCTL7=SREF_1+INCH_7;+EOS;
	ADC12IE = 0x80;  //enable adcisr only on last conversion       
	ADC12CTL0=SHT0_15+SHT1_15+MSC+REF2_5V+REFON+ADC12ON+ENC; //ADC config 
	set_cpld_bit(DCORSENSOR,DCORPRESEN);
	 set_cpld_bit(DCORSENSOR,DCORTEMPEN);
 	
 	 
}

void adcIsr(MotorChannel* mc,RotaryValve* rv,Heater* h)
//called (like all ISRs!) with interrupts DISABLED
//so, we must enable them for cases where we don't return in < 40 microseconds
{
 uint16 voltCnts, ADC3, ADC7;
 
   if (ADC12IV == 20)  //this IV for memory register 7 prog for ADC ch 7
    {  //sequence completed - process data
       //first read all data as fast as possible so that int can be reenabled
	    mc[0].motorCurrent=ADC12MEM0;

        mc[1].motorCurrent = ADC12MEM1;

        voltCnts = ADC12MEM2;  //approx 205 counts/volt

        ADC3 = ADC12MEM3;
        
        if(mc[1].pressureSenseSwitch==1) mc[1].pressure1=ADC12MEM4;
	 	else mc[1].pressure2=ADC12MEM4;

	 	if(h->tempSenseSwitch==1) h->tempSense1=ADC12MEM5;
	 	 else h->tempSense2=ADC12MEM5;

 		h->thermoCouple=ADC12MEM6;
        ADC7 = ADC12MEM7;  ///this is the last conversion, and clears the ifg
 
    //all data read and ready to renable int
    _EINT();
    if (mc[0].motorCurrent > mc[0].maxCurrent)
	      MotorOverCurrent(&mc[0]);

    if(mc[1].motorCurrent > mc[1].maxCurrent )
          MotorOverCurrent(&mc[1]);

           //scale PWM duty to achieve approximently the same shaft velocity
           // regardless of input voltage
    TBCCR5 = (((9*PWMmaxCOUNT/10UL)<<16) - 1650UL*(voltCnts-9*205))>>16;
    
    if(mc[1].pressureSenseSwitch==1)
	  {
 	   set_cpld_bit(DCORSENSOR,DCORPRESSL);  //this does our EINT()!
 	 //  set_cpld_bit(DCORSENSOR,DCORPRESEN);
 	   mc[0].pressure1=mc[1].pressure1;
 	   mc[1].pressureSenseSwitch=0;
 	  }
    else
      {
 	  clear_cpld_bit(DCORSENSOR,DCORPRESSL); //this does our EINT()!
	//  set_cpld_bit(DCORSENSOR,DCORPRESEN);
	  mc[0].pressure2=mc[1].pressure2;
 	  mc[1].pressureSenseSwitch=1;
 	  }

 	if(h->tempSenseSwitch==1)
 	{//replace these with specific fcn calls
 	// set_cpld_io(DCORSENSOR,get_cpld_io(DCORSENSOR)|(DCORTEMPSL+DCORTEMPEN));
 	 set_cpld_bit(DCORSENSOR,DCORTEMPSL);  
 	 h->tempSenseSwitch=0;
 	 }
     else
 	 {
 	 clear_cpld_bit(DCORSENSOR,DCORTEMPSL);  //this does our EINT()!
//	 set_cpld_bit(DCORSENSOR,DCORTEMPEN);
 	 h->tempSenseSwitch=1;
 	 }
 }	 
}
