#include <msp430.h>
#include "SCA_config.h"

void SCA_initGPIO(){

    //Select P1.1 P1.2 P1.3 as SAC0 function
    P1SEL0 |= BIT1 + BIT2 + BIT3;
    P1SEL1 |= BIT1 + BIT2 + BIT3;

    //Select P3.1 P3.2 P3.3 as SAC2 function
    P3SEL0 |= BIT1 + BIT2 + BIT3;
    P3SEL1 |= BIT1 + BIT2 + BIT3;

    //Select P3.5 P3.6 P3.7 as SAC3 function
    P3SEL0 |= BIT5 + BIT6 + BIT7;
    P3SEL1 |= BIT5 + BIT6 + BIT7;

    //Select P1.5 P1.6 P1.7 as SAC1 function
    P1SEL0 |= BIT5 + BIT6 + BIT7;
    P1SEL1 |= BIT5 + BIT6 + BIT7;

    // Configure ADC A1,A4,A5,A6,A7,A8,A9,A10, A11 pins
    P1SEL0 |= BIT4 + BIT5 + BIT6 + BIT7;
    P1SEL1 |= BIT4 + BIT5 + BIT6 + BIT7;
    P5SEL0 |= BIT0 | BIT1 | BIT2 | BIT3;
    P5SEL1 |= BIT0 | BIT1 | BIT2 | BIT3;

    // Configure Comparator0 input & output
//    P1SEL0 |= BIT0 | BIT1;                    // Select eCOMP0 input function on P1.0/C0 and P1.1/C1
//    P1SEL1 |= BIT0 | BIT1;
    P2DIR |= BIT0;
    P2SEL1 |= BIT0;                           // Select CPOUT function on P2.0/COUT

    // Configure Comparator1 input & output
    P2SEL0 |= BIT4 | BIT5;                    // Select eCOMP1 input function on P2.4/C1  and P2.5/C0
    P2SEL1 |= BIT4 | BIT4;
    P2DIR |= BIT1;
    P2SEL1 |= BIT1;                           // Select CPOUT function on P2.1/COUT

    // Configure GPIOs for PWMs
    P6DIR |= BIT0 | BIT1 | BIT2 | BIT3;               // P6.0 P6.1 P6.2 P6.3 output
    P6SEL0 |= BIT0 | BIT1 | BIT2 | BIT3;              // P6.0 P6.1 P6.2 P6.3 options select

    // Configure ADC A8,A9,A10, A11 pins
    P5SEL0 |= BIT0 | BIT1 | BIT2 | BIT3;
    P5SEL1 |= BIT0 | BIT1 | BIT2 | BIT3;

    // Configure GPIOs for PWMs and Crystal
    P6DIR |= BIT0 | BIT1 | BIT2 | BIT3;               // P6.0 P6.1 P6.2 P6.3 output
    P6SEL0 |= BIT0 | BIT1 | BIT2 | BIT3;              // P6.0 P6.1 P6.2 P6.3 options select
    P2SEL1 |= BIT6 | BIT7;                            // P2.6~P2.7: crystal pins

    // Configure GPIOs for LEDs
    P1DIR |= BIT0;                                           // Set P1.0/LED to output direction
    P1OUT &= ~BIT0;                                          // P1.0 LED1 off
    P6DIR |= BIT6;                                           // Set P6.6/LED to output direction
    P6OUT &= ~BIT6;                                          // P6.6 LED2 off
//       P2DIR |= BIT0;                                           // Set P2.0/LED to output direction
//       P2OUT &= ~BIT0;                                          // P2.0 LED off - disabled for comparator eval
//       P2DIR |= BIT1;                                           // Set P2.1/LED to output direction
//       P2OUT &= ~BIT1;                                          // P2.1 LED off - disabled for comparator eval
    P2DIR |= BIT2;                                           // Set P2.2/LED to output direction
    P2OUT &= ~BIT2;                                          // P2.2 LED off
    P2DIR |= BIT4;                                           // Set P2.4/LED to output direction
    P2OUT &= ~BIT4;                                          // P2.45 LED off
    P2DIR |= BIT5;                                           // Set P2.5/LED to output direction
    P2OUT &= ~BIT5;                                          // P2.5 LED off
    P3DIR |= BIT1;                                           // Set P3.1/LED to output direction
    P3OUT &= ~BIT1;                                          // P3.1 LED off
    P3DIR |= BIT2;                                           // Set P3.2/LED to output direction
    P3OUT &= ~BIT2;                                          // P3.2 LED off
    P3DIR |= BIT3;                                           // Set P3.3/LED to output direction
    P3OUT &= ~BIT3;                                          // P3.3 LED off

    // Configure GPIOS for I2C
    P1SEL0 |= BIT2 | BIT3;                  // I2C pins(can't be used for I2C when used for analog)

}

