/******************************************************************************
*
* FILENAME:
*
* serial.c
*
* DESCRIPTION:
*
* NOTES:
*
* None.
*
* MODIFICATION HISTORY:
*
******************************************************************************/

/***************************** Include Files *********************************/
#include "xbasic_types.h"
#include "xparameters.h"
#define UART_LITE
#if defined UART_LITE
#include "xuartlite.h"
#else
#include "xuartns550.h"
#endif
#include "xstatus.h"

/************************** Constant Definitions *****************************/

#define TEST_BUFFER_SIZE 1

/***************** Macros (Inline Functions) Definitions *********************/
/*
 * This macro checks the status for functions called. It performs a return
 * if the status was not success.  The purpose of this macro is to allow
 * centralized status checking.
 *
 */
#define STATUS_CHECK(Status)            \
    {                                   \
        if (Status != XST_SUCCESS)      \
        {                               \
            return Status;              \
        }                               \
    }


#if defined UART_LITE
XUartLite UartLite;
#else
XUartNs550 UartNs550;
#endif
Xuint8 SendBuffer[TEST_BUFFER_SIZE];
Xuint8 RecvBuffer[TEST_BUFFER_SIZE];

/****************************************************************************
*
* FUNCTION:
*
* UartInit
*
* DESCRIPTION:
*
****************************************************************************/

XStatus UartInit( void )
{

#if defined UART_LITE
    XStatus Status;

    /* Initialize the UART Lite driver so that it's ready to use,
     * specify the device ID that is generated in xparameters.h
     */
    Status = XUartLite_Initialize(&UartLite, XPAR_RS232_DEVICE_ID);
    STATUS_CHECK(Status);

    /* Perform a self-test to ensure that the hardware was built
     * correctly
     */
    Status = XUartLite_SelfTest(&UartLite);
    STATUS_CHECK(Status);


    return XST_SUCCESS;

#else

    XStatus Status;
    Xuint16 Options = XUN_OPTION_FIFOS_ENABLE;


    /* Initialize the UART Lite driver so that it's ready to use,
     * specify the device ID that is generated in xparameters.h
     */
    Status = XUartNs550_Initialize(&UartNs550, XPAR_OPB_UART16550_0_DEVICE_ID);
    STATUS_CHECK(Status);

    /* Perform a self-test to ensure that the hardware was built
     * correctly
     */
    Status = XUartNs550_SelfTest(&UartNs550);
    STATUS_CHECK(Status);

    /* Enable the local loopback so data that is sent will be received,
     * and keep the FIFOs enabled
     */
    XUartNs550_SetOptions(&UartNs550, Options);


#endif
}


/****************************************************************************
*
* FUNCTION:
*
* recv_char
*
* DESCRIPTION:
*
* Blocking Call to receive a single charater from the UART.
*
****************************************************************************/
Xuint8 recv_char( void )
{
	unsigned int ReceivedCount;

	ReceivedCount = 0;
  	while(!ReceivedCount)
  	{	

#if defined UART_LITE
		ReceivedCount = XUartLite_Recv(&UartLite, RecvBuffer, 1);
#else
   		ReceivedCount = XUartNs550_Recv(&UartNs550, RecvBuffer,1);
#endif
	}

  return *RecvBuffer;

}


/****************************************************************************
*
* FUNCTION:
*
* send_char
*
* DESCRIPTION:
*
* Blocking Call to receive a single charater from the UART.
*
****************************************************************************/
void send_char(Xuint8 data)
{
	unsigned int SentCount;
	
	SendBuffer[0] = data;

#if defined UART_LITE
	SentCount = XUartLite_Send(&UartLite, SendBuffer, TEST_BUFFER_SIZE);	
	
	/*
	 Block until the byte has been sent.
   */
  while (XUartLite_IsSending(&UartLite))
  {
  }
#else
   SentCount = XUartNs550_Send(&UartNs550, SendBuffer, TEST_BUFFER_SIZE);

    while (XUartNs550_IsSending(&UartNs550))
    {
    }

#endif
	
} 
ÿ