/* ==================================================================================
File name:       F280XADC_6CH.C
                    
Originator:		ECS Development

Description: This file contains source for the F280X general purpose 
6 conversions ADC driver

Target: TMS320F280x family
              
=====================================================================================
History:
-------------------------------------------------------------------------------------
 04-15-2005	Version 3.20: Using DSP280x v. 1.10 or higher 
------------------------------------------------------------------------------------*/

#include "DSP280x_Device.h"
#include "f280xadc_6ch.h"

#define CPU_CLOCK_SPEED      10.000L   // for a 100MHz CPU clock speed
#define ADC_usDELAY 10000L
#define DELAY_US(A)  DSP28x_usDelay(((((long double) A * 1000.0L) / (long double)CPU_CLOCK_SPEED) - 9.0L) / 5.0L)

extern void DSP28x_usDelay(unsigned long Count);

void F280Xadc06_Init(ADC6CH *p)
{
    DELAY_US(ADC_usDELAY); 
  
    AdcRegs.ADCTRL1.all = ADC_RESET_FLAG; 		// Reset the ADC Module 
	asm(" NOP ");
	asm(" NOP ");
	
	DELAY_US(ADC_usDELAY);			    		// Delay before powering up rest of ADC 

    AdcRegs.ADCTRL3.bit.ADCBGRFDN = 0x3;		// Power up bandgap/reference circuitry 
	DELAY_US(ADC_usDELAY);			    		// Delay before powering up rest of ADC 
	DELAY_US(ADC_usDELAY);			    		// Delay before powering up rest of ADC 

    AdcRegs.ADCTRL3.bit.ADCPWDN = 1;	   		// Power up rest of ADC
	DELAY_US(ADC_usDELAY);	
	DELAY_US(ADC_usDELAY);			    		// Delay before powering up rest of ADC 

    AdcRegs.ADCTRL3.bit.ADCCLKPS = 16;     		// Set up ADCTRL3 register 
    AdcRegs.ADCTRL1.all = ADC6CH_ADCTRL1_INIT; 	// Set up ADCTRL1 register 
    AdcRegs.ADCTRL2.all = ADC6CH_ADCTRL2_INIT;  // Set up ADCTRL2 register 
	AdcRegs.ADCMAXCONV.bit.MAX_CONV1 = 5;       // Specify Six conversions  
    AdcRegs.ADCCHSELSEQ1.all = p->ChSelect1;    // Configure channel selection 
    AdcRegs.ADCCHSELSEQ2.all = p->ChSelect2;    // Configure channel selection 

    AdcRegs.ADCREFSEL.all = 39;                 // Set up the ADC reference select register
    AdcRegs.ADCOFFTRIM.all = 65534;             // Set up the ADC offset trim register

    // Set up Event Trigger with CNT_zero enable for Time-base of EPWM1
    EPwm1Regs.ETSEL.bit.SOCAEN = 1;     // Enable SOCA
    EPwm1Regs.ETSEL.bit.SOCASEL = 1;    // Enable CNT_zero event for SOCA
    EPwm1Regs.ETPS.bit.SOCAPRD = 1;     // Generate SOCA on the 1st event
	EPwm1Regs.ETCLR.bit.SOCA = 1;       // Clear SOCA flag

}

