1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 Xilinx, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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
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.
31 ******************************************************************************/
32 /*****************************************************************************/
34 * @file xiicps_slave.c
36 * Handles slave transfers
38 * <pre> MODIFICATION HISTORY:
40 * Ver Who Date Changes
41 * ----- -- -------- ---------------------------------------------
42 * 1.00a jz 01/30/10 First release
43 * 1.04a kpc 08/30/13 Avoid buffer overwrite in SlaveRecvData function
47 ******************************************************************************/
49 /***************************** Include Files *********************************/
52 /************************** Constant Definitions *****************************/
54 /**************************** Type Definitions *******************************/
56 /***************** Macros (Inline Functions) Definitions *********************/
58 /************************** Function Prototypes ******************************/
59 extern int TransmitFifoFill(XIicPs *InstancePtr);
61 static int SlaveRecvData(XIicPs *InstancePtr);
63 /************************* Variable Definitions *****************************/
65 /*****************************************************************************/
67 * This function sets up the device to be a slave.
69 * @param InstancePtr is a pointer to the XIicPs instance.
70 * @param SlaveAddr is the address of the slave we are receiving from.
75 * Interrupt is always enabled no matter the tranfer is interrupt-
76 * driven or polled mode. Whether device will be interrupted or not
77 * depends on whether the device is connected to an interrupt
78 * controller and interrupt for the device is enabled.
80 ****************************************************************************/
81 void XIicPs_SetupSlave(XIicPs *InstancePtr, u16 SlaveAddr)
83 volatile u32 ControlReg;
86 Xil_AssertVoid(InstancePtr != NULL);
87 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
88 Xil_AssertVoid(XIICPS_ADDR_MASK >= SlaveAddr);
90 BaseAddr = InstancePtr->Config.BaseAddress;
92 ControlReg = XIicPs_In32(BaseAddr + XIICPS_CR_OFFSET);
95 * Set up master, AckEn, nea and also clear fifo.
97 ControlReg |= XIICPS_CR_ACKEN_MASK | XIICPS_CR_CLR_FIFO_MASK;
98 ControlReg |= XIICPS_CR_NEA_MASK;
99 ControlReg &= ~XIICPS_CR_MS_MASK;
101 XIicPs_WriteReg(BaseAddr, XIICPS_CR_OFFSET,
104 XIicPs_DisableAllInterrupts(BaseAddr);
106 XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
107 XIICPS_ADDR_OFFSET, SlaveAddr);
112 /*****************************************************************************/
114 * This function setup a slave interrupt-driven send. It set the repeated
115 * start for the device is the tranfer size is larger than FIFO depth.
116 * Data processing for the send is initiated by the interrupt handler.
118 * @param InstancePtr is a pointer to the XIicPs instance.
119 * @param MsgPtr is the pointer to the send buffer.
120 * @param ByteCount is the number of bytes to be sent.
124 * @note This send routine is for interrupt-driven transfer only.
126 ****************************************************************************/
127 void XIicPs_SlaveSend(XIicPs *InstancePtr, u8 *MsgPtr, int ByteCount)
132 * Assert validates the input arguments
134 Xil_AssertVoid(InstancePtr != NULL);
135 Xil_AssertVoid(MsgPtr != NULL);
136 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
139 BaseAddr = InstancePtr->Config.BaseAddress;
140 InstancePtr->SendBufferPtr = MsgPtr;
141 InstancePtr->SendByteCount = ByteCount;
142 InstancePtr->RecvBufferPtr = NULL;
144 XIicPs_EnableInterrupts(BaseAddr,
145 XIICPS_IXR_DATA_MASK | XIICPS_IXR_COMP_MASK |
146 XIICPS_IXR_TO_MASK | XIICPS_IXR_NACK_MASK |
147 XIICPS_IXR_TX_OVR_MASK);
150 /*****************************************************************************/
152 * This function setup a slave interrupt-driven receive.
153 * Data processing for the receive is handled by the interrupt handler.
155 * @param InstancePtr is a pointer to the XIicPs instance.
156 * @param MsgPtr is the pointer to the receive buffer.
157 * @param ByteCount is the number of bytes to be received.
161 * @note This routine is for interrupt-driven transfer only.
163 ****************************************************************************/
164 void XIicPs_SlaveRecv(XIicPs *InstancePtr, u8 *MsgPtr, int ByteCount)
167 * Assert validates the input arguments.
169 Xil_AssertVoid(InstancePtr != NULL);
170 Xil_AssertVoid(MsgPtr != NULL);
171 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
174 InstancePtr->RecvBufferPtr = MsgPtr;
175 InstancePtr->RecvByteCount = ByteCount;
176 InstancePtr->SendBufferPtr = NULL;
178 XIicPs_EnableInterrupts(InstancePtr->Config.BaseAddress,
179 XIICPS_IXR_DATA_MASK | XIICPS_IXR_COMP_MASK |
180 XIICPS_IXR_NACK_MASK | XIICPS_IXR_TO_MASK |
181 XIICPS_IXR_RX_OVR_MASK | XIICPS_IXR_RX_UNF_MASK);
185 /*****************************************************************************/
187 * This function sends a buffer in polled mode as a slave.
189 * @param InstancePtr is a pointer to the XIicPs instance.
190 * @param MsgPtr is the pointer to the send buffer.
191 * @param ByteCount is the number of bytes to be sent.
194 * - XST_SUCCESS if everything went well.
195 * - XST_FAILURE if master sends us data or master terminates the
196 * transfer before all data has sent out.
198 * @note This send routine is for polled mode transfer only.
200 ****************************************************************************/
201 int XIicPs_SlaveSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, int ByteCount)
203 volatile u32 IntrStatusReg;
204 volatile u32 StatusReg;
211 * Assert validates the input arguments.
213 Xil_AssertNonvoid(InstancePtr != NULL);
214 Xil_AssertNonvoid(MsgPtr != NULL);
215 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
217 BaseAddr = InstancePtr->Config.BaseAddress;
218 InstancePtr->SendBufferPtr = MsgPtr;
219 InstancePtr->SendByteCount = ByteCount;
222 * Use RXRW bit in status register to wait master to start a read.
224 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
225 while (((StatusReg & XIICPS_SR_RXRW_MASK) == 0) && (!Error)) {
228 * If master tries to send us data, it is an error.
230 if (StatusReg & XIICPS_SR_RXDV_MASK) {
234 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
242 * Clear the interrupt status register.
244 IntrStatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_ISR_OFFSET);
245 XIicPs_WriteReg(BaseAddr, XIICPS_ISR_OFFSET, IntrStatusReg);
248 * Send data as long as there is more data to send and
249 * there are no errors.
251 while ((InstancePtr->SendByteCount > 0) && (!Error)){
254 * Find out how many can be sent.
256 BytesToSend = InstancePtr->SendByteCount;
257 if (BytesToSend > XIICPS_FIFO_DEPTH) {
258 BytesToSend = XIICPS_FIFO_DEPTH;
261 for(Tmp = 0; Tmp < BytesToSend; Tmp ++) {
262 XIicPs_SendByte(InstancePtr);
265 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
268 * Wait for master to read the data out of fifo.
270 while (((StatusReg & XIICPS_SR_TXDV_MASK) != 0) && (!Error)) {
273 * If master terminates the transfer before all data is
274 * sent, it is an error.
276 IntrStatusReg = XIicPs_ReadReg(BaseAddr,
278 if (IntrStatusReg & XIICPS_IXR_NACK_MASK) {
284 XIicPs_WriteReg(BaseAddr, XIICPS_ISR_OFFSET,
287 StatusReg = XIicPs_ReadReg(BaseAddr,
298 /*****************************************************************************/
300 * This function receives a buffer in polled mode as a slave.
302 * @param InstancePtr is a pointer to the XIicPs instance.
303 * @param MsgPtr is the pointer to the receive buffer.
304 * @param ByteCount is the number of bytes to be received.
307 * - XST_SUCCESS if everything went well.
308 * - XST_FAILURE if timed out.
310 * @note This receive routine is for polled mode transfer only.
312 ****************************************************************************/
313 int XIicPs_SlaveRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, int ByteCount)
315 volatile u32 IntrStatusReg;
316 volatile u32 StatusReg;
320 * Assert validates the input arguments.
322 Xil_AssertNonvoid(InstancePtr != NULL);
323 Xil_AssertNonvoid(MsgPtr != NULL);
324 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
326 BaseAddr = InstancePtr->Config.BaseAddress;
327 InstancePtr->RecvBufferPtr = MsgPtr;
328 InstancePtr->RecvByteCount = ByteCount;
330 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
333 * Clear the interrupt status register.
335 IntrStatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_ISR_OFFSET);
336 XIicPs_WriteReg(BaseAddr, XIICPS_ISR_OFFSET, IntrStatusReg);
339 * Clear the status register.
341 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
342 XIicPs_WriteReg(BaseAddr, XIICPS_SR_OFFSET, StatusReg);
344 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
345 while (InstancePtr->RecvByteCount > 0) {
347 /* Wait for master to put data */
348 while ((StatusReg & XIICPS_SR_RXDV_MASK) == 0) {
349 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
352 * If master terminates the transfer before we get all
353 * the data or the master tries to read from us,
356 IntrStatusReg = XIicPs_ReadReg(BaseAddr,
358 if ((IntrStatusReg & (XIICPS_IXR_DATA_MASK |
359 XIICPS_IXR_COMP_MASK)) &&
360 ((StatusReg & XIICPS_SR_RXDV_MASK) == 0) &&
361 (InstancePtr->RecvByteCount > 0)) {
367 * Clear the interrupt status register.
369 XIicPs_WriteReg(BaseAddr, XIICPS_ISR_OFFSET,
374 * Read all data from FIFO.
376 while ((StatusReg & XIICPS_SR_RXDV_MASK) &&
377 (InstancePtr->RecvByteCount > 0)){
379 XIicPs_RecvByte(InstancePtr);
381 StatusReg = XIicPs_ReadReg(BaseAddr,
389 /*****************************************************************************/
391 * The interrupt handler for slave mode. It does the protocol handling for
392 * the interrupt-driven transfers.
394 * Completion events and errors are signaled to upper layer for proper
399 * The interrupts that are handled are:
401 * If the instance is sending, it means that the master wants to read more
402 * data from us. Send more data, and check whether we are done with this
405 * If the instance is receiving, it means that the master has writen
406 * more data to us. Receive more data, and check whether we are done with
410 * This marks that stop sequence has been sent from the master, transfer
411 * is about to terminate. However, for receiving, the master may have
412 * written us some data, so receive that first.
414 * It is an error if the amount of transfered data is less than expected.
417 * This marks that master does not want our data. It is for send only.
420 * These interrupts are marked as error.
424 * @param InstancePtr is a pointer to the XIicPs instance.
430 ****************************************************************************/
431 void XIicPs_SlaveInterruptHandler(XIicPs *InstancePtr)
433 volatile u32 IntrStatusReg;
440 * Assert validates the input arguments.
442 Xil_AssertVoid(InstancePtr != NULL);
443 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
445 BaseAddr = InstancePtr->Config.BaseAddress;
448 * Read the Interrupt status register.
450 IntrStatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_ISR_OFFSET);
453 * Write the status back to clear the interrupts so no events are missed
454 * while processing this interrupt.
456 XIicPs_WriteReg(BaseAddr, XIICPS_ISR_OFFSET, IntrStatusReg);
459 * Use the Mask register AND with the Interrupt Status register so
460 * disabled interrupts are not processed.
462 IntrStatusReg &= ~(XIicPs_ReadReg(BaseAddr, XIICPS_IMR_OFFSET));
465 * Determine whether the device is sending.
467 if (InstancePtr->RecvBufferPtr == NULL) {
473 * This means master wants to do more data transfers.
474 * Also check for completion of transfer, signal upper layer if done.
476 if (0 != (IntrStatusReg & XIICPS_IXR_DATA_MASK)) {
478 LeftOver = TransmitFifoFill(InstancePtr);
480 * We may finish send here
484 XIICPS_EVENT_COMPLETE_SEND;
487 LeftOver = SlaveRecvData(InstancePtr);
489 /* We may finish the receive here */
491 StatusEvent |= XIICPS_EVENT_COMPLETE_RECV;
497 * Complete interrupt.
499 * In slave mode, it means the master has done with this transfer, so
500 * we signal the application using completion event.
502 if (0 != (IntrStatusReg & XIICPS_IXR_COMP_MASK)) {
504 if (InstancePtr->SendByteCount > 0) {
505 StatusEvent |= XIICPS_EVENT_ERROR;
507 StatusEvent |= XIICPS_EVENT_COMPLETE_SEND;
510 LeftOver = SlaveRecvData(InstancePtr);
512 StatusEvent |= XIICPS_EVENT_ERROR;
514 StatusEvent |= XIICPS_EVENT_COMPLETE_RECV;
520 * Nack interrupt, pass this information to application.
522 if (0 != (IntrStatusReg & XIICPS_IXR_NACK_MASK)) {
523 StatusEvent |= XIICPS_EVENT_NACK;
527 * All other interrupts are treated as error.
529 if (0 != (IntrStatusReg & (XIICPS_IXR_TO_MASK |
530 XIICPS_IXR_RX_UNF_MASK |
531 XIICPS_IXR_TX_OVR_MASK |
532 XIICPS_IXR_RX_OVR_MASK))){
534 StatusEvent |= XIICPS_EVENT_ERROR;
538 * Signal application if there are any events.
540 if (0 != StatusEvent) {
541 InstancePtr->StatusHandler(InstancePtr->CallBackRef,
546 /*****************************************************************************/
549 * This function handles continuation of receiving data. It is invoked
550 * from interrupt handler.
552 * @param InstancePtr is a pointer to the XIicPs instance.
554 * @return Number of bytes still expected by the instance.
558 ****************************************************************************/
559 static int SlaveRecvData(XIicPs *InstancePtr)
561 volatile u32 StatusReg;
564 BaseAddr = InstancePtr->Config.BaseAddress;
566 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
568 while ((StatusReg & XIICPS_SR_RXDV_MASK) &&
569 (InstancePtr->RecvByteCount > 0)) {
570 XIicPs_RecvByte(InstancePtr);
571 StatusReg = XIicPs_ReadReg(BaseAddr, XIICPS_SR_OFFSET);
574 return InstancePtr->RecvByteCount;