1 /* $Id: xiic_l.c,v 1.2 2002/12/05 19:32:40 meinelte Exp $ */
2 /******************************************************************************
4 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
5 * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
6 * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
7 * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
8 * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
9 * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
10 * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
11 * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
12 * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
13 * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
14 * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
15 * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
16 * FOR A PARTICULAR PURPOSE.
18 * (c) Copyright 2002 Xilinx Inc.
19 * All rights reserved.
21 ******************************************************************************/
22 /*****************************************************************************/
27 * This file contains low-level driver functions that can be used to access the
28 * device. The user should refer to the hardware device specification for more
29 * details of the device operation.
32 * MODIFICATION HISTORY:
34 * Ver Who Date Changes
35 * ----- --- ------- -----------------------------------------------
36 * 1.01b jhl 5/13/02 First release
37 * 1.01b jhl 10/14/02 Corrected bug in the receive function, the setup of the
38 * interrupt status mask was not being done in the loop such
39 * that a read would sometimes fail on the last byte because
40 * the transmit error which should have been ignored was
41 * being used. This would leave an extra byte in the FIFO
42 * and the bus throttled such that the next operation would
43 * also fail. Also updated the receive function to not
44 * disable the device after the last byte until after the
45 * bus transitions to not busy which is more consistent
46 * with the expected behavior.
47 * 1.01c ecm 12/05/02 new rev
50 ****************************************************************************/
52 /***************************** Include Files *******************************/
54 #include "xbasic_types.h"
56 #include "xipif_v1_23_b.h"
59 /************************** Constant Definitions ***************************/
61 /**************************** Type Definitions *****************************/
64 /***************** Macros (Inline Functions) Definitions *******************/
67 /******************************************************************************
69 * This macro clears the specified interrupt in the IPIF interrupt status
70 * register. It is non-destructive in that the register is read and only the
71 * interrupt specified is cleared. Clearing an interrupt acknowledges it.
73 * @param BaseAddress contains the IPIF registers base address.
75 * @param InterruptMask contains the interrupts to be disabled
83 * Signature: void XIic_mClearIisr(u32 BaseAddress,
86 ******************************************************************************/
87 #define XIic_mClearIisr(BaseAddress, InterruptMask) \
88 XIIF_V123B_WRITE_IISR((BaseAddress), \
89 XIIF_V123B_READ_IISR(BaseAddress) & (InterruptMask))
91 /******************************************************************************
93 * This macro sends the address for a 7 bit address during both read and write
94 * operations. It takes care of the details to format the address correctly.
95 * This macro is designed to be called internally to the drivers.
97 * @param SlaveAddress contains the address of the slave to send to.
99 * @param Operation indicates XIIC_READ_OPERATION or XIIC_WRITE_OPERATION
107 * Signature: void XIic_mSend7BitAddr(u16 SlaveAddress, u8 Operation);
109 ******************************************************************************/
110 #define XIic_mSend7BitAddress(BaseAddress, SlaveAddress, Operation) \
112 u8 LocalAddr = (u8)(SlaveAddress << 1); \
113 LocalAddr = (LocalAddr & 0xFE) | (Operation); \
114 XIo_Out8(BaseAddress + XIIC_DTR_REG_OFFSET, LocalAddr); \
117 /************************** Function Prototypes ****************************/
119 static unsigned RecvData (u32 BaseAddress
, u8
* BufferPtr
,
121 static unsigned SendData (u32 BaseAddress
, u8
* BufferPtr
,
124 /************************** Variable Definitions **************************/
127 /****************************************************************************/
129 * Receive data as a master on the IIC bus. This function receives the data
130 * using polled I/O and blocks until the data has been received. It only
131 * supports 7 bit addressing and non-repeated start modes of operation. The
132 * user is responsible for ensuring the bus is not busy if multiple masters
133 * are present on the bus.
135 * @param BaseAddress contains the base address of the IIC device.
136 * @param Address contains the 7 bit IIC address of the device to send the
138 * @param BufferPtr points to the data to be sent.
139 * @param ByteCount is the number of bytes to be sent.
143 * The number of bytes received.
149 ******************************************************************************/
150 unsigned XIic_Recv (u32 BaseAddress
, u8 Address
,
151 u8
* BufferPtr
, unsigned ByteCount
)
154 unsigned RemainingByteCount
;
156 /* Tx error is enabled incase the address (7 or 10) has no device to answer
157 * with Ack. When only one byte of data, must set NO ACK before address goes
158 * out therefore Tx error must not be enabled as it will go off immediately
159 * and the Rx full interrupt will be checked. If full, then the one byte
160 * was received and the Tx error will be disabled without sending an error
163 XIic_mClearIisr (BaseAddress
,
164 XIIC_INTR_RX_FULL_MASK
| XIIC_INTR_TX_ERROR_MASK
|
165 XIIC_INTR_ARB_LOST_MASK
);
167 /* Set receive FIFO occupancy depth for 1 byte (zero based)
169 XIo_Out8 (BaseAddress
+ XIIC_RFD_REG_OFFSET
, 0);
171 /* 7 bit slave address, send the address for a read operation
172 * and set the state to indicate the address has been sent
174 XIic_mSend7BitAddress (BaseAddress
, Address
, XIIC_READ_OPERATION
);
176 /* MSMS gets set after putting data in FIFO. Start the master receive
177 * operation by setting CR Bits MSMS to Master, if the buffer is only one
178 * byte, then it should not be acknowledged to indicate the end of data
180 CntlReg
= XIIC_CR_MSMS_MASK
| XIIC_CR_ENABLE_DEVICE_MASK
;
181 if (ByteCount
== 1) {
182 CntlReg
|= XIIC_CR_NO_ACK_MASK
;
185 /* Write out the control register to start receiving data and call the
186 * function to receive each byte into the buffer
188 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
, CntlReg
);
190 /* Clear the latched interrupt status for the bus not busy bit which must
191 * be done while the bus is busy
193 XIic_mClearIisr (BaseAddress
, XIIC_INTR_BNB_MASK
);
195 /* Try to receive the data from the IIC bus */
197 RemainingByteCount
= RecvData (BaseAddress
, BufferPtr
, ByteCount
);
199 * The receive is complete, disable the IIC device and return the number of
200 * bytes that was received
202 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
, 0);
204 /* Return the number of bytes that was received */
206 return ByteCount
- RemainingByteCount
;
209 /******************************************************************************
211 * Receive the specified data from the device that has been previously addressed
212 * on the IIC bus. This function assumes that the 7 bit address has been sent
213 * and it should wait for the transmit of the address to complete.
215 * @param BaseAddress contains the base address of the IIC device.
216 * @param BufferPtr points to the buffer to hold the data that is received.
217 * @param ByteCount is the number of bytes to be received.
221 * The number of bytes remaining to be received.
225 * This function does not take advantage of the receive FIFO because it is
226 * designed for minimal code space and complexity. It contains loops that
227 * that could cause the function not to return if the hardware is not working.
229 * This function assumes that the calling function will disable the IIC device
230 * after this function returns.
232 ******************************************************************************/
233 static unsigned RecvData (u32 BaseAddress
, u8
* BufferPtr
, unsigned ByteCount
)
239 /* Attempt to receive the specified number of bytes on the IIC bus */
241 while (ByteCount
> 0) {
242 /* Setup the mask to use for checking errors because when receiving one
243 * byte OR the last byte of a multibyte message an error naturally
244 * occurs when the no ack is done to tell the slave the last byte
246 if (ByteCount
== 1) {
248 XIIC_INTR_ARB_LOST_MASK
| XIIC_INTR_BNB_MASK
;
251 XIIC_INTR_ARB_LOST_MASK
|
252 XIIC_INTR_TX_ERROR_MASK
| XIIC_INTR_BNB_MASK
;
255 /* Wait for the previous transmit and the 1st receive to complete
256 * by checking the interrupt status register of the IPIF
259 IntrStatus
= XIIF_V123B_READ_IISR (BaseAddress
);
260 if (IntrStatus
& XIIC_INTR_RX_FULL_MASK
) {
263 /* Check the transmit error after the receive full because when
264 * sending only one byte transmit error will occur because of the
265 * no ack to indicate the end of the data
267 if (IntrStatus
& IntrStatusMask
) {
272 CntlReg
= XIo_In8 (BaseAddress
+ XIIC_CR_REG_OFFSET
);
274 /* Special conditions exist for the last two bytes so check for them
275 * Note that the control register must be setup for these conditions
276 * before the data byte which was already received is read from the
277 * receive FIFO (while the bus is throttled
279 if (ByteCount
== 1) {
280 /* For the last data byte, it has already been read and no ack
281 * has been done, so clear MSMS while leaving the device enabled
282 * so it can get off the IIC bus appropriately with a stop.
284 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
,
285 XIIC_CR_ENABLE_DEVICE_MASK
);
288 /* Before the last byte is received, set NOACK to tell the slave IIC
289 * device that it is the end, this must be done before reading the byte
292 if (ByteCount
== 2) {
293 /* Write control reg with NO ACK allowing last byte to
294 * have the No ack set to indicate to slave last byte read.
296 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
,
297 CntlReg
| XIIC_CR_NO_ACK_MASK
);
300 /* Read in data from the FIFO and unthrottle the bus such that the
301 * next byte is read from the IIC bus
303 *BufferPtr
++ = XIo_In8 (BaseAddress
+ XIIC_DRR_REG_OFFSET
);
305 /* Clear the latched interrupt status so that it will be updated with
306 * the new state when it changes, this must be done after the receive
309 XIic_mClearIisr (BaseAddress
, XIIC_INTR_RX_FULL_MASK
|
310 XIIC_INTR_TX_ERROR_MASK
|
311 XIIC_INTR_ARB_LOST_MASK
);
315 /* Wait for the bus to transition to not busy before returning, the IIC
316 * device cannot be disabled until this occurs. It should transition as
317 * the MSMS bit of the control register was cleared before the last byte
318 * was read from the FIFO.
321 if (XIIF_V123B_READ_IISR (BaseAddress
) & XIIC_INTR_BNB_MASK
) {
329 /****************************************************************************/
331 * Send data as a master on the IIC bus. This function sends the data
332 * using polled I/O and blocks until the data has been sent. It only supports
333 * 7 bit addressing and non-repeated start modes of operation. The user is
334 * responsible for ensuring the bus is not busy if multiple masters are present
337 * @param BaseAddress contains the base address of the IIC device.
338 * @param Address contains the 7 bit IIC address of the device to send the
340 * @param BufferPtr points to the data to be sent.
341 * @param ByteCount is the number of bytes to be sent.
345 * The number of bytes sent.
351 ******************************************************************************/
352 unsigned XIic_Send (u32 BaseAddress
, u8 Address
,
353 u8
* BufferPtr
, unsigned ByteCount
)
355 unsigned RemainingByteCount
;
357 /* Put the address into the FIFO to be sent and indicate that the operation
358 * to be performed on the bus is a write operation
360 XIic_mSend7BitAddress (BaseAddress
, Address
, XIIC_WRITE_OPERATION
);
362 /* Clear the latched interrupt status so that it will be updated with the
363 * new state when it changes, this must be done after the address is put
366 XIic_mClearIisr (BaseAddress
, XIIC_INTR_TX_EMPTY_MASK
|
367 XIIC_INTR_TX_ERROR_MASK
| XIIC_INTR_ARB_LOST_MASK
);
369 /* MSMS must be set after putting data into transmit FIFO, indicate the
370 * direction is transmit, this device is master and enable the IIC device
372 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
,
373 XIIC_CR_MSMS_MASK
| XIIC_CR_DIR_IS_TX_MASK
|
374 XIIC_CR_ENABLE_DEVICE_MASK
);
376 /* Clear the latched interrupt
377 * status for the bus not busy bit which must be done while the bus is busy
379 XIic_mClearIisr (BaseAddress
, XIIC_INTR_BNB_MASK
);
381 /* Send the specified data to the device on the IIC bus specified by the
384 RemainingByteCount
= SendData (BaseAddress
, BufferPtr
, ByteCount
);
387 * The send is complete, disable the IIC device and return the number of
388 * bytes that was sent
390 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
, 0);
392 return ByteCount
- RemainingByteCount
;
395 /******************************************************************************
397 * Send the specified buffer to the device that has been previously addressed
398 * on the IIC bus. This function assumes that the 7 bit address has been sent
399 * and it should wait for the transmit of the address to complete.
401 * @param BaseAddress contains the base address of the IIC device.
402 * @param BufferPtr points to the data to be sent.
403 * @param ByteCount is the number of bytes to be sent.
407 * The number of bytes remaining to be sent.
411 * This function does not take advantage of the transmit FIFO because it is
412 * designed for minimal code space and complexity. It contains loops that
413 * that could cause the function not to return if the hardware is not working.
415 ******************************************************************************/
416 static unsigned SendData (u32 BaseAddress
, u8
* BufferPtr
, unsigned ByteCount
)
420 /* Send the specified number of bytes in the specified buffer by polling
421 * the device registers and blocking until complete
423 while (ByteCount
> 0) {
424 /* Wait for the transmit to be empty before sending any more data
425 * by polling the interrupt status register
428 IntrStatus
= XIIF_V123B_READ_IISR (BaseAddress
);
430 if (IntrStatus
& (XIIC_INTR_TX_ERROR_MASK
|
431 XIIC_INTR_ARB_LOST_MASK
|
432 XIIC_INTR_BNB_MASK
)) {
436 if (IntrStatus
& XIIC_INTR_TX_EMPTY_MASK
) {
440 /* If there is more than one byte to send then put the next byte to send
441 * into the transmit FIFO
444 XIo_Out8 (BaseAddress
+ XIIC_DTR_REG_OFFSET
,
447 /* Set the stop condition before sending the last byte of data so that
448 * the stop condition will be generated immediately following the data
449 * This is done by clearing the MSMS bit in the control register.
451 XIo_Out8 (BaseAddress
+ XIIC_CR_REG_OFFSET
,
452 XIIC_CR_ENABLE_DEVICE_MASK
|
453 XIIC_CR_DIR_IS_TX_MASK
);
455 /* Put the last byte to send in the transmit FIFO */
457 XIo_Out8 (BaseAddress
+ XIIC_DTR_REG_OFFSET
,
461 /* Clear the latched interrupt status register and this must be done after
462 * the transmit FIFO has been written to or it won't clear
464 XIic_mClearIisr (BaseAddress
, XIIC_INTR_TX_EMPTY_MASK
);
466 /* Update the byte count to reflect the byte sent and clear the latched
467 * interrupt status so it will be updated for the new state
472 /* Wait for the bus to transition to not busy before returning, the IIC
473 * device cannot be disabled until this occurs.
474 * Note that this is different from a receive operation because the stop
475 * condition causes the bus to go not busy.
478 if (XIIF_V123B_READ_IISR (BaseAddress
) & XIIC_INTR_BNB_MASK
) {