void F280Xadc06_Bipolar_Update(ADC6CH *p)
{
       int DatQ15;
       long Tmp;

        // Wait until ADC conversion is completed
        while (AdcRegs.ADCST.bit.SEQ1_BSY == 1)
        {};

        DatQ15 = AdcRegs.ADCRESULT0^0x8000;   	// Convert raw result to Q15 (bipolar signal)
        Tmp = (long)p->Ch1Gain*(long)DatQ15;    // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch1Out = (int)(Tmp>>13);             // Convert Q28 to Q15
        p->Ch1Out += p->Ch1Offset;              // Add offset 

        DatQ15 = AdcRegs.ADCRESULT1^0x8000;  	// Convert raw result to Q15 (bipolar signal)
        Tmp = (long)p->Ch2Gain*(long)DatQ15;    // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch2Out = (int)(Tmp>>13);             // Convert Q28 to Q15
        p->Ch2Out += p->Ch2Offset;              // Add offset 

        DatQ15 = AdcRegs.ADCRESULT2^0x8000;  	// Convert raw result to Q15 (bipolar signal)
        Tmp = (long)p->Ch3Gain*(long)DatQ15;    // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch3Out = (int)(Tmp>>13);             // Convert Q28 to Q15
        p->Ch3Out += p->Ch3Offset;              // Add offset 

        DatQ15 = AdcRegs.ADCRESULT3^0x8000;  	// Convert raw result to Q15 (bipolar signal)
        Tmp = (long)p->Ch4Gain*(long)DatQ15;    // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch4Out = (int)(Tmp>>13);             // Convert Q28 to Q15
        p->Ch4Out += p->Ch4Offset;              // Add offset 

        DatQ15 = AdcRegs.ADCRESULT4^0x8000;  	// Convert raw result to Q15 (bipolar signal)
        Tmp = (long)p->Ch5Gain*(long)DatQ15;    // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch5Out = (int)(Tmp>>13);             // Convert Q28 to Q15
        p->Ch5Out += p->Ch5Offset;              // Add offset 

        DatQ15 = AdcRegs.ADCRESULT5^0x8000;  	// Convert raw result to Q15 (bipolar signal)
        Tmp = (long)p->Ch6Gain*(long)DatQ15;    // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch6Out = (int)(Tmp>>13);             // Convert Q28 to Q15
        p->Ch6Out += p->Ch6Offset;              // Add offset 

        AdcRegs.ADCTRL2.all |= 0x4040;          // Reset the sequence
}            


void F280Xadc06_Unipolar_Update(ADC6CH *p)
{
       int DatQ15;
       long Tmp;

        // Wait until ADC conversion is completed
        while (AdcRegs.ADCST.bit.SEQ1_BSY == 1)
        {};

        DatQ15 = (AdcRegs.ADCRESULT0>>1)&0x7FFF;   	// Convert raw result to Q15 (unipolar signal)
        Tmp = (long)p->Ch1Gain*(long)DatQ15;        // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch1Out = (int16)(Tmp>>13);               // Convert Q28 to Q15
        p->Ch1Out += p->Ch1Offset;                  // Add offset 

        DatQ15 = (AdcRegs.ADCRESULT1>>1)&0x7FFF;   	// Convert raw result to Q15 (unipolar signal)
        Tmp = (long)p->Ch2Gain*(long)DatQ15;        // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch2Out = (int)(Tmp>>13);                 // Convert Q28 to Q15
        p->Ch2Out += p->Ch2Offset;                  // Add offset 

        DatQ15 = (AdcRegs.ADCRESULT2>>1)&0x7FFF;   	// Convert raw result to Q15 (unipolar signal)
        Tmp = (long)p->Ch3Gain*(long)DatQ15;        // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch3Out = (int)(Tmp>>13);                 // Convert Q28 to Q15
        p->Ch3Out += p->Ch3Offset;                  // Add offset 

        DatQ15 = (AdcRegs.ADCRESULT3>>1)&0x7FFF;   	// Convert raw result to Q15 (unipolar signal)
        Tmp = (long)p->Ch4Gain*(long)DatQ15;        // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch4Out = (int)(Tmp>>13);                 // Convert Q28 to Q15
        p->Ch4Out += p->Ch4Offset;                  // Add offset 

        DatQ15 = (AdcRegs.ADCRESULT4>>1)&0x7FFF;   	// Convert raw result to Q15 (unipolar signal)
        Tmp = (long)p->Ch5Gain*(long)DatQ15;        // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch5Out = (int)(Tmp>>13);                 // Convert Q28 to Q15
        p->Ch5Out += p->Ch5Offset;                  // Add offset 

        DatQ15 = (AdcRegs.ADCRESULT5>>1)&0x7FFF;   	// Convert raw result to Q15 (unipolar signal)
        Tmp = (long)p->Ch6Gain*(long)DatQ15;        // Tmp = gain*dat => Q28 = Q13*Q15
        p->Ch6Out = (int)(Tmp>>13);                 // Convert Q28 to Q15
        p->Ch6Out += p->Ch6Offset;                  // Add offset 

        AdcRegs.ADCTRL2.all |= 0x4040;              // Reset the sequence
}


