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