void SCA_configGlobal(SCA_globalParams *globalParams){
    // Configure reference module
    PMMCTL0_H = PMMPW_H;                      // Unlock the PMM registers

    switch(globalParams->voltageReference){
        case 0:
            PMMCTL2 = INTREFEN | REFVSEL_2;           // Enable internal 2.5V reference
        break;
        case 1:
            PMMCTL2 = INTREFEN | REFVSEL_1;           // Enable internal 2.5V reference
        break;
        case 2:
            PMMCTL2 = INTREFEN | REFVSEL_0;           // Enable internal 2.5V reference
        break;
    }

    while(!(PMMCTL2 & REFGENRDY));            // Poll till internal reference settles

    // Initialize a timer for periodic wakeup
    // Configure Timer_B
    TB0CCR0 = 32768 / globalParams->wakeFrequency;                   // 0.5sec
    TB0CCTL0 = CCIE;                       // TBCCR0 interrupt enabled
    TB0CTL = TBSSEL__ACLK |                // Use ACLK
        MC__UP |                           // Up Mode
        TBCLR;                             // Clear counter

}

void SCA_configINT(SCA_intParams *intParams, uint8_t index){
    switch(index){
        case 0:
          switch(intParams->mode)
          {
          case 0:
              P4IE &= ~ BIT0;
              break;
          case 1:
              P4OUT |= BIT0;                          // Configure P4.0 as pulled-up
              P4REN |= BIT0;                          // P4.0 pull-up register enable
              P4IES |= BIT0;                          // P4.0 Hi/Low edge
              P4IE |= BIT0;                           // P4.0 interrupt enabled
              break;
          }
        case 1:
            switch(intParams->mode)
            {
            case 0:
                P4IE &= ~ BIT1;
            break;
            case 1:
                P4OUT |= BIT1;                          // Configure P4.1 as pulled-up
                P4REN |= BIT1;                          // P4.1 pull-up register enable
                P4IES |= BIT1;                          // P4.1 Hi/Low edge
                P4IE |= BIT1;                           // P4.1 interrupt enabled
            break;
            }
        break;
        case 2:
            switch(intParams->mode)
            {
            case 0:
                P3IE &= ~ BIT0;
            break;
            case 1:
                P3OUT |= BIT0;                          // Configure P3.0 as pulled-up
                P3REN |= BIT0;                          // P3.0 pull-up register enable
                P3IES |= BIT0;                          // P3.0 Hi/Low edge
                P3IE |= BIT0;                           // P3.0 interrupt enabled
            break;
            }
        break;
        case 3:
            switch(intParams->mode)
            {
            case 0:
                P2IE &= ~ BIT3;
            break;
            case 1:
                P2OUT |= BIT3;                          // Configure P3.0 as pulled-up
                P2REN |= BIT3;                          // P3.0 pull-up register enable
                P2IES |= BIT3;                          // P3.0 Hi/Low edge
                P2IE |= BIT3;                           // P3.0 interrupt enabled
            break;
            }
        break;
    }
}

