]> git.sur5r.net Git - freertos/blob - Demo/PPC405_Xilinx_Virtex4_GCC/TestApp_Peripheral/src/xuartlite_intr_tapp_example.c
Add PPC405 port in V10.1 format.
[freertos] / Demo / PPC405_Xilinx_Virtex4_GCC / TestApp_Peripheral / src / xuartlite_intr_tapp_example.c
1 #define TESTAPP_GEN
2 \r
3 /* $Id: xuartlite_intr_tapp_example.c,v 1.1 2007/05/15 07:00:27 mta Exp $ */\r
4 /*****************************************************************************\r
5 *\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
19 *\r
20 *       (c) Copyright 2002-2006 Xilinx Inc.\r
21 *       All rights reserved.\r
22 *\r
23 *******************************************************************************/\r
24 /******************************************************************************/\r
25 /**\r
26 *\r
27 * @file xuartlite_intr_tapp_example.c\r
28 *\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
31 *\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
34 *\r
35 * @note\r
36 *\r
37 * None.\r
38 *\r
39 * <pre>\r
40 * MODIFICATION HISTORY:\r
41 *\r
42 * Ver   Who  Date     Changes\r
43 * ----- ---- -------- -----------------------------------------------\r
44 * 1.00b sv   06/08/06 Created for supporting Test App Interrupt examples\r
45 * </pre>\r
46 ******************************************************************************/\r
47 \r
48 /***************************** Include Files *********************************/\r
49 \r
50 #include "xparameters.h"\r
51 #include "xuartlite.h"\r
52 #include "xintc.h"\r
53 \r
54 #ifdef __MICROBLAZE__\r
55 #include "mb_interface.h"\r
56 #else\r
57 #include "xexception_l.h"\r
58 #endif\r
59 \r
60 \r
61 /************************** Constant Definitions *****************************/\r
62 \r
63 /*\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
67  */\r
68 #ifndef TESTAPP_GEN\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
72 #endif\r
73 \r
74 /*\r
75  * The following constant controls the length of the buffers to be sent\r
76  * and received with the UartLite device.\r
77  */\r
78 #define TEST_BUFFER_SIZE            100\r
79 \r
80 \r
81 /**************************** Type Definitions *******************************/\r
82 \r
83 \r
84 /***************** Macros (Inline Functions) Definitions *********************/\r
85 \r
86 \r
87 /************************** Function Prototypes ******************************/\r
88 \r
89 XStatus UartLiteIntrExample(XIntc *IntcInstancePtr,\r
90                             XUartLite *UartLiteInstancePtr,\r
91                             Xuint16 UartLiteDeviceId,\r
92                             Xuint16 UartLiteIntrId);\r
93 \r
94 \r
95 static void UartLiteSendHandler(void *CallBackRef, unsigned int EventData);\r
96 \r
97 static void UartLiteRecvHandler(void *CallBackRef, unsigned int EventData);\r
98 \r
99 \r
100 static XStatus UartLiteSetupIntrSystem(XIntc *IntcInstancePtr,\r
101                                        XUartLite *UartLiteInstancePtr,\r
102                                        Xuint16 UartLiteIntrId);\r
103 \r
104 static void UartLiteDisableIntrSystem(XIntc *IntrInstancePtr,\r
105                                       Xuint16 UartLiteIntrId);\r
106 \r
107 \r
108 /************************** Variable Definitions *****************************/\r
109 \r
110 /*\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
113  */\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
117 #endif\r
118 \r
119 \r
120 \r
121 /*\r
122  * The following variables are shared between non-interrupt processing and\r
123  * interrupt processing such that they must be global.\r
124  */\r
125 \r
126 /*\r
127  * The following buffers are used in this example to send and receive data\r
128  * with the UartLite.\r
129  */\r
130 Xuint8 SendBuffer[TEST_BUFFER_SIZE];\r
131 Xuint8 ReceiveBuffer[TEST_BUFFER_SIZE];\r
132 \r
133 /*\r
134  * The following counter is used to determine when the entire buffer has\r
135  * been sent.\r
136  */\r
137 static volatile int TotalSentCount;\r
138 \r
139 \r
140 /******************************************************************************/\r
141 /**\r
142 *\r
143 * Main function to call the UartLite interrupt example.\r
144 *\r
145 * @param    None.\r
146 *\r
147 * @return   XST_SUCCESS if successful, else XST_FAILURE.\r
148 *\r
149 * @note     None\r
150 *\r
151 *******************************************************************************/\r
152 #ifndef TESTAPP_GEN\r
153 int main(void)\r
154 {\r
155     XStatus Status;\r
156 \r
157     /*\r
158      * Run the UartLite Interrupt example , specify the Device ID that is\r
159      * generated in xparameters.h.\r
160      */\r
161     Status = UartLiteIntrExample(&IntcInstance,\r
162                                  &UartLiteInst,\r
163                                  UARTLITE_DEVICE_ID,\r
164                                  UARTLITE_IRPT_INTR);\r
165     if (Status != XST_SUCCESS)\r
166     {\r
167         return XST_FAILURE;\r
168     }\r
169 \r
170     return XST_SUCCESS;\r
171 }\r
172 #endif\r
173 \r
174 /****************************************************************************/\r
175 /**\r
176 *\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
180 *\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
184 *\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
188 *\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
196 *\r
197 * @return   XST_SUCCESS if successful, otherwise XST_FAILURE.\r
198 *\r
199 * @note\r
200 *\r
201 * This function contains an infinite loop such that if interrupts are not\r
202 * working it may never return.\r
203 *\r
204 ****************************************************************************/\r
205 XStatus UartLiteIntrExample(XIntc *IntcInstancePtr,\r
206                             XUartLite *UartLiteInstPtr,\r
207                             Xuint16 UartLiteDeviceId,\r
208                             Xuint16 UartLiteIntrId)\r
209 \r
210 {\r
211     XStatus Status;\r
212     Xuint32 Index;\r
213 \r
214     /*\r
215      * Initialize the UartLite driver so that it's ready to use.\r
216      */\r
217     Status = XUartLite_Initialize(UartLiteInstPtr, UartLiteDeviceId);\r
218     if (Status != XST_SUCCESS)\r
219     {\r
220         return XST_FAILURE;\r
221     }\r
222 \r
223     /*\r
224      * Perform a self-test to ensure that the hardware was built correctly.\r
225      */\r
226     Status = XUartLite_SelfTest(UartLiteInstPtr);\r
227     if (Status != XST_SUCCESS)\r
228     {\r
229         return XST_FAILURE;\r
230     }\r
231 \r
232     /*\r
233      * Connect the UartLite to the interrupt subsystem such that interrupts can\r
234      * occur. This function is application specific.\r
235      */\r
236     Status = UartLiteSetupIntrSystem(IntcInstancePtr,\r
237                                      UartLiteInstPtr,\r
238                                      UartLiteIntrId);\r
239     if (Status != XST_SUCCESS)\r
240     {\r
241         return XST_FAILURE;\r
242     }\r
243 \r
244     /*\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
249      */\r
250     XUartLite_SetSendHandler(UartLiteInstPtr, UartLiteSendHandler,\r
251                              UartLiteInstPtr);\r
252     XUartLite_SetRecvHandler(UartLiteInstPtr, UartLiteRecvHandler,\r
253                              UartLiteInstPtr);\r
254 \r
255     /*\r
256      * Enable the interrupt of the UartLite so that the interrupts will occur.\r
257      */\r
258     XUartLite_EnableInterrupt(UartLiteInstPtr);\r
259 \r
260     /*\r
261      * Initialize the send buffer bytes with a pattern to send.\r
262      */\r
263     for (Index = 0; Index < TEST_BUFFER_SIZE; Index++)\r
264     {\r
265         SendBuffer[Index] = Index;\r
266     }\r
267 \r
268     /*\r
269      * Send the buffer using the UartLite.\r
270      */\r
271     XUartLite_Send(UartLiteInstPtr, SendBuffer, TEST_BUFFER_SIZE);\r
272 \r
273     /*\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
276      */\r
277     while ((TotalSentCount != TEST_BUFFER_SIZE))\r
278     {\r
279     }\r
280 \r
281 \r
282     UartLiteDisableIntrSystem(IntcInstancePtr, UartLiteIntrId);\r
283 \r
284     return XST_SUCCESS;\r
285 }\r
286 \r
287 /*****************************************************************************/\r
288 /**\r
289 *\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
294 *\r
295 * This handler provides an example of how to handle data for the UartLite, but\r
296 * is application specific.\r
297 *\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
301 *           receive events.\r
302 *\r
303 * @return   None.\r
304 *\r
305 * @note     None.\r
306 *\r
307 ****************************************************************************/\r
308 static void UartLiteSendHandler(void *CallBackRef, unsigned int EventData)\r
309 {\r
310     TotalSentCount = EventData;\r
311 }\r
312 \r
313 /****************************************************************************/\r
314 /**\r
315 *\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
322 *\r
323 * This handler provides an example of how to handle data for the UartLite, but\r
324 * is application specific.\r
325 *\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
329 *           receive events.\r
330 *\r
331 * @return   None.\r
332 *\r
333 * @note     None.\r
334 *\r
335 ****************************************************************************/\r
336 static void UartLiteRecvHandler(void *CallBackRef, unsigned int EventData)\r
337 {\r
338 \r
339 }\r
340 \r
341 /****************************************************************************/\r
342 /**\r
343 *\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
349 *\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
356 *\r
357 * @return   XST_SUCCESS if successful, otherwise XST_FAILURE.\r
358 *\r
359 * @note     None.\r
360 *\r
361 ****************************************************************************/\r
362 XStatus UartLiteSetupIntrSystem(XIntc *IntcInstancePtr,\r
363                                 XUartLite *UartLiteInstPtr,\r
364                                 Xuint16 UartLiteIntrId)\r
365 {\r
366     XStatus Status;\r
367 \r
368 #ifndef TESTAPP_GEN\r
369     /*\r
370      * Initialize the interrupt controller driver so that it is ready to use.\r
371      */\r
372     Status = XIntc_Initialize(IntcInstancePtr, INTC_DEVICE_ID);\r
373     if (Status != XST_SUCCESS)\r
374     {\r
375         return XST_FAILURE;\r
376     }\r
377 #endif\r
378 \r
379     /*\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
383      */\r
384     Status = XIntc_Connect(IntcInstancePtr, UartLiteIntrId,\r
385                            (XInterruptHandler)XUartLite_InterruptHandler,\r
386                            (void *)UartLiteInstPtr);\r
387     if (Status != XST_SUCCESS)\r
388     {\r
389         return XST_FAILURE;\r
390     }\r
391 \r
392 #ifndef TESTAPP_GEN\r
393     /*\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
397      */\r
398     Status = XIntc_Start(IntcInstancePtr, XIN_REAL_MODE);\r
399     if (Status != XST_SUCCESS)\r
400     {\r
401         return XST_FAILURE;\r
402     }\r
403 #endif\r
404 \r
405     /*\r
406      * Enable the interrupt for the UartLite.\r
407      */\r
408     XIntc_Enable(IntcInstancePtr, UartLiteIntrId);\r
409 \r
410 #ifndef TESTAPP_GEN\r
411 \r
412     /*\r
413      * Initialize the PPC exception table.\r
414      */\r
415     XExc_Init();\r
416 \r
417     /*\r
418      * Register the interrupt controller handler with the exception table.\r
419      */\r
420     XExc_RegisterHandler(XEXC_ID_NON_CRITICAL_INT,\r
421                         (XExceptionHandler)XIntc_InterruptHandler,\r
422                         IntcInstancePtr);\r
423 \r
424     /*\r
425      * Enable non-critical exceptions.\r
426      */\r
427     XExc_mEnableExceptions(XEXC_NON_CRITICAL);\r
428 \r
429 \r
430 #endif /* TESTAPP_GEN */\r
431 \r
432     return XST_SUCCESS;\r
433 }\r
434 \r
435 /*****************************************************************************/\r
436 /**\r
437 *\r
438 * This function disables the interrupts that occur for the UartLite.\r
439 *\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
444 *\r
445 * @return   None.\r
446 *\r
447 * @note     None.\r
448 *\r
449 ******************************************************************************/\r
450 static void UartLiteDisableIntrSystem(XIntc *IntcInstancePtr,\r
451                                       Xuint16 UartLiteIntrId)\r
452 {\r
453 \r
454     /*\r
455      * Disconnect and disable the interrupt for the UartLite\r
456      */\r
457     XIntc_Disconnect(IntcInstancePtr, UartLiteIntrId);\r
458 \r
459 }\r
460 \r
461 \r