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