/**
  ******************************************************************************
  * @file    TemperatureSensorCircuitry.c
  * @author  Johan Zwarteveld
  * @version V1.0
  * @date    05-08-2015
  * @brief   Temperature Sensor Circuit Driver
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "TemperatureSensorCircuitry.h"
#include "DelayTimer.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Num_of_Results 32
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile uint16_t A2results[Num_of_Results];
volatile uint16_t A3results[Num_of_Results];
/* Private function prototypes -----------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
void TSC_Init(void)
{
	/* Setup the shared reference module */
	REFCTL0 = 0x00;																		// Disable shared reference module

	/* Initialize A/D conversion module */
	ADC12CTL0 = ADC12MSC | ADC12SHT0_8 | ADC12ON;										// set multi sample and conversion,
																						// set sample and hold time to 512 adc12 clock cycles.
	ADC12CTL1 = ADC12SHP | ADC12DIV_3 | ADC12CONSEQ_3;									// Use sampling timer, repeated sequence of channels
	ADC12CTL2 = ADC12SR | ADC12REFBURST | ADC12RES_2;									// Set max sample rate to 50ksps,
																						// use reference buffer only during conversion
	ADC12MCTL0 = ADC12SREF_0 | ADC12INCH_2;												// Vr+=AVcc, Vr-=AVss, channel = A2
	ADC12MCTL1 = ADC12SREF_0 | ADC12INCH_3 | ADC12EOS;									// Vr+=AVcc, Vr-=AVss, channel = A3, end of sequence

	ADC12IE |= ADC12IFG1;
}

void TSC_Measure(uint16_t *TempSensor1, uint16_t *TempSensor2)
{
	uint8_t i;
	uint32_t Temp1;
	uint32_t Temp2;
	// turn temperature sensor circuitry on
	P3OUT |= (BIT4 | BIT6);

	// wait for the circuitry to stablize
	DelayTimer_Wait(10);

	// Make sure all interrupt flags are cleared
	ADC12IFG = 0x00;

	// start measurement
	ADC12CTL1 |= ADC12CONSEQ_3;
	ADC12CTL0 |= (ADC12ENC | ADC12SC);

	// Sleep until measurement is done
	__bis_SR_register( LPM3_bits + GIE );

	// turn temperature sensor circuitry off
	P3OUT &= ~(BIT4 | BIT6);

	// Calculate average measurement value
	Temp1 = A2results[0];
	Temp2 = A3results[0];
	for (i = (Num_of_Results-1); i > 0; i--)
	{
		Temp1 += A2results[i];
		Temp2 += A3results[i];
	}
	*TempSensor1 = (uint16_t)(Temp1 >> 5);
	*TempSensor2 = (uint16_t)(Temp2 >> 5);
}
/* Private functions  --------------------------------------------------------*/
/* Interrupt Service Routine -------------------------------------------------*/
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12ISR (void)
#else
#error Compiler not supported!
#endif
{
	static uint8_t index = 0;

	switch(__even_in_range(ADC12IV,34))
	{
		case 0: break;								// Vector  0:  No interrupt
		case 2: break;								// Vector  2:  ADC overflow
		case 4: break;								// Vector  4:  ADC timing overflow
		case 6: break;								// Vector  6:  ADC12IFG0
		case 8:										// Vector  8:  ADC12IFG1
			A2results[index] = ADC12MEM0;
			A3results[index] = ADC12MEM1;

			index++;

			if ((index & (Num_of_Results-1)) == 0x00)
			{
				ADC12CTL1 &= ~ADC12CONSEQ_3;		// stop any conversion
				ADC12CTL0 &= ~(ADC12ENC | ADC12SC);
				index = 0;
				LPM4_EXIT;
			}
			break;
		case 10: break;								// Vector 10:  ADC12IFG2
		case 12: break;								// Vector 12:  ADC12IFG3
		case 14: break;								// Vector 14:  ADC12IFG4
		case 16: break;								// Vector 16:  ADC12IFG5
		case 18: break;								// Vector 18:  ADC12IFG6
		case 20: break;								// Vector 20:  ADC12IFG7
		case 22: break;								// Vector 22:  ADC12IFG8
		case 24: break;								// Vector 24:  ADC12IFG9
		case 26: break;								// Vector 26:  ADC12IFG10
		case 28: break;								// Vector 28:  ADC12IFG11
		case 30: break;								// Vector 30:  ADC12IFG12
		case 32: break;								// Vector 32:  ADC12IFG13
		case 34: break;								// Vector 34:  ADC12IFG14
		default: break;
	}
}
