/*
 *******************************************************************************************************
 * Compiler:                IAR C Compiler for 8051                                                    *
 * Target platform:         Chipcon CC2510                                                             *
 * Author:                  TBR, KHO, ESY, MJB                                                         *
 * Last Update:             30 Jan 2010 MJB                                                            *
 *******************************************************************************************************
*/

#ifndef TWOWAY_H
#define TWOWAY_H


/***********************************************************************************
* INCLUDES
*/

#include "hal.h"
#include "iocc2510.h"

/***********************************************************************************
* Compile-time options
*/

// Choose mode by comment/uncommenting the #define below

#define MASTER          // Uncomment this line if compiling code for the Master (beacon)
#ifndef MASTER
#define SLAVE           // If MASTER is not defiened, define unit as SLAVE
#endif

// Choose Modulation (MSK or GFSK)

#define MSK           // Uncomment this line to select MSK modulation
#ifndef MSK
#define GFSK
#endif

// Include the AGC code by uncommenting this define
#define AGC

// Uncomment the following line to 'mix' in local audio (so that you can hear yourself)
#define LOCALFEEDBACK

/***********************************************************************************
* Global definitions
*/

#define MAC_ADDR                    0xC3    // The system's 1 byte MAC address
#define RADIO_BASE_FREQ             2400000 // Radio frequency (kHz) for channel 0

// Number of ADC samples per period
#define AF_LEN  60

// Audio and RF buffer sizes
#define AF_BUF_SIZE  AF_LEN    // Size of audio buffer (in words) = number of ADC samples

// This is the Audio DAC value sent to the PWM whenever a bad packet is received
// It should be set to the nominal ADC value with 'no audio'
#define NO_SIGNAL_VALUE 0x036E 

// Audio sampling rate settings (Timer1 period adjustments)
// Note: Timer 1 is clocked by the 26.000 MHz XTAL, so one timer tic equals 38.46 ns

#define NORMAL_SAMPLE_PERIOD    3249                // 3249 * 38.46 ns -> ~125 us (8 kHz sample rate)
#define CLOCK_SKEW_STEP_SIZE       2                //    2 * 38.46 ns -> ~77 ns

#define SPEEDUP   AF_LEN/2 - 5
#define SLOWDOWN  AF_LEN/2 + 5

// Packet format and segment lengths (bytes)
#define PHY_FIELD_LEN               1         // Length byte
#define MAC_FIELD_LEN               1         // MAC address
#define DF_LEN                     90         // Data field length = Audio frame length times 1.5
#define MAC_APPENDED_STATUS_LEN     2         // Status bytes added by PHY (CC2510)

#ifdef MASTER
#define TX_PAYLOAD_LEN  (MAC_FIELD_LEN + DF_LEN)
#define RX_PAYLOAD_LEN  (MAC_FIELD_LEN + DF_LEN)
#else
#define RX_PAYLOAD_LEN  (MAC_FIELD_LEN + DF_LEN)
#define TX_PAYLOAD_LEN  (MAC_FIELD_LEN + DF_LEN)
#endif

// DMA Channel Allocation
#define DMA_RX  1
#define DMA_TX  2

// Frequency hopping
#define FH_ACTIVE_TABLE_LEN 4
#define CHANNEL_1    2    // 2400 +   2*.300 = 2400.600 MHz
#define CHANNEL_2    6    // 2400 +   6*.300 = 2401.500 MHz
#define CHANNEL_3  242    // 2400 + 242*.300 = 2472.600 MHz
#define CHANNEL_4  246    // 2400 + 246*.300 = 2473.500 MHz

// Timer flags
#define T3OVFIF                         0x01

// RX Off modes
#define RXOFFMODE_IDLE                  0x00
#define RXOFFMODE_FSTXON                0x04
#define RXOFFMODE_TX                    0x08
#define RXOFFMODE_RX                    0x0C

// Marcstates
#define MARCSTATE_TX                    0x13
#define MARCSTATE_RX                    0x0D
#define MARCSTATE_IDLE                  0x01

// PKTSTATUS register bit definitions
#define SFD                             0x08

// T2 (Frame timer) Timeout defines
// T2 tic period is 128*6/26.000 MHz = 29.538 usec and counts DOWN
#define FRAME_TIMEOUT_DEFAULT           254       // (T2CT) Timeout = 254 * 29.538 us = 7.503 ms
#define LISTENFORMASTER                 102       // Start listening for master 2920 usec before the end of frame
                                                  // or about 200 usec before packet is expected

// T3 Timeout defines (First parameter is timeout in 4.923076923 us, Second parameter is multiplier since this is a 8 bit timer only)
// T3 tic period is 128/26 = 4.923076923 us
#define MASTER_TX_TIMEOUT_WO_CALIB      196, 3    // Timeout = 196 * 4.923076923 us * 3 = 2894.8 us
#define MASTER_RX_TIMEOUT_WO_CALIB      163, 2    // Timeout = 163 * 4.923076923 us * 2 = 1604.9 us
#define SLAVE_TX_TIMEOUT_WO_CALIB       196, 3    // Timeout = 196 * 4.923076923 us * 3 = 2894.8 us
#define SLAVE_RX_TIMEOUT_WO_CALIB       163, 2    // Timeout = 163 * 4.923076923 us * 2 = 1604.9 us
#define LISTEN_FOR_BEACON_TIMEOUT       250, 26   // Timeout = 250 * 4.923076923 us * 26 = 32.000 msec

