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_multi_master.c
|
36 |
|
|
* @addtogroup iic_v3_1
|
37 |
|
|
* @{
|
38 |
|
|
*
|
39 |
|
|
* Contains multi-master functions for the XIic component. This file is
|
40 |
|
|
* necessary if multiple masters are on the IIC bus such that arbitration can
|
41 |
|
|
* be lost or the bus can be busy.
|
42 |
|
|
*
|
43 |
|
|
* <pre>
|
44 |
|
|
* MODIFICATION HISTORY:
|
45 |
|
|
*
|
46 |
|
|
* Ver Who Date Changes
|
47 |
|
|
* ----- --- ------- -----------------------------------------------
|
48 |
|
|
* 1.01b jhl 3/27/02 Reparitioned the driver
|
49 |
|
|
* 1.01c ecm 12/05/02 new rev
|
50 |
|
|
* 1.13a wgr 03/22/07 Converted to new coding style.
|
51 |
|
|
* 2.00a ktn 10/22/09 Converted all register accesses to 32 bit access.
|
52 |
|
|
* Updated to use the HAL APIs/macros.
|
53 |
|
|
* Some of the macros have been renamed to remove _m from
|
54 |
|
|
* the name and some of the macros have been renamed to be
|
55 |
|
|
* consistent, see the xiic_i.h and xiic_l.h files for further
|
56 |
|
|
* information
|
57 |
|
|
* </pre>
|
58 |
|
|
*
|
59 |
|
|
****************************************************************************/
|
60 |
|
|
|
61 |
|
|
/***************************** Include Files *******************************/
|
62 |
|
|
|
63 |
|
|
#include "xiic.h"
|
64 |
|
|
#include "xiic_i.h"
|
65 |
|
|
|
66 |
|
|
/************************** Constant Definitions ***************************/
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
/**************************** Type Definitions *****************************/
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
/***************** Macros (Inline Functions) Definitions *******************/
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
/************************** Function Prototypes ****************************/
|
76 |
|
|
|
77 |
|
|
static void BusNotBusyHandler(XIic *InstancePtr);
|
78 |
|
|
static void ArbitrationLostHandler(XIic *InstancePtr);
|
79 |
|
|
|
80 |
|
|
/************************** Variable Definitions **************************/
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/****************************************************************************/
|
84 |
|
|
/**
|
85 |
|
|
* This function includes multi-master code such that multi-master events are
|
86 |
|
|
* handled properly. Multi-master events include a loss of arbitration and
|
87 |
|
|
* the bus transitioning from busy to not busy. This function allows the
|
88 |
|
|
* multi-master processing to be optional. This function must be called prior
|
89 |
|
|
* to allowing any multi-master events to occur, such as after the driver is
|
90 |
|
|
* initialized.
|
91 |
|
|
*
|
92 |
|
|
* @param None.
|
93 |
|
|
*
|
94 |
|
|
* @return None.
|
95 |
|
|
*
|
96 |
|
|
* @note None.
|
97 |
|
|
*
|
98 |
|
|
******************************************************************************/
|
99 |
|
|
void XIic_MultiMasterInclude()
|
100 |
|
|
{
|
101 |
|
|
XIic_ArbLostFuncPtr = ArbitrationLostHandler;
|
102 |
|
|
XIic_BusNotBusyFuncPtr = BusNotBusyHandler;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
/*****************************************************************************/
|
106 |
|
|
/**
|
107 |
|
|
*
|
108 |
|
|
* The IIC bus busy signals when a master has control of the bus. Until the bus
|
109 |
|
|
* is released, i.e. not busy, other devices must wait to use it.
|
110 |
|
|
*
|
111 |
|
|
* When this interrupt occurs, it signals that the previous master has released
|
112 |
|
|
* the bus for another user.
|
113 |
|
|
*
|
114 |
|
|
* This interrupt is only enabled when the master Tx is waiting for the bus.
|
115 |
|
|
*
|
116 |
|
|
* This interrupt causes the following tasks:
|
117 |
|
|
* - Disable Bus not busy interrupt
|
118 |
|
|
* - Enable bus Ack
|
119 |
|
|
* Should the slave receive have disabled acknowledgement, enable to allow
|
120 |
|
|
* acknowledgment of the sending of our address to again be addresed as
|
121 |
|
|
* slave
|
122 |
|
|
* - Flush Rx FIFO
|
123 |
|
|
* Should the slave receive have disabled acknowledgement, a few bytes may
|
124 |
|
|
* be in FIFO if Rx full did not occur because of not enough byte in FIFO
|
125 |
|
|
* to have caused an interrupt.
|
126 |
|
|
* - Send status to user via status callback with the value:
|
127 |
|
|
* XII_BUS_NOT_BUSY_EVENT
|
128 |
|
|
*
|
129 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
130 |
|
|
*
|
131 |
|
|
* @return None.
|
132 |
|
|
*
|
133 |
|
|
* @note None.
|
134 |
|
|
*
|
135 |
|
|
******************************************************************************/
|
136 |
|
|
static void BusNotBusyHandler(XIic *InstancePtr)
|
137 |
|
|
{
|
138 |
|
|
u32 Status;
|
139 |
|
|
u32 CntlReg;
|
140 |
|
|
|
141 |
|
|
/*
|
142 |
|
|
* Should the slave receive have disabled acknowledgement,
|
143 |
|
|
* enable to allow acknowledgment of the sending of our address to
|
144 |
|
|
* again be addresed as slave.
|
145 |
|
|
*/
|
146 |
|
|
CntlReg = XIic_ReadReg(InstancePtr->BaseAddress, XIIC_CR_REG_OFFSET);
|
147 |
|
|
XIic_WriteReg(InstancePtr->BaseAddress, XIIC_CR_REG_OFFSET,
|
148 |
|
|
(CntlReg & ~XIIC_CR_NO_ACK_MASK));
|
149 |
|
|
|
150 |
|
|
/*
|
151 |
|
|
* Flush Tx FIFO by toggling TxFIFOResetBit. FIFO runs normally at 0
|
152 |
|
|
* Do this incase needed to Tx FIFO with more than expected if what
|
153 |
|
|
* was set to Tx was less than what the Master expected - read more
|
154 |
|
|
* from this slave so FIFO had junk in it.
|
155 |
|
|
*/
|
156 |
|
|
XIic_FlushTxFifo(InstancePtr);
|
157 |
|
|
|
158 |
|
|
/*
|
159 |
|
|
* Flush Rx FIFO should slave Rx had a problem, sent No ack but
|
160 |
|
|
* still received a few bytes. Should the slave receive have disabled
|
161 |
|
|
* acknowledgement, clear Rx FIFO.
|
162 |
|
|
*/
|
163 |
|
|
XIic_FlushRxFifo(InstancePtr);
|
164 |
|
|
|
165 |
|
|
/*
|
166 |
|
|
* Send Application messaging status via callbacks. Disable either Tx or
|
167 |
|
|
* Receive interrupt. Which callback depends on messaging direction.
|
168 |
|
|
*/
|
169 |
|
|
Status = XIic_ReadIier(InstancePtr->BaseAddress);
|
170 |
|
|
if (InstancePtr->RecvBufferPtr == NULL) {
|
171 |
|
|
/*
|
172 |
|
|
* Slave was sending data (master was reading), disable
|
173 |
|
|
* all the transmit interrupts.
|
174 |
|
|
*/
|
175 |
|
|
XIic_WriteIier(InstancePtr->BaseAddress,
|
176 |
|
|
(Status & ~XIIC_TX_INTERRUPTS));
|
177 |
|
|
}
|
178 |
|
|
else {
|
179 |
|
|
/*
|
180 |
|
|
* Slave was receiving data (master was writing) disable receive
|
181 |
|
|
* interrupts.
|
182 |
|
|
*/
|
183 |
|
|
XIic_WriteIier(InstancePtr->BaseAddress,
|
184 |
|
|
(Status & ~XIIC_INTR_RX_FULL_MASK));
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/*
|
188 |
|
|
* Send Status in StatusHandler callback.
|
189 |
|
|
*/
|
190 |
|
|
InstancePtr->StatusHandler(InstancePtr->StatusCallBackRef,
|
191 |
|
|
XII_BUS_NOT_BUSY_EVENT);
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
/*****************************************************************************/
|
195 |
|
|
/**
|
196 |
|
|
*
|
197 |
|
|
* When multiple IIC devices attempt to use the bus simultaneously, only
|
198 |
|
|
* a single device will be able to keep control as a master. Those devices
|
199 |
|
|
* that didn't retain control over the bus are said to have lost arbitration.
|
200 |
|
|
* When arbitration is lost, this interrupt occurs sigaling the user that
|
201 |
|
|
* the message did not get sent as expected.
|
202 |
|
|
*
|
203 |
|
|
* This function, at arbitration lost:
|
204 |
|
|
* - Disables Tx empty, � empty and Tx error interrupts
|
205 |
|
|
* - Clears any Tx empty, � empty Rx Full or Tx error interrupts
|
206 |
|
|
* - Clears Arbitration lost interrupt,
|
207 |
|
|
* - Flush Tx FIFO
|
208 |
|
|
* - Call StatusHandler callback with the value: XII_ARB_LOST_EVENT
|
209 |
|
|
*
|
210 |
|
|
* @param InstancePtr is a pointer to the XIic instance to be worked on.
|
211 |
|
|
*
|
212 |
|
|
* @return None.
|
213 |
|
|
*
|
214 |
|
|
* @note None.
|
215 |
|
|
*
|
216 |
|
|
******************************************************************************/
|
217 |
|
|
static void ArbitrationLostHandler(XIic *InstancePtr)
|
218 |
|
|
{
|
219 |
|
|
/*
|
220 |
|
|
* Disable Tx empty and � empty and Tx error interrupts before clearing
|
221 |
|
|
* them so they won't occur again.
|
222 |
|
|
*/
|
223 |
|
|
XIic_DisableIntr(InstancePtr->BaseAddress, XIIC_TX_INTERRUPTS);
|
224 |
|
|
|
225 |
|
|
/*
|
226 |
|
|
* Clear any Tx empty, � empty Rx Full or Tx error interrupts.
|
227 |
|
|
*/
|
228 |
|
|
XIic_ClearIntr(InstancePtr->BaseAddress, XIIC_TX_INTERRUPTS);
|
229 |
|
|
|
230 |
|
|
XIic_FlushTxFifo(InstancePtr);
|
231 |
|
|
|
232 |
|
|
/*
|
233 |
|
|
* Update Status via StatusHandler callback.
|
234 |
|
|
*/
|
235 |
|
|
InstancePtr->StatusHandler(InstancePtr->StatusCallBackRef,
|
236 |
|
|
XII_ARB_LOST_EVENT);
|
237 |
|
|
}
|
238 |
|
|
/** @} */
|