There was an error in this gadget

Saturday, January 7, 2012

Timer counter polled example

This is from xilinx examples

====================================================================

/***************************** Include Files *********************************/

#include "xparameters.h"
#include "xtmrctr.h"

/************************** Constant Definitions *****************************/

/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are only defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define TMRCTR_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID


/*
 * This example only uses the 1st of the 2 timer counters contained in a
 * single timer counter hardware device
 */
#define TIMER_COUNTER_0 0


/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/


/************************** Function Prototypes ******************************/

int TmrCtrPolledExample(u16 DeviceId, u8 TmrCtrNumber);

/************************** Variable Definitions *****************************/

XTmrCtr TimerCounter; /* The instance of the Tmrctr Device */


/*****************************************************************************/
/**
* Main function to call the polled example.
*
* @param None.
*
* @return XST_SUCCESS to indicate success, else XST_FAILURE to indicate
* a Failure.
*
* @note None.
*
******************************************************************************/
int main(void)
{

int Status;

/*
* Run the Timer Counter - Polled Example
*/
Status = TmrCtrPolledExample(TMRCTR_DEVICE_ID, TIMER_COUNTER_0);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;

}


/*****************************************************************************/
/**
* This function does a minimal test on the timer counter device and
* driver as a design example. The purpose of this function is to illustrate
* how to use the XTmrCtr component in a polled mode.
*
*
* @param DeviceId is the XPAR__DEVICE_ID value from
* xparameters.h
* @param TmrCtrNumber is the timer counter of the device to operate on.
* Each device may contain multiple timer counters.
* The timer number is a zero based number with a range of
* 0 - (XTC_DEVICE_TIMER_COUNT - 1).
*
* @return XST_SUCCESS to indicate success, else XST_FAILURE to indicate
* a Failure.
*
* @note
*
* This function contains a loop which waits for the value of a timer counter
* to change. If the hardware is not working correctly, this function may not
* return.
*
****************************************************************************/
int TmrCtrPolledExample(u16 DeviceId, u8 TmrCtrNumber)
{
int Status;
u32 Value1;
u32 Value2;
XTmrCtr *TmrCtrInstancePtr = &TimerCounter;

/*
* Initialize the timer counter so that it's ready to use,
* specify the device ID that is generated in xparameters.h
*/
Status = XTmrCtr_Initialize(TmrCtrInstancePtr, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Perform a self-test to ensure that the hardware was built
* correctly, use the 1st timer in the device (0)
*/
Status = XTmrCtr_SelfTest(TmrCtrInstancePtr, TmrCtrNumber);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}


/*
* Enable the Autoreload mode of the timer counters.
*/
XTmrCtr_SetOptions(TmrCtrInstancePtr, TmrCtrNumber,
XTC_AUTO_RELOAD_OPTION);

/*
* Get a snapshot of the timer counter value before it's started
* to compare against later
*/
Value1 = XTmrCtr_GetValue(TmrCtrInstancePtr, TmrCtrNumber);

/*
* Start the timer counter such that it's incrementing by default
*/
XTmrCtr_Start(TmrCtrInstancePtr, TmrCtrNumber);

/*
* Read the value of the timer counter and wait for it to change,
* since it's incrementing it should change, if the hardware is not
* working for some reason, this loop could be infinite such that the
* function does not return
*/
while (1) {
Value2 = XTmrCtr_GetValue(TmrCtrInstancePtr, TmrCtrNumber);
if (Value1 != Value2) {
break;
}
}

/*
* Disable the Autoreload mode of the timer counters.
*/
XTmrCtr_SetOptions(TmrCtrInstancePtr, TmrCtrNumber, 0);

return XST_SUCCESS;
}

============================================================================================================================================================================================================================

Timer Counter low level functions


#define XTmrCtr_WriteReg  (  BaseAddress,
  TmrCtrNumber,
  RegOffset,
  ValueToWrite    )  

   Write a specified value to a register of a timer counter.