void SCA_configPGA(SCA_pgaParams *pgaParams, uint8_t index){
    switch(index){
        case 0:
          switch(pgaParams->mode)
                {
                    case 0:
                        SAC0OA = 0;                  // Disable SAC0
                    break;
                    case 1:
                        SAC0OA = 0;                               // Disable SAC0
                        SAC0OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                        SAC0OA |= OAPM;                           // Select low speed and low power mode
                        SAC0PGA = ((pgaParams->gain)<<4) + MSEL_2;// Set Non-inverting PGA mode with Gain
                        SAC0OA |= SACEN + OAEN;                   // Enable SAC0 and OA
                    break;
                    case 2:
                        SAC0OA = 0;                               // Disable SAC0
                        SAC0OA |= NMUXEN + PMUXEN;                // Enable negative and positive input
                        SAC0OA |= OAPM;                           // Select low speed and low power mode
                        SAC0OA |= SACEN + OAEN;                   // Enable SAC and OA
                    break;
                    case 3:
                        SAC0OA = 0;                               // Disable SAC0
                        SAC0OA |= NMUXEN + PMUXEN + NSEL_1 + OAPM;// Enable negative and positive input
                                                                  // Select PGA source as OA negative input
                                                                  // Select low speed and low power mode
                        SAC0PGA |= MSEL_1;                        // Set as Unity-Gain Buffer Mode
                        SAC0OA |= SACEN + OAEN;                   // Enable SAC and OA
                    break;
                    case 4:
                        SAC0OA = 0;                               // Disable SAC0
                        SAC0DAC = DACSREF_1;                      // Select int Vref as DAC reference
                        SAC0DAT = pgaParams->dac;                // Initial DAC data
                        SAC0DAC |= DACEN;                         // Enable DAC

                        SAC0OA = NMUXEN + PMUXEN + PSEL_1 + NSEL_1;//Select positive and negative pin input
                        SAC0OA |= OAPM;                            // Select low speed and low power mode
                        SAC0PGA = MSEL_1;                          // Set OA as buffer mode
                        SAC0OA |= SACEN + OAEN;                    // Enable SAC and OA
                    break;
                    case 5:
                        SAC0OA = 0;                               // Disable SAC0

                        SAC0DAC = DACSREF_1;                      // Select int Vref as DAC reference
                        SAC0DAT = pgaParams->dac;                // Initial DAC data
                        SAC0DAC |= DACEN;                         // Enable DAC

                        SAC0OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                        SAC0OA |= OAPM;                           // Select low speed and low power mode
                        SAC0PGA = (pgaParams->gain<<4) + MSEL_0; // Set inverting PGA mode with Gain
                        SAC0OA |= SACEN + OAEN;                   // Enable SAC and OA
                    break;
                    case 6:
                        SAC0OA = 0;                               // Disable SAC0

                        SAC0DAC = DACSREF_1;                      // Select int Vref as DAC reference
                        SAC0DAT = pgaParams->dac;                 // Initial DAC data
                        SAC0DAC |= DACEN;                         // Enable DAC

                        SAC0OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                        SAC0OA |= OAPM;                           // Select low speed and low power mode
                        SAC0PGA = (pgaParams->gain<<4) + MSEL_3;  // Set inverting PGA paired mode with Gain

                        SAC0OA |= SACEN + OAEN;                   // Enable SAC and OA
                    break;
                }
          break;
          case 2:
              switch(pgaParams->mode)
                    {
                        case 0:
                            SAC2OA = 0;                               // Disable SAC2
                        case 1:
                            SAC2OA = 0;                               // Disable SAC2
                            SAC2OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select Pair OA source
                            SAC2OA |= OAPM;                           // Select low speed and low power mode
                            SAC2PGA = (pgaParams->gain<<4) + MSEL_2;  // Set Non-inverting PGA mode with Gain
                            SAC2OA |= SACEN + OAEN;                   // Enable SAC0 and OA
                        break;
                        case 2:
                            SAC2OA = 0;                               // Disable SAC2
                            SAC2OA |= NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;// Enable negative and positive input
                            SAC2OA |= OAPM;                           // Select low speed and low power mode
                            SAC2OA |= SACEN + OAEN;                   // Enable SAC and OA
                        break;
                        case 3:
                            SAC2OA = 0;                               // Disable SAC2
                            SAC2OA |= NMUXEN + PMUXEN + NSEL_1 + OAPM;// Enable negative and positive input
                                                                      // Select PGA source as OA negative input
                                                                      // Select low speed and low power mode
                            SAC2PGA |= MSEL_1;                        // Set as Unity-Gain Buffer Mode
                            SAC2OA |= SACEN + OAEN;                   // Enable SAC and OA
                        break;
                        case 4:
                            SAC2OA = 0;                               // Disable SAC2
                            SAC2DAC = DACSREF_1;                      // Select int Vref as DAC reference
                            SAC2DAT = pgaParams->dac;                 // Initial DAC data
                            SAC2DAC |= DACEN;                         // Enable DAC

                            SAC2OA = NMUXEN + PMUXEN + PSEL_1 + NSEL_1;//Select positive and negative pin input
                            SAC2OA |= OAPM;                            // Select low speed and low power mode
                            SAC2PGA = MSEL_1;                          // Set OA as buffer mode
                            SAC2OA |= SACEN + OAEN;                    // Enable SAC and OA
                        break;
                        case 5:
                            SAC2OA = 0;                               // Disable SAC2
                            SAC2DAC = DACSREF_1;                      // Select int Vref as DAC reference
                            SAC2DAT = pgaParams->dac;                 // Initial DAC data
                            SAC2DAC |= DACEN;                         // Enable DAC

                            SAC2OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                            SAC2OA |= OAPM;                           // Select low speed and low power mode
                            SAC2PGA = (pgaParams->gain<<4) + MSEL_0;  // Set inverting PGA mode with Gain
                            SAC2OA |= SACEN + OAEN;                   // Enable SAC and OA
                        break;
                    }
              break;
              case 1:
                  switch(pgaParams->mode)
                        {
                            case 0:
                                SAC1OA = 0;                               // Disable SAC1
                            break;
                            case 1:
                                SAC1OA = 0;                               // Disable SAC1
                                SAC1OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                                SAC1OA |= OAPM;                           // Select low speed and low power mode
                                SAC1PGA = (pgaParams->gain<<4) + MSEL_2;  // Set Non-inverting PGA mode with Gain
                                SAC1OA |= SACEN + OAEN;                   // Enable SAC0 and OA
                            break;
                            case 2:
                                SAC1OA = 0;                               // Disable SAC1
                                SAC1OA |= NMUXEN + PMUXEN;                // Enable negative and positive input
                                SAC1OA |= OAPM;                           // Select low speed and low power mode
                                SAC1OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                            case 3:
                                SAC1OA = 0;                               // Disable SAC1
                                SAC1OA |= NMUXEN + PMUXEN + NSEL_1 + OAPM;// Enable negative and positive input
                                                                          // Select PGA source as OA negative input
                                                                          // Select low speed and low power mode
                                SAC0PGA |= MSEL_1;                        // Set as Unity-Gain Buffer Mode
                                SAC1OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                            case 4:
                                SAC1OA = 0;                               // Disable SAC1
                                SAC1DAC = DACSREF_1;                      // Select int Vref as DAC reference
                                SAC1DAT = pgaParams->dac;                 // Initial DAC data
                                SAC1DAC |= DACEN;                         // Enable DAC

                                SAC1OA = NMUXEN + PMUXEN + PSEL_1 + NSEL_1;//Select positive and negative pin input
                                SAC1OA |= OAPM;                            // Select low speed and low power mode
                                SAC1PGA = MSEL_1;                          // Set OA as buffer mode
                                SAC1OA |= SACEN + OAEN;                    // Enable SAC and OA
                            break;
                            case 5:
                                SAC1OA = 0;                               // Disable SAC1
                                SAC1DAC = DACSREF_1;                      // Select int Vref as DAC reference
                                SAC1DAT = pgaParams->dac;                 // Initial DAC data
                                SAC1DAC |= DACEN;                         // Enable DAC

                                SAC1OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                                SAC1OA |= OAPM;                           // Select low speed and low power mode
                                SAC1PGA = (pgaParams->gain<<4) + MSEL_0;  // Set inverting PGA  mode with Gain
                                SAC1OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                            case 6:
                                SAC1OA = 0;                               // Disable SAC1
                                SAC1DAC = DACSREF_1;                      // Select int Vref as DAC reference
                                SAC1DAT = pgaParams->dac;                 // Initial DAC data
                                SAC1DAC |= DACEN;                         // Enable DAC

                                SAC1OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                                SAC1OA |= OAPM;                           // Select low speed and low power mode
                                SAC1PGA = (pgaParams->gain<<4) + MSEL_3;  // Set inverting PGA paired mode with Gain
                                SAC1OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                        }
                  break;
              case 3:
                  switch(pgaParams->mode)
                        {
                            case 0:
                                SAC3OA = 0;                               // Disable SAC3
                            case 1:
                                SAC3OA = 0;                               // Disable SAC3
                                SAC3OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                                SAC3OA |= OAPM;                           // Select low speed and low power mode
                                SAC3PGA = (pgaParams->gain<<4) + MSEL_2;  // Set Non-inverting PGA mode with Gain
                                SAC3OA |= SACEN + OAEN;                   // Enable SAC0 and OA
                            break;
                            case 2:
                                SAC3OA = 0;                               // Disable SAC3
                                SAC3OA |= NMUXEN + PMUXEN;                // Enable negative and positive input
                                SAC3OA |= OAPM;                           // Select low speed and low power mode
                                SAC3OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                            case 3:
                                SAC3OA = 0;                               // Disable SAC3
                                SAC3OA |= NMUXEN + PMUXEN + NSEL_1 + OAPM;// Enable negative and positive input
                                                                          // Select PGA source as OA negative input
                                                                          // Select low speed and low power mode
                                SAC3PGA |= MSEL_1;                        // Set as Unity-Gain Buffer Mode
                                SAC3OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                            case 4:
                                SAC3OA = 0;                               // Disable SAC3
                                SAC3DAC = DACSREF_1;                      // Select int Vref as DAC reference
                                SAC3DAT = pgaParams->dac;                 // Initial DAC data
                                SAC3DAC |= DACEN;                         // Enable DAC

                                SAC3OA = NMUXEN + PMUXEN + PSEL_1 + NSEL_1;//Select positive and negative pin input
                                SAC3OA |= OAPM;                            // Select low speed and low power mode
                                SAC3PGA = MSEL_1;                          // Set OA as buffer mode
                                SAC3OA |= SACEN + OAEN;                    // Enable SAC and OA
                            break;
                            case 5:
                                SAC3OA = 0;                               // Disable SAC3
                                SAC3DAC = DACSREF_1;                      // Select int Vref as DAC reference
                                SAC3DAT = pgaParams->dac;                 // Initial DAC data
                                SAC3DAC |= DACEN;                         // Enable DAC

                                SAC3OA = NMUXEN + PMUXEN + pgaParams->inputP + NSEL_1;//Select positive and negative pin input
                                SAC3OA |= OAPM;                           // Select low speed and low power mode
                                SAC3PGA = (pgaParams->gain<<4) + MSEL_0;  // Set inverting PGA mode with Gain
                                SAC3OA |= SACEN + OAEN;                   // Enable SAC and OA
                            break;
                        }
                  break;
    }
}

