1 |
4 |
DFC |
/******************************************************************************
|
2 |
|
|
*
|
3 |
|
|
* Copyright (C) 2002 - 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 xiic_l.c
|
36 |
|
|
* @addtogroup iic_v3_1
|
37 |
|
|
* @{
|
38 |
|
|
*
|
39 |
|
|
* This file contains low-level driver functions that can be used to access the
|
40 |
|
|
* device in normal and dynamic controller mode. The user should refer to the
|
41 |
|
|
* hardware device specification for more details of the device operation.
|
42 |
|
|
*
|
43 |
|
|
* <pre>
|
44 |
|
|
* MODIFICATION HISTORY:
|
45 |
|
|
*
|
46 |
|
|
* Ver Who Date Changes
|
47 |
|
|
* ----- --- ------- -----------------------------------------------
|
48 |
|
|
* 1.01b jhl 05/13/02 First release
|
49 |
|
|
* 1.01b jhl 10/14/02 Corrected bug in the receive function, the setup of the
|
50 |
|
|
* interrupt status mask was not being done in the loop such
|
51 |
|
|
* that a read would sometimes fail on the last byte because
|
52 |
|
|
* the transmit error which should have been ignored was
|
53 |
|
|
* being used. This would leave an extra byte in the FIFO
|
54 |
|
|
* and the bus throttled such that the next operation would
|
55 |
|
|
* also fail. Also updated the receive function to not
|
56 |
|
|
* disable the device after the last byte until after the
|
57 |
|
|
* bus transitions to not busy which is more consistent
|
58 |
|
|
* with the expected behavior.
|
59 |
|
|
* 1.01c ecm 12/05/02 new rev
|
60 |
|
|
* 1.02a mta 03/09/06 Implemented Repeated Start in the Low Level Driver.
|
61 |
|
|
* 1.03a mta 04/04/06 Implemented Dynamic IIC core routines.
|
62 |
|
|
* 1.03a ecm 06/15/06 Fixed the hang in low_level_eeprom_test with -O0
|
63 |
|
|
* Added polling loops for BNB to allow the slave to
|
64 |
|
|
* respond correctly. Also added polling loop prior
|
65 |
|
|
* to reset in _Recv.
|
66 |
|
|
* 1.13a wgr 03/22/07 Converted to new coding style.
|
67 |
|
|
* 1.13b ecm 11/29/07 added BB polling loops to the DynSend and DynRecv
|
68 |
|
|
* routines to handle the race condition with BNB in IISR.
|
69 |
|
|
* 2.00a sdm 10/22/09 Converted all register accesses to 32 bit access.
|
70 |
|
|
* Updated to use the HAL APIs/macros.
|
71 |
|
|
* Some of the macros have been renamed to remove _m from
|
72 |
|
|
* the name and Some of the macros have been renamed to be
|
73 |
|
|
* consistent, see the xiic_i.h and xiic_l.h files for
|
74 |
|
|
* further information.
|
75 |
|
|
* 2.02a sdm 10/08/10 Updated to disable the device at the end of the transfer,
|
76 |
|
|
* only when addressed as slave in XIic_Send for CR565373.
|
77 |
|
|
* 2.04a sdm 07/22/11 Removed a compiler warning by adding parenthesis around &
|
78 |
|
|
* at line 479.
|
79 |
|
|
* 2.08a adk 29/07/13 In Low level driver In repeated start condition the
|
80 |
|
|
* Direction of Tx bit must be disabled in Receive
|
81 |
|
|
* condition It Fixes the CR:685759 Changes are done
|
82 |
|
|
* in the function XIic_Recv.
|
83 |
|
|
* 3.2 sk 11/10/15 Used UINTPTR instead of u32 for Baseaddress CR# 867425.
|
84 |
|
|
* Changed the prototypes of RecvData, SendData,
|
85 |
|
|
* DynRecvData, DynSendData APIs.
|
86 |
|
|
* 3.2 sd 18/02/16 In Low level driver in repeated start condition
|
87 |
|
|
* NACK for last byte is added. Changes are done in
|
88 |
|
|
* XIic_Recv for CR# 862303
|
89 |
|
|
* 3.3 sk 06/17/16 Added bus busy checks for slave send/recv and master
|
90 |
|
|
* send/recv.
|
91 |
|
|
* 3.3 als 06/27/16 Added Low-level XIic_CheckIsBusBusy API.
|
92 |
|
|
* 3.3 als 06/27/16 Added low-level XIic_WaitBusFree API.
|
93 |
|
|
* 3.4 nk 16/11/16 Reduced sleeping time in Bus-busy check.
|
94 |
|
|
* </pre>
|
95 |
|
|
*
|
96 |
|
|
****************************************************************************/
|
97 |
|
|
|
98 |
|
|
/***************************** Include Files *******************************/
|
99 |
|
|
|
100 |
|
|
#include <sleep.h>
|
101 |
|
|
#include "xil_types.h"
|
102 |
|
|
#include "xil_assert.h"
|
103 |
|
|
#include "xiic_l.h"
|
104 |
|
|
|
105 |
|
|
/************************** Constant Definitions ***************************/
|
106 |
|
|
|
107 |
|
|
/**************************** Type Definitions *****************************/
|
108 |
|
|
|
109 |
|
|
/***************** Macros (Inline Functions) Definitions *******************/
|
110 |
|
|
|
111 |
|
|
/************************** Function Prototypes ****************************/
|
112 |
|
|
|
113 |
|
|
static unsigned RecvData(UINTPTR BaseAddress, u8 *BufferPtr,
|
114 |
|
|
unsigned ByteCount, u8 Option);
|
115 |
|
|
static unsigned SendData(UINTPTR BaseAddress, u8 *BufferPtr,
|
116 |
|
|
unsigned ByteCount, u8 Option);
|
117 |
|
|
|
118 |
|
|
static unsigned DynRecvData(UINTPTR BaseAddress, u8 *BufferPtr, u8 ByteCount);
|
119 |
|
|
static unsigned DynSendData(UINTPTR BaseAddress, u8 *BufferPtr,
|
120 |
|
|
u8 ByteCount, u8 Option);
|
121 |
|
|
|
122 |
|
|
/************************** Variable Definitions **************************/
|
123 |
|
|
|
124 |
|
|
/****************************************************************************/
|
125 |
|
|
/**
|
126 |
|
|
* Receive data as a master on the IIC bus. This function receives the data
|
127 |
|
|
* using polled I/O and blocks until the data has been received. It only
|
128 |
|
|
* supports 7 bit addressing mode of operation. This function returns zero
|
129 |
|
|
* if bus is busy.
|
130 |
|
|
*
|
131 |
|
|
* @param BaseAddress contains the base address of the IIC device.
|
132 |
|
|
* @param Address contains the 7 bit IIC address of the device to send the
|
133 |
|
|
* specified data to.
|
134 |
|
|
* @param BufferPtr points to the data to be sent.
|
135 |
|
|
* @param ByteCount is the number of bytes to be sent.
|
136 |
|
|
* @param Option indicates whether to hold or free the bus after reception
|
137 |
|
|
* of data, XIIC_STOP = end with STOP condition,
|
138 |
|
|
* XIIC_REPEATED_START = don't end with STOP condition.
|
139 |
|
|
*
|
140 |
|
|
* @return The number of bytes received.
|
141 |
|
|
*
|
142 |
|
|
* @note None.
|
143 |
|
|
*
|
144 |
|
|
******************************************************************************/
|
145 |
|
|
unsigned XIic_Recv(UINTPTR BaseAddress, u8 Address,
|
146 |
|
|
u8 *BufferPtr, unsigned ByteCount, u8 Option)
|
147 |
|
|
{
|
148 |
|
|
u32 CntlReg;
|
149 |
|
|
unsigned RemainingByteCount;
|
150 |
|
|
volatile u32 StatusReg;
|
151 |
|
|
|
152 |
|
|
/* Tx error is enabled incase the address (7 or 10) has no device to
|
153 |
|
|
* answer with Ack. When only one byte of data, must set NO ACK before
|
154 |
|
|
* address goes out therefore Tx error must not be enabled as it will go
|
155 |
|
|
* off immediately and the Rx full interrupt will be checked. If full,
|
156 |
|
|
* then the one byte was received and the Tx error will be disabled
|
157 |
|
|
* without sending an error callback msg
|
158 |
|
|
*/
|
159 |
|
|
XIic_ClearIisr(BaseAddress,
|
160 |
|
|
XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK |
|
161 |
|
|
XIIC_INTR_ARB_LOST_MASK);
|
162 |
|
|
|
163 |
|
|
/* Set receive FIFO occupancy depth for 1 byte (zero based) */
|
164 |
|
|
XIic_WriteReg(BaseAddress, XIIC_RFD_REG_OFFSET, 0);
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
/* Check to see if already Master on the Bus.
|
168 |
|
|
* If Repeated Start bit is not set send Start bit by setting MSMS bit
|
169 |
|
|
* else Send the address
|
170 |
|
|
*/
|
171 |
|
|
CntlReg = XIic_ReadReg(BaseAddress, XIIC_CR_REG_OFFSET);
|
172 |
|
|
if ((CntlReg & XIIC_CR_REPEATED_START_MASK) == 0) {
|
173 |
|
|
/* 7 bit slave address, send the address for a read operation
|
174 |
|
|
* and set the state to indicate the address has been sent
|
175 |
|
|
*/
|
176 |
|
|
XIic_Send7BitAddress(BaseAddress, Address,
|
177 |
|
|
XIIC_READ_OPERATION);
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
/* MSMS gets set after putting data in FIFO. Start the master
|
181 |
|
|
* receive operation by setting CR Bits MSMS to Master, if the
|
182 |
|
|
* buffer is only one byte, then it should not be acknowledged
|
183 |
|
|
* to indicate the end of data
|
184 |
|
|
*/
|
185 |
|
|
CntlReg = XIIC_CR_MSMS_MASK | XIIC_CR_ENABLE_DEVICE_MASK;
|
186 |
|
|
if (ByteCount == 1) {
|
187 |
|
|
CntlReg |= XIIC_CR_NO_ACK_MASK;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
/* Write out the control register to start receiving data and
|
191 |
|
|
* call the function to receive each byte into the buffer
|
192 |
|
|
*/
|
193 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET, CntlReg);
|
194 |
|
|
|
195 |
|
|
/* Clear the latched interrupt status for the bus not busy bit
|
196 |
|
|
* which must be done while the bus is busy
|
197 |
|
|
*/
|
198 |
|
|
StatusReg = XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET);
|
199 |
|
|
|
200 |
|
|
while ((StatusReg & XIIC_SR_BUS_BUSY_MASK) == 0) {
|
201 |
|
|
StatusReg = XIic_ReadReg(BaseAddress,
|
202 |
|
|
XIIC_SR_REG_OFFSET);
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_BNB_MASK);
|
206 |
|
|
} else {
|
207 |
|
|
/* Before writing 7bit slave address the Direction of Tx bit
|
208 |
|
|
* must be disabled
|
209 |
|
|
*/
|
210 |
|
|
CntlReg &= ~XIIC_CR_DIR_IS_TX_MASK;
|
211 |
|
|
if (ByteCount == 1) {
|
212 |
|
|
CntlReg |= XIIC_CR_NO_ACK_MASK;
|
213 |
|
|
}
|
214 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET, CntlReg);
|
215 |
|
|
/* Already owns the Bus indicating that its a Repeated Start
|
216 |
|
|
* call. 7 bit slave address, send the address for a read
|
217 |
|
|
* operation and set the state to indicate the address has been
|
218 |
|
|
* sent
|
219 |
|
|
*/
|
220 |
|
|
XIic_Send7BitAddress(BaseAddress, Address,
|
221 |
|
|
XIIC_READ_OPERATION);
|
222 |
|
|
}
|
223 |
|
|
/* Try to receive the data from the IIC bus */
|
224 |
|
|
|
225 |
|
|
RemainingByteCount = RecvData(BaseAddress, BufferPtr,
|
226 |
|
|
ByteCount, Option);
|
227 |
|
|
|
228 |
|
|
CntlReg = XIic_ReadReg(BaseAddress, XIIC_CR_REG_OFFSET);
|
229 |
|
|
if ((CntlReg & XIIC_CR_REPEATED_START_MASK) == 0) {
|
230 |
|
|
/* The receive is complete, disable the IIC device if the Option
|
231 |
|
|
* is to release the Bus after Reception of data and return the
|
232 |
|
|
* number of bytes that was received
|
233 |
|
|
*/
|
234 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET, 0);
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
/* Wait until I2C bus is freed, exit if timed out. */
|
238 |
|
|
if (XIic_WaitBusFree(BaseAddress) != XST_SUCCESS) {
|
239 |
|
|
return 0;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
/* Return the number of bytes that was received */
|
243 |
|
|
return ByteCount - RemainingByteCount;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
/******************************************************************************
|
247 |
|
|
*
|
248 |
|
|
* Receive the specified data from the device that has been previously addressed
|
249 |
|
|
* on the IIC bus. This function assumes that the 7 bit address has been sent
|
250 |
|
|
* and it should wait for the transmit of the address to complete.
|
251 |
|
|
*
|
252 |
|
|
* @param BaseAddress contains the base address of the IIC device.
|
253 |
|
|
* @param BufferPtr points to the buffer to hold the data that is
|
254 |
|
|
* received.
|
255 |
|
|
* @param ByteCount is the number of bytes to be received.
|
256 |
|
|
* @param Option indicates whether to hold or free the bus after reception
|
257 |
|
|
* of data, XIIC_STOP = end with STOP condition,
|
258 |
|
|
* XIIC_REPEATED_START = don't end with STOP condition.
|
259 |
|
|
*
|
260 |
|
|
* @return The number of bytes remaining to be received.
|
261 |
|
|
*
|
262 |
|
|
* @note
|
263 |
|
|
*
|
264 |
|
|
* This function does not take advantage of the receive FIFO because it is
|
265 |
|
|
* designed for minimal code space and complexity. It contains loops that
|
266 |
|
|
* that could cause the function not to return if the hardware is not working.
|
267 |
|
|
*
|
268 |
|
|
* This function assumes that the calling function will disable the IIC device
|
269 |
|
|
* after this function returns.
|
270 |
|
|
*
|
271 |
|
|
******************************************************************************/
|
272 |
|
|
static unsigned RecvData(UINTPTR BaseAddress, u8 *BufferPtr,
|
273 |
|
|
unsigned ByteCount, u8 Option)
|
274 |
|
|
{
|
275 |
|
|
u32 CntlReg;
|
276 |
|
|
u32 IntrStatusMask;
|
277 |
|
|
u32 IntrStatus;
|
278 |
|
|
|
279 |
|
|
/* Attempt to receive the specified number of bytes on the IIC bus */
|
280 |
|
|
|
281 |
|
|
while (ByteCount > 0) {
|
282 |
|
|
/* Setup the mask to use for checking errors because when
|
283 |
|
|
* receiving one byte OR the last byte of a multibyte message an
|
284 |
|
|
* error naturally occurs when the no ack is done to tell the
|
285 |
|
|
* slave the last byte
|
286 |
|
|
*/
|
287 |
|
|
if (ByteCount == 1) {
|
288 |
|
|
IntrStatusMask =
|
289 |
|
|
XIIC_INTR_ARB_LOST_MASK | XIIC_INTR_BNB_MASK;
|
290 |
|
|
} else {
|
291 |
|
|
IntrStatusMask =
|
292 |
|
|
XIIC_INTR_ARB_LOST_MASK |
|
293 |
|
|
XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_BNB_MASK;
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
/* Wait for the previous transmit and the 1st receive to
|
297 |
|
|
* complete by checking the interrupt status register of the
|
298 |
|
|
* IPIF
|
299 |
|
|
*/
|
300 |
|
|
while (1) {
|
301 |
|
|
IntrStatus = XIic_ReadIisr(BaseAddress);
|
302 |
|
|
if (IntrStatus & XIIC_INTR_RX_FULL_MASK) {
|
303 |
|
|
break;
|
304 |
|
|
}
|
305 |
|
|
/* Check the transmit error after the receive full
|
306 |
|
|
* because when sending only one byte transmit error
|
307 |
|
|
* will occur because of the no ack to indicate the end
|
308 |
|
|
* of the data
|
309 |
|
|
*/
|
310 |
|
|
if (IntrStatus & IntrStatusMask) {
|
311 |
|
|
return ByteCount;
|
312 |
|
|
}
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
CntlReg = XIic_ReadReg(BaseAddress, XIIC_CR_REG_OFFSET);
|
316 |
|
|
|
317 |
|
|
/* Special conditions exist for the last two bytes so check for
|
318 |
|
|
* them. Note that the control register must be setup for these
|
319 |
|
|
* conditions before the data byte which was already received is
|
320 |
|
|
* read from the receive FIFO (while the bus is throttled
|
321 |
|
|
*/
|
322 |
|
|
if (ByteCount == 1) {
|
323 |
|
|
if (Option == XIIC_STOP) {
|
324 |
|
|
|
325 |
|
|
/* If the Option is to release the bus after the
|
326 |
|
|
* last data byte, it has already been read and
|
327 |
|
|
* no ack has been done, so clear MSMS while
|
328 |
|
|
* leaving the device enabled so it can get off
|
329 |
|
|
* the IIC bus appropriately with a stop
|
330 |
|
|
*/
|
331 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
332 |
|
|
XIIC_CR_ENABLE_DEVICE_MASK);
|
333 |
|
|
}
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
/* Before the last byte is received, set NOACK to tell the slave
|
337 |
|
|
* IIC device that it is the end, this must be done before
|
338 |
|
|
* reading the byte from the FIFO
|
339 |
|
|
*/
|
340 |
|
|
if (ByteCount == 2) {
|
341 |
|
|
/* Write control reg with NO ACK allowing last byte to
|
342 |
|
|
* have the No ack set to indicate to slave last byte
|
343 |
|
|
* read
|
344 |
|
|
*/
|
345 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
346 |
|
|
CntlReg | XIIC_CR_NO_ACK_MASK);
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
/* Read in data from the FIFO and unthrottle the bus such that
|
350 |
|
|
* the next byte is read from the IIC bus
|
351 |
|
|
*/
|
352 |
|
|
*BufferPtr++ = (u8) XIic_ReadReg(BaseAddress,
|
353 |
|
|
XIIC_DRR_REG_OFFSET);
|
354 |
|
|
|
355 |
|
|
if ((ByteCount == 1) && (Option == XIIC_REPEATED_START)) {
|
356 |
|
|
|
357 |
|
|
/* RSTA bit should be set only when the FIFO is
|
358 |
|
|
* completely Empty.
|
359 |
|
|
*/
|
360 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
361 |
|
|
XIIC_CR_ENABLE_DEVICE_MASK | XIIC_CR_MSMS_MASK
|
362 |
|
|
| XIIC_CR_REPEATED_START_MASK);
|
363 |
|
|
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
/* Clear the latched interrupt status so that it will be updated
|
367 |
|
|
* with the new state when it changes, this must be done after
|
368 |
|
|
* the receive register is read
|
369 |
|
|
*/
|
370 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_RX_FULL_MASK |
|
371 |
|
|
XIIC_INTR_TX_ERROR_MASK |
|
372 |
|
|
XIIC_INTR_ARB_LOST_MASK);
|
373 |
|
|
ByteCount--;
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
if (Option == XIIC_STOP) {
|
377 |
|
|
|
378 |
|
|
/* If the Option is to release the bus after Reception of data,
|
379 |
|
|
* wait for the bus to transition to not busy before returning,
|
380 |
|
|
* the IIC device cannot be disabled until this occurs. It
|
381 |
|
|
* should transition as the MSMS bit of the control register was
|
382 |
|
|
* cleared before the last byte was read from the FIFO
|
383 |
|
|
*/
|
384 |
|
|
while (1) {
|
385 |
|
|
if (XIic_ReadIisr(BaseAddress) & XIIC_INTR_BNB_MASK) {
|
386 |
|
|
break;
|
387 |
|
|
}
|
388 |
|
|
}
|
389 |
|
|
}
|
390 |
|
|
|
391 |
|
|
return ByteCount;
|
392 |
|
|
}
|
393 |
|
|
|
394 |
|
|
/****************************************************************************/
|
395 |
|
|
/**
|
396 |
|
|
* Send data as a master on the IIC bus. This function sends the data
|
397 |
|
|
* using polled I/O and blocks until the data has been sent. It only supports
|
398 |
|
|
* 7 bit addressing mode of operation. This function returns zero
|
399 |
|
|
* if bus is busy.
|
400 |
|
|
*
|
401 |
|
|
* @param BaseAddress contains the base address of the IIC device.
|
402 |
|
|
* @param Address contains the 7 bit IIC address of the device to send the
|
403 |
|
|
* specified data to.
|
404 |
|
|
* @param BufferPtr points to the data to be sent.
|
405 |
|
|
* @param ByteCount is the number of bytes to be sent.
|
406 |
|
|
* @param Option indicates whether to hold or free the bus after
|
407 |
|
|
* transmitting the data.
|
408 |
|
|
*
|
409 |
|
|
* @return The number of bytes sent.
|
410 |
|
|
*
|
411 |
|
|
* @note None.
|
412 |
|
|
*
|
413 |
|
|
******************************************************************************/
|
414 |
|
|
unsigned XIic_Send(UINTPTR BaseAddress, u8 Address,
|
415 |
|
|
u8 *BufferPtr, unsigned ByteCount, u8 Option)
|
416 |
|
|
{
|
417 |
|
|
unsigned RemainingByteCount;
|
418 |
|
|
u32 ControlReg;
|
419 |
|
|
volatile u32 StatusReg;
|
420 |
|
|
|
421 |
|
|
/* Wait until I2C bus is freed, exit if timed out. */
|
422 |
|
|
if (XIic_WaitBusFree(BaseAddress) != XST_SUCCESS) {
|
423 |
|
|
return 0;
|
424 |
|
|
}
|
425 |
|
|
|
426 |
|
|
/* Check to see if already Master on the Bus.
|
427 |
|
|
* If Repeated Start bit is not set send Start bit by setting
|
428 |
|
|
* MSMS bit else Send the address.
|
429 |
|
|
*/
|
430 |
|
|
ControlReg = XIic_ReadReg(BaseAddress, XIIC_CR_REG_OFFSET);
|
431 |
|
|
if ((ControlReg & XIIC_CR_REPEATED_START_MASK) == 0) {
|
432 |
|
|
/*
|
433 |
|
|
* Put the address into the FIFO to be sent and indicate
|
434 |
|
|
* that the operation to be performed on the bus is a
|
435 |
|
|
* write operation
|
436 |
|
|
*/
|
437 |
|
|
XIic_Send7BitAddress(BaseAddress, Address,
|
438 |
|
|
XIIC_WRITE_OPERATION);
|
439 |
|
|
/* Clear the latched interrupt status so that it will
|
440 |
|
|
* be updated with the new state when it changes, this
|
441 |
|
|
* must be done after the address is put in the FIFO
|
442 |
|
|
*/
|
443 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_TX_EMPTY_MASK |
|
444 |
|
|
XIIC_INTR_TX_ERROR_MASK |
|
445 |
|
|
XIIC_INTR_ARB_LOST_MASK);
|
446 |
|
|
|
447 |
|
|
/*
|
448 |
|
|
* MSMS must be set after putting data into transmit FIFO,
|
449 |
|
|
* indicate the direction is transmit, this device is master
|
450 |
|
|
* and enable the IIC device
|
451 |
|
|
*/
|
452 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
453 |
|
|
XIIC_CR_MSMS_MASK | XIIC_CR_DIR_IS_TX_MASK |
|
454 |
|
|
XIIC_CR_ENABLE_DEVICE_MASK);
|
455 |
|
|
|
456 |
|
|
/*
|
457 |
|
|
* Clear the latched interrupt
|
458 |
|
|
* status for the bus not busy bit which must be done while
|
459 |
|
|
* the bus is busy
|
460 |
|
|
*/
|
461 |
|
|
StatusReg = XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET);
|
462 |
|
|
while ((StatusReg & XIIC_SR_BUS_BUSY_MASK) == 0) {
|
463 |
|
|
StatusReg = XIic_ReadReg(BaseAddress,
|
464 |
|
|
XIIC_SR_REG_OFFSET);
|
465 |
|
|
}
|
466 |
|
|
|
467 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_BNB_MASK);
|
468 |
|
|
}
|
469 |
|
|
else {
|
470 |
|
|
/*
|
471 |
|
|
* Already owns the Bus indicating that its a Repeated Start
|
472 |
|
|
* call. 7 bit slave address, send the address for a write
|
473 |
|
|
* operation and set the state to indicate the address has
|
474 |
|
|
* been sent.
|
475 |
|
|
*/
|
476 |
|
|
XIic_Send7BitAddress(BaseAddress, Address,
|
477 |
|
|
XIIC_WRITE_OPERATION);
|
478 |
|
|
}
|
479 |
|
|
|
480 |
|
|
/* Send the specified data to the device on the IIC bus specified by the
|
481 |
|
|
* the address
|
482 |
|
|
*/
|
483 |
|
|
RemainingByteCount = SendData(BaseAddress, BufferPtr,
|
484 |
|
|
ByteCount, Option);
|
485 |
|
|
|
486 |
|
|
ControlReg = XIic_ReadReg(BaseAddress, XIIC_CR_REG_OFFSET);
|
487 |
|
|
if ((ControlReg & XIIC_CR_REPEATED_START_MASK) == 0) {
|
488 |
|
|
/*
|
489 |
|
|
* The Transmission is completed, disable the IIC device if
|
490 |
|
|
* the Option is to release the Bus after transmission of data
|
491 |
|
|
* and return the number of bytes that was received. Only wait
|
492 |
|
|
* if master, if addressed as slave just reset to release
|
493 |
|
|
* the bus.
|
494 |
|
|
*/
|
495 |
|
|
if ((ControlReg & XIIC_CR_MSMS_MASK) != 0) {
|
496 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
497 |
|
|
(ControlReg & ~XIIC_CR_MSMS_MASK));
|
498 |
|
|
StatusReg = XIic_ReadReg(BaseAddress,
|
499 |
|
|
XIIC_SR_REG_OFFSET);
|
500 |
|
|
while ((StatusReg & XIIC_SR_BUS_BUSY_MASK) != 0) {
|
501 |
|
|
StatusReg = XIic_ReadReg(BaseAddress,
|
502 |
|
|
XIIC_SR_REG_OFFSET);
|
503 |
|
|
}
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
if ((XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET) &
|
507 |
|
|
XIIC_SR_ADDR_AS_SLAVE_MASK) != 0) {
|
508 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET, 0);
|
509 |
|
|
}
|
510 |
|
|
}
|
511 |
|
|
|
512 |
|
|
return ByteCount - RemainingByteCount;
|
513 |
|
|
}
|
514 |
|
|
|
515 |
|
|
/******************************************************************************
|
516 |
|
|
*
|
517 |
|
|
* Send the specified buffer to the device that has been previously addressed
|
518 |
|
|
* on the IIC bus. This function assumes that the 7 bit address has been sent
|
519 |
|
|
* and it should wait for the transmit of the address to complete.
|
520 |
|
|
*
|
521 |
|
|
* @param BaseAddress contains the base address of the IIC device.
|
522 |
|
|
* @param BufferPtr points to the data to be sent.
|
523 |
|
|
* @param ByteCount is the number of bytes to be sent.
|
524 |
|
|
* @param Option indicates whether to hold or free the bus after
|
525 |
|
|
* transmitting the data.
|
526 |
|
|
*
|
527 |
|
|
* @return The number of bytes remaining to be sent.
|
528 |
|
|
*
|
529 |
|
|
* @note
|
530 |
|
|
*
|
531 |
|
|
* This function does not take advantage of the transmit FIFO because it is
|
532 |
|
|
* designed for minimal code space and complexity. It contains loops that
|
533 |
|
|
* that could cause the function not to return if the hardware is not working.
|
534 |
|
|
*
|
535 |
|
|
******************************************************************************/
|
536 |
|
|
static unsigned SendData(UINTPTR BaseAddress, u8 *BufferPtr,
|
537 |
|
|
unsigned ByteCount, u8 Option)
|
538 |
|
|
{
|
539 |
|
|
u32 IntrStatus;
|
540 |
|
|
|
541 |
|
|
/*
|
542 |
|
|
* Send the specified number of bytes in the specified buffer by polling
|
543 |
|
|
* the device registers and blocking until complete
|
544 |
|
|
*/
|
545 |
|
|
while (ByteCount > 0) {
|
546 |
|
|
/*
|
547 |
|
|
* Wait for the transmit to be empty before sending any more
|
548 |
|
|
* data by polling the interrupt status register
|
549 |
|
|
*/
|
550 |
|
|
while (1) {
|
551 |
|
|
IntrStatus = XIic_ReadIisr(BaseAddress);
|
552 |
|
|
|
553 |
|
|
if (IntrStatus & (XIIC_INTR_TX_ERROR_MASK |
|
554 |
|
|
XIIC_INTR_ARB_LOST_MASK |
|
555 |
|
|
XIIC_INTR_BNB_MASK)) {
|
556 |
|
|
return ByteCount;
|
557 |
|
|
}
|
558 |
|
|
|
559 |
|
|
if (IntrStatus & XIIC_INTR_TX_EMPTY_MASK) {
|
560 |
|
|
break;
|
561 |
|
|
}
|
562 |
|
|
}
|
563 |
|
|
/* If there is more than one byte to send then put the
|
564 |
|
|
* next byte to send into the transmit FIFO
|
565 |
|
|
*/
|
566 |
|
|
if (ByteCount > 1) {
|
567 |
|
|
XIic_WriteReg(BaseAddress, XIIC_DTR_REG_OFFSET,
|
568 |
|
|
*BufferPtr++);
|
569 |
|
|
}
|
570 |
|
|
else {
|
571 |
|
|
if (Option == XIIC_STOP) {
|
572 |
|
|
/*
|
573 |
|
|
* If the Option is to release the bus after
|
574 |
|
|
* the last data byte, Set the stop Option
|
575 |
|
|
* before sending the last byte of data so
|
576 |
|
|
* that the stop Option will be generated
|
577 |
|
|
* immediately following the data. This is
|
578 |
|
|
* done by clearing the MSMS bit in the
|
579 |
|
|
* control register.
|
580 |
|
|
*/
|
581 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
582 |
|
|
XIIC_CR_ENABLE_DEVICE_MASK |
|
583 |
|
|
XIIC_CR_DIR_IS_TX_MASK);
|
584 |
|
|
}
|
585 |
|
|
|
586 |
|
|
/*
|
587 |
|
|
* Put the last byte to send in the transmit FIFO
|
588 |
|
|
*/
|
589 |
|
|
XIic_WriteReg(BaseAddress, XIIC_DTR_REG_OFFSET,
|
590 |
|
|
*BufferPtr++);
|
591 |
|
|
|
592 |
|
|
if (Option == XIIC_REPEATED_START) {
|
593 |
|
|
XIic_ClearIisr(BaseAddress,
|
594 |
|
|
XIIC_INTR_TX_EMPTY_MASK);
|
595 |
|
|
/*
|
596 |
|
|
* Wait for the transmit to be empty before
|
597 |
|
|
* setting RSTA bit.
|
598 |
|
|
*/
|
599 |
|
|
while (1) {
|
600 |
|
|
IntrStatus =
|
601 |
|
|
XIic_ReadIisr(BaseAddress);
|
602 |
|
|
if (IntrStatus &
|
603 |
|
|
XIIC_INTR_TX_EMPTY_MASK) {
|
604 |
|
|
/*
|
605 |
|
|
* RSTA bit should be set only
|
606 |
|
|
* when the FIFO is completely
|
607 |
|
|
* Empty.
|
608 |
|
|
*/
|
609 |
|
|
XIic_WriteReg(BaseAddress,
|
610 |
|
|
XIIC_CR_REG_OFFSET,
|
611 |
|
|
XIIC_CR_REPEATED_START_MASK |
|
612 |
|
|
XIIC_CR_ENABLE_DEVICE_MASK |
|
613 |
|
|
XIIC_CR_DIR_IS_TX_MASK |
|
614 |
|
|
XIIC_CR_MSMS_MASK);
|
615 |
|
|
break;
|
616 |
|
|
}
|
617 |
|
|
}
|
618 |
|
|
}
|
619 |
|
|
}
|
620 |
|
|
|
621 |
|
|
/*
|
622 |
|
|
* Clear the latched interrupt status register and this must be
|
623 |
|
|
* done after the transmit FIFO has been written to or it won't
|
624 |
|
|
* clear
|
625 |
|
|
*/
|
626 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_TX_EMPTY_MASK);
|
627 |
|
|
|
628 |
|
|
/*
|
629 |
|
|
* Update the byte count to reflect the byte sent and clear
|
630 |
|
|
* the latched interrupt status so it will be updated for the
|
631 |
|
|
* new state
|
632 |
|
|
*/
|
633 |
|
|
ByteCount--;
|
634 |
|
|
}
|
635 |
|
|
|
636 |
|
|
if (Option == XIIC_STOP) {
|
637 |
|
|
/*
|
638 |
|
|
* If the Option is to release the bus after transmission of
|
639 |
|
|
* data, Wait for the bus to transition to not busy before
|
640 |
|
|
* returning, the IIC device cannot be disabled until this
|
641 |
|
|
* occurs. Note that this is different from a receive operation
|
642 |
|
|
* because the stop Option causes the bus to go not busy.
|
643 |
|
|
*/
|
644 |
|
|
while (1) {
|
645 |
|
|
if (XIic_ReadIisr(BaseAddress) &
|
646 |
|
|
XIIC_INTR_BNB_MASK) {
|
647 |
|
|
break;
|
648 |
|
|
}
|
649 |
|
|
}
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
return ByteCount;
|
653 |
|
|
}
|
654 |
|
|
|
655 |
|
|
/*****************************************************************************/
|
656 |
|
|
/**
|
657 |
|
|
* Receive data as a master on the IIC bus. This function receives the data
|
658 |
|
|
* using polled I/O and blocks until the data has been received. It only
|
659 |
|
|
* supports 7 bit addressing. This function returns zero if bus is busy.
|
660 |
|
|
*
|
661 |
|
|
* @param BaseAddress contains the base address of the IIC Device.
|
662 |
|
|
* @param Address contains the 7 bit IIC Device address of the device to
|
663 |
|
|
* send the specified data to.
|
664 |
|
|
* @param BufferPtr points to the data to be sent.
|
665 |
|
|
* @param ByteCount is the number of bytes to be sent. This value can't be
|
666 |
|
|
* greater than 255 and needs to be greater than 0.
|
667 |
|
|
*
|
668 |
|
|
* @return The number of bytes received.
|
669 |
|
|
*
|
670 |
|
|
* @note Upon entry to this function, the IIC interface needs to be
|
671 |
|
|
* already enabled in the CR register.
|
672 |
|
|
*
|
673 |
|
|
******************************************************************************/
|
674 |
|
|
unsigned XIic_DynRecv(UINTPTR BaseAddress, u8 Address, u8 *BufferPtr, u8 ByteCount)
|
675 |
|
|
{
|
676 |
|
|
unsigned RemainingByteCount;
|
677 |
|
|
u32 StatusRegister;
|
678 |
|
|
|
679 |
|
|
/*
|
680 |
|
|
* Clear the latched interrupt status so that it will be updated with
|
681 |
|
|
* the new state when it changes.
|
682 |
|
|
*/
|
683 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_TX_EMPTY_MASK |
|
684 |
|
|
XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_ARB_LOST_MASK);
|
685 |
|
|
|
686 |
|
|
/*
|
687 |
|
|
* Send the 7 bit slave address for a read operation and set the state
|
688 |
|
|
* to indicate the address has been sent. Upon writing the address, a
|
689 |
|
|
* start condition is initiated. MSMS is automatically set to master
|
690 |
|
|
* when the address is written to the Fifo. If MSMS was already set,
|
691 |
|
|
* then a re-start is sent prior to the address.
|
692 |
|
|
*/
|
693 |
|
|
XIic_DynSend7BitAddress(BaseAddress, Address, XIIC_READ_OPERATION);
|
694 |
|
|
|
695 |
|
|
/*
|
696 |
|
|
* Wait for the bus to go busy.
|
697 |
|
|
*/
|
698 |
|
|
StatusRegister = XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET);
|
699 |
|
|
|
700 |
|
|
while (( StatusRegister & XIIC_SR_BUS_BUSY_MASK)
|
701 |
|
|
!= XIIC_SR_BUS_BUSY_MASK) {
|
702 |
|
|
StatusRegister = XIic_ReadReg(BaseAddress,
|
703 |
|
|
XIIC_SR_REG_OFFSET);
|
704 |
|
|
}
|
705 |
|
|
|
706 |
|
|
/*
|
707 |
|
|
* Clear the latched interrupt status for the bus not busy bit which
|
708 |
|
|
* must be done while the bus is busy.
|
709 |
|
|
*/
|
710 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_BNB_MASK);
|
711 |
|
|
|
712 |
|
|
/*
|
713 |
|
|
* Write to the Tx Fifo the dynamic stop control bit with the number of
|
714 |
|
|
* bytes that are to be read over the IIC interface from the presently
|
715 |
|
|
* addressed device.
|
716 |
|
|
*/
|
717 |
|
|
XIic_DynSendStop(BaseAddress, ByteCount);
|
718 |
|
|
|
719 |
|
|
/*
|
720 |
|
|
* Receive the data from the IIC bus.
|
721 |
|
|
*/
|
722 |
|
|
RemainingByteCount = DynRecvData(BaseAddress, BufferPtr, ByteCount);
|
723 |
|
|
|
724 |
|
|
/* Wait until I2C bus is freed, exit if timed out. */
|
725 |
|
|
if (XIic_WaitBusFree(BaseAddress) != XST_SUCCESS) {
|
726 |
|
|
return 0;
|
727 |
|
|
}
|
728 |
|
|
|
729 |
|
|
/*
|
730 |
|
|
* The receive is complete. Return the number of bytes that were
|
731 |
|
|
* received.
|
732 |
|
|
*/
|
733 |
|
|
return ByteCount - RemainingByteCount;
|
734 |
|
|
}
|
735 |
|
|
|
736 |
|
|
/*****************************************************************************/
|
737 |
|
|
/**
|
738 |
|
|
* Receive the specified data from the device that has been previously addressed
|
739 |
|
|
* on the IIC bus. This function assumes the following:
|
740 |
|
|
* - The Rx Fifo occupancy depth has been set to its max.
|
741 |
|
|
* - Upon entry, the Rx Fifo is empty.
|
742 |
|
|
* - The 7 bit address has been sent.
|
743 |
|
|
* - The dynamic stop and number of bytes to receive has been written to Tx
|
744 |
|
|
* Fifo.
|
745 |
|
|
*
|
746 |
|
|
* @param BaseAddress contains the base address of the IIC Device.
|
747 |
|
|
* @param BufferPtr points to the buffer to hold the data that is
|
748 |
|
|
* received.
|
749 |
|
|
* @param ByteCount is the number of bytes to be received. The range of
|
750 |
|
|
* this value is greater than 0 and not higher than 255.
|
751 |
|
|
*
|
752 |
|
|
* @return The number of bytes remaining to be received.
|
753 |
|
|
*
|
754 |
|
|
* @note This function contains loops that could cause the function not
|
755 |
|
|
* to return if the hardware is not working.
|
756 |
|
|
*
|
757 |
|
|
******************************************************************************/
|
758 |
|
|
static unsigned DynRecvData(UINTPTR BaseAddress, u8 *BufferPtr, u8 ByteCount)
|
759 |
|
|
{
|
760 |
|
|
u32 StatusReg;
|
761 |
|
|
u32 IntrStatus;
|
762 |
|
|
u32 IntrStatusMask;
|
763 |
|
|
|
764 |
|
|
while (ByteCount > 0) {
|
765 |
|
|
|
766 |
|
|
/*
|
767 |
|
|
* Setup the mask to use for checking errors because when
|
768 |
|
|
* receiving one byte OR the last byte of a multibyte message
|
769 |
|
|
* an error naturally occurs when the no ack is done to tell
|
770 |
|
|
* the slave the last byte.
|
771 |
|
|
*/
|
772 |
|
|
if (ByteCount == 1) {
|
773 |
|
|
IntrStatusMask =
|
774 |
|
|
XIIC_INTR_ARB_LOST_MASK | XIIC_INTR_BNB_MASK;
|
775 |
|
|
} else {
|
776 |
|
|
IntrStatusMask =
|
777 |
|
|
XIIC_INTR_ARB_LOST_MASK |
|
778 |
|
|
XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_BNB_MASK;
|
779 |
|
|
}
|
780 |
|
|
|
781 |
|
|
/*
|
782 |
|
|
* Wait for a byte to show up in the Rx Fifo.
|
783 |
|
|
*/
|
784 |
|
|
while (1) {
|
785 |
|
|
IntrStatus = XIic_ReadIisr(BaseAddress);
|
786 |
|
|
StatusReg = XIic_ReadReg(BaseAddress,
|
787 |
|
|
XIIC_SR_REG_OFFSET);
|
788 |
|
|
|
789 |
|
|
if ((StatusReg & XIIC_SR_RX_FIFO_EMPTY_MASK) !=
|
790 |
|
|
XIIC_SR_RX_FIFO_EMPTY_MASK) {
|
791 |
|
|
break;
|
792 |
|
|
}
|
793 |
|
|
/*
|
794 |
|
|
* Check the transmit error after the receive full
|
795 |
|
|
* because when sending only one byte transmit error
|
796 |
|
|
* will occur because of the no ack to indicate the end
|
797 |
|
|
* of the data.
|
798 |
|
|
*/
|
799 |
|
|
if (IntrStatus & IntrStatusMask) {
|
800 |
|
|
return ByteCount;
|
801 |
|
|
}
|
802 |
|
|
}
|
803 |
|
|
|
804 |
|
|
/*
|
805 |
|
|
* Read in byte from the Rx Fifo. If the Fifo reached the
|
806 |
|
|
* programmed occupancy depth as programmed in the Rx occupancy
|
807 |
|
|
* reg, this read access will un throttle the bus such that
|
808 |
|
|
* the next byte is read from the IIC bus.
|
809 |
|
|
*/
|
810 |
|
|
*BufferPtr++ = XIic_ReadReg(BaseAddress, XIIC_DRR_REG_OFFSET);
|
811 |
|
|
ByteCount--;
|
812 |
|
|
}
|
813 |
|
|
|
814 |
|
|
return ByteCount;
|
815 |
|
|
}
|
816 |
|
|
|
817 |
|
|
/*****************************************************************************/
|
818 |
|
|
/**
|
819 |
|
|
* Send data as a master on the IIC bus. This function sends the data using
|
820 |
|
|
* polled I/O and blocks until the data has been sent. It only supports 7 bit
|
821 |
|
|
* addressing. This function returns zero if bus is busy.
|
822 |
|
|
*
|
823 |
|
|
* @param BaseAddress contains the base address of the IIC Device.
|
824 |
|
|
* @param Address contains the 7 bit IIC address of the device to send the
|
825 |
|
|
* specified data to.
|
826 |
|
|
* @param BufferPtr points to the data to be sent.
|
827 |
|
|
* @param ByteCount is the number of bytes to be sent.
|
828 |
|
|
* @param Option: XIIC_STOP = end with STOP condition,
|
829 |
|
|
* XIIC_REPEATED_START = don't end with STOP condition.
|
830 |
|
|
*
|
831 |
|
|
* @return The number of bytes sent.
|
832 |
|
|
*
|
833 |
|
|
* @note None.
|
834 |
|
|
*
|
835 |
|
|
******************************************************************************/
|
836 |
|
|
unsigned XIic_DynSend(UINTPTR BaseAddress, u16 Address, u8 *BufferPtr,
|
837 |
|
|
u8 ByteCount, u8 Option)
|
838 |
|
|
{
|
839 |
|
|
unsigned RemainingByteCount;
|
840 |
|
|
u32 StatusRegister;
|
841 |
|
|
|
842 |
|
|
/* Wait until I2C bus is freed, exit if timed out. */
|
843 |
|
|
if (XIic_WaitBusFree(BaseAddress) != XST_SUCCESS) {
|
844 |
|
|
return 0;
|
845 |
|
|
}
|
846 |
|
|
|
847 |
|
|
/*
|
848 |
|
|
* Clear the latched interrupt status so that it will be updated with
|
849 |
|
|
* the new state when it changes, this must be done after the address
|
850 |
|
|
* is put in the FIFO
|
851 |
|
|
*/
|
852 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_TX_EMPTY_MASK |
|
853 |
|
|
XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_ARB_LOST_MASK);
|
854 |
|
|
|
855 |
|
|
/*
|
856 |
|
|
* Put the address into the Fifo to be sent and indicate that the
|
857 |
|
|
* operation to be performed on the bus is a write operation. Upon
|
858 |
|
|
* writing the address, a start condition is initiated. MSMS is
|
859 |
|
|
* automatically set to master when the address is written to the Fifo.
|
860 |
|
|
* If MSMS was already set, then a re-start is sent prior to the
|
861 |
|
|
* address.
|
862 |
|
|
*/
|
863 |
|
|
if(!(Address & XIIC_TX_DYN_STOP_MASK)) {
|
864 |
|
|
|
865 |
|
|
XIic_DynSend7BitAddress(BaseAddress, Address,
|
866 |
|
|
XIIC_WRITE_OPERATION);
|
867 |
|
|
} else {
|
868 |
|
|
XIic_DynSendStartStopAddress(BaseAddress, Address,
|
869 |
|
|
XIIC_WRITE_OPERATION);
|
870 |
|
|
}
|
871 |
|
|
|
872 |
|
|
/*
|
873 |
|
|
* Wait for the bus to go busy.
|
874 |
|
|
*/
|
875 |
|
|
StatusRegister = XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET);
|
876 |
|
|
|
877 |
|
|
while (( StatusRegister & XIIC_SR_BUS_BUSY_MASK) !=
|
878 |
|
|
XIIC_SR_BUS_BUSY_MASK) {
|
879 |
|
|
StatusRegister = XIic_ReadReg(BaseAddress,
|
880 |
|
|
XIIC_SR_REG_OFFSET);
|
881 |
|
|
}
|
882 |
|
|
|
883 |
|
|
/*
|
884 |
|
|
* Clear the latched interrupt status for the bus not busy bit which
|
885 |
|
|
* must be done while the bus is busy.
|
886 |
|
|
*/
|
887 |
|
|
XIic_ClearIisr(BaseAddress, XIIC_INTR_BNB_MASK);
|
888 |
|
|
|
889 |
|
|
/*
|
890 |
|
|
* Send the specified data to the device on the IIC bus specified by the
|
891 |
|
|
* the address.
|
892 |
|
|
*/
|
893 |
|
|
RemainingByteCount = DynSendData(BaseAddress, BufferPtr, ByteCount,
|
894 |
|
|
Option);
|
895 |
|
|
|
896 |
|
|
/*
|
897 |
|
|
* The send is complete return the number of bytes that was sent.
|
898 |
|
|
*/
|
899 |
|
|
return ByteCount - RemainingByteCount;
|
900 |
|
|
}
|
901 |
|
|
|
902 |
|
|
/******************************************************************************
|
903 |
|
|
*
|
904 |
|
|
* Send the specified buffer to the device that has been previously addressed
|
905 |
|
|
* on the IIC bus. This function assumes that the 7 bit address has been sent.
|
906 |
|
|
*
|
907 |
|
|
* @param BaseAddress contains the base address of the IIC Device.
|
908 |
|
|
* @param BufferPtr points to the data to be sent.
|
909 |
|
|
* @param ByteCount is the number of bytes to be sent.
|
910 |
|
|
* @param Option: XIIC_STOP = end with STOP condition, XIIC_REPEATED_START
|
911 |
|
|
* = don't end with STOP condition.
|
912 |
|
|
*
|
913 |
|
|
* @return The number of bytes remaining to be sent.
|
914 |
|
|
*
|
915 |
|
|
* @note This function does not take advantage of the transmit Fifo
|
916 |
|
|
* because it is designed for minimal code space and complexity.
|
917 |
|
|
*
|
918 |
|
|
******************************************************************************/
|
919 |
|
|
static unsigned DynSendData(UINTPTR BaseAddress, u8 *BufferPtr,
|
920 |
|
|
u8 ByteCount, u8 Option)
|
921 |
|
|
{
|
922 |
|
|
u32 IntrStatus;
|
923 |
|
|
|
924 |
|
|
while (ByteCount > 0) {
|
925 |
|
|
/*
|
926 |
|
|
* Wait for the transmit to be empty before sending any more
|
927 |
|
|
* data by polling the interrupt status register.
|
928 |
|
|
*/
|
929 |
|
|
while (1) {
|
930 |
|
|
IntrStatus = XIic_ReadIisr(BaseAddress);
|
931 |
|
|
if (IntrStatus & (XIIC_INTR_TX_ERROR_MASK |
|
932 |
|
|
XIIC_INTR_ARB_LOST_MASK |
|
933 |
|
|
XIIC_INTR_BNB_MASK)) {
|
934 |
|
|
/*
|
935 |
|
|
* Error condition (NACK or ARB Lost or BNB
|
936 |
|
|
* Error Has occurred. Clear the Control
|
937 |
|
|
* register to send a STOP condition on the Bus
|
938 |
|
|
* and return the number of bytes still to
|
939 |
|
|
* transmit.
|
940 |
|
|
*/
|
941 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
942 |
|
|
0x03);
|
943 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
944 |
|
|
0x01);
|
945 |
|
|
|
946 |
|
|
return ByteCount;
|
947 |
|
|
}
|
948 |
|
|
|
949 |
|
|
/*
|
950 |
|
|
* Check for the transmit Fifo to become Empty.
|
951 |
|
|
*/
|
952 |
|
|
if (IntrStatus & XIIC_INTR_TX_EMPTY_MASK) {
|
953 |
|
|
break;
|
954 |
|
|
}
|
955 |
|
|
}
|
956 |
|
|
|
957 |
|
|
/*
|
958 |
|
|
* Send data to Tx Fifo. If a stop condition is specified and
|
959 |
|
|
* the last byte is being sent, then set the dynamic stop bit.
|
960 |
|
|
*/
|
961 |
|
|
if ((ByteCount == 1) && (Option == XIIC_STOP)) {
|
962 |
|
|
/*
|
963 |
|
|
* The MSMS will be cleared automatically upon setting
|
964 |
|
|
* dynamic stop.
|
965 |
|
|
*/
|
966 |
|
|
XIic_WriteReg(BaseAddress, XIIC_DTR_REG_OFFSET,
|
967 |
|
|
XIIC_TX_DYN_STOP_MASK | *BufferPtr++);
|
968 |
|
|
} else {
|
969 |
|
|
XIic_WriteReg(BaseAddress, XIIC_DTR_REG_OFFSET,
|
970 |
|
|
*BufferPtr++);
|
971 |
|
|
}
|
972 |
|
|
|
973 |
|
|
/*
|
974 |
|
|
* Update the byte count to reflect the byte sent.
|
975 |
|
|
*/
|
976 |
|
|
ByteCount--;
|
977 |
|
|
}
|
978 |
|
|
|
979 |
|
|
if (Option == XIIC_STOP) {
|
980 |
|
|
/*
|
981 |
|
|
* If the Option is to release the bus after transmission of
|
982 |
|
|
* data, Wait for the bus to transition to not busy before
|
983 |
|
|
* returning, the IIC device cannot be disabled until this
|
984 |
|
|
* occurs.
|
985 |
|
|
*/
|
986 |
|
|
while (1) {
|
987 |
|
|
if (XIic_ReadIisr(BaseAddress) & XIIC_INTR_BNB_MASK) {
|
988 |
|
|
break;
|
989 |
|
|
}
|
990 |
|
|
}
|
991 |
|
|
}
|
992 |
|
|
|
993 |
|
|
return ByteCount;
|
994 |
|
|
}
|
995 |
|
|
|
996 |
|
|
/******************************************************************************
|
997 |
|
|
*
|
998 |
|
|
* Initialize the IIC core for Dynamic Functionality.
|
999 |
|
|
*
|
1000 |
|
|
* @param BaseAddress contains the base address of the IIC Device.
|
1001 |
|
|
*
|
1002 |
|
|
* @return XST_SUCCESS if Successful else XST_FAILURE.
|
1003 |
|
|
*
|
1004 |
|
|
* @note None.
|
1005 |
|
|
*
|
1006 |
|
|
******************************************************************************/
|
1007 |
|
|
int XIic_DynInit(UINTPTR BaseAddress)
|
1008 |
|
|
{
|
1009 |
|
|
u32 Status;
|
1010 |
|
|
|
1011 |
|
|
/*
|
1012 |
|
|
* Reset IIC Core.
|
1013 |
|
|
*/
|
1014 |
|
|
XIic_WriteReg(BaseAddress, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
|
1015 |
|
|
|
1016 |
|
|
/*
|
1017 |
|
|
* Set receive Fifo depth to maximum (zero based).
|
1018 |
|
|
*/
|
1019 |
|
|
XIic_WriteReg(BaseAddress, XIIC_RFD_REG_OFFSET,
|
1020 |
|
|
IIC_RX_FIFO_DEPTH - 1);
|
1021 |
|
|
|
1022 |
|
|
/*
|
1023 |
|
|
* Reset Tx Fifo.
|
1024 |
|
|
*/
|
1025 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
1026 |
|
|
XIIC_CR_TX_FIFO_RESET_MASK);
|
1027 |
|
|
|
1028 |
|
|
/*
|
1029 |
|
|
* Enable IIC Device, remove Tx Fifo reset & disable general call.
|
1030 |
|
|
*/
|
1031 |
|
|
XIic_WriteReg(BaseAddress, XIIC_CR_REG_OFFSET,
|
1032 |
|
|
XIIC_CR_ENABLE_DEVICE_MASK);
|
1033 |
|
|
|
1034 |
|
|
/*
|
1035 |
|
|
* Read status register and verify IIC Device is in initial state. Only
|
1036 |
|
|
* the Tx Fifo and Rx Fifo empty bits should be set.
|
1037 |
|
|
*/
|
1038 |
|
|
Status = XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET);
|
1039 |
|
|
if(Status == (XIIC_SR_RX_FIFO_EMPTY_MASK |
|
1040 |
|
|
XIIC_SR_TX_FIFO_EMPTY_MASK)) {
|
1041 |
|
|
return XST_SUCCESS;
|
1042 |
|
|
}
|
1043 |
|
|
|
1044 |
|
|
return XST_FAILURE;
|
1045 |
|
|
}
|
1046 |
|
|
|
1047 |
|
|
/*****************************************************************************
|
1048 |
|
|
*
|
1049 |
|
|
* This is a function which tells whether the I2C bus is busy or free.
|
1050 |
|
|
*
|
1051 |
|
|
* @param BaseAddr is the base address of the I2C core to work on.
|
1052 |
|
|
*
|
1053 |
|
|
* @return
|
1054 |
|
|
* - TRUE if the bus is busy.
|
1055 |
|
|
* - FALSE if the bus is NOT busy.
|
1056 |
|
|
*
|
1057 |
|
|
* @note None.
|
1058 |
|
|
*
|
1059 |
|
|
******************************************************************************/
|
1060 |
|
|
u32 XIic_CheckIsBusBusy(UINTPTR BaseAddress)
|
1061 |
|
|
{
|
1062 |
|
|
u32 StatusReg;
|
1063 |
|
|
|
1064 |
|
|
StatusReg = XIic_ReadReg(BaseAddress, XIIC_SR_REG_OFFSET);
|
1065 |
|
|
if (StatusReg & XIIC_SR_BUS_BUSY_MASK) {
|
1066 |
|
|
return TRUE;
|
1067 |
|
|
} else {
|
1068 |
|
|
return FALSE;
|
1069 |
|
|
}
|
1070 |
|
|
}
|
1071 |
|
|
|
1072 |
|
|
/******************************************************************************/
|
1073 |
|
|
/**
|
1074 |
|
|
* This function will wait until the I2C bus is free or timeout.
|
1075 |
|
|
*
|
1076 |
|
|
* @param BaseAddress contains the base address of the I2C device.
|
1077 |
|
|
*
|
1078 |
|
|
* @return
|
1079 |
|
|
* - XST_SUCCESS if the I2C bus was freed before the timeout.
|
1080 |
|
|
* - XST_FAILURE otherwise.
|
1081 |
|
|
*
|
1082 |
|
|
* @note None.
|
1083 |
|
|
*
|
1084 |
|
|
*******************************************************************************/
|
1085 |
|
|
u32 XIic_WaitBusFree(UINTPTR BaseAddress)
|
1086 |
|
|
{
|
1087 |
|
|
u32 BusyCount = 0;
|
1088 |
|
|
|
1089 |
|
|
while (XIic_CheckIsBusBusy(BaseAddress)) {
|
1090 |
|
|
if (BusyCount++ > 10000) {
|
1091 |
|
|
return XST_FAILURE;
|
1092 |
|
|
}
|
1093 |
|
|
usleep(100);
|
1094 |
|
|
}
|
1095 |
|
|
|
1096 |
|
|
return XST_SUCCESS;
|
1097 |
|
|
}
|
1098 |
|
|
/** @} */
|