1 |
4 |
DFC |
/******************************************************************************
|
2 |
|
|
*
|
3 |
|
|
* Copyright (C) 2002 - 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 xiic_slave.c
|
36 |
|
|
* @addtogroup iic_v3_1
|
37 |
|
|
* @{
|
38 |
|
|
*
|
39 |
|
|
* Contains slave functions for the XIic component. This file is necessary when
|
40 |
|
|
* slave operations, sending and receiving data as a slave on the IIC bus,
|
41 |
|
|
* are desired.
|
42 |
|
|
*
|
43 |
|
|
* <pre>
|
44 |
|
|
* MODIFICATION HISTORY:
|
45 |
|
|
*
|
46 |
|
|
* Ver Who Date Changes
|
47 |
|
|
* ----- --- ------- -----------------------------------------------
|
48 |
|
|
* 1.01b jhl 3/26/02 repartioned the driver
|
49 |
|
|
* 1.01c ecm 12/05/02 new rev
|
50 |
|
|
* 1.13a wgr 03/22/07 Converted to new coding style.
|
51 |
|
|
* 1.15a ktn 03/18/09 Minor changes to comply to Doxygen
|
52 |
|
|
* 2.00a ktn 10/22/09 Converted all register accesses to 32 bit access.
|
53 |
|
|
* Updated to use the HAL APIs/macros.
|
54 |
|
|
* Removed the macro XIic_mEnterCriticalRegion and used
|
55 |
|
|
* XIic_IntrGlobalDisable int its place.
|
56 |
|
|
* Removed the macro XIic_mExitCriticalRegion and used
|
57 |
|
|
* XIic_IntrGlobalEnable in its place.
|
58 |
|
|
* Some of the macros have been renamed to remove _m from
|
59 |
|
|
* the name and some of the macros have been renamed to be
|
60 |
|
|
* consistent, see the xiic_i.h and xiic_l.h files for further
|
61 |
|
|
* information
|
62 |
|
|
* 2.03a rkv 01/25/11 Updated in NAAS interrupt handler to support data
|
63 |
|
|
* recieved less than FIFO size prior to NAAS interrupt.
|
64 |
|
|
* Fixed for CR590212.
|
65 |
|
|
* 2.04a sdm 07/22/11 Added IsSlaveSetAckOff flag to the instance structure.
|
66 |
|
|
* The IsSlaveSetAckOff is set when the Slave has set the
|
67 |
|
|
* Ack Off in the RecvSlaveData function and is cleared in the
|
68 |
|
|
* NotAddrAsSlaveHandler when the master has released the
|
69 |
|
|
* bus. This flag is to be used by slave applications for
|
70 |
|
|
* recovering when it has gone out of sync with the master.
|
71 |
|
|
* CR 615004.
|
72 |
|
|
* 3.1 adk 01/08/15 When configured as a slave return the actual number of
|
73 |
|
|
* bytes have been received/sent by the Master
|
74 |
|
|
* to the user callback (CR: 828504).
|
75 |
|
|
* </pre>
|
76 |
|
|
*
|
77 |
|
|
****************************************************************************/
|
78 |
|
|
|
79 |
|
|
/***************************** Include Files *******************************/
|
80 |
|
|
|
81 |
|
|
#include "xiic.h"
|
82 |
|
|
#include "xiic_i.h"
|
83 |
|
|
|
84 |
|
|
/************************** Constant Definitions ***************************/
|
85 |
|
|
|
86 |
|
|
/**************************** Type Definitions *****************************/
|
87 |
|
|
|
88 |
|
|
/***************** Macros (Inline Functions) Definitions *******************/
|
89 |
|
|
|
90 |
|
|
/************************** Function Prototypes ****************************/
|
91 |
|
|
|
92 |
|
|
static void AddrAsSlaveHandler(XIic *InstancePtr);
|
93 |
|
|
static void NotAddrAsSlaveHandler(XIic *InstancePtr);
|
94 |
|
|
static void RecvSlaveData(XIic *InstancePtr);
|
95 |
|
|
static void SendSlaveData(XIic *InstancePtr);
|
96 |
|
|
|
97 |
|
|
/************************** Variable Definitions **************************/
|
98 |
|
|
|
99 |
|
|
/*****************************************************************************/
|
100 |
|
|
/**
|
101 |
|
|
*
|
102 |
|
|
* This function includes slave code such that slave events will be processsed.
|
103 |
|
|
* It is necessary to allow slave code to be optional to reduce the size of
|
104 |
|
|
* the driver. This function may be called at any time but must be prior to
|
105 |
|
|
* being selected as a slave on the IIC bus. This function may be called prior
|
106 |
|
|
* to the Cfg_Initialize() function and must be called before any functions in
|
107 |
|
|
* this file are called.
|
108 |
|
|
*
|
109 |
|
|
* @param None.
|
110 |
|
|
*
|
111 |
|
|
* @return None.
|
112 |
|
|
*
|
113 |
|
|
* @note None.
|
114 |
|
|
*
|
115 |
|
|
******************************************************************************/
|
116 |
|
|
void XIic_SlaveInclude()
|
117 |
|
|
{
|
118 |
|
|
XIic_AddrAsSlaveFuncPtr = AddrAsSlaveHandler;
|
119 |
|
|
XIic_NotAddrAsSlaveFuncPtr = NotAddrAsSlaveHandler;
|
120 |
|
|
XIic_RecvSlaveFuncPtr = RecvSlaveData;
|
121 |
|
|
XIic_SendSlaveFuncPtr = SendSlaveData;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/*****************************************************************************/
|
125 |
|
|
/**
|
126 |
|
|
*
|
127 |
|
|
* This function sends data as a slave on the IIC bus and should not be called
|
128 |
|
|
* until an event has occurred that indicates the device has been selected by
|
129 |
|
|
* a master attempting read from the slave (XII_MASTER_READ_EVENT).
|
130 |
|
|
*
|
131 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
132 |
|
|
* @param TxMsgPtr is a pointer to the data to be transmitted.
|
133 |
|
|
* @param ByteCount is the number of message bytes to be sent.
|
134 |
|
|
*
|
135 |
|
|
* @return
|
136 |
|
|
* - XST_SUCCESS indicates the message transmission has been
|
137 |
|
|
* initiated.
|
138 |
|
|
* - XST_IIC_NOT_SLAVE indicates the device has not been
|
139 |
|
|
* selected to be a slave on the IIC bus such that data
|
140 |
|
|
* cannot be sent.
|
141 |
|
|
*
|
142 |
|
|
* @note None.
|
143 |
|
|
*
|
144 |
|
|
******************************************************************************/
|
145 |
|
|
int XIic_SlaveSend(XIic *InstancePtr, u8 *TxMsgPtr, int ByteCount)
|
146 |
|
|
{
|
147 |
|
|
u32 IntrStatus;
|
148 |
|
|
u32 Status;
|
149 |
|
|
|
150 |
|
|
/*
|
151 |
|
|
* If the device is not a slave on the IIC bus then indicate an error
|
152 |
|
|
* because data cannot be sent on the bus.
|
153 |
|
|
*/
|
154 |
|
|
Status = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_SR_REG_OFFSET);
|
155 |
|
|
if ((Status & XIIC_SR_ADDR_AS_SLAVE_MASK) == 0) {
|
156 |
|
|
return XST_IIC_NOT_SLAVE;
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
XIic_IntrGlobalDisable(InstancePtr->BaseAddress);
|
160 |
|
|
|
161 |
|
|
/*
|
162 |
|
|
* Save message state and invalidate the receive buffer pointer to
|
163 |
|
|
* indicate the direction of transfer is sending.
|
164 |
|
|
*/
|
165 |
|
|
InstancePtr->SendByteCount = ByteCount;
|
166 |
|
|
InstancePtr->SendBufferPtr = TxMsgPtr;
|
167 |
|
|
InstancePtr->RecvBufferPtr = NULL;
|
168 |
|
|
|
169 |
|
|
/*
|
170 |
|
|
* Start sending the specified data and then interrupt processing will
|
171 |
|
|
* complete it.
|
172 |
|
|
*/
|
173 |
|
|
XIic_TransmitFifoFill(InstancePtr, XIIC_SLAVE_ROLE);
|
174 |
|
|
|
175 |
|
|
/* Clear any pending Tx empty, Tx Error and interrupt then enable them.
|
176 |
|
|
* The Tx error interrupt indicates when the message is complete.
|
177 |
|
|
* If data remaining to be sent, clear and enable Tx � empty interrupt.
|
178 |
|
|
*/
|
179 |
|
|
IntrStatus = (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK);
|
180 |
|
|
if (InstancePtr->SendByteCount > 1) {
|
181 |
|
|
IntrStatus |= XIIC_INTR_TX_HALF_MASK;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/*
|
185 |
|
|
* Clear the interrupts in the status and then enable them and then
|
186 |
|
|
* exit the critical region.
|
187 |
|
|
*/
|
188 |
|
|
XIic_ClearEnableIntr(InstancePtr->BaseAddress, IntrStatus);
|
189 |
|
|
|
190 |
|
|
XIic_IntrGlobalEnable(InstancePtr->BaseAddress);
|
191 |
|
|
|
192 |
|
|
return XST_SUCCESS;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/*****************************************************************************/
|
196 |
|
|
/**
|
197 |
|
|
*
|
198 |
|
|
* This function sends data as a slave on the IIC bus and should not be called
|
199 |
|
|
* until an event has occurred that indicates the device has been selected by
|
200 |
|
|
* a master attempting read from the slave (XII_MASTER_READ_EVENT).
|
201 |
|
|
*
|
202 |
|
|
* If more data is received than specified a No Acknowledge will be sent to
|
203 |
|
|
* signal the Master to stop sending data. Any received data is read to prevent
|
204 |
|
|
* the slave device from throttling the bus.
|
205 |
|
|
*
|
206 |
|
|
* @param InstancePtr is a pointer to the Iic instance to be worked on.
|
207 |
|
|
* @param RxMsgPtr is a pointer to the data to be transmitted.
|
208 |
|
|
* @param ByteCount is the number of message bytes to be sent.
|
209 |
|
|
*
|
210 |
|
|
* @return
|
211 |
|
|
* - XST_SUCCESS indicates the message transmission has been
|
212 |
|
|
* initiated.
|
213 |
|
|
* - XST_IIC_NOT_SLAVE indicates the device has not been selected
|
214 |
|
|
* to be a slave on the IIC bus such that data cannot be received.
|
215 |
|
|
*
|
216 |
|
|
* @internal
|
217 |
|
|
*
|
218 |
|
|
* The master signals the message completion differently depending on the
|
219 |
|
|
* repeated start options.
|
220 |
|
|
*
|
221 |
|
|
* When the master is not using repeated start:
|
222 |
|
|
* - Not Adressed As Slave NAAS interrupt signals the master has sent a stop
|
223 |
|
|
* condition and is no longer sending data. This doesn't imply that the master
|
224 |
|
|
* will not send a No Ack. It covers when the master fails to send No
|
225 |
|
|
* Ackowledge before releasing the bus.
|
226 |
|
|
* - Tx Error interrupt signals end of message.
|
227 |
|
|
*
|
228 |
|
|
* When the master is using repeated start:
|
229 |
|
|
* - the Tx Error interrupt signals the master finished sending the msg.
|
230 |
|
|
* - NAAS interrupt will not signal when message is complete as the
|
231 |
|
|
* master may want to write or read another message with this device.
|
232 |
|
|
*
|
233 |
|
|
* To prevent throttling, the slave must contine to read discard the data
|
234 |
|
|
* when the receive buffer is full. When unexpected bytes are received, No Ack
|
235 |
|
|
* must be set and the Rx buffer continually read until either NAAS
|
236 |
|
|
* or Bus Not Busy BND interrupt signals the master is no longer
|
237 |
|
|
* interacting with this slave. At this point the Ack is set to ON allowing
|
238 |
|
|
* this device to acknowlefge the an address sent to it for the next
|
239 |
|
|
* slave message.
|
240 |
|
|
*
|
241 |
|
|
* The slave will always receive 1 byte before the bus is throttled causing a
|
242 |
|
|
* receive pending interrupt before this routine is executed. After one byte
|
243 |
|
|
* the bus will throttle. The depth is set to the proper amount immediatelly
|
244 |
|
|
* allowing the master to send more bytes and then to again throttle, but at the
|
245 |
|
|
* proper fifo depth. The interrupt is a level. Clearing and enabling will cause
|
246 |
|
|
* the Rx interrupt to pend at the correct level.
|
247 |
|
|
*
|
248 |
|
|
******************************************************************************/
|
249 |
|
|
int XIic_SlaveRecv(XIic *InstancePtr, u8 *RxMsgPtr, int ByteCount)
|
250 |
|
|
{
|
251 |
|
|
u32 Status;
|
252 |
|
|
|
253 |
|
|
/*
|
254 |
|
|
* If the device is not a slave on the IIC bus then indicate an error
|
255 |
|
|
* because data cannot be received on the bus.
|
256 |
|
|
*/
|
257 |
|
|
Status = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_SR_REG_OFFSET);
|
258 |
|
|
if ((Status & XIIC_SR_ADDR_AS_SLAVE_MASK) == 0) {
|
259 |
|
|
return XST_IIC_NOT_SLAVE;
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
XIic_IntrGlobalDisable(InstancePtr->BaseAddress);
|
263 |
|
|
|
264 |
|
|
/*
|
265 |
|
|
* Save message state and invalidate the send buffer pointer to indicate
|
266 |
|
|
* the direction of transfer is receive.
|
267 |
|
|
*/
|
268 |
|
|
InstancePtr->RecvByteCount = ByteCount;
|
269 |
|
|
InstancePtr->RecvBufferPtr = RxMsgPtr;
|
270 |
|
|
InstancePtr->SendBufferPtr = NULL;
|
271 |
|
|
|
272 |
|
|
/*
|
273 |
|
|
* Set receive FIFO occupancy depth so the Rx interrupt will occur
|
274 |
|
|
* when all bytes received or if more bytes than will fit in FIFO,
|
275 |
|
|
* set to max depth.
|
276 |
|
|
*/
|
277 |
|
|
if (ByteCount > IIC_RX_FIFO_DEPTH) {
|
278 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress, XIIC_RFD_REG_OFFSET,
|
279 |
|
|
IIC_RX_FIFO_DEPTH - 1);
|
280 |
|
|
} else {
|
281 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress, XIIC_RFD_REG_OFFSET,
|
282 |
|
|
ByteCount - 1);
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/*
|
286 |
|
|
* Clear and enable receive full interrupt except when the bytes to
|
287 |
|
|
* receive is only 1, don't clear interrupt as it is the only one your
|
288 |
|
|
* going to get.
|
289 |
|
|
*/
|
290 |
|
|
if (ByteCount > 1) {
|
291 |
|
|
XIic_ClearIntr(InstancePtr->BaseAddress,
|
292 |
|
|
XIIC_INTR_RX_FULL_MASK);
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
XIic_EnableIntr(InstancePtr->BaseAddress, XIIC_INTR_RX_FULL_MASK);
|
296 |
|
|
|
297 |
|
|
XIic_IntrGlobalEnable(InstancePtr->BaseAddress);
|
298 |
|
|
|
299 |
|
|
return XST_SUCCESS;
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
/*****************************************************************************/
|
303 |
|
|
/**
|
304 |
|
|
*
|
305 |
|
|
* This function is called when the IIC device is Addressed As a Slave (AAS).
|
306 |
|
|
* This occurs when another device on the bus, a master, has addressed this
|
307 |
|
|
* device to receive a message.
|
308 |
|
|
*
|
309 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
310 |
|
|
*
|
311 |
|
|
* @return None.
|
312 |
|
|
*
|
313 |
|
|
* @note None.
|
314 |
|
|
*
|
315 |
|
|
******************************************************************************/
|
316 |
|
|
static void AddrAsSlaveHandler(XIic *InstancePtr)
|
317 |
|
|
{
|
318 |
|
|
u32 Status;
|
319 |
|
|
int CallValue;
|
320 |
|
|
|
321 |
|
|
/*
|
322 |
|
|
* Disable AAS interrupt to clear the interrupt condition since this is
|
323 |
|
|
* interrupt does not go away and enable the not addressed as a slave
|
324 |
|
|
* interrrupt to tell when the master stops data transfer.
|
325 |
|
|
*/
|
326 |
|
|
XIic_DisableIntr(InstancePtr->BaseAddress, XIIC_INTR_AAS_MASK);
|
327 |
|
|
XIic_ClearEnableIntr(InstancePtr->BaseAddress, XIIC_INTR_NAAS_MASK);
|
328 |
|
|
|
329 |
|
|
/*
|
330 |
|
|
* Determine how the slave is being addressed and call the handler to
|
331 |
|
|
* notify the user of the event.
|
332 |
|
|
*/
|
333 |
|
|
Status = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_SR_REG_OFFSET);
|
334 |
|
|
|
335 |
|
|
/*
|
336 |
|
|
* Determine if the master is trying to perform a read or write
|
337 |
|
|
* operation.
|
338 |
|
|
*/
|
339 |
|
|
if (Status & XIIC_SR_MSTR_RDING_SLAVE_MASK) {
|
340 |
|
|
CallValue = XII_MASTER_READ_EVENT;
|
341 |
|
|
} else {
|
342 |
|
|
CallValue = XII_MASTER_WRITE_EVENT;
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
/*
|
346 |
|
|
* If being addressed with general call also indicate to handler.
|
347 |
|
|
*/
|
348 |
|
|
if (Status & XIIC_SR_GEN_CALL_MASK) {
|
349 |
|
|
CallValue |= XII_GENERAL_CALL_EVENT;
|
350 |
|
|
}
|
351 |
|
|
|
352 |
|
|
InstancePtr->StatusHandler(InstancePtr->StatusCallBackRef, CallValue);
|
353 |
|
|
return;
|
354 |
|
|
}
|
355 |
|
|
|
356 |
|
|
/*****************************************************************************/
|
357 |
|
|
/**
|
358 |
|
|
*
|
359 |
|
|
* This function is called when the IIC device receives Not Addressed As Slave
|
360 |
|
|
* (NAAS) interrupt which indicates that the master has released the bus implying
|
361 |
|
|
* a data transfer is complete.
|
362 |
|
|
*
|
363 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
364 |
|
|
*
|
365 |
|
|
* @return None.
|
366 |
|
|
*
|
367 |
|
|
* @note None.
|
368 |
|
|
*
|
369 |
|
|
******************************************************************************/
|
370 |
|
|
static void NotAddrAsSlaveHandler(XIic *InstancePtr)
|
371 |
|
|
{
|
372 |
|
|
u32 Status;
|
373 |
|
|
u32 CntlReg;
|
374 |
|
|
u8 BytesToRead;
|
375 |
|
|
u8 LoopCnt;
|
376 |
|
|
u32 TxFifoOcy;
|
377 |
|
|
|
378 |
|
|
/*
|
379 |
|
|
* Disable NAAS so that the condition will not continue to interrupt
|
380 |
|
|
* and enable the addressed as slave interrupt to know when a master
|
381 |
|
|
* selects a slave on the bus.
|
382 |
|
|
*/
|
383 |
|
|
XIic_DisableIntr(InstancePtr->BaseAddress, XIIC_INTR_NAAS_MASK);
|
384 |
|
|
XIic_ClearEnableIntr(InstancePtr->BaseAddress, XIIC_INTR_AAS_MASK);
|
385 |
|
|
|
386 |
|
|
/*
|
387 |
|
|
* In the slave transmitter case pass the actual number of
|
388 |
|
|
* bytes being recievd by the master to the user callback.
|
389 |
|
|
*/
|
390 |
|
|
Status = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_SR_REG_OFFSET);
|
391 |
|
|
TxFifoOcy = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_TFO_REG_OFFSET);
|
392 |
|
|
if (!(Status & XIIC_SR_TX_FIFO_EMPTY_MASK)) {
|
393 |
|
|
InstancePtr->SendByteCount = InstancePtr->Stats.SendBytes -
|
394 |
|
|
(TxFifoOcy+1) ;
|
395 |
|
|
} else {
|
396 |
|
|
InstancePtr->SendByteCount = InstancePtr->Stats.SendBytes;
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
/*
|
401 |
|
|
* Flush Tx FIFO by toggling TxFIFOResetBit. FIFO runs normally at 0
|
402 |
|
|
* Do this incase needed to Tx FIFO with more than expected if what
|
403 |
|
|
* was set to Tx was less than what the Master expected - read more
|
404 |
|
|
* from this slave so FIFO had junk in it.
|
405 |
|
|
*/
|
406 |
|
|
XIic_FlushTxFifo(InstancePtr);
|
407 |
|
|
|
408 |
|
|
/*
|
409 |
|
|
* NAAS interrupt was asserted but received data in recieve FIFO is
|
410 |
|
|
* less than Rc_FIFO_PIRQ to assert an receive full interrupt,in this
|
411 |
|
|
* condition as data recieved is valid we have to read data before FIFO
|
412 |
|
|
* flush.
|
413 |
|
|
*/
|
414 |
|
|
Status = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_SR_REG_OFFSET);
|
415 |
|
|
|
416 |
|
|
if (!(Status & XIIC_SR_RX_FIFO_EMPTY_MASK)) {
|
417 |
|
|
BytesToRead = (XIic_ReadReg(InstancePtr->BaseAddress,
|
418 |
|
|
XIIC_RFO_REG_OFFSET)) + 1;
|
419 |
|
|
if (InstancePtr->RecvByteCount > BytesToRead) {
|
420 |
|
|
|
421 |
|
|
for (LoopCnt = 0; LoopCnt < BytesToRead; LoopCnt++) {
|
422 |
|
|
XIic_ReadRecvByte(InstancePtr);
|
423 |
|
|
}
|
424 |
|
|
}
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
/*
|
428 |
|
|
* Flush Rx FIFO should slave Rx had a problem, sent No ack but
|
429 |
|
|
* still received a few bytes. Should the slave receive have disabled
|
430 |
|
|
* acknowledgement, clear Rx FIFO.
|
431 |
|
|
*/
|
432 |
|
|
XIic_FlushRxFifo(InstancePtr);
|
433 |
|
|
|
434 |
|
|
/*
|
435 |
|
|
* Set FIFO occupancy depth = 1 so that the first byte will throttle
|
436 |
|
|
* next recieve msg.
|
437 |
|
|
*/
|
438 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress, XIIC_RFD_REG_OFFSET, 0);
|
439 |
|
|
|
440 |
|
|
/*
|
441 |
|
|
* Should the slave receive have disabled acknowledgement,
|
442 |
|
|
* enable to allow acknowledgment for receipt of our address to
|
443 |
|
|
* again be used as a slave.
|
444 |
|
|
*/
|
445 |
|
|
CntlReg = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_CR_REG_OFFSET);
|
446 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress, XIIC_CR_REG_OFFSET,
|
447 |
|
|
(CntlReg & ~XIIC_CR_NO_ACK_MASK));
|
448 |
|
|
|
449 |
|
|
InstancePtr->IsSlaveSetAckOff = FALSE;
|
450 |
|
|
|
451 |
|
|
/*
|
452 |
|
|
* Which callback depends on messaging direction, the buffer pointer NOT
|
453 |
|
|
* being used indicates the direction of data transfer.
|
454 |
|
|
*/
|
455 |
|
|
Status = XIic_ReadIier(InstancePtr->BaseAddress);
|
456 |
|
|
if (InstancePtr->RecvBufferPtr == NULL) {
|
457 |
|
|
/*
|
458 |
|
|
* Slave was sending data so disable all transmit interrupts and
|
459 |
|
|
* call the callback handler to indicate the transfer is
|
460 |
|
|
* complete.
|
461 |
|
|
*/
|
462 |
|
|
XIic_WriteIier(InstancePtr->BaseAddress,
|
463 |
|
|
(Status & ~XIIC_TX_INTERRUPTS));
|
464 |
|
|
InstancePtr->SendHandler(InstancePtr->SendCallBackRef,
|
465 |
|
|
InstancePtr->SendByteCount);
|
466 |
|
|
}
|
467 |
|
|
else {
|
468 |
|
|
/*
|
469 |
|
|
* Slave was receiving data so disable receive full interrupt
|
470 |
|
|
* and call the callback handler to notify the transfer is
|
471 |
|
|
* complete.
|
472 |
|
|
*/
|
473 |
|
|
XIic_WriteIier(InstancePtr->BaseAddress,
|
474 |
|
|
(Status & ~XIIC_INTR_RX_FULL_MASK));
|
475 |
|
|
InstancePtr->RecvHandler(InstancePtr->RecvCallBackRef,
|
476 |
|
|
InstancePtr->RecvByteCount);
|
477 |
|
|
}
|
478 |
|
|
InstancePtr->RecvByteCount = 0;
|
479 |
|
|
return;
|
480 |
|
|
}
|
481 |
|
|
|
482 |
|
|
/*****************************************************************************/
|
483 |
|
|
/**
|
484 |
|
|
*
|
485 |
|
|
* This function handles data received from the IIC bus as a slave.
|
486 |
|
|
*
|
487 |
|
|
* When the slave expects more than the master has to send, the slave will stall
|
488 |
|
|
* waiting for data.
|
489 |
|
|
*
|
490 |
|
|
* When more data is received than data expected a Nack is done to signal master
|
491 |
|
|
* to stop sending data. The excess data is discarded to prevent bus throttling.
|
492 |
|
|
*
|
493 |
|
|
* The buffer may be full and the master continues to send data if the master
|
494 |
|
|
* and slave have different message lengths. This condition is handled by sending
|
495 |
|
|
* No Ack to the master and reading Rx data until the master stops sending data
|
496 |
|
|
* to prevent but throttling from locking up the bus. To ever receive as a slave
|
497 |
|
|
* again, must know when to renable bus ACKs. NAAS is used to detect when the
|
498 |
|
|
* master is finished sending messages for any mode.
|
499 |
|
|
|
500 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
501 |
|
|
*
|
502 |
|
|
* @return None.
|
503 |
|
|
*
|
504 |
|
|
* @note None.
|
505 |
|
|
*
|
506 |
|
|
******************************************************************************/
|
507 |
|
|
static void RecvSlaveData(XIic *InstancePtr)
|
508 |
|
|
{
|
509 |
|
|
u32 CntlReg;
|
510 |
|
|
u8 BytesToRead;
|
511 |
|
|
u8 LoopCnt;
|
512 |
|
|
u8 Temp;
|
513 |
|
|
|
514 |
|
|
/*
|
515 |
|
|
* When receive buffer has no room for the receive data discard it.
|
516 |
|
|
*/
|
517 |
|
|
if (InstancePtr->RecvByteCount == 0) {
|
518 |
|
|
/*
|
519 |
|
|
* Set ACKnowlege OFF to signal master to stop sending data.
|
520 |
|
|
*/
|
521 |
|
|
CntlReg = XIic_ReadReg(InstancePtr->BaseAddress,
|
522 |
|
|
XIIC_CR_REG_OFFSET);
|
523 |
|
|
CntlReg |= XIIC_CR_NO_ACK_MASK;
|
524 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress, XIIC_CR_REG_OFFSET,
|
525 |
|
|
CntlReg);
|
526 |
|
|
|
527 |
|
|
/*
|
528 |
|
|
* Set a Flag to indicate that the Slave has set the ACK Off.
|
529 |
|
|
*/
|
530 |
|
|
InstancePtr->IsSlaveSetAckOff = TRUE;
|
531 |
|
|
|
532 |
|
|
|
533 |
|
|
/*
|
534 |
|
|
* Clear excess received data to prevent bus throttling and set
|
535 |
|
|
* receive FIFO occupancy to throttle at the 1st byte received.
|
536 |
|
|
*/
|
537 |
|
|
XIic_FlushRxFifo(InstancePtr);
|
538 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress,
|
539 |
|
|
XIIC_RFD_REG_OFFSET, 0);
|
540 |
|
|
|
541 |
|
|
return;
|
542 |
|
|
}
|
543 |
|
|
/*
|
544 |
|
|
* Use occupancy count to determine how many bytes to read from the
|
545 |
|
|
* FIFO, count is zero based so add 1, read that number of bytes from
|
546 |
|
|
* the FIFO.
|
547 |
|
|
*/
|
548 |
|
|
BytesToRead = (XIic_ReadReg(InstancePtr->BaseAddress,
|
549 |
|
|
XIIC_RFO_REG_OFFSET)) + 1;
|
550 |
|
|
for (LoopCnt = 0; LoopCnt < BytesToRead; LoopCnt++) {
|
551 |
|
|
XIic_ReadRecvByte(InstancePtr);
|
552 |
|
|
}
|
553 |
|
|
|
554 |
|
|
/*
|
555 |
|
|
* Set receive FIFO depth for the number of bytes to be received such
|
556 |
|
|
* that a receive interrupt will occur, the count is 0 based, the
|
557 |
|
|
* last byte of the message has to be received seperately to ack the
|
558 |
|
|
* message.
|
559 |
|
|
*/
|
560 |
|
|
if (InstancePtr->RecvByteCount > IIC_RX_FIFO_DEPTH) {
|
561 |
|
|
Temp = IIC_RX_FIFO_DEPTH - 1;
|
562 |
|
|
} else {
|
563 |
|
|
if (InstancePtr->RecvByteCount == 0) {
|
564 |
|
|
Temp = 0;
|
565 |
|
|
} else {
|
566 |
|
|
Temp = InstancePtr->RecvByteCount - 1;
|
567 |
|
|
}
|
568 |
|
|
}
|
569 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress,
|
570 |
|
|
XIIC_RFD_REG_OFFSET, (u32) Temp);
|
571 |
|
|
|
572 |
|
|
return;
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
/*****************************************************************************/
|
576 |
|
|
/**
|
577 |
|
|
*
|
578 |
|
|
* This function sends data on the IIC bus as a slave.
|
579 |
|
|
*
|
580 |
|
|
* When message data has been sent, but the master keeps reading data, the FIFO
|
581 |
|
|
* is filled to prevent bus throttling. There is no way to notify master of this
|
582 |
|
|
* condition. While sending data as a slave a transmit error indicates the
|
583 |
|
|
* master has completed the data transfer.
|
584 |
|
|
*
|
585 |
|
|
* NAAS interrupt signals when repeated start occurred and the msg is finished
|
586 |
|
|
* and BNB signals when the master sent a stop.
|
587 |
|
|
*
|
588 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
589 |
|
|
*
|
590 |
|
|
* @return None.
|
591 |
|
|
*
|
592 |
|
|
* @note None.
|
593 |
|
|
*
|
594 |
|
|
******************************************************************************/
|
595 |
|
|
static void SendSlaveData(XIic *InstancePtr)
|
596 |
|
|
{
|
597 |
|
|
/*
|
598 |
|
|
* When message has been sent, but master keeps reading data, must put a
|
599 |
|
|
* byte in the FIFO or bus will throttle. There is no way to notify
|
600 |
|
|
* master of this condition.
|
601 |
|
|
*/
|
602 |
|
|
if (InstancePtr->SendByteCount == 0) {
|
603 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress,
|
604 |
|
|
XIIC_DTR_REG_OFFSET, 0xFF);
|
605 |
|
|
return;
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
/*
|
609 |
|
|
* Send the data by filling the transmit FIFO.
|
610 |
|
|
*/
|
611 |
|
|
XIic_TransmitFifoFill(InstancePtr, XIIC_SLAVE_ROLE);
|
612 |
|
|
/*
|
613 |
|
|
* When the amount of data remaining to send is less than the half mark
|
614 |
|
|
* of the FIFO making the use of � empty interrupt unnecessary,
|
615 |
|
|
* disable it. Is this a problem that it's checking against 1 rather
|
616 |
|
|
* than half?
|
617 |
|
|
*/
|
618 |
|
|
if (InstancePtr->SendByteCount < 1) {
|
619 |
|
|
XIic_DisableIntr(InstancePtr->BaseAddress,
|
620 |
|
|
XIIC_INTR_TX_HALF_MASK);
|
621 |
|
|
}
|
622 |
|
|
return;
|
623 |
|
|
}
|
624 |
|
|
/** @} */
|