void SCA_configPWM(SCA_pwmParams *pwmParams, uint8_t index){
    // Setup Timer3_B
    TB3CCR0 = 100-1;                                          // PWM Period
    switch(index){
        case 0:
            TB3CCTL1 = OUTMOD_7;                              // CCR1 reset/set
            TB3CCR1 = pwmParams->dutyValue;                   // CCR1 PWM duty cycle
        break;
        case 1:
            TB3CCTL2 = OUTMOD_7;                              // CCR2 reset/set
            TB3CCR2 = pwmParams->dutyValue;                   // CCR2 PWM duty cycle
        break;
        case 2:
            TB3CCTL3 = OUTMOD_7;                              // CCR3 reset/set
            TB3CCR3 = pwmParams->dutyValue;                   // CCR3 PWM duty cycle
        break;
        case 3:
            TB3CCTL4 = OUTMOD_7;                              // CCR4 reset/set
            TB3CCR4 = pwmParams->dutyValue;                   // CCR4 PWM duty cycle
        break;
    }
    TB3CTL = TBSSEL_1 | MC_1 | TBCLR;                         // ACLK, up mode, clear TBR
}

void SCA_getADC(SCA_adcResults *adcResults, uint8_t index){
    switch(index){
        case 0:
            ADCMCTL0 |= ADCINCH_1;                                // A1 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult1 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_1;
        break;
        case 1:
            ADCMCTL0 |= ADCINCH_5;                                // A5 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult5 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_5;
        break;
        case 2:
            ADCMCTL0 |= ADCINCH_10;                               // A10 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult10 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_10;
        break;
        case 3:
            ADCMCTL0 |= ADCINCH_11;                               // A11 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult11 = ADC_Result;
            ADCCTL0 &= ~ADCENC;     // Disable
            ADCMCTL0 &= ~ADCINCH_11;
        break;
        case 4:
            ADCMCTL0 |= ADCINCH_4;                                // A4 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult4 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_4;
        break;
        case 5:
            ADCMCTL0 |= ADCINCH_6;                                // A6 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult6 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_6;
        break;
        case 6:
            ADCMCTL0 |= ADCINCH_7;                                // A7 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult7 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_7;
        break;
        case 7:
            ADCMCTL0 |= ADCINCH_8;                                // A8 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult8 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_8;
        break;
        case 8:
            ADCMCTL0 |= ADCINCH_9;                                // A9 ADC input select; Vref=AVCC
            ADCCTL0 |= ADCENC | ADCSC;                            // Sampling and conversion start
            __bis_SR_register(LPM0_bits | GIE);                   // LPM0, ADC_ISR will force exit
            __no_operation();                                     // For debug only
            adcResults->adcResult9 = ADC_Result;
            ADCCTL0 &= ~ADCENC;                                   // Disable
            ADCMCTL0 &= ~ADCINCH_9;
        break;
    }
}

