/**
 * ----------------------------------------HSLU T&A------------------------------------------
 *
 * $HeadURL: svn://ceesar.dyndns.info/home/_svn/hasler/software/us-transmitter/trunk/Software_CC2430_IAR_v7.20/US_Transmitter_Main.c $
 * $LastChangedBy: tahussma $
 * $LastChangedRevision: 485 $
 * $LastChangedDate: 2008-11-07 15:33:21 +0100 (Fr, 07 Nov 2008) $
 * $Id: US_Transmitter_Main.c 485 2008-11-07 14:33:21Z tahussma $
 *
 *
-------------------------------------------------------------------------------------------
 *
 * Description:
 * This project includes the software for the US transmitter used as a badge to localise
 * the badge in a specific area.
 *
 *
 **/



//Includes
#include "global.h"
#include "channel.h"
#include "hal.h"
#include "ioCC2430.h"
#include "rf.h"

//Global Variables

UINT8 timer3Interrupt;          //Timer 3 Interrupt Flag
extern UINT16 syncWait;
extern BYTE sendUS;
UINT8 myST0;
UINT8 myST1;
UINT8 myST2;

#define PACKET_LENGTH 32
#define BAUD 115200

void prepareAccAlert();
void sendPreparedPacket();

void addtoSleep2(UINT16 msec);
UINT32 setFrequency(UINT8 channel);
void init_LED(void);
void initRS232(void);
void Timer3Init(UINT16 usec);
void initTimer1(void);
void initTimer4(void);

void zeroCrossingPoint(void);


/******************************************************************************
MAIN FUNCTION
******************************************************************************/
void main(void)
{
	SET_32KHZ_CLOCK_SOURCE(CRYSTAL);
	SET_MAIN_CLOCK_SOURCE(CRYSTAL);
	CLKCON &= 0x00;                   //set clock to 32MHz

        initRS232();

	//Timer3Init(TIMER3PERIODUS);          //set timer 3 to timer3Period usec
	rf_Init(setFrequency(CHANNEL));

	TXCTRLL = 0xFF;                           // Change PA-Output Power

        timer3Interrupt=0;

	INT_SETFLAG(INUM_RF, INT_CLR);
	INT_ENABLE(INUM_RF, INT_ON);
	INT_SETFLAG(INUM_T3, INT_CLR);
	INT_ENABLE(INUM_T3, INT_ON);
	RFIM |= 0x10;       //enable interrupt for start of RF receiving (SFD)

        //Enable extern Interrupt on P1_0
        PICTL |= 0x02;            //falling edge
        P1SEL &= ~0x01;
	P1DIR &= ~0x01;
	P1IFG &= ~0x01; // clear interrupt flag P1_0
        P1IEN = 0x01;  // disable interrupt P1_0
	//INT_SETFLAG(INUM_P1INT, INT_CLR);
	INT_ENABLE(INUM_P1INT, INT_ON);

        initTimer4();
        initTimer1();
        TIMER1_RUN(TRUE);

        INT_GLOBAL_ENABLE(TRUE);

	printf("everything should be initialized now\n");
	
	//First turn on power to analog part of radio
	RFPWR = 0x04;
	while(RFPWR & 0x10);

	while(1){

		rf_waitAndCheckRFPacket();
		
	}//end while(1)
}


void initRS232(void)
{
    UART_SETUP(0,BAUD,HIGH_STOP);
    INT_SETFLAG(INUM_UTX0, INT_SET);
    INT_ENABLE(INUM_UTX0, INT_OFF);
    INT_SETFLAG(INUM_URX0, INT_CLR);
    INT_ENABLE(INUM_URX0, INT_OFF);
    U0CSR |= UART_ENABLE_RECEIVE;
}


/**
 * Berechnet die Frequenz anhand des übergebenen Channels
 *
 * @param [in] channel der gewünschte Kanal
 * @return die Frequenz des Kanals.
 */
UINT32 setFrequency(UINT8 channel)
{
	UINT32 myfrequency;
	myfrequency = 5*(channel-11);
	myfrequency += 2405;
	myfrequency *= 1000;
	return myfrequency;
}


/******************************************************************************
addtoSleep2
Reinitializes Sleep Timer with number of milliseconds to sleep (msec)
******************************************************************************/
void addtoSleep2(UINT16 msec){
	UINT32 sleepTimer = 0;
	sleepTimer |= ST0;
	sleepTimer |= (UINT32)ST1 <<  8;
	sleepTimer |= (UINT32)ST2 << 16;
	sleepTimer += (msec * ((UINT32)33));
	sleepTimer -= (UINT32)msec >> 2;
	ST2 = (UINT8)(sleepTimer >> 16);
	ST1 = (UINT8)(sleepTimer >> 8);
	ST0 = (UINT8) sleepTimer;
}




