#include "global_typedefs.h"
#include "i2c_GPS_Slave.h"
#include "gps_driver.h"

static uint_2 I2CBufferPtr;
static uint_1 I2CBuffer[MAX_BUFFER_LENGTH];

extern NavSolMsg_Type solution;            //This has a blend of all the messages 
extern NavTimeUTC_Type utc_time;            //This will be my utc time.  I think
extern NavPosLLHMsg_Type pos_llh;           //Lat long and altitude

/*---------------------------------------------------------------------------*/
void I2C_Slave_Init(void)
{
  P3SEL |= 0x0A;             // Select I2C pins 1010 (Turns P3.3 and P3.1 to 1, peripheral function. (SDA and SCL lines)
  P3SEL &= ~BIT0;            // Make sure the I2C isolator is turned on (Turns P3.0 (STE0) to 0, Digital IO)
  U0CTL = SWRST;            // Reset USART state machine (USART0 Control Register |= SWRST... what is SWRST?
  U0CTL |= I2C + SYNC;       // Recommended init procedure (I2C bit set to I2C Mode, SYNC bit set to I2C mode)
  U0CTL &= ~I2CEN;           // Recommended init procedure (Turns OFF I2C)
  I2COA = GPSAddress;      // Set own address
  U0CTL &= ~MST;             // Set slave mode (Master Register = 0 for slave)
  I2CTCTL |= I2CSSEL1;       // Source clock - SMCLK
  I2CPSC = 0x00;             // I2C clock = clock source/1  (Clock Prescaler register, 0x0 means divide by 1)
  I2CSCLH = 0x03;            // SCL high period = 5*I2C clock (Shift clock high register)
  I2CSCLL = 0x03;            // SCL low period  = 5*I2C clock (Shift clock low register)
  U0CTL |= I2CEN;            // Enable I2C  (Turns ON I2C)
  I2CIE = 0xFF;              // Enable ALL interrupts (I2C interrupt enable register)  
  //I2CIE = TXRDYIE + RXRDYIE; // Enable transmit and receive interrupts (I2C interrupt enable register)  
}

/*---------------------------------------------------------------------------*/
// ISR functions for slave mode
void I2C_IsrTX_Slave (void)
{
  I2CDRB = I2CBuffer[I2CBufferPtr++];
}

void I2C_IsrRX_Slave (void)
{
  uint_1 PP_command;
  uint_2 sizeof_solution;

  PP_command = I2CDRB;
  switch (PP_command)
  {
    case COMMAND_GET_GPS_LLH_DATA:
      I2CBufferPtr = 0;
      memcpy(I2CBuffer, &pos_llh, sizeof(pos_llh) ); // Place data in buffer     
      //debug_printf("%0x,%0x,%0x,%0x\n",I2CBuffer[5],I2CBuffer[6],I2CBuffer[7],I2CBuffer[8]);
      break;
   case COMMAND_GET_GPS_NAV_DATA:
      I2CBufferPtr = 0;
      memcpy(I2CBuffer, &solution, sizeof(solution) ); // Place data in buffer     
      //debug_printf("%0x,%0x,%0x,%0x\n",I2CBuffer[5],I2CBuffer[6],I2CBuffer[7],I2CBuffer[8]);
      break;
    case COMMAND_GET_GPS_UTC_TIME:
      I2CBufferPtr = 0;
      memcpy(I2CBuffer, &utc_time, sizeof(utc_time) ); // Place data in buffer       
      //debug_printf("%0x,%0x,%0x,%0x,%0x,%0x,%0x,%0x,%0x,%0x\n",I2CBuffer[0],I2CBuffer[1],I2CBuffer[2],I2CBuffer[3],I2CBuffer[4],I2CBuffer[5],I2CBuffer[6],I2CBuffer[7],I2CBuffer[8],I2CBuffer[9]);
      //debug_printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",I2CBuffer[0],I2CBuffer[1],I2CBuffer[2],I2CBuffer[3],I2CBuffer[4],I2CBuffer[5],I2CBuffer[6],I2CBuffer[7],I2CBuffer[8],I2CBuffer[9]);
      break;  

    default:
      break;
  }  //end of switch case
  //}  //end of while loop
}    //end of task


