/**************************************************************************************************
---------------------------------------------------------------------------------------------------
	Copyright (c) 2004, Jonathan Bagg
	All rights reserved.

	 Redistribution and use in source and binary forms, with or without modification, are permitted 
	 provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of 
	  conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of 
	  conditions and the following disclaimer in the documentation and/or other materials provided 
	  with the distribution.
    * Neither the name of Jonathan Bagg nor the names of its contributors may be used to 
	  endorse or promote products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------------------------------
   Project name : Infidigm AVR Drivers
   Processor	: ATMega64, ATMega128
   File name    : rf.h
---------------------------------------------------------------------------------------------------
   Modifications: 

   Revision 1.0  2004/07/05 	Bagg
   - Finished for release
   - Size = 1346
   - max speed  ~ 28,800
---------------------------------------------------------------------------------------------------
   Created      : 10 March 2004    	           Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
   RF Communications Module Driver; Model = BiM2 or 3
---------------------------------------------------------------------------------------------------
**************************************************************************************************/

/**************************************************************************************************
*   RF Packet Structure
*
*	1. 0xAA,0xAA = 10101010, 10101010
*		This Balences / centres the Data Slicer on the RF receiver
*	2. Unique 0x95 is send to mark the begining of a new packet. 0x95 is never used anywhere else.
*	3. Next the 'Destination Address' byte and 'length' of the message byte are encoded into three bytes.
*	4. The Message bytes are then send.  Two Message bytes are encoded at a time, yeilding three 
*		bytes to be transmitted
*	5. Finally the checksum is encoded and sent.
**************************************************************************************************/

#include "defines.h"

/**************************************************************************************************
*   Unit RF Address ( Can be 0-255.  255 is a 'broadcast-to-all' )
**************************************************************************************************/
#define RFaddress		10

/**************************************************************************************************
*   RF Driver Definitions
**************************************************************************************************/
#define RF_BAUD_RATE	19200         // RF baud rate
#define RF_BAUD_SELECT	(F_CPU/(RF_BAUD_RATE*16l)-1)
#define RFmsglen		32			//Maximum message length
#define	RFenPort		PORTC		//Physical Connection to Micro		
#define	EnRX			PC7
#define	EnTX			PC6
#define RFsleep			0			//RF Mode states
#define RFtx			1
#define RFrx			2
#define RFtxSize		64			//Transmit Buffer Size
#define RFrxSize		64			//Receive Buffer Size

/**************************************************************************************************
*   GLOBAL VARIABLES
**************************************************************************************************/
unsigned char RFmode;						//Contains RF Mode states
unsigned char RFrxpnt;						//RX Buffer Take-off point - for Applicatioon
unsigned char RFrxEND;						//RX Buffer Fillup point
unsigned char RFrxtemp;					//Temp RX Buffer Fillup point until checksum=ok
unsigned char RFtxBuff [RFtxSize];		//Transmit Buffer
unsigned char RFrxBuff [RFrxSize];		//Reciever Buffer
volatile unsigned char RFtxUsed;			//Used size of the Used TX Buffer 
volatile unsigned char RFrxUsed;			//Used size of the Used RX Buffer
volatile unsigned char newmsg;			//New Message Flag

/**************************************************************************************************
*
*   Function          : startRF
*
*   Description       : Sets up UART0 for use with the RF module.
*
*   Parameters        : None
*
*   Functions Called  : None
*
*   Returns           : None
*
**************************************************************************************************/
void startRF(void);

/**************************************************************************************************
*
*   Function          : RFsend
*
*   Description       : The function is responsible for building the RF TX Packet.  It begins by
*						changing the RF Mode to Transmit Mode. Next, the beginning of Packet is 
*						placed on the TX Buffer, then the address and length is encoded and placed
*						on the TX buffer. The function goes into a loop encoding the data / message
*						until the end is reached.  Finally the checksum is encoded and added to the
*						TX Buffer.
*
*   Parameters        : RF address to send message to, data (can be char, int, or array), sizeof(data)
*
*   Functions Called  : RFtxbyte(), RFencode()
*
*   Returns           : None
*
**************************************************************************************************/
void RFsend(char address, void *data, char length);

/**************************************************************************************************
*
*   Function          : RFencode ( INTERNAL )
*
*   Description       : The function splits up the two bytes passed to it into four nibbles. Each 
*						Nibble is encoded using RFtxTABLE and the result is packed into three bytes
*						and placed on the TX Buffer.
*
*   Parameters        : data to encode
*
*   Functions Called  : RFtxbyte()
*
*   Returns           : None
*
**************************************************************************************************/


/**************************************************************************************************
*
*   Function          : RFtxbyte ( INTERNAL )
*
*   Description       : The function waits for room on the TX Buffer and then places the passed 
*						data on it.
*
*   Parameters        : byte to put on the TX Buffer
*
*   Functions Called  : None
*
*   Returns           : None
*
**************************************************************************************************/


/**************************************************************************************************
*
*   Signal	          : SIG_UART0_TRANS
*
*   Description       : Takes the TX Buffer and Dumps it out UART0 until TX Buffer is empty; which 
*						is when 'RFtxUsed' = 0. When the TX Buffer is empty the ISR puts the RF
*						Module into RX mode.
*
*   Parameters        : None
*
*   Functions Called  : None
*
*   Returns           : None
*
**************************************************************************************************/

/**************************************************************************************************
*
*   Signal	          : SIG_UART0_RECV
*
*   Description       : Extracts / decodes the RF packet. Responsible for looking for a new message,
*						testing for valid address, valid length, and a valid checksum. If anything
*						fails, then the message will not be appended onto the buffer.
*
*   Parameters        : None
*
*   Functions Called  : RFdecode()
*
*   Returns           : None
*
**************************************************************************************************/

/**************************************************************************************************
*
*   Function          : RFdecode ( INTERNAL )
*
*   Description       : The function takes the three bytes of packed encoded 6-bit nibbles, splits
*						them up and decodes them using RFrxTABLE.  It then takes the decoded four 
*						Nibbles and places them on the RX Buffer in the form of two bytes. It also
*						checks, and passes on a Bad decode.
*
*   Parameters        : data to Decode
*
*   Functions Called  : RFrxTABLE
*
*   Returns           : 1 for valid decode, 0 for Bad decode
*
**************************************************************************************************/