// RX packet status
#define PKT_OK                          0x01
#define CRC_ERROR                       0x02
#define	TIMEOUT_ERROR                   0x04
#define PKT_ERROR                       0x08
#define DMA_ERROR                       0x10
#define PKT_STATUS_UNKNOWN              0xFF

// Packet status values/bits
#define CRC_OK_MASK     0x80
#define RSSI_VAL_IDX      0
#define CRC_LQI_VAL_IDX   1

// LED, Pushbutton Assignments

#define LED_BLUE_REG  P0
#define LED_BLUE   0x80   // P0_7
#define LED_RED_REG P1
#define LED_RED    0x01   // P1_0
#define LED_ORANGE_REG P1
#define LED_ORANGE 0x04   // P1_2
#define LED_GREEN_REG P1
#define LED_GREEN  0x08   // P1_3

#define PB_SPARE_REG P0
#define PB_SPARE   0x10   // P0_4
#define PB_TALK_REG P2
#define PB_TALK    0x01   // P2_0

// VGA Gain (Microphone)

#define VGA_GAIN_REG    P0
#define VGA_GAIN_BIT_0  0x20    // bit 5
#define VGA_GAIN_BIT_1  0x40    // bit 6
#define VGA_GAIN_MSK    0x60

/***********************************************************************************
* STRUCTURE DECLARATIONS
*/

#ifdef MASTER

// TX struct
typedef struct{
  BYTE macPayloadLen;                   // Length byte
  BYTE macField;                        // MAC address
  BYTE payload[DF_LEN];                 // Audio samples
} __xdata TX_MASTER_STRUCT;

// RX struct
typedef struct{
  BYTE macPayloadLen;                   // Length byte
  BYTE macField;                        // MAC address
  BYTE payload[DF_LEN];                 // Audio samples
  BYTE append[MAC_APPENDED_STATUS_LEN]; // Status bytes (RSSI, LQI)
} __xdata RX_MASTER_STRUCT;

#endif

#ifdef SLAVE

// RX struct
typedef struct{
  BYTE macPayloadLen;                   // Length byte
  BYTE macField;                        // MAC address
  BYTE payload[DF_LEN];                 // Audio samples
  BYTE append[MAC_APPENDED_STATUS_LEN]; // Status bytes (RSSI, LQI)
} __xdata RX_SLAVE_STRUCT;

// TX struct
typedef struct{
  BYTE macPayloadLen;                   // Length byte
  BYTE macField;                        // MAC address
  BYTE payload[DF_LEN];                 // Audio samples
} __xdata TX_SLAVE_STRUCT;

#endif

/***********************************************************************************
* Function prototype declarations
*/

// tw_dma.c
void initDma(void);
void dmaToRadio(WORD length, WORD srcAddr);
void dmaFromRadio(WORD length, WORD dstAddr);

// tw_adc.c
void initAdc(void);

// tw_dsm.c
void initDsm(void);

// tw_rf.c
BOOL initRf(UINT32 frequency);
BOOL rfSendPacket(UINT8 timeout, UINT8 multiplier);
#ifdef MASTER
void rfReceivePacket(RX_MASTER_STRUCT __xdata * rxData, UINT8 timeout, UINT8 t3_multiplier, UINT8 packetlen);
#else
void rfReceivePacket(RX_SLAVE_STRUCT __xdata * rxData, UINT8 timeout, UINT8 t3_multiplier, UINT8 packetlen);
void ListenforMaster(RX_SLAVE_STRUCT __xdata * rxData, UINT8 timeout, UINT8 t3_multiplier, UINT8 packetlen);
#endif

// tw_mac.c
void initFrameTimer(void);
void startFrameTimer(UINT8 cnt);
void macTimer3Init(void);
void macsetT3TimeoutAndWait(UINT8 timeout, UINT8 multiplier);
void setChannel (UINT8 ch);

/***********************************************************************************
* Global extern variables
*/

#ifdef MASTER
extern TX_MASTER_STRUCT __xdata MAStxData;      // Data packet structers
extern RX_MASTER_STRUCT __xdata MASrxData;
#endif

#ifdef SLAVE
extern RX_SLAVE_STRUCT __xdata SLVrxData;       // Data packet structers
extern TX_SLAVE_STRUCT __xdata SLVtxData;
#endif

extern DMA_DESC __xdata *dmaDescTx;             // Pointer to the Tx DMA descriptor
extern DMA_DESC __xdata *dmaDescRx;             // Pointer to the Rx DMA descriptor

extern volatile UINT8 __xdata rxPacketStatus;

extern UINT8 __xdata activeTable[FH_ACTIVE_TABLE_LEN];

extern UINT16 __xdata audioIn[2][AF_BUF_SIZE];      // Audio In (microphone) Buffer
extern UINT8  __xdata activeIn;                     // Audio Input active buffer (0 | 1)
extern UINT16 __xdata audioOut[2][AF_BUF_SIZE];     // Audio Out (playback) Buffer
extern UINT8  __xdata activeOut;                    // Audio Out active buffer (0 | 1)
extern UINT8  __xdata rxBufferAvailable;            // Bit will be set high when the buffer is available

#define BUFFERA 0x40          // Bit 6 will be set whenever RX Buffer A is available
#define BUFFERB 0x80          // Bit 7 will be set whenever RX Buffer B is available

extern UINT8 audioInindx;
extern UINT8 audioOutindx;
extern UINT16 timerSampleRate;
extern BOOL __xdata audioFrameReady;
extern UINT8 sampleCount;
extern UINT8 __xdata ActiveChIdx;
extern BOOL sfdDetected;
extern BOOL dmaDone;
#endif


/***********************************************************************************
  Copyright 2008 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.
***********************************************************************************/