/******************************************************************************
Interrupt for wake up from sleep timer
***************************************************************************/
#pragma vector=ST_VECTOR
__interrupt void powermodes_ST_IRQ(void){
	INT_SETFLAG(INUM_ST, INT_CLR);
}


/******************************************************************************
Interrupt for starting to read the RF packet (SFD Flag)
******************************************************************************/
#pragma vector=RF_VECTOR
__interrupt void RF_IRQ(void){
	INT_SETFLAG(INUM_RF, INT_CLR);
	RFIF = 0x00;
	T3CTL |= 0x04;     // reset counter timer 3
	TIMER3_RUN(TRUE);  // start timer3
	timer3Interrupt=0;
	//addtoSleep2(1000);                 // sleep in PM2 for x milliseconds
	//nur ST0-ST2 sichern!! in dieser intsubroutine 1.Zeile
	myST0=ST0;
	myST1=ST1;
	myST2=ST2;
}


/******************************************************************************
Interrupt for TIMER3PERIODUS (value set in global.h) timer 3
******************************************************************************/
#pragma vector=T3_VECTOR
__interrupt void T3_IRQ(void){
	INT_SETFLAG(INUM_T3, INT_CLR);
	timer3Interrupt++;
	// after 3 * TIMER3PERIODUS stop the timer
	if(timer3Interrupt >=3)
	{
		TIMER3_RUN(FALSE);  // stop timer3
	}
}
/******************************************************************************
Interrupt for TIMER1
******************************************************************************/
#pragma vector=T1_VECTOR
__interrupt void T1_IRQ(void){
	INT_SETFLAG(INUM_T1, INT_CLR);
        // Interrupt Nulldurchgang aktivieren

        if(T1CTL&0x80){         //CH2 Interrupt
          T1CTL &= ~0x80;
          P1IEN |= 0x01;        // enable interrupt P1_0
          P1IFG &= ~0x01;
          IEN2 |= 0x10;
	  //INT_ENABLE(INUM_P1INT, INT_ON);
        }

        if(T1CTL&0x10)          //Overflow Interrupt
        {
           T1CTL &= ~0x10;
           P1IEN &= ~0x01;  // disable interrupt P1_0
           IEN2 &= ~0x10;
	   //INT_ENABLE(INUM_P1INT, INT_OFF);
        }

}

/******************************************************************************
Interrupt for TIMER4
******************************************************************************/
#pragma vector=T4_VECTOR
__interrupt void T4_IRQ(void){
  INT_SETFLAG(INUM_T4, INT_CLR);

  zeroCrossingPoint();
  TIMER4_RUN(FALSE);
  T4CTL |= 0x02;
}

///******************************************************************************
//Interrupt on pin P1_0
//******************************************************************************/
#pragma vector=P1INT_VECTOR
__interrupt void P1_IRQ(void){

  EA = FALSE;
  if(P1IFG & 0x01){
    //P1IFG &= ~0x01; // clear interrupt flag P1_0
    //INT_SETFLAG(INUM_P1INT, INT_CLR);
    P1IFG &= ~0x01;
    //zeroCrossingPoint();
    TIMER4_RUN(TRUE);
    P1IEN &= ~0x01; // disable interrupt P1_0
    IEN2 &= ~0x10;
    //INT_ENABLE(INUM_P1INT, INT_OFF);
  }
  EA = TRUE;
}

///******************************************************************************
//Zero Crossing Point detection
//******************************************************************************/

void zeroCrossingPoint(void)
{
  T1CNTL = 0x00;
}

///******************************************************************************
// init
//******************************************************************************/

void initTimer1(void)
{
  //Interrupt Timer 1
  IEN1 |= 0x02;                 //CLK/32

  //Timer 1 modulo mode
  T1CTL = 0x0A;
  T1CC0L = 0x10;                // 10000 us
  T1CC0H = 0x27;
  TIMIF |= 0x40;

  //Channel 1 PWM
  PERCFG |= 0x40;               //Alt.2
  IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH);
  T1CCTL1 = 0x1C;
  T1CC1H = 0x00;                //"PWM aus"
  T1CC1L = 0x00;

  //Channel2
  T1CCTL2 = 0x4C;
  T1CC2H =  0x25;
  T1CC2L =  0x80;
}

void initTimer4(void)
{
  PERCFG |= 0x10;

  IEN1 |= 0x10;
  //Timer 4
  T4CTL = 0xAE;
  T4CC0 = 0xC8;
  TIMIF &= ~0x08;
  //Interrupt Flag
}

void Timer3Init(UINT16 usec)
{
	usec=usec >> 2;
	T3CTL   = 0xEE; //Interrupt enabled and modulo counter Tick freq/128
	T3CCTL0 = 0x00;
	T3CC0   = usec; //set overflow to usec
	T3CCTL1 = 0x00;
	T3CC1   = 0x00;
}

