/**
 * ----------------------------------------HSLU T&A------------------------------------------
 *
 * $HeadURL: svn://ceesar.dyndns.info/home/_svn/hasler/software/us-transmitter/trunk/Software_CC2430_IAR_v7.20/rf.c $
 * $LastChangedBy: tasteige $
 * $LastChangedRevision: 363 $
 * $LastChangedDate: 2008-09-02 15:15:04 +0200 (Di, 02 Sep 2008) $
 * $Id: rf.c 363 2008-09-02 13:15:04Z tasteige $
 *
 *
-------------------------------------------------------------------------------------------
 *
 * Description:
 *
 *  Listen with the radio Part on the advice to send a signal with US
 *
 **/


#include "rf.h"


BYTE myPacket[128];
BYTE signalStrength;
BYTE linkQuality;
BYTE packetLength;
UINT16 actualTime;
BYTE sendUS=0;

UINT16 pwmTable [256] =
{
	0, 15, 31, 47, 63, 80, 96, 112, 129, 146, 162, 179,
	196, 213, 230, 248, 265, 283, 300, 318, 336, 354, 372,
	390, 408, 427, 445, 464, 483, 502, 521, 540, 559, 578,
	598, 618, 637, 657, 677, 697, 717, 738, 758, 779, 800,
	821, 842, 863, 884, 906, 927, 949, 971, 993, 1015,
	1037, 1059, 1082, 1105, 1127, 1150, 1174, 1197, 1220,
	1244, 1267, 1291, 1315, 1340, 1364, 1388, 1413, 1438,
	1463, 1488, 1513, 1538, 1564, 1590, 1616, 1642, 1668,
	1694, 1721, 1748, 1774, 1802, 1829, 1856, 1884, 1911,
	1939, 1968, 1996, 2024, 2053, 2082, 2111, 2140, 2169,
	2199, 2229, 2259, 2289, 2319, 2350, 2380, 2411, 2442,
	2474, 2505, 2537, 2569, 2601, 2633, 2666, 2698, 2731,
	2764, 2798, 2831, 2865, 2899, 2933, 2968, 3002, 3037,
	3072, 3107, 3143, 3179, 3215, 3251, 3287, 3324, 3361,
	3398, 3435, 3473, 3511, 3549, 3587, 3625, 3664, 3703,
	3743, 3782, 3822, 3862, 3902, 3943, 3983, 4025, 4066,
	4107, 4149, 4191, 4234, 4276, 4319, 4362, 4406, 4450,
	4494, 4538, 4582, 4627, 4672, 4718, 4764, 4810, 4856,
	4902, 4949, 4996, 5044, 5092, 5140, 5188, 5237, 5286,
	5335, 5385, 5435, 5485, 5535, 5586, 5638, 5689, 5741,
	5793, 5846, 5898, 5952, 6005, 6059, 6113, 6168, 6223,
	6278, 6333, 6389, 6446, 6502, 6559, 6617, 6674, 6732,
	6791, 6850, 6909, 6968, 7028, 7089, 7149, 7211, 7272,
	7334, 7396, 7459, 7522, 7585, 7649, 7714, 7778, 7843,
	7909, 7975, 8041, 8108, 8175, 8242, 8310, 8379, 8448,
	8517, 8587, 8657, 8728, 8799, 8870, 8942, 9015, 9088,
	9161, 9235, 9309, 9384, 9459, 9535, 9611, 9688, 9765,
	9843, 9921, 10000
};

UINT16 syncWait = 1;
UINT16 myTime=0;
UINT16 countSinceLastRFSignal=0;

static UINT8 seqNumber = 0;

void setPWMTime(UINT8 value);

/**
* Initializes the timer 3 for measuring TIMER3PERIODUS (normally 600us) cycles!
*
*/

void rf_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;
}


/******************************************************************************
* @fn  rfInit
*
* @brief
*      This function configures the radio for simple send and receive operation.
*      Advanced IEEE 802.15.4 functionality such as Address Decoding, AutoAck
*      etc is not employed. CRC value is automatically calculated to enable
*      detection of packet corruption. The desired frequency is set. The
*      function returns TRUE if the configuration is successful.
*
* Parameters:
*
* @param  UINT32 frequency
*         The desired Radio Frequency in kHz.
*
* @return BOOL
*         Returns TRUE if the configuration is successful and FALSE otherwise.
*/

BOOL rf_Init(UINT32 frequency)
{
	myTime=myAddr * 50;
	BOOL status;

	// Setting the frequency
	halRfSetRadioFrequency(frequency);

	// Checking that the entered frequency is valid
	if (frequency > 2047000)
	{
		//turning on power to analog part of radio and waiting for voltage regulator.
		RFPWR = 0x04;
		while((RFPWR & 0x10)){}
		// Turning off Address Decoding
		MDMCTRL0H &= ~ADR_DECODE;
		// Setting for AUTO CRC
		MDMCTRL0L |= AUTO_CRC;
		// Turning on AUTO_TX2RX
		FSMTC1 = ((FSMTC1 & (~AUTO_TX2RX_OFF & ~RX2RX_TIME_OFF))  | ACCEPT_ACKPKT);
		// Turning off abortRxOnSrxon.
		FSMTC1 &= ~0x20;

		status = TRUE;
	}
	else {
		status = FALSE;
	}

	return status;
}


/**
*   Call this function in Main() to wait on the RF Packet and to check it.
*
*/

void rf_waitAndCheckRFPacket()
{
	// wait on radio signal
	do{
					         //on and the RF infrastructure is not running any more;
		packetLength = halRfReceivePacket(myPacket, &signalStrength, &linkQuality, TIMEOUTRF);
	}
	while(!(rf_checkPacket(myPacket)));   //check if the CC2430 received his packet
}



/**
*   Analysis of the received packet. It sets also the time the badge has to wait
*   until it's his turn with sending the US signal
*
*   BYTE*packet: a pointer on the Array where the packet is stored. The array has to have space
*                for at least 128 bytes!
*
*   return: true if correct packet, 0 if the packet is not usable.
*/

BOOL rf_checkPacket(BYTE* packet){
  UINT8 PWMValue = 0;


	if((packetLength > 0)&&(packet[9]==ID)&&(seqNumber!=packet[2]))
	{
            PWMValue = packet[11];
            seqNumber = packet[2];
            printf("WiPoM: PWM-Wert=%d\r<n",PWMValue);

            setPWMTime(PWMValue);
	}
	return 1;
}


void setPWMTime(UINT8 value)
{
  UINT16 pwmValue;

  pwmValue = pwmTable[value];
  T1CC1H = (pwmValue >> 8);
  T1CC1L = pwmValue;
}