Parameters:
 BaseAddress  is the base address of the timer counter device.
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1).
 RegOffset  contain the offset from the 1st register of the timer counter to select the specific register of the timer counter.
 ValueToWrite  is the 32 bit value to be written to the register.


===================================================================================================================================================
#define XTmrCtr_SetLoadReg  (  BaseAddress,  
  TmrCtrNumber,  
  RegisterValue    )   
 
   Set the Load Register of a timer counter to the specified value.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1).  
 RegisterValue  is the 32 bit value to be written to the register. 
 
================================================================================================================================================
#define XTmrCtr_SetControlStatusReg  (  BaseAddress,  
  TmrCtrNumber,  
  RegisterValue    )   
 
   Set the Control Status Register of a timer counter to the specified value.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1).  
 RegisterValue  is the 32 bit value to be written to the register. 
 
===================================================================================================================================================
#define XTmrCtr_ReadReg  (  BaseAddress,  
  TmrCtrNumber,  
  RegOffset    )   
 
   Read one of the timer counter registers.


Parameters:
 BaseAddress  contains the base address of the timer counter device.  
 TmrCtrNumber  contains the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1).  
 RegOffset  contains the offset from the 1st register of the timer counter to select the specific register of the timer counter. 
 

================================================================================================================================================
#define XTmrCtr_LoadTimerCounterReg  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Cause the timer counter to load it's Timer Counter Register with the value in the Load Register.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 
 
=================================================================================================================================================
#define XTmrCtr_HasEventOccurred  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Determine if a timer counter event has occurred. Events are defined to be when a capture has occurred or the counter has roller over.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 
 
================================================================================================================================================
#define XTmrCtr_GetTimerCounterReg  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Get the Timer Counter Register of a timer counter.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 

Returns:
The value read from the register, a 32 bit value. 
================================================================================================================================================
#define XTmrCtr_GetLoadReg  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Get the Load Register of a timer counter.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 

Returns:
The value read from the register, a 32 bit value. 

================================================================================================================================================
#define XTmrCtr_GetControlStatusReg  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Get the Control Status Register of a timer counter.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 

Returns:
The value read from the register, a 32 bit value. 
================================================================================================================================================
#define XTmrCtr_EnableIntr  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Enable the interrupt for a timer counter.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 
 
================================================================================================================================================
#define XTmrCtr_Enable  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Enable a timer counter such that it starts running.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 
 
================================================================================================================================================
#define XTmrCtr_DisableIntr  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Disable the interrupt for a timer counter.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 
 
================================================================================================================================================
#define XTmrCtr_Disable  (  BaseAddress,  
  TmrCtrNumber    )   
 
   Disable a timer counter such that it stops running.


Parameters:
 BaseAddress  is the base address of the device.  
 TmrCtrNumber  is the specific timer counter within the device, a zero based number, 0 - (XTC_DEVICE_TIMER_COUNT - 1). 

Returns:
None.
Note:
C-Style signature: void XTmrCtr_Disable(u32 BaseAddress, u8 TmrCtrNumber);  
===============================================================================================================================================
Control Status Register Bit Definitions 
Control Status Register bit masks Used to configure the timer counter device. 

 
#define  XTC_CSR_ENABLE_ALL_MASK 
#define  XTC_CSR_ENABLE_PWM_MASK 
#define  XTC_CSR_INT_OCCURED_MASK 
#define  XTC_CSR_ENABLE_TMR_MASK 
#define  XTC_CSR_ENABLE_INT_MASK 
#define  XTC_CSR_LOAD_MASK 
#define  XTC_CSR_AUTO_RELOAD_MASK 
#define  XTC_CSR_EXT_CAPTURE_MASK 
#define  XTC_CSR_EXT_GENERATE_MASK 
#define  XTC_CSR_DOWN_COUNT_MASK 
#define  XTC_CSR_CAPTURE_MODE_MASK 


