/**************************************************************************************************
  Filename:       _hal_uart.c
  Revised:        $Date: 2013-05-17 11:25:11 -0700 (Fri, 17 May 2013) $
  Revision:       $Revision: 34355 $

  Description:    This file contains the interface to the UART.


  Copyright 2012-2013 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.
**************************************************************************************************/

/* ------------------------------------------------------------------------------------------------
 *                                          Includes
 * ------------------------------------------------------------------------------------------------
 */
#include "OSAL.h"
#include "hal_uart.h"
#include "hw_ioc.h"
#include "hal_lcd.h"
/*********************************************************************
 * LOCAL VARIABLES
 */
#define UART_RX_NO_EMPTY 0 
#define UART_RX_EMPTY    1
static bool UartRxFlag = UART_RX_EMPTY;

/* ------------------------------------------------------------------------------------------------
 *                                           Global Functions
 * ------------------------------------------------------------------------------------------------
 */

/*************************************************************************************************
 * @fn      HalUARTInit()
 *
 * @brief   Initialize the UART
 *
 * @param   none
 *
 * @return  none
 *************************************************************************************************/
void HalUARTInit(void)
{
   SysCtrlPeripheralEnable(HAL_UART_SYS_CTRL);

  /* Setup PB0 as UART_CTS, PD3 as UART_RTS  
   * PA1 as UART_TX and PA0 as UART_RX
   */ 
  IOCPinConfigPeriphOutput(GPIO_A_BASE, GPIO_PIN_1, IOC_MUX_OUT_SEL_UART1_TXD);
  IOCPinConfigPeriphInput(GPIO_A_BASE, GPIO_PIN_0, IOC_UARTRXD_UART1);
  GPIOPinTypeUARTInput(GPIO_A_BASE, GPIO_PIN_0);
  GPIOPinTypeUARTOutput(GPIO_A_BASE, GPIO_PIN_1);
  
  IntEnable(HAL_UART_INT_CTRL);
  UARTConfigSetExpClk(HAL_UART_PORT, SysCtrlClockGet(), HAL_UART_BR_38400,
                         (UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE));

  /* FIFO level set to 1/8th for both RX and TX which is 2 bytes */
  //UARTFIFOLevelSet(HAL_UART_PORT, UART_FIFO_TX1_8, UART_FIFO_RX1_8);//only allowed timeout interrupt 
  UARTFIFOEnable(HAL_UART_PORT);

  /* Clear and enable UART TX, RX, CTS and Recieve Timeout interrupt */
  UARTIntClear(HAL_UART_PORT, (UART_INT_RX | UART_INT_TX | UART_INT_CTS | UART_INT_RT ));
  UARTIntEnable(HAL_UART_PORT, (/*UART_INT_RX | UART_INT_TX | UART_INT_CTS | */ UART_INT_RT));
  UARTEnable(HAL_UART_PORT);
}

/*************************************************************************************************
 * @fn      Hal_UARTPoll
 *
 * @brief   This routine simulate polling and has to be called by the main loop
 *
 * @param   void
 *
 * @return  void
 *************************************************************************************************/
void HalUARTPoll(void)
{
  if(UartRxFlag == UART_RX_NO_EMPTY)
  {
       uint8 *uarttemp;
       uarttemp = (uint8 *)osal_msg_allocate( (sizeof(uint8)*16));
       while (UARTCharsAvail(HAL_UART_PORT))         
          *uarttemp++ = UARTCharGetNonBlocking(HAL_UART_PORT);
       
       HalLcdWriteString ( "got bytes", HAL_LCD_LINE_7);
       UartRxFlag = UART_RX_EMPTY;
       
       osal_msg_deallocate ( uarttemp );
  }
}

/*************************************************************************************************
 * @fn      UART Rx/Tx ISR
 *
 * @brief   Called when a serial byte is ready to read and/or write.
 * NOTE:   Assumes that uartRecord.configured is TRUE if this interrupt is enabled.
 *
 * @param   void
 *
 * @return  void
**************************************************************************************************/
void HalUartISR(void)
{
  UARTIntClear(HAL_UART_PORT, (UART_INT_RX |  UART_INT_RT));
    
  if(UARTCharsAvail(HAL_UART_PORT))
  {
       UartRxFlag = UART_RX_NO_EMPTY;
  }
  
  UARTIntClear(HAL_UART_PORT, (UART_INT_TX | UART_INT_CTS));
  

}

/*************************************************************************************************
 * @fn      HalUARTRead()
 *
 * @brief   Read a buffer from the UART
 *
 * @param   port - UART port (not used.)
 *          ppBuffer - pointer to a pointer that points to the data that will be read
 *          length - length of the requested buffer
 *
 * @return  length of buffer that was read
 *************************************************************************************************/
uint16 HalUARTRead ( uint8 port, uint8 *pBuffer, uint16 length )
{

}

/*************************************************************************************************
 * @fn      HalUARTWrite()
 *
 * @brief   Write a buffer to the UART
 *
 * @param   port    - UART port (not used.)
 *          pBuffer - pointer to the buffer that will be written
 *          length  - length of
 *
 * @return  length of the buffer that was sent
 *************************************************************************************************/
uint16 HalUARTWrite(uint8 port, uint8 *pBuffer, uint16 length)
{

}

/**************************************************************************************************
*/