void SCA_Config_RTC(int interval){
    P2SEL1 |= BIT6 | BIT7;
    //TBD
}

// This function configures multiple comparators
void SCA_configCOMP(SCA_compParams *compParams, uint8_t index){
    switch(index){
    case 0:
    switch(compParams->mode){
        case 0:
            CP0CTL0 = 0;
            CP0CTL1 = 0;
        break;
        case 1:
        // Setup eCOMP
            switch(compParams->inputN){
                case GPIO:
                    CP0CTL0 |= CPNSEL0;                      // Select P1.0/C0 and P1.1/C1 as eCOMP input
                break;
                case DAC:
                    CP0CTL0 |= CPNSEL_6;                     // Select DAC as input for V- terminal
                    CP0DACCTL |= CPDACREFS | CPDACEN;        // Select on-chip VREF and enable DAC
                    CP0DACDATA |= compParams->dac;           // CPDACBUF1=On-chip VREF *DAC Value
                break;
                case PGA0:
                    CP0CTL0 |= CPNSEL_1;
                break;
                case PGA2:
                    CP0CTL0 |= CPNSEL_5;
                break;
            }
            switch(compParams->inputP){
                case GPIO:
                    CP0CTL0 |= CPPSEL0;                        // Select P1.0/C0 and P1.1/C1 as eCOMP input
                break;
                case DAC:
                    CP0CTL0 |= CPPSEL_6;                       // Select DAC as input for V- terminal
                    CP0DACCTL |= CPDACREFS | CPDACEN;          // Select on-chip VREF and enable DAC
                    CP0DACDATA |= compParams->dac;             // CPDACBUF1=On-chip VREF *DAC Value
                break;
                case PGA0:
                    CP0CTL0 |= CPPSEL_5;
                break;
            }
            CP0CTL0 |= CPPEN | CPNEN;                          // Enable eCOMP input

            CP0CTL1 |= CPEN | CPMSEL;                          // Turn on eCOMP, in low power mode

        break;
    }
    break;
    case 1:
    switch(compParams->mode){
        case 0:
            CP1CTL0 = 0;
            CP1CTL1 = 0;
        break;
        case 1:
            // Setup eCOMP
                switch(compParams->inputN){
                    case GPIO:
                        CP1CTL0 |= CPNSEL0;                        // Select P1.0/C0 and P1.1/C1 as eCOMP input
                    break;
                    case DAC:
                        CP1CTL0 |= CPNSEL_6;                       // Select DAC as input for V- terminal
                        CP1DACCTL |= CPDACREFS | CPDACEN;          // Select on-chip VREF and enable DAC
                        CP1DACDATA |= compParams->dac;             // CPDACBUF1=On-chip VREF *DAC Value
                    break;
                    case PGA1:
                        CP1CTL0 |= CPNSEL_1;
                    break;
                    case PGA3:
                        CP1CTL0 |= CPNSEL_5;
                    break;
                }
                switch(compParams->inputP){
                    case GPIO:
                        CP1CTL0 |= CPPSEL0;                         // Select P1.0/C0 and P1.1/C1 as eCOMP input
                    break;
                    case DAC:
                        CP1CTL0 |= CPPSEL_6;                        // Select DAC as input for V- terminal
                        CP1DACCTL |= CPDACREFS | CPDACEN;           // Select on-chip VREF and enable DAC
                        CP1DACDATA |= compParams->dac;              // CPDACBUF1=On-chip VREF *DAC Value

                    break;
                    case PGA1:
                        CP1CTL0 |= CPPSEL_5;
                    break;
                }
            CP1CTL0 |= CPPEN | CPNEN;                              // Enable eCOMP input

            CP1CTL1 |= CPEN | CPMSEL;                              // Turn on eCOMP, in low power mode

//            CP1CTL1 |= CPEN;                                     // Turn on eCOMP, in high speed mode

        break;
    }
    break;
    }
}

