]> git.sur5r.net Git - freertos/blob - Demo/PPC405_Xilinx_Virtex4_GCC/TestApp_Peripheral/src/xgpio_tapp_example.c
e5f199a56800767db29c860a0811b8442e2f9217
[freertos] / Demo / PPC405_Xilinx_Virtex4_GCC / TestApp_Peripheral / src / xgpio_tapp_example.c
1 #define TESTAPP_GEN
2 \r
3 /* $Id: xgpio_tapp_example.c,v 1.1 2007/05/15 06:49:42 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 2005 Xilinx Inc.\r
21 *       All rights reserved.\r
22 *\r
23 ******************************************************************************/\r
24 /*****************************************************************************/\r
25 /**\r
26 * @file xgpio_tapp_example.c\r
27 *\r
28 * This file contains a example for using GPIO hardware and driver.\r
29 * This example assumes that there is a UART Device or STDIO Device in the\r
30 * hardware system.\r
31 *\r
32 * This example can be run on the Xilinx ML300 board with either the PowerPC or\r
33 * the MicroBlaze processor using the Prototype Pins & LEDs of the board\r
34 * connected to the GPIO and the Push Buttons connected.\r
35 *\r
36 * @note\r
37 *\r
38 * None\r
39 *\r
40 * <pre>\r
41 * MODIFICATION HISTORY:\r
42 *\r
43 * Ver   Who  Date     Changes\r
44 * ----- ---- -------- -----------------------------------------------\r
45 * 1.00a sv   04/15/05 Initial release for TestApp integration.\r
46 * </pre>\r
47 *\r
48 *****************************************************************************/\r
49 \r
50 /***************************** Include Files ********************************/\r
51 \r
52 #include "xparameters.h"\r
53 #include "xgpio.h"\r
54 #include "stdio.h"\r
55 #include "xstatus.h"\r
56 \r
57 /************************** Constant Definitions ****************************/\r
58 \r
59 /*\r
60  * The following constant is used to wait after an LED is turned on to make\r
61  * sure that it is visible to the human eye.  This constant might need to be\r
62  * tuned for faster or slower processor speeds.\r
63  */\r
64 #define LED_DELAY     1000000\r
65 \r
66 /* following constant is used to determine which channel of the GPIO is\r
67  * used if there are 2 channels supported in the GPIO.\r
68  */\r
69 #define LED_CHANNEL 1\r
70 \r
71 #define LED_MAX_BLINK   0x1     /* Number of times the LED Blinks */\r
72 \r
73 #define GPIO_BITWIDTH   16      /* This is the width of the GPIO */\r
74 \r
75 #define printf xil_printf       /* A smaller footprint printf */\r
76 \r
77 /*\r
78  * The following constants map to the XPAR parameters created in the\r
79  * xparameters.h file. They are defined here such that a user can easily\r
80  * change all the needed parameters in one place.\r
81  */\r
82 #ifndef TESTAPP_GEN\r
83 #define GPIO_OUTPUT_DEVICE_ID  XPAR_LEDS_4BIT_DEVICE_ID\r
84 #define GPIO_INPUT_DEVICE_ID   XPAR_LEDS_4BIT_DEVICE_ID\r
85 #endif /* TESTAPP_GEN */\r
86 \r
87 /**************************** Type Definitions ******************************/\r
88 \r
89 \r
90 /***************** Macros (Inline Functions) Definitions *******************/\r
91 \r
92 \r
93 /************************** Function Prototypes ****************************/\r
94 \r
95 XStatus GpioOutputExample(Xuint16 DeviceId, Xuint32 GpioWidth);\r
96 \r
97 XStatus GpioInputExample(Xuint16 DeviceId, Xuint32 *DataRead);\r
98 \r
99 void GpioDriverHandler(void *CallBackRef);\r
100 \r
101 \r
102 \r
103 /************************** Variable Definitions **************************/\r
104 \r
105 /*\r
106  * The following are declared globally so they are zeroed and so they are\r
107  * easily accessible from a debugger\r
108  */\r
109 XGpio GpioOutput;   /* The driver instance for GPIO Device configured as O/P */\r
110 XGpio GpioInput;    /* The driver instance for GPIO Device configured as I/P */\r
111 \r
112 \r
113 /*****************************************************************************/\r
114 /**\r
115 * Main function to call the example.This function is not included if the\r
116 * example is generated from the TestAppGen test tool.\r
117 *\r
118 * @param    None\r
119 *\r
120 * @return   XST_SUCCESS if successful, XST_FAILURE if unsuccessful\r
121 *\r
122 * @note     None\r
123 *\r
124 ******************************************************************************/\r
125 #ifndef TESTAPP_GEN\r
126 int main(void)\r
127 {\r
128     XStatus Status;\r
129     Xuint32 InputData;\r
130 \r
131     Status = GpioOutputExample(GPIO_OUTPUT_DEVICE_ID, GPIO_BITWIDTH);\r
132     if (Status != XST_SUCCESS)\r
133     {\r
134         return XST_FAILURE;\r
135     }\r
136 \r
137     Status = GpioInputExample(GPIO_INPUT_DEVICE_ID, &InputData);\r
138     if (Status != XST_SUCCESS)\r
139     {\r
140         return XST_FAILURE;\r
141     }\r
142 \r
143     printf("Data read from GPIO Input is  0x%x \n", (int)InputData);\r
144 \r
145     return XST_SUCCESS;\r
146 }\r
147 #endif\r
148 \r
149 \r
150 /*****************************************************************************/\r
151 /**\r
152 *\r
153 * This function does a minimal test on the GPIO device configured as OUTPUT\r
154 * and driver as a  example.\r
155 *\r
156 *\r
157 * @param    DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from\r
158 *           xparameters.h\r
159 * @param    GpioWidth is the width of the GPIO\r
160 *\r
161 * @return   XST_SUCCESS if successful, XST_FAILURE if unsuccessful\r
162 *\r
163 * @note     None\r
164 *\r
165 ****************************************************************************/\r
166 XStatus GpioOutputExample(Xuint16 DeviceId, Xuint32 GpioWidth)\r
167 {\r
168     Xuint32 Data;\r
169     volatile int Delay;\r
170     Xuint32 LedBit;\r
171     Xuint32 LedLoop;\r
172     XStatus Status;\r
173 \r
174     /*\r
175      * Initialize the GPIO driver so that it's ready to use,\r
176      * specify the device ID that is generated in xparameters.h\r
177      */\r
178     Status = XGpio_Initialize(&GpioOutput, DeviceId);\r
179     if (Status != XST_SUCCESS)\r
180     {\r
181         return XST_FAILURE;\r
182     }\r
183 \r
184 \r
185     /*\r
186      * Set the direction for all signals to be outputs\r
187      */\r
188     XGpio_SetDataDirection(&GpioOutput, LED_CHANNEL, 0x0);\r
189 \r
190     /*\r
191      * Set the GPIO outputs to low\r
192      */\r
193     XGpio_DiscreteWrite(&GpioOutput, LED_CHANNEL, 0x0);\r
194 \r
195     for (LedBit = 0x0; LedBit < GpioWidth; LedBit++)\r
196     {\r
197 \r
198         for (LedLoop = 0; LedLoop < LED_MAX_BLINK; LedLoop++)\r
199         {\r
200 \r
201             /*\r
202              * Set the GPIO Output to High\r
203              */\r
204             XGpio_DiscreteWrite(&GpioOutput, LED_CHANNEL, 1 << LedBit);\r
205 \r
206 #ifndef __SIM__\r
207             /*\r
208              * Wait a small amount of time so the LED is visible\r
209              */\r
210             for (Delay = 0; Delay < LED_DELAY; Delay++);\r
211 \r
212 #endif\r
213             /*\r
214              * Read the state of the data so that it can be  verified\r
215              */\r
216             /* Data = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL); */\r
217 \r
218 \r
219             /*\r
220              * If the data read back is not the same as the data\r
221              * written then return FAILURE\r
222              */\r
223             /*if (Data != (1 << LedBit))\r
224             {\r
225                 return XST_FAILURE;\r
226             }*/\r
227 \r
228 \r
229             /*\r
230              * Clear the GPIO Output\r
231              */\r
232             XGpio_DiscreteClear(&GpioOutput, LED_CHANNEL, 1 << LedBit);\r
233 \r
234 \r
235             /*\r
236              * Read the state of the data so that it can be verified\r
237              */\r
238             /* Data = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);*/\r
239 \r
240 \r
241             /*\r
242              * If the data read back is not the same as the data\r
243              * written then return FAILURE\r
244              */\r
245             /* if (Data & ( 1 << LedBit))\r
246             {\r
247                 return XST_FAILURE;\r
248             }*/\r
249 \r
250 \r
251 #ifndef __SIM__\r
252             /*\r
253              * Wait a small amount of time so the LED is visible\r
254              */\r
255             for (Delay = 0; Delay < LED_DELAY; Delay++);\r
256 #endif\r
257 \r
258         }\r
259 \r
260     }\r
261 \r
262     return XST_SUCCESS;\r
263 \r
264 }\r
265 \r
266 \r
267 /******************************************************************************/\r
268 /**\r
269 *\r
270 * This function  performs a test on the GPIO driver/device with the GPIO\r
271 * configured as INPUT\r
272 *\r
273 * @param    DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from\r
274 *           xparameters.h\r
275 * @param    DataRead is the pointer where the data read from GPIO Input is\r
276 *           returned\r
277 *\r
278 * @return   XST_SUCCESS if the Test is successful, otherwise XST_FAILURE\r
279 *\r
280 * @note     None.\r
281 *\r
282 ******************************************************************************/\r
283 XStatus GpioInputExample(Xuint16 DeviceId, Xuint32 *DataRead)\r
284 {\r
285     XStatus Status;\r
286 \r
287     /*\r
288      * Initialize the GPIO driver so that it's ready to use,\r
289      * specify the device ID that is generated in xparameters.h\r
290      */\r
291     Status = XGpio_Initialize(&GpioInput, DeviceId);\r
292     if (Status != XST_SUCCESS)\r
293     {\r
294         return XST_FAILURE;\r
295     }\r
296 \r
297     /*\r
298      * Set the direction for all signals to be inputs\r
299      */\r
300     XGpio_SetDataDirection(&GpioInput, LED_CHANNEL, 0xFFFFFFFF);\r
301 \r
302     /*\r
303      * Read the state of the data so that it can be  verified\r
304      */\r
305     *DataRead = XGpio_DiscreteRead(&GpioInput, LED_CHANNEL);\r
306 \r
307     return XST_SUCCESS;\r
308 \r
309 }\r
310 \r
311 \r