3 /* $Id: xgpio_tapp_example.c,v 1.1 2007/05/15 06:49:42 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 2005 Xilinx Inc.
\r
21 * All rights reserved.
\r
23 ******************************************************************************/
\r
24 /*****************************************************************************/
\r
26 * @file xgpio_tapp_example.c
\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
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
41 * MODIFICATION HISTORY:
\r
43 * Ver Who Date Changes
\r
44 * ----- ---- -------- -----------------------------------------------
\r
45 * 1.00a sv 04/15/05 Initial release for TestApp integration.
\r
48 *****************************************************************************/
\r
50 /***************************** Include Files ********************************/
\r
52 #include "xparameters.h"
\r
55 #include "xstatus.h"
\r
57 /************************** Constant Definitions ****************************/
\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
64 #define LED_DELAY 1000000
\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
69 #define LED_CHANNEL 1
\r
71 #define LED_MAX_BLINK 0x1 /* Number of times the LED Blinks */
\r
73 #define GPIO_BITWIDTH 16 /* This is the width of the GPIO */
\r
75 #define printf xil_printf /* A smaller footprint printf */
\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
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
87 /**************************** Type Definitions ******************************/
\r
90 /***************** Macros (Inline Functions) Definitions *******************/
\r
93 /************************** Function Prototypes ****************************/
\r
95 XStatus GpioOutputExample(Xuint16 DeviceId, Xuint32 GpioWidth);
\r
97 XStatus GpioInputExample(Xuint16 DeviceId, Xuint32 *DataRead);
\r
99 void GpioDriverHandler(void *CallBackRef);
\r
103 /************************** Variable Definitions **************************/
\r
106 * The following are declared globally so they are zeroed and so they are
\r
107 * easily accessible from a debugger
\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
113 /*****************************************************************************/
\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
120 * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful
\r
124 ******************************************************************************/
\r
125 #ifndef TESTAPP_GEN
\r
131 Status = GpioOutputExample(GPIO_OUTPUT_DEVICE_ID, GPIO_BITWIDTH);
\r
132 if (Status != XST_SUCCESS)
\r
134 return XST_FAILURE;
\r
137 Status = GpioInputExample(GPIO_INPUT_DEVICE_ID, &InputData);
\r
138 if (Status != XST_SUCCESS)
\r
140 return XST_FAILURE;
\r
143 printf("Data read from GPIO Input is 0x%x \n", (int)InputData);
\r
145 return XST_SUCCESS;
\r
150 /*****************************************************************************/
\r
153 * This function does a minimal test on the GPIO device configured as OUTPUT
\r
154 * and driver as a example.
\r
157 * @param DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from
\r
159 * @param GpioWidth is the width of the GPIO
\r
161 * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful
\r
165 ****************************************************************************/
\r
166 XStatus GpioOutputExample(Xuint16 DeviceId, Xuint32 GpioWidth)
\r
169 volatile int Delay;
\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
178 Status = XGpio_Initialize(&GpioOutput, DeviceId);
\r
179 if (Status != XST_SUCCESS)
\r
181 return XST_FAILURE;
\r
186 * Set the direction for all signals to be outputs
\r
188 XGpio_SetDataDirection(&GpioOutput, LED_CHANNEL, 0x0);
\r
191 * Set the GPIO outputs to low
\r
193 XGpio_DiscreteWrite(&GpioOutput, LED_CHANNEL, 0x0);
\r
195 for (LedBit = 0x0; LedBit < GpioWidth; LedBit++)
\r
198 for (LedLoop = 0; LedLoop < LED_MAX_BLINK; LedLoop++)
\r
202 * Set the GPIO Output to High
\r
204 XGpio_DiscreteWrite(&GpioOutput, LED_CHANNEL, 1 << LedBit);
\r
208 * Wait a small amount of time so the LED is visible
\r
210 for (Delay = 0; Delay < LED_DELAY; Delay++);
\r
214 * Read the state of the data so that it can be verified
\r
216 /* Data = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL); */
\r
220 * If the data read back is not the same as the data
\r
221 * written then return FAILURE
\r
223 /*if (Data != (1 << LedBit))
\r
225 return XST_FAILURE;
\r
230 * Clear the GPIO Output
\r
232 XGpio_DiscreteClear(&GpioOutput, LED_CHANNEL, 1 << LedBit);
\r
236 * Read the state of the data so that it can be verified
\r
238 /* Data = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);*/
\r
242 * If the data read back is not the same as the data
\r
243 * written then return FAILURE
\r
245 /* if (Data & ( 1 << LedBit))
\r
247 return XST_FAILURE;
\r
253 * Wait a small amount of time so the LED is visible
\r
255 for (Delay = 0; Delay < LED_DELAY; Delay++);
\r
262 return XST_SUCCESS;
\r
267 /******************************************************************************/
\r
270 * This function performs a test on the GPIO driver/device with the GPIO
\r
271 * configured as INPUT
\r
273 * @param DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from
\r
275 * @param DataRead is the pointer where the data read from GPIO Input is
\r
278 * @return XST_SUCCESS if the Test is successful, otherwise XST_FAILURE
\r
282 ******************************************************************************/
\r
283 XStatus GpioInputExample(Xuint16 DeviceId, Xuint32 *DataRead)
\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
291 Status = XGpio_Initialize(&GpioInput, DeviceId);
\r
292 if (Status != XST_SUCCESS)
\r
294 return XST_FAILURE;
\r
298 * Set the direction for all signals to be inputs
\r
300 XGpio_SetDataDirection(&GpioInput, LED_CHANNEL, 0xFFFFFFFF);
\r
303 * Read the state of the data so that it can be verified
\r
305 *DataRead = XGpio_DiscreteRead(&GpioInput, LED_CHANNEL);
\r
307 return XST_SUCCESS;
\r