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