Defines 
#define  XTC_DEVICE_TIMER_COUNT 
#define  XTmrCtr_ReadReg(BaseAddress, TmrCtrNumber, RegOffset) 
#define  XTmrCtr_WriteReg(BaseAddress, TmrCtrNumber, RegOffset, ValueToWrite) 
#define  XTmrCtr_SetControlStatusReg(BaseAddress, TmrCtrNumber, RegisterValue) 
#define  XTmrCtr_GetControlStatusReg(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_GetTimerCounterReg(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_SetLoadReg(BaseAddress, TmrCtrNumber, RegisterValue) 
#define  XTmrCtr_GetLoadReg(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_Enable(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_Disable(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_EnableIntr(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_DisableIntr(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_LoadTimerCounterReg(BaseAddress, TmrCtrNumber) 
#define  XTmrCtr_HasEventOccurred(BaseAddress, TmrCtrNumber) 

timer counter

Main Functions in timer counter 






Functions 
int  XTmrCtr_Initialize (XTmrCtr *InstancePtr, u16 DeviceId) 
void  XTmrCtr_Start (XTmrCtr *InstancePtr, u8 TmrCtrNumber) 
void  XTmrCtr_Stop (XTmrCtr *InstancePtr, u8 TmrCtrNumber) 
u32  XTmrCtr_GetValue (XTmrCtr *InstancePtr, u8 TmrCtrNumber) 
void  XTmrCtr_SetResetValue (XTmrCtr *InstancePtr, u8 TmrCtrNumber, u32 ResetValue) 
u32  XTmrCtr_GetCaptureValue (XTmrCtr *InstancePtr, u8 TmrCtrNumber) 
void  XTmrCtr_Reset (XTmrCtr *InstancePtr, u8 TmrCtrNumber) 
int  XTmrCtr_IsExpired (XTmrCtr *InstancePtr, u8 TmrCtrNumber) 

===========================================
u32 XTmrCtr_GetCaptureValue  (  XTmrCtr *  InstancePtr,
  u8  TmrCtrNumber
 )  

   Returns the timer counter value that was captured the last time the external capture input was asserted




u32 XTmrCtr_GetValue  (  XTmrCtr *  InstancePtr,
  u8  TmrCtrNumber
 )  

   Get the current value of the specified timer counter. The timer counter may be either incrementing or decrementing based upon the current mode of operation.

============================================================
int XTmrCtr_Initialize  (  XTmrCtr *  InstancePtr,  
  u16  DeviceId 
 )   
 
   Initializes a specific timer/counter instance/driver. Initialize fields of the XTmrCtr structure, then reset the timer/counter
==============================================================
 int XTmrCtr_IsExpired  (  XTmrCtr *  InstancePtr,  
  u8  TmrCtrNumber 
 )   
 
   Checks if the specified timer counter of the device has expired. In capture mode, expired is defined as a capture occurred. In compare mode, expired is defined as the timer counter rolled over/under for up/down counting.

When interrupts are enabled, the expiration causes an interrupt. This function is typically used to poll a timer counter to determine when it has expired.
 
====================================================================
void XTmrCtr_Reset  (  XTmrCtr *  InstancePtr,  
  u8  TmrCtrNumber 
 )   
 
   Resets the specified timer counter of the device. A reset causes the timer counter to set it's value to the reset value.
 
void XTmrCtr_SetResetValue  (  XTmrCtr *  InstancePtr,  
  u8  TmrCtrNumber,  
  u32  ResetValue 
 )   
 
   Set the reset value for the specified timer counter. This is the value that is loaded into the timer counter when it is reset. This value is also loaded when the timer counter is started.
====================================================================
 
void XTmrCtr_Start  (  XTmrCtr *  InstancePtr,  
  u8  TmrCtrNumber 
 )   
 
   Starts the specified timer counter of the device such that it starts running. The timer counter is reset before it is started and the reset value is loaded into the timer counter.

If interrupt mode is specified in the options, it is necessary for the caller to connect the interrupt handler of the timer/counter to the interrupt source, typically an interrupt controller, and enable the interrupt within the interrupt controller.
 ======================================================================
void XTmrCtr_Stop  (  XTmrCtr *  InstancePtr,  
  u8  TmrCtrNumber 
 )   
 
   Stops the timer counter by disabling it.