SCA_pgaParams pgaParams[NUM_PGA] = {
                                     {
    .mode = PGA0_MODE,
    .inputP = PGA0_INPUTP,
    .gain = (PGA0_GAIN<<4),
    .dac = PGA0_DAC
                                     },
                                     {
    .mode = PGA1_MODE,
    .inputP = PGA1_INPUTP,
    .gain = (PGA1_GAIN<<4),
    .dac = PGA1_DAC
                                     },
                                     {
    .mode = PGA2_MODE,
    .inputP = PGA2_INPUTP,
    .gain = (PGA2_GAIN<<4),
    .dac = PGA2_DAC
                                     },
                                     {
    .mode = PGA3_MODE,
    .inputP = PGA3_INPUTP,
    .gain = (PGA3_GAIN<<4),
    .dac = PGA3_DAC
                                     }
};

SCA_pwmParams pwmParams[NUM_PWM] = {
{.dutyValue = 50},
{.dutyValue = 40},
{.dutyValue = 10},
{.dutyValue = 0}
};

SCA_compParams compParams[NUM_COMP] = {
{
      .mode = COMP0_MODE,
      .inputP = COMP0_INPUTP,
      .inputN = COMP0_INPUTN,
      .dac = COMP0_DAC
},
{
      .mode = COMP1_MODE,
      .inputP = COMP1_INPUTP,
      .inputN = COMP1_INPUTN,
      .dac = COMP1_DAC
}
};

SCA_intParams intParams[NUM_INT] = {
{
  .mode= ENABLED
},
{
  .mode= ENABLED
},
{
  .mode= ENABLED
},
{
  .mode= ENABLED
}
};

SCA_adcParams adcParams[NUM_ADC] = {
{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
},{
.mode= ENABLED
}
};

SCA_globalParams globalParams = {
     .powerMode = LOW_POWER,
     .voltageReference = VREF_2_5V,
     .wakeFrequency = 1
};

