]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/iicps_v2_1/src/xiicps.c
e789682f1fa3d0b2ae616fb04699766140a285af
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / iicps_v2_1 / src / xiicps.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 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 *
35 * @file xiicps.c
36 * @addtogroup iicps_v2_1
37 * @{
38 *
39 * Contains implementation of required functions for the XIicPs driver.
40 * See xiicps.h for detailed description of the device and driver.
41 *
42 * <pre> MODIFICATION HISTORY:
43 *
44 * Ver   Who     Date     Changes
45 * ----- ------  -------- --------------------------------------------
46 * 1.00a drg/jz  01/30/10 First release
47 * 1.00a sdm     09/21/11 Updated the InstancePtr->Options in the
48 *                        XIicPs_CfgInitialize by calling XIicPs_GetOptions.
49 * 2.1   hk      04/25/14 Explicitly reset CR and clear FIFO in Abort function
50 *                        and state the same in the comments. CR# 784254.
51 *                        Fix for CR# 761060 - provision for repeated start.
52 *
53 * </pre>
54 *
55 ******************************************************************************/
56
57 /***************************** Include Files *********************************/
58
59 #include "xiicps.h"
60
61 /************************** Constant Definitions *****************************/
62
63 /**************************** Type Definitions *******************************/
64
65 /***************** Macros (Inline Functions) Definitions *********************/
66
67 /************************** Function Prototypes ******************************/
68
69 static void StubHandler(void *CallBackRef, u32 StatusEvent);
70
71 /************************** Variable Definitions *****************************/
72
73
74 /*****************************************************************************/
75 /**
76 *
77 * Initializes a specific XIicPs instance such that the driver is ready to use.
78 *
79 * The state of the device after initialization is:
80 *   - Device is disabled
81 *   - Slave mode
82 *
83 * @param        InstancePtr is a pointer to the XIicPs instance.
84 * @param        ConfigPtr is a reference to a structure containing information
85 *               about a specific IIC device. This function initializes an
86 *               InstancePtr object for a specific device specified by the
87 *               contents of Config.
88 * @param        EffectiveAddr is the device base address in the virtual memory
89 *               address space. The caller is responsible for keeping the address
90 *               mapping from EffectiveAddr to the device physical base address
91 *               unchanged once this function is invoked. Unexpected errors may
92 *               occur if the address mapping changes after this function is
93 *               called. If address translation is not used, use
94 *               ConfigPtr->BaseAddress for this parameter, passing the physical
95 *               address instead.
96 *
97 * @return       The return value is XST_SUCCESS if successful.
98 *
99 * @note         None.
100 *
101 ******************************************************************************/
102 int XIicPs_CfgInitialize(XIicPs *InstancePtr, XIicPs_Config *ConfigPtr,
103                                   u32 EffectiveAddr)
104 {
105         /*
106          * Assert validates the input arguments.
107          */
108         Xil_AssertNonvoid(InstancePtr != NULL);
109         Xil_AssertNonvoid(ConfigPtr != NULL);
110
111         /*
112          * Set some default values.
113          */
114         InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
115         InstancePtr->Config.BaseAddress = EffectiveAddr;
116         InstancePtr->Config.InputClockHz = ConfigPtr->InputClockHz;
117         InstancePtr->StatusHandler = StubHandler;
118         InstancePtr->CallBackRef = NULL;
119
120         InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
121
122         /*
123          * Reset the IIC device to get it into its initial state. It is expected
124          * that device configuration will take place after this initialization
125          * is done, but before the device is started.
126          */
127         XIicPs_Reset(InstancePtr);
128
129         /*
130          * Keep a copy of what options this instance has.
131          */
132         InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
133
134         /* Initialize repeated start flag to 0 */
135         InstancePtr->IsRepeatedStart = 0;
136
137         return XST_SUCCESS;
138 }
139
140 /*****************************************************************************/
141 /**
142 * Check whether the I2C bus is busy
143 *
144 * @param        InstancePtr is a pointer to the XIicPs instance.
145 *
146 * @return
147 *               - TRUE if the bus is busy.
148 *               - FALSE if the bus is not busy.
149 *
150 * @note         None.
151 *
152 ******************************************************************************/
153 int XIicPs_BusIsBusy(XIicPs *InstancePtr)
154 {
155         u32 StatusReg;
156
157         StatusReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
158                                            XIICPS_SR_OFFSET);
159         if (StatusReg & XIICPS_SR_BA_MASK) {
160                 return TRUE;
161         }else {
162                 return FALSE;
163         }
164 }
165
166 /*****************************************************************************/
167 /**
168 *
169 * This is a stub for the status callback. The stub is here in case the upper
170 * layers forget to set the handler.
171 *
172 * @param        CallBackRef is a pointer to the upper layer callback reference.
173 * @param        StatusEvent is the event that just occurred.
174 * @param        ByteCount is the number of bytes transferred up until the event
175 *               occurred.
176 *
177 * @return       None.
178 *
179 * @note         None.
180 *
181 ******************************************************************************/
182 static void StubHandler(void *CallBackRef, u32 StatusEvent)
183 {
184         (void) CallBackRef;
185         (void) StatusEvent;
186         Xil_AssertVoidAlways();
187 }
188
189
190 /*****************************************************************************/
191 /**
192 *
193 * Aborts a transfer in progress by resetting the FIFOs. The byte counts are
194 * cleared.
195 *
196 * @param        InstancePtr is a pointer to the XIicPs instance.
197 *
198 * @return       None.
199 *
200 * @note         None.
201 *
202 ******************************************************************************/
203 void XIicPs_Abort(XIicPs *InstancePtr)
204 {
205         u32 IntrMaskReg;
206         u32 IntrStatusReg;
207
208         Xil_AssertVoid(InstancePtr != NULL);
209         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
210
211         /*
212          * Enter a critical section, so disable the interrupts while we clear
213          * the FIFO and the status register.
214          */
215         IntrMaskReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
216                                            XIICPS_IMR_OFFSET);
217         XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
218                           XIICPS_IDR_OFFSET, XIICPS_IXR_ALL_INTR_MASK);
219
220         /*
221          * Reset the settings in config register and clear the FIFOs.
222          */
223         XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
224                           XIICPS_CR_RESET_VALUE | XIICPS_CR_CLR_FIFO_MASK);
225
226         /*
227          * Read, then write the interrupt status to make sure there are no
228          * pending interrupts.
229          */
230         IntrStatusReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
231                                          XIICPS_ISR_OFFSET);
232         XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
233                           XIICPS_ISR_OFFSET, IntrStatusReg);
234
235         /*
236          * Restore the interrupt state.
237          */
238         IntrMaskReg = XIICPS_IXR_ALL_INTR_MASK & (~IntrMaskReg);
239         XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
240                           XIICPS_IER_OFFSET, IntrMaskReg);
241
242 }
243
244 /*****************************************************************************/
245 /**
246 *
247 * Resets the IIC device. Reset must only be called after the driver has been
248 * initialized. The configuration of the device after reset is the same as its
249 * configuration after initialization.  Any data transfer that is in progress is
250 * aborted.
251 *
252 * The upper layer software is responsible for re-configuring (if necessary)
253 * and reenabling interrupts for the IIC device after the reset.
254 *
255 * @param        InstancePtr is a pointer to the XIicPs instance.
256 *
257 * @return       None.
258 *
259 * @note         None.
260 *
261 ******************************************************************************/
262 void XIicPs_Reset(XIicPs *InstancePtr)
263 {
264
265         Xil_AssertVoid(InstancePtr != NULL);
266         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
267
268         /*
269          * Abort any transfer that is in progress.
270          */
271         XIicPs_Abort(InstancePtr);
272
273         /*
274          * Reset any values so the software state matches the hardware device.
275          */
276         XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
277                           XIICPS_CR_RESET_VALUE);
278         XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
279                           XIICPS_TIME_OUT_OFFSET, XIICPS_TO_RESET_VALUE);
280         XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_IDR_OFFSET,
281                           XIICPS_IXR_ALL_INTR_MASK);
282
283 }
284 /*****************************************************************************/
285 /**
286 * Put more data into the transmit FIFO, number of bytes is ether expected
287 * number of bytes for this transfer or available space in FIFO, which ever
288 * is less.
289 *
290 * @param        InstancePtr is a pointer to the XIicPs instance.
291 *
292 * @return       Number of bytes left for this instance.
293 *
294 * @note         This is function is shared by master and slave.
295 *
296 ******************************************************************************/
297 int TransmitFifoFill(XIicPs *InstancePtr)
298 {
299         u8 AvailBytes;
300         int LoopCnt;
301         int NumBytesToSend;
302
303         /*
304          * Determine number of bytes to write to FIFO.
305          */
306         AvailBytes = XIICPS_FIFO_DEPTH -
307                 XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
308                                            XIICPS_TRANS_SIZE_OFFSET);
309
310         if (InstancePtr->SendByteCount > AvailBytes) {
311                 NumBytesToSend = AvailBytes;
312         } else {
313                 NumBytesToSend = InstancePtr->SendByteCount;
314         }
315
316         /*
317          * Fill FIFO with amount determined above.
318          */
319         for (LoopCnt = 0; LoopCnt < NumBytesToSend; LoopCnt++) {
320                 XIicPs_SendByte(InstancePtr);
321         }
322
323         return InstancePtr->SendByteCount;
324 }
325 /** @} */