It is the callers' responsibility to disconnect the interrupt handler of the timer_counter from the interrupt source, typically an interrupt controller, and disable the interrupt within the interrupt controller.
 =======================================================================
this is an example from xilinx 
======================================================================

******************************************************************************/

/***************************** Include Files *********************************/

#include "xparameters.h"
#include "xstatus.h"
#include "xtmrctr_l.h"

/************************** Constant Definitions *****************************/

/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are only defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define TMRCTR_BASEADDR XPAR_TMRCTR_0_BASEADDR

/*
 * This example only uses the 1st of the 2 timer counters contained in a
 * single timer counter hardware device
 */
#define TIMER_COUNTER_0 0

/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/


/************************** Function Prototypes ******************************/

int TmrCtrLowLevelExample(u32 TmrCtrBaseAddress, u8 TimerCounter);

/************************** Variable Definitions *****************************/


/*****************************************************************************/
/**
* This function is the main function of the Tmrctr low level example.
*
* @param None.
*
* @return XST_SUCCESS to indicate success, else XST_FAILURE to indicate
* a Failure.
*
* @note None.
*
*
*****************************************************************************/
int main(void)
{
int Status;

/*
* Run the Timer Counter - Low Level example .
*/
Status = TmrCtrLowLevelExample(TMRCTR_BASEADDR, TIMER_COUNTER_0);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;

}

/*****************************************************************************/
/**
* This function does a minimal test on the timer counter device and
* the low level driver as a design example.  The purpose of this function is
* to illustrate how to use the XTmrCtr low level driver.
*
*
* @param TmrCtrBaseAddress is the base address of the device.
* @param TmrCtrNumber is the timer counter of the device to operate on.
* Each device may contain multiple timer counters.
* The timer number is a zero based number with a range of
* 0 - (XTC_DEVICE_TIMER_COUNT - 1).
*
* @return XST_SUCCESS to indicate success, else XST_FAILURE to indicate
* a Failure.
*
* @note
*
* This function contains a loop which waits for the value of a timer counter
* to change.  If the hardware is not working correctly, this function may not
* return.
*
****************************************************************************/
int TmrCtrLowLevelExample(u32 TmrCtrBaseAddress, u8 TmrCtrNumber)
{
u32 Value1;
u32 Value2;
u32 ControlStatus;

/*
* Clear the Control Status Register
*/
XTmrCtr_SetControlStatusReg(TmrCtrBaseAddress, TmrCtrNumber,0x0);

/*
* Set the value that is loaded into the timer counter and cause it to
* be loaded into the timer counter
*/
XTmrCtr_SetLoadReg(TmrCtrBaseAddress, TmrCtrNumber, 0xDEADBEEF);
XTmrCtr_LoadTimerCounterReg(TmrCtrBaseAddress, TmrCtrNumber);

/*
* Clear the Load Timer bit in the Control Status Register
*/
ControlStatus = XTmrCtr_GetControlStatusReg(TmrCtrBaseAddress,
TmrCtrNumber);
XTmrCtr_SetControlStatusReg(TmrCtrBaseAddress, TmrCtrNumber,
ControlStatus & (~XTC_CSR_LOAD_MASK));

/*
* Get a snapshot of the timer counter value before it's started
* to compare against later
*/
Value1 = XTmrCtr_GetTimerCounterReg(TmrCtrBaseAddress, TmrCtrNumber);

/*
* Start the timer counter such that it's incrementing by default
*/
XTmrCtr_Enable(TmrCtrBaseAddress, TmrCtrNumber);

/*
* Read the value of the timer counter and wait for it to change,
* since it's incrementing it should change, if the hardware is not
* working for some reason, this loop could be infinite such that the
* function does not return
*/
while (1) {
Value2 = XTmrCtr_GetTimerCounterReg(TmrCtrBaseAddress, TmrCtrNumber);

if (Value1 != Value2) {
break;
}
}

/*
* Disable the timer counter such that it stops incrementing
*/
XTmrCtr_Disable(TmrCtrBaseAddress, TmrCtrNumber);

return XST_SUCCESS;
}







