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