3 /* $Id: xuartlite_intr_tapp_example.c,v 1.1 2007/05/15 07:00:27 mta Exp $ */
\r
4 /*****************************************************************************
\r
6 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
\r
7 * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
\r
8 * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
\r
9 * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
\r
10 * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
\r
11 * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
\r
12 * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
\r
13 * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
\r
14 * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
\r
15 * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
\r
16 * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
\r
17 * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
\r
18 * FOR A PARTICULAR PURPOSE.
\r
20 * (c) Copyright 2002-2006 Xilinx Inc.
\r
21 * All rights reserved.
\r
23 *******************************************************************************/
\r
24 /******************************************************************************/
\r
27 * @file xuartlite_intr_tapp_example.c
\r
29 * This file contains a design example using the UartLite driver and
\r
30 * hardware device using the interrupt mode for transmission of data.
\r
32 * This example works with a PPC processor. Refer the examples of Interrupt
\r
33 * controller for an example of using interrupts with the MicroBlaze processor.
\r
40 * MODIFICATION HISTORY:
\r
42 * Ver Who Date Changes
\r
43 * ----- ---- -------- -----------------------------------------------
\r
44 * 1.00b sv 06/08/06 Created for supporting Test App Interrupt examples
\r
46 ******************************************************************************/
\r
48 /***************************** Include Files *********************************/
\r
50 #include "xparameters.h"
\r
51 #include "xuartlite.h"
\r
54 #ifdef __MICROBLAZE__
\r
55 #include "mb_interface.h"
\r
57 #include "xexception_l.h"
\r
61 /************************** Constant Definitions *****************************/
\r
64 * The following constants map to the XPAR parameters created in the
\r
65 * xparameters.h file. They are defined here such that a user can easily
\r
66 * change all the needed parameters in one place.
\r
69 #define UARTLITE_DEVICE_ID XPAR_RS232_UART_DEVICE_ID
\r
70 #define INTC_DEVICE_ID XPAR_OPB_INTC_0_DEVICE_ID
\r
71 #define UARTLITE_IRPT_INTR XPAR_OPB_INTC_0_RS232_UART_INTERRUPT_INTR
\r
75 * The following constant controls the length of the buffers to be sent
\r
76 * and received with the UartLite device.
\r
78 #define TEST_BUFFER_SIZE 100
\r
81 /**************************** Type Definitions *******************************/
\r
84 /***************** Macros (Inline Functions) Definitions *********************/
\r
87 /************************** Function Prototypes ******************************/
\r
89 XStatus UartLiteIntrExample(XIntc *IntcInstancePtr,
\r
90 XUartLite *UartLiteInstancePtr,
\r
91 Xuint16 UartLiteDeviceId,
\r
92 Xuint16 UartLiteIntrId);
\r
95 static void UartLiteSendHandler(void *CallBackRef, unsigned int EventData);
\r
97 static void UartLiteRecvHandler(void *CallBackRef, unsigned int EventData);
\r
100 static XStatus UartLiteSetupIntrSystem(XIntc *IntcInstancePtr,
\r
101 XUartLite *UartLiteInstancePtr,
\r
102 Xuint16 UartLiteIntrId);
\r
104 static void UartLiteDisableIntrSystem(XIntc *IntrInstancePtr,
\r
105 Xuint16 UartLiteIntrId);
\r
108 /************************** Variable Definitions *****************************/
\r
111 * The instances to support the device drivers are global such that the
\r
112 * are initialized to zero each time the program runs.
\r
114 #ifndef TESTAPP_GEN
\r
115 static XIntc IntcInstance; /* The instance of the Interrupt Controller */
\r
116 static XUartLite UartLiteInst; /* The instance of the UartLite Device */
\r
122 * The following variables are shared between non-interrupt processing and
\r
123 * interrupt processing such that they must be global.
\r
127 * The following buffers are used in this example to send and receive data
\r
128 * with the UartLite.
\r
130 Xuint8 SendBuffer[TEST_BUFFER_SIZE];
\r
131 Xuint8 ReceiveBuffer[TEST_BUFFER_SIZE];
\r
134 * The following counter is used to determine when the entire buffer has
\r
137 static volatile int TotalSentCount;
\r
140 /******************************************************************************/
\r
143 * Main function to call the UartLite interrupt example.
\r
147 * @return XST_SUCCESS if successful, else XST_FAILURE.
\r
151 *******************************************************************************/
\r
152 #ifndef TESTAPP_GEN
\r
158 * Run the UartLite Interrupt example , specify the Device ID that is
\r
159 * generated in xparameters.h.
\r
161 Status = UartLiteIntrExample(&IntcInstance,
\r
163 UARTLITE_DEVICE_ID,
\r
164 UARTLITE_IRPT_INTR);
\r
165 if (Status != XST_SUCCESS)
\r
167 return XST_FAILURE;
\r
170 return XST_SUCCESS;
\r
174 /****************************************************************************/
\r
177 * This function does a minimal test on the UartLite device and driver as a
\r
178 * design example. The purpose of this function is to illustrate how to use
\r
179 * the XUartLite component.
\r
181 * This function sends data and expects to receive the same data through the
\r
182 * UartLite. The user must provide a physical loopback such that data which
\r
183 * is transmitted will be received.
\r
185 * This function uses the interrupt driver mode of the UartLite. The calls to
\r
186 * the UartLite driver in the interrupt handlers, should only use the
\r
187 * non-blocking calls.
\r
189 * @param IntcInstancePtr is a pointer to the instance of the INTC component.
\r
190 * @param UartLiteInstPtr is a pointer to the instance of UartLite component.
\r
191 * @param UartLiteDeviceId is the Device ID of the UartLite Device and is the
\r
192 * XPAR_<UARTLITE_instance>_DEVICE_ID value from xparameters.h.
\r
193 * @param UartLiteIntrId is the Interrupt ID and is typically
\r
194 * XPAR_<INTC_instance>_<UARTLITE_instance>_IP2INTC_IRPT_INTR
\r
195 * value from xparameters.h.
\r
197 * @return XST_SUCCESS if successful, otherwise XST_FAILURE.
\r
201 * This function contains an infinite loop such that if interrupts are not
\r
202 * working it may never return.
\r
204 ****************************************************************************/
\r
205 XStatus UartLiteIntrExample(XIntc *IntcInstancePtr,
\r
206 XUartLite *UartLiteInstPtr,
\r
207 Xuint16 UartLiteDeviceId,
\r
208 Xuint16 UartLiteIntrId)
\r
215 * Initialize the UartLite driver so that it's ready to use.
\r
217 Status = XUartLite_Initialize(UartLiteInstPtr, UartLiteDeviceId);
\r
218 if (Status != XST_SUCCESS)
\r
220 return XST_FAILURE;
\r
224 * Perform a self-test to ensure that the hardware was built correctly.
\r
226 Status = XUartLite_SelfTest(UartLiteInstPtr);
\r
227 if (Status != XST_SUCCESS)
\r
229 return XST_FAILURE;
\r
233 * Connect the UartLite to the interrupt subsystem such that interrupts can
\r
234 * occur. This function is application specific.
\r
236 Status = UartLiteSetupIntrSystem(IntcInstancePtr,
\r
239 if (Status != XST_SUCCESS)
\r
241 return XST_FAILURE;
\r
245 * Setup the handlers for the UartLite that will be called from the
\r
246 * interrupt context when data has been sent and received,
\r
247 * specify a pointer to the UartLite driver instance as the callback
\r
248 * reference so the handlers are able to access the instance data.
\r
250 XUartLite_SetSendHandler(UartLiteInstPtr, UartLiteSendHandler,
\r
252 XUartLite_SetRecvHandler(UartLiteInstPtr, UartLiteRecvHandler,
\r
256 * Enable the interrupt of the UartLite so that the interrupts will occur.
\r
258 XUartLite_EnableInterrupt(UartLiteInstPtr);
\r
261 * Initialize the send buffer bytes with a pattern to send.
\r
263 for (Index = 0; Index < TEST_BUFFER_SIZE; Index++)
\r
265 SendBuffer[Index] = Index;
\r
269 * Send the buffer using the UartLite.
\r
271 XUartLite_Send(UartLiteInstPtr, SendBuffer, TEST_BUFFER_SIZE);
\r
274 * Wait for the entire buffer to be transmitted, the function may get
\r
275 * locked up in this loop if the interrupts are not working correctly.
\r
277 while ((TotalSentCount != TEST_BUFFER_SIZE))
\r
282 UartLiteDisableIntrSystem(IntcInstancePtr, UartLiteIntrId);
\r
284 return XST_SUCCESS;
\r
287 /*****************************************************************************/
\r
290 * This function is the handler which performs processing to send data to the
\r
291 * UartLite. It is called from an interrupt context such that the amount of
\r
292 * processing performed should be minimized. It is called when the transmit
\r
293 * FIFO of the UartLite is empty and more data can be sent through the UartLite.
\r
295 * This handler provides an example of how to handle data for the UartLite, but
\r
296 * is application specific.
\r
298 * @param CallBackRef contains a callback reference from the driver.
\r
299 * In this case it is the instance pointer for the UartLite driver.
\r
300 * @param EventData contains the number of bytes sent or received for sent and
\r
307 ****************************************************************************/
\r
308 static void UartLiteSendHandler(void *CallBackRef, unsigned int EventData)
\r
310 TotalSentCount = EventData;
\r
313 /****************************************************************************/
\r
316 * This function is the handler which performs processing to receive data from
\r
317 * the UartLite. It is called from an interrupt context such that the amount of
\r
318 * processing performed should be minimized. It is called when any data is
\r
319 * present in the receive FIFO of the UartLite such that the data can be
\r
320 * retrieved from the UartLite. The amount of data present in the FIFO is not
\r
321 * known when this function is called.
\r
323 * This handler provides an example of how to handle data for the UartLite, but
\r
324 * is application specific.
\r
326 * @param CallBackRef contains a callback reference from the driver, in this
\r
327 * case it is the instance pointer for the UartLite driver.
\r
328 * @param EventData contains the number of bytes sent or received for sent and
\r
335 ****************************************************************************/
\r
336 static void UartLiteRecvHandler(void *CallBackRef, unsigned int EventData)
\r
341 /****************************************************************************/
\r
344 * This function setups the interrupt system such that interrupts can occur
\r
345 * for the UartLite. This function is application specific since the actual
\r
346 * system may or may not have an interrupt controller. The UartLite could be
\r
347 * directly connected to a processor without an interrupt controller. The
\r
348 * user should modify this function to fit the application.
\r
350 * @param IntcInstancePtr is a pointer to the instance of the INTC component.
\r
351 * @param UartLiteInstPtr is a pointer to the instance of UartLite component.
\r
352 * XPAR_<UARTLITE_instance>_DEVICE_ID value from xparameters.h.
\r
353 * @param UartLiteIntrId is the Interrupt ID and is typically
\r
354 * XPAR_<INTC_instance>_<UARTLITE_instance>_IP2INTC_IRPT_INTR
\r
355 * value from xparameters.h.
\r
357 * @return XST_SUCCESS if successful, otherwise XST_FAILURE.
\r
361 ****************************************************************************/
\r
362 XStatus UartLiteSetupIntrSystem(XIntc *IntcInstancePtr,
\r
363 XUartLite *UartLiteInstPtr,
\r
364 Xuint16 UartLiteIntrId)
\r
368 #ifndef TESTAPP_GEN
\r
370 * Initialize the interrupt controller driver so that it is ready to use.
\r
372 Status = XIntc_Initialize(IntcInstancePtr, INTC_DEVICE_ID);
\r
373 if (Status != XST_SUCCESS)
\r
375 return XST_FAILURE;
\r
380 * Connect a device driver handler that will be called when an interrupt
\r
381 * for the device occurs, the device driver handler performs the specific
\r
382 * interrupt processing for the device.
\r
384 Status = XIntc_Connect(IntcInstancePtr, UartLiteIntrId,
\r
385 (XInterruptHandler)XUartLite_InterruptHandler,
\r
386 (void *)UartLiteInstPtr);
\r
387 if (Status != XST_SUCCESS)
\r
389 return XST_FAILURE;
\r
392 #ifndef TESTAPP_GEN
\r
394 * Start the interrupt controller such that interrupts are enabled for
\r
395 * all devices that cause interrupts, specific real mode so that
\r
396 * the UART can cause interrupts thru the interrupt controller.
\r
398 Status = XIntc_Start(IntcInstancePtr, XIN_REAL_MODE);
\r
399 if (Status != XST_SUCCESS)
\r
401 return XST_FAILURE;
\r
406 * Enable the interrupt for the UartLite.
\r
408 XIntc_Enable(IntcInstancePtr, UartLiteIntrId);
\r
410 #ifndef TESTAPP_GEN
\r
413 * Initialize the PPC exception table.
\r
418 * Register the interrupt controller handler with the exception table.
\r
420 XExc_RegisterHandler(XEXC_ID_NON_CRITICAL_INT,
\r
421 (XExceptionHandler)XIntc_InterruptHandler,
\r
425 * Enable non-critical exceptions.
\r
427 XExc_mEnableExceptions(XEXC_NON_CRITICAL);
\r
430 #endif /* TESTAPP_GEN */
\r
432 return XST_SUCCESS;
\r
435 /*****************************************************************************/
\r
438 * This function disables the interrupts that occur for the UartLite.
\r
440 * @param IntcInstancePtr is a pointer to the instance of the INTC component.
\r
441 * @param UartLiteIntrId is the Interrupt ID and is typically
\r
442 * XPAR_<INTC_instance>_<UARTLITE_instance>_IP2INTC_IRPT_INTR
\r
443 * value from xparameters.h.
\r
449 ******************************************************************************/
\r
450 static void UartLiteDisableIntrSystem(XIntc *IntcInstancePtr,
\r
451 Xuint16 UartLiteIntrId)
\r
455 * Disconnect and disable the interrupt for the UartLite
\r
457 XIntc_Disconnect(IntcInstancePtr, UartLiteIntrId);
\r