Friday, January 6, 2012

urt interrupt

this is from a xilinx example but i had to do some modifications and i added an interrupt controller
and made a connection for the interrupt pin for the RS232 interfaces and also i had to these interrupts to
the interrupt controller interrupts port.

=============================

/***************************** Include Files *********************************/

#include "xparameters.h"
#include "xuartlite.h"
#include "xintc.h"
#include "xil_exception.h"

/************************** Constant Definitions *****************************/

/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define UARTLITE_DEVICE_ID      XPAR_UARTLITE_0_DEVICE_ID
#define INTC_DEVICE_ID          XPAR_INTC_0_DEVICE_ID
#define UARTLITE_INT_IRQ_ID     XPAR_INTC_0_UARTLITE_1_VEC_ID

/*
 * The following constant controls the length of the buffers to be sent
 * and received with the UartLite device.
 */
#define TEST_BUFFER_SIZE        100


/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/


/************************** Function Prototypes ******************************/

int UartLiteIntrExample(u16 DeviceId);

int SetupInterruptSystem(XUartLite *UartLitePtr);

void SendHandler(void *CallBackRef, unsigned int EventData);

void RecvHandler(void *CallBackRef, unsigned int EventData);

/************************** Variable Definitions *****************************/

 XUartLite UartLite;            /* The instance of the UartLite Device */

 XIntc InterruptController;     /* The instance of the Interrupt Controller */

/*
 * The following variables are shared between non-interrupt processing and
 * interrupt processing such that they must be global.
 */

/*
 * The following buffers are used in this example to send and receive data
 * with the UartLite.
 */
u8 SendBuffer[TEST_BUFFER_SIZE];
u8 ReceiveBuffer[TEST_BUFFER_SIZE];

/*
 * The following counters are used to determine when the entire buffer has
 * been sent and received.
 */
static volatile int TotalReceivedCount;
static volatile int TotalSentCount;


