/**************************************************************************************************
    Filename:       mt.c
    Revised:        $Date: 2011-04-04 
    Revision:       $Revision: 1 $

    Description: This file defines the main functionality of the interface to MT via ZNP.

    Copyright 2006-2007 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.
**************************************************************************************************/

#include "hal_cc2530znp_target_board.H"
#include "mt.h"
#include "znp_interface_spi.h"
#if HOST_MT

/* 1st byte is the length of the data field, 2nd/3rd bytes are command field. */
#define MT_RPC_FRAME_HDR_SZ                3

extern unsigned char znpBuf[100];

/* mtInit This function initializes the host MT environment. input parameters None.
output parameters None. @return None.
*/
void mtInit(void)
{
  /*    Initialize UART debug console:*/
    UCA0CTL1 |= UCSSEL_2;                     /* SMCLK*/
    UCA0BR0 = 26; UCA0BR1 = 0;                /* 4mHz smclk w/modulation for 9,600bps, table 15-5 */
    UCA0MCTL = UCBRS_0 + +UCBRF_1 + UCOS16;   /* Modulation UCBRSx=1, over sampling     */ 
    UCA0CTL1 &= ~UCSWRST;                     /* **Initialize USCI state machine***/
    IE2 |= UCA0RXIE;                          /* Enable USCI_A0 RX interrupt*/
}

/* mtRx This function translates and then transfers a UART MT packet as an RPC packet.
input parameters None. output parameters None. @return TRUE if the Rx data was processed; FALSE otherwise.
*/
uint8 mtRx(void)
{
  unsigned char *pBuf = znpBuf;
  uint8 len = halUARTRead(HAL_PORT_MT, pBuf, ZNP_BUF_LEN-1);
  unsigned char *data;
  uint8 idx = 0;
  do {
    while (len)
    {
      if (pBuf[idx++] == SOP_VALUE)
      {
        break;
      }
      len--;
    }
    if (len > MT_RPC_FRAME_HDR_SZ)
    {
      uint8 cnt = pBuf[idx] + MT_RPC_FRAME_HDR_SZ; /*cnt = number of bytes in the packet to be sent as RPC command */
                                                   /*As idx points to start of Packet (which has the length(number of bytes) of the data)*/
                                                   /*RPC frame header = 3 bytes (length byte + 2 bytes for command id)*/
      if ((calcFCS((pBuf+idx), cnt)) == pBuf[idx+cnt]) /*check if the data is correctly received */
      {
         data = (pBuf + idx);     
         int x =0;
         while(cnt)
         {
         /*Now populate the znpBuf with pBuf data received over UART.*/
          znpBuf[x] = *data++;
          x++;
          cnt--;
         }
        /*Now znpBuf has the data to be sent */
        sendMessage();
      }
    }
  } while (len);
  return TRUE;
}

/** mtTx This function translates and then transfers an RPC packet as a UART MT packet.
input parameters pBuf - Pointer to the buffer to be used. 
output parameters None. @return None.
*/
void mtTx(uint8 *pBuf)
{
  /*send start of frame*/
  uint8 tmp = SOP_VALUE;
  halUARTWrite(HAL_PORT_MT, &tmp, 1);
  
  /*tmp = number of bytes to be sent over UART interface 
  tmp  =  (*pbuf) [= number of data bytes in response from ZNP] + 3 [(header size = one length byte + 2 command bytes)]
  */
  tmp = *pBuf + MT_RPC_FRAME_HDR_SZ;
  halUARTWrite(HAL_PORT_MT, pBuf, tmp);
  
  /*send FCS*/
  tmp = calcFCS(pBuf, tmp);
  halUARTWrite(HAL_PORT_MT, &tmp, 1);
}

/* calcFCS This function calculates the FCS checksum for the MT protocol serial message to the
Z-Tool PC GUI. input parameters pBuf - Pointer to the end of a buffer to calculate the FCS, len - Length of the pBuf.
output parameters None. @return The calculated FCS.
*/
uint8 calcFCS(uint8 *pBuf, uint8 len)
{
  uint8 rtrn = 0;

  while (len--)
  {
    rtrn ^= *pBuf++;
  }

  return rtrn;
}
#endif
