]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/BSP/microblaze_0/libsrc/gpio_v4_0/src/xgpio.c
Common scheduler code:
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / BSP / microblaze_0 / libsrc / gpio_v4_0 / src / xgpio.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2002 - 2014 Xilinx, Inc.  All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 * @file xgpio.c
35 *
36 * The implementation of the XGpio driver's basic functionality. See xgpio.h
37 * for more information about the driver.
38 *
39 * @note
40 *
41 * None
42 *
43 * <pre>
44 * MODIFICATION HISTORY:
45 *
46 * Ver   Who  Date     Changes
47 * ----- ---- -------- -----------------------------------------------
48 * 1.00a rmm  02/04/02 First release
49 * 2.00a jhl  12/16/02 Update for dual channel and interrupt support
50 * 2.01a jvb  12/13/05 Changed Initialize() into CfgInitialize(), and made
51 *                     CfgInitialize() take a pointer to a config structure
52 *                     instead of a device id. Moved Initialize() into
53 *                     xgpio_sinit.c, and had Initialize() call CfgInitialize()
54 *                     after it retrieved the config structure using the device
55 *                     id. Removed include of xparameters.h along with any
56 *                     dependencies on xparameters.h and the _g.c config table.
57 * 2.11a mta  03/21/07 Updated to new coding style, added GetDataDirection
58 * 2.12a sv   11/21/07 Updated driver to support access through DCR bus
59 * 3.00a sv   11/21/09 Updated to use HAL Processor APIs. Renamed the
60 *                     macros to remove _m from the name.
61 * </pre>
62 *
63 *****************************************************************************/
64
65 /***************************** Include Files ********************************/
66
67 #include "xgpio.h"
68 #include "xstatus.h"
69
70 /************************** Constant Definitions ****************************/
71
72 /**************************** Type Definitions ******************************/
73
74 /***************** Macros (Inline Functions) Definitions ********************/
75
76 /************************** Variable Definitions ****************************/
77
78
79 /************************** Function Prototypes *****************************/
80
81
82 /****************************************************************************/
83 /**
84 * Initialize the XGpio instance provided by the caller based on the
85 * given configuration data.
86 *
87 * Nothing is done except to initialize the InstancePtr.
88 *
89 * @param        InstancePtr is a pointer to an XGpio instance. The memory the
90 *               pointer references must be pre-allocated by the caller. Further
91 *               calls to manipulate the driver through the XGpio API must be
92 *               made with this pointer.
93 * @param        Config is a reference to a structure containing information
94 *               about a specific GPIO device. This function initializes an
95 *               InstancePtr object for a specific device specified by the
96 *               contents of Config. This function can initialize multiple
97 *               instance objects with the use of multiple calls giving different
98 *               Config information on each call.
99 * @param        EffectiveAddr is the device base address in the virtual memory
100 *               address space. The caller is responsible for keeping the address
101 *               mapping from EffectiveAddr to the device physical base address
102 *               unchanged once this function is invoked. Unexpected errors may
103 *               occur if the address mapping changes after this function is
104 *               called. If address translation is not used, use
105 *               Config->BaseAddress for this parameters, passing the physical
106 *               address instead.
107 *
108 * @return
109 *               - XST_SUCCESS   Initialization was successfull.
110 *
111 * @note         None.
112 *
113 *****************************************************************************/
114 int XGpio_CfgInitialize(XGpio * InstancePtr, XGpio_Config * Config,
115                         u32 EffectiveAddr)
116 {
117         /*
118          * Assert arguments
119          */
120         Xil_AssertNonvoid(InstancePtr != NULL);
121
122         /*
123          * Set some default values.
124          */
125 #if (XPAR_XGPIO_USE_DCR_BRIDGE != 0)
126         InstancePtr->BaseAddress = ((EffectiveAddr >> 2)) & 0xFFF;
127 #else
128         InstancePtr->BaseAddress = EffectiveAddr;
129 #endif
130
131         InstancePtr->InterruptPresent = Config->InterruptPresent;
132         InstancePtr->IsDual = Config->IsDual;
133
134         /*
135          * Indicate the instance is now ready to use, initialized without error
136          */
137         InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
138         return (XST_SUCCESS);
139 }
140
141
142 /****************************************************************************/
143 /**
144 * Set the input/output direction of all discrete signals for the specified
145 * GPIO channel.
146 *
147 * @param        InstancePtr is a pointer to an XGpio instance to be worked on.
148 * @param        Channel contains the channel of the GPIO (1 or 2) to operate on.
149 * @param        DirectionMask is a bitmask specifying which discretes are input
150 *               and which are output. Bits set to 0 are output and bits set to 1
151 *               are input.
152 *
153 * @return       None.
154 *
155 * @note         The hardware must be built for dual channels if this function
156 *               is used with any channel other than 1.  If it is not, this
157 *               function will assert.
158 *
159 *****************************************************************************/
160 void XGpio_SetDataDirection(XGpio * InstancePtr, unsigned Channel,
161                             u32 DirectionMask)
162 {
163         Xil_AssertVoid(InstancePtr != NULL);
164         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
165         Xil_AssertVoid((Channel == 1) ||
166                      ((Channel == 2) && (InstancePtr->IsDual == TRUE)));
167
168         XGpio_WriteReg(InstancePtr->BaseAddress,
169                         ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_TRI_OFFSET,
170                         DirectionMask);
171 }
172
173 /****************************************************************************/
174 /**
175 * Get the input/output direction of all discrete signals for the specified
176 * GPIO channel.
177 *
178 * @param        InstancePtr is a pointer to an XGpio instance to be worked on.
179 * @param        Channel contains the channel of the GPIO (1 or 2) to operate on.
180 *
181 * @return       Bitmask specifying which discretes are input and
182 *               which are output. Bits set to 0 are output and bits set to 1 are
183 *               input.
184 *
185 * @note
186 *
187 * The hardware must be built for dual channels if this function is used
188 * with any channel other than 1.  If it is not, this function will assert.
189 *
190 *****************************************************************************/
191 u32 XGpio_GetDataDirection(XGpio *InstancePtr, unsigned Channel)
192 {
193         Xil_AssertNonvoid(InstancePtr != NULL);
194         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
195         Xil_AssertNonvoid((Channel == 1)  ||
196                 ((Channel == 2) &&
197                 (InstancePtr->IsDual == TRUE)));
198
199         return XGpio_ReadReg(InstancePtr->BaseAddress,
200                 ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_TRI_OFFSET);
201 }
202
203 /****************************************************************************/
204 /**
205 * Read state of discretes for the specified GPIO channnel.
206 *
207 * @param        InstancePtr is a pointer to an XGpio instance to be worked on.
208 * @param        Channel contains the channel of the GPIO (1 or 2) to operate on.
209 *
210 * @return       Current copy of the discretes register.
211 *
212 * @note         The hardware must be built for dual channels if this function
213 *               is used with any channel other than 1.  If it is not, this
214 *               function will assert.
215 *
216 *****************************************************************************/
217 u32 XGpio_DiscreteRead(XGpio * InstancePtr, unsigned Channel)
218 {
219         Xil_AssertNonvoid(InstancePtr != NULL);
220         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
221         Xil_AssertNonvoid((Channel == 1) ||
222                         ((Channel == 2) && (InstancePtr->IsDual == TRUE)));
223
224         return XGpio_ReadReg(InstancePtr->BaseAddress,
225                               ((Channel - 1) * XGPIO_CHAN_OFFSET) +
226                               XGPIO_DATA_OFFSET);
227 }
228
229 /****************************************************************************/
230 /**
231 * Write to discretes register for the specified GPIO channel.
232 *
233 * @param        InstancePtr is a pointer to an XGpio instance to be worked on.
234 * @param        Channel contains the channel of the GPIO (1 or 2) to operate on.
235 * @param        Data is the value to be written to the discretes register.
236 *
237 * @return       None.
238 *
239 * @note         The hardware must be built for dual channels if this function
240 *               is  used with any channel other than 1.  If it is not, this
241 *               function will assert. See also XGpio_DiscreteSet() and
242 *               XGpio_DiscreteClear().
243 *
244 *****************************************************************************/
245 void XGpio_DiscreteWrite(XGpio * InstancePtr, unsigned Channel, u32 Data)
246 {
247         Xil_AssertVoid(InstancePtr != NULL);
248         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
249         Xil_AssertVoid((Channel == 1) ||
250                      ((Channel == 2) && (InstancePtr->IsDual == TRUE)));
251
252         XGpio_WriteReg(InstancePtr->BaseAddress,
253                         ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_DATA_OFFSET,
254                         Data);
255 }