/******************************************************************************/
/**
*
* Main function to call the UartLite interrupt example.
*
* @param None
*
* @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful
*
* @note None
*
*******************************************************************************/
int main(void)
{
int Status;

/*
* Run the UartLite Interrupt example, specify the Device ID that is
* generated in xparameters.h.
*/
Status = UartLiteIntrExample(UARTLITE_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;
}

/****************************************************************************/
/**
*
* This function does a minimal test on the UartLite device and driver as a
* design example. The purpose of this function is to illustrate
* how to use the XUartLite component.
*
* This function sends data and expects to receive the same data through the
* UartLite. The user must provide a physical loopback such that data which is
* transmitted will be received.
*
* This function uses interrupt driver mode of the UartLite device. The calls
* to the UartLite driver in the handlers should only use the non-blocking
* calls.
*
* @param DeviceId is the Device ID of the UartLite Device and is the
* XPAR__DEVICE_ID value from xparameters.h.
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if interrupts are not
* working it may never return.
*
****************************************************************************/
int UartLiteIntrExample(u16 DeviceId)
{
int Status;
int Index;

/*
* Initialize the UartLite driver so that it's ready to use.
*/
Status = XUartLite_Initialize(&UartLite, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XUartLite_SelfTest(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Connect the UartLite to the interrupt subsystem such that interrupts can
* occur. This function is application specific.
*/
Status = SetupInterruptSystem(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Setup the handlers for the UartLite that will be called from the
* interrupt context when data has been sent and received, specify a
* pointer to the UartLite driver instance as the callback reference so
* that the handlers are able to access the instance data.
*/
XUartLite_SetSendHandler(&UartLite, SendHandler, &UartLite);
XUartLite_SetRecvHandler(&UartLite, RecvHandler, &UartLite);

/*
* Enable the interrupt of the UartLite so that interrupts will occur.
*/
XUartLite_EnableInterrupt(&UartLite);

/*
* Initialize the send buffer bytes with a pattern to send and the
* the receive buffer bytes to zero to allow the receive data to be
* verified.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
SendBuffer[Index] = Index;
ReceiveBuffer[Index] = 0;
}

/*
* Start receiving data before sending it since there is a loopback.
*/
XUartLite_Recv(&UartLite, ReceiveBuffer, TEST_BUFFER_SIZE);

/*
* Send the buffer using the UartLite.
*/
XUartLite_Send(&UartLite, SendBuffer, TEST_BUFFER_SIZE);

/*
* Wait for the entire buffer to be received, letting the interrupt
* processing work in the background, this function may get locked
* up in this loop if the interrupts are not working correctly.
*/
while ((TotalReceivedCount != TEST_BUFFER_SIZE) ||
(TotalSentCount != TEST_BUFFER_SIZE)) {
}

/*
* Verify the entire receive buffer was successfully received.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
if (ReceiveBuffer[Index] != SendBuffer[Index]) {
return XST_FAILURE;
}
}

return XST_SUCCESS;
}

/*****************************************************************************/
/**
*
* This function is the handler which performs processing to send data to the
* UartLite. It is called from an interrupt context such that the amount of
* processing performed should be minimized. It is called when the transmit
* FIFO of the UartLite is empty and more data can be sent through the UartLite.
*
* This handler provides an example of how to handle data for the UartLite,
* but is application specific.
*
* @param CallBackRef contains a callback reference from the driver.
* In this case it is the instance pointer for the UartLite driver.
* @param EventData contains the number of bytes sent or received for sent
* and receive events.
*
* @return None.
*
* @note None.
*
****************************************************************************/
void SendHandler(void *CallBackRef, unsigned int EventData)
{
TotalSentCount = EventData;
}

/****************************************************************************/
/**
*
* This function is the handler which performs processing to receive data from
* the UartLite. It is called from an interrupt context such that the amount of
* processing performed should be minimized.  It is called data is present in
* the receive FIFO of the UartLite such that the data can be retrieved from
* the UartLite. The size of the data present in the FIFO is not known when
* this function is called.
*
* This handler provides an example of how to handle data for the UartLite,
* but is application specific.
*
* @param CallBackRef contains a callback reference from the driver, in
* this case it is the instance pointer for the UartLite driver.
* @param EventData contains the number of bytes sent or received for sent
* and receive events.
*
* @return None.
*
* @note None.
*
****************************************************************************/
void RecvHandler(void *CallBackRef, unsigned int EventData)
{
TotalReceivedCount = EventData;
}

/****************************************************************************/
/**
*
* This function setups the interrupt system such that interrupts can occur
* for the UartLite device. This function is application specific since the
* actual system may or may not have an interrupt controller. The UartLite
* could be directly connected to a processor without an interrupt controller.
* The user should modify this function to fit the application.
*
* @param    UartLitePtr contains a pointer to the instance of the UartLite
*           component which is going to be connected to the interrupt
*           controller.
*
* @return   XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note     None.
*
****************************************************************************/
int SetupInterruptSystem(XUartLite *UartLitePtr)
{

int Status;


/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}


/*
* Connect a device driver handler that will be called when an interrupt
* for the device occurs, the device driver handler performs the
* specific interrupt processing for the device.
*/
Status = XIntc_Connect(&InterruptController, UARTLITE_INT_IRQ_ID,
  (XInterruptHandler)XUartLite_InterruptHandler,
  (void *)UartLitePtr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Start the interrupt controller such that interrupts are enabled for
* all devices that cause interrupts, specific real mode so that
* the UartLite can cause interrupts through the interrupt controller.
*/
Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Enable the interrupt for the UartLite device.
*/
XIntc_Enable(&InterruptController, UARTLITE_INT_IRQ_ID);

/*
* Initialize the exception table.
*/
Xil_ExceptionInit();

/*
* Register the interrupt controller handler with the exception table.
*/
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XIntc_InterruptHandler,
&InterruptController);

/*
* Enable exceptions.
*/
Xil_ExceptionEnable();

return XST_SUCCESS;
}

urt selftest

this is also from xilinx examples

=====================================================


/***************************** Include Files *********************************/

#include "xparameters.h"
#include "xuartlite.h"

/************************** Constant Definitions *****************************/

/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define UARTLITE_DEVICE_ID XPAR_UARTLITE_0_DEVICE_ID


/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/


/************************** Function Prototypes ******************************/

int UartLiteSelfTestExample(u16 DeviceId);

/************************** Variable Definitions *****************************/

XUartLite UartLite; /* Instance of the UartLite device */

/*****************************************************************************/
/**
*
* Main function to call the example. This function is not included if the
* example is generated from the TestAppGen test tool.
*
* @param None.
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note None.
*
******************************************************************************/
#ifndef TESTAPP_GEN
int main(void)
{
int Status;

/*
* Run the UartLite self test example, specify the Device ID that is
* generated in xparameters.h
*/
Status = UartLiteSelfTestExample(UARTLITE_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;

}
#endif

/*****************************************************************************/
/**
*
* This function does a minimal test on the UartLite device and driver as a
* design example. The purpose of this function is to illustrate
* how to use the XUartLite component.
*
*
* @param DeviceId is the XPAR__DEVICE_ID value from
* xparameters.h.
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note None.
*
****************************************************************************/
int UartLiteSelfTestExample(u16 DeviceId)
{
int Status;

/*
* Initialize the UartLite driver so that it is ready to use.
*/
Status = XUartLite_Initialize(&UartLite, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XUartLite_SelfTest(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;
}

urt polled example send packet and then receive packet then verify


This is also one of xilinx examples


/***************************** Include Files *********************************/

#include "xparameters.h"
#include "xstatus.h"
#include "xuartlite.h"

/************************** Constant Definitions *****************************/

/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define UARTLITE_DEVICE_ID XPAR_UARTLITE_0_DEVICE_ID

/*
 * The following constant controls the length of the buffers to be sent
 * and received with the UartLite, this constant must be 16 bytes or less since
 * this is a single threaded non-interrupt driven example such that the
 * entire buffer will fit into the transmit and receive FIFOs of the UartLite.
 */
#define TEST_BUFFER_SIZE 16

/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/


/************************** Function Prototypes ******************************/

int UartLitePolledExample(u16 DeviceId);

/************************** Variable Definitions *****************************/

XUartLite UartLite; /* Instance of the UartLite Device */

/*
 * The following buffers are used in this example to send and receive data
 * with the UartLite.
 */
u8 SendBuffer[TEST_BUFFER_SIZE]; /* Buffer for Transmitting Data */
u8 RecvBuffer[TEST_BUFFER_SIZE]; /* Buffer for Receiving Data */

/*****************************************************************************/
/**
*
* Main function to call the Uartlite polled example.
*
* @param None.
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note None.
*
******************************************************************************/
int main(void)
{
int Status;

/*
* Run the UartLite polled example, specify the Device ID that is
* generated in xparameters.h
*/
Status = UartLitePolledExample(UARTLITE_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;

}


/****************************************************************************/
/**
* This function does a minimal test on the UartLite device and driver as a
* design example. The purpose of this function is to illustrate
* how to use the XUartLite component.
*
* This function sends data and expects to receive the data thru the UartLite
* such that a  physical loopback must be done with the transmit and receive
* signals of the UartLite.
*
* This function polls the UartLite and does not require the use of interrupts.
*
* @param DeviceId is the Device ID of the UartLite and is the
* XPAR__DEVICE_ID value from xparameters.h.
*
* @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful.
*
*
* @note
*
* This function calls the UartLite driver functions in a blocking mode such that
* if the transmit data does not loopback to the receive, this function may
* not return.
*
****************************************************************************/
int UartLitePolledExample(u16 DeviceId)
{
int Status;
unsigned int SentCount;
unsigned int ReceivedCount = 0;
int Index;

/*
* Initialize the UartLite driver so that it is ready to use.
*/
Status = XUartLite_Initialize(&UartLite, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XUartLite_SelfTest(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* Initialize the send buffer bytes with a pattern to send and the
* the receive buffer bytes to zero.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
SendBuffer[Index] = Index;
RecvBuffer[Index] = 0;
}

/*
* Send the buffer through the UartLite waiting til the data can be sent
* (block), if the specified number of bytes was not sent successfully,
* then an error occurred.
*/
SentCount = XUartLite_Send(&UartLite, SendBuffer, TEST_BUFFER_SIZE);
if (SentCount != TEST_BUFFER_SIZE) {
return XST_FAILURE;
}

/*
* Receive the number of bytes which is transfered.
* Data may be received in fifo with some delay hence we continuously
* check the receive fifo for valid data and update the receive buffer
* accordingly.
*/
while (1) {
ReceivedCount += XUartLite_Recv(&UartLite,
  RecvBuffer + ReceivedCount,
  TEST_BUFFER_SIZE - ReceivedCount);
if (ReceivedCount == TEST_BUFFER_SIZE) {
break;
}
}

/*
* Check the receive buffer data against the send buffer and verify the
* data was correctly received.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
if (SendBuffer[Index] != RecvBuffer[Index]) {
return XST_FAILURE;
}
}

return XST_SUCCESS;
}

urt send buffer example

this example from xilinx and i tried it and it is working



/***************************** Include Files *********************************/

#include "xparameters.h"
#include "xstatus.h"
#include "xuartlite_l.h"

/************************** Constant Definitions *****************************/


/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define UARTLITE_BASEADDR   XPAR_UARTLITE_0_BASEADDR

/*
 * The following constant controls the length of the buffers to be sent
 * and received with the UartLite, this constant must be 16 bytes or less so the
 * entire buffer will fit into the transmit and receive FIFOs of the UartLite.
 */
#define TEST_BUFFER_SIZE 16


/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/


/************************** Function Prototypes ******************************/

int UartLiteLowLevelExample(u32 UartliteBaseAddress);

/************************** Variable Definitions *****************************/

/*
 * The following buffers are used in this example to send and receive data
 * with the UartLite.
 */
u8 SendBuffer[TEST_BUFFER_SIZE]; /* Buffer for Transmitting Data */
u8 RecvBuffer[TEST_BUFFER_SIZE]; /* Buffer for Receiving Data */


/*****************************************************************************/
/**
*
* Main function to call the example.
*
* @param None.
*
* @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful.
*
* @note None.
*
******************************************************************************/
int main(void)
{
int Status;

/*
* Run the UartLite Low level example, specify the BaseAddress that is
* generated in xparameters.h.
*/
Status = UartLiteLowLevelExample(UARTLITE_BASEADDR);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

return XST_SUCCESS;

}


/*****************************************************************************/
/**
*
* This function does a minimal test on the UartLite device using the low-level
* driver macros and functions. This function sends data and expects to receive
* the data through the UartLite. A physical loopback must be done by the user
* with the transmit and receive signals of the UartLite.
*
* @param UartliteBaseAddress is the base address of the UartLite device
* and is the XPAR__BASEADDR value from
* xparameters.h.
*
* @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful.
*
* @note None.
*
******************************************************************************/
int UartLiteLowLevelExample(u32 UartliteBaseAddress)
{
int Index;

/*
* Initialize the send buffer bytes with a pattern to send and the
* the receive buffer bytes to zero.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
SendBuffer[Index] = Index + 'B';
RecvBuffer[Index] = 0;
}


/*
* Send the entire transmit buffer.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
XUartLite_SendByte(UartliteBaseAddress, SendBuffer[Index]);
}

/*
* Receive the entire buffer's worth. Note that the RecvByte function
* blocks waiting for a character.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
RecvBuffer[Index] = XUartLite_RecvByte(UartliteBaseAddress);
}

/*
* Check the receive buffer data against the send buffer and verify the
* data was correctly received.
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
if (SendBuffer[Index] != RecvBuffer[Index]) {
return XST_FAILURE;
}
}

return XST_SUCCESS;



}