1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 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 /*****************************************************************************/
36 * @addtogroup iicps_v3_0
40 * This is an implementation of IIC driver in the PS block. The device can
41 * be either a master or a slave on the IIC bus. This implementation supports
42 * both interrupt mode transfer and polled mode transfer. Only 7-bit address
43 * is used in the driver, although the hardware also supports 10-bit address.
45 * IIC is a 2-wire serial interface. The master controls the clock, so it can
46 * regulate when it wants to send or receive data. The slave is under control of
47 * the master, it must respond quickly since it has no control of the clock and
48 * must send/receive data as fast or as slow as the master does.
50 * The higher level software must implement a higher layer protocol to inform
51 * the slave what to send to the master.
53 * <b>Initialization & Configuration</b>
55 * The XIicPs_Config structure is used by the driver to configure itself. This
56 * configuration structure is typically created by the tool-chain based on HW
59 * To support multiple runtime loading and initialization strategies employed by
60 * various operating systems, the driver instance can be initialized in the
63 * - XIicPs_LookupConfig(DeviceId) - Use the device identifier to find
64 * the static configuration structure defined in xiicps_g.c. This is
65 * setup by the tools. For some operating systems the config structure
66 * will be initialized by the software and this call is not needed.
68 * - XIicPs_CfgInitialize(InstancePtr, CfgPtr, EffectiveAddr) - Uses a
69 * configuration structure provided by the caller. If running in a
70 * system with address translation, the provided virtual memory base
71 * address replaces the physical address in the configuration
74 * <b>Multiple Masters</b>
76 * More than one master can exist, bus arbitration is defined in the IIC
77 * standard. Lost of arbitration causes arbitration loss interrupt on the device.
79 * <b>Multiple Slaves</b>
81 * Multiple slaves are supported by selecting them with unique addresses. It is
82 * up to the system designer to be sure all devices on the IIC bus have
87 * The IIC hardware can use 7 or 10 bit addresses. The driver provides the
88 * ability to control which address size is sent in messages as a master to a
92 * The hardware FIFO is 32 bytes deep. The user must know the limitations of
93 * other IIC devices on the bus. Some are only able to receive a limited number
94 * of bytes in a single transfer.
98 * The data rate is set by values in the control register. The formula for
99 * determining the correct register values is:
100 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
102 * When the device is configured as a slave, the slck setting controls the
103 * sample rate and so must be set to be at least as fast as the fastest scl
104 * expected to be seen in the system.
106 * <b>Polled Mode Operation</b>
108 * This driver supports polled mode transfers.
112 * The user must connect the interrupt handler of the driver,
113 * XIicPs_InterruptHandler to an interrupt system such that it will be called
114 * when an interrupt occurs. This function does not save and restore the
115 * processor context such that the user must provide this processing.
117 * The driver handles the following interrupts:
118 * - Transfer complete
120 * - Transfer not Acknowledged
121 * - Transfer Time out
122 * - Monitored slave ready - master mode only
124 * - Transmit FIFO overflow
125 * - Receive FIFO underflow
130 * Bus busy is checked before the setup of a master mode device, to avoid
131 * unnecessary arbitration loss interrupt.
133 * <b>RTOS Independence</b>
135 * This driver is intended to be RTOS and processor independent. It works with
136 * physical addresses only. Any needs for dynamic memory management, threads or
137 * thread mutual exclusion, virtual memory, or cache control must be satisfied by
138 * the layer above this driver.
140 *<b>Repeated Start</b>
142 * The I2C controller does not indicate completion of a receive transfer if HOLD
143 * bit is set. Due to this errata, repeated start cannot be used if a receive
144 * transfer is followed by any other transfer.
146 * <pre> MODIFICATION HISTORY:
148 * Ver Who Date Changes
149 * ----- ------ -------- -----------------------------------------------
150 * 1.00a drg/jz 01/30/08 First release
151 * 1.00a sdm 09/21/11 Fixed an issue in the XIicPs_SetOptions and
152 * XIicPs_ClearOptions where the InstancePtr->Options
153 * was not updated correctly.
154 * Updated the InstancePtr->Options in the
155 * XIicPs_CfgInitialize by calling XIicPs_GetOptions.
156 * Updated the XIicPs_SetupMaster to not check for
157 * Bus Busy condition when the Hold Bit is set.
158 * Removed some unused variables.
159 * 1.01a sg 03/30/12 Fixed an issue in XIicPs_MasterSendPolled where a
160 * check for transfer completion is added, which indicates
161 * the completion of current transfer.
162 * 1.02a sg 08/29/12 Updated the logic to arrive at the best divisors
163 * to achieve I2C clock with minimum error for
165 * 1.03a hk 05/04/13 Initialized BestDivA and BestDivB to 0.
166 * This is fix for CR#704398 to remove warning.
167 * 2.0 hk 03/07/14 Added check for error status in the while loop that
168 * checks for completion.
169 * (XIicPs_MasterSendPolled function). CR# 762244, 764875.
170 * Limited frequency set when 100KHz or 400KHz is
171 * selected. This is a hardware limitation. CR#779290.
172 * 2.1 hk 04/24/14 Fix for CR# 789821 to handle >14 byte transfers.
173 * Explicitly reset CR and clear FIFO in Abort function
174 * and state the same in the comments. CR# 784254.
175 * Fix for CR# 761060 - provision for repeated start.
176 * 2.2 hk 08/23/14 Slave monitor mode changes - clear FIFO, enable
177 * read mode and clear transfer size register.
178 * Disable NACK to avoid interrupts on each retry.
179 * 2.3 sk 10/07/14 Repeated start feature deleted.
180 * 3.0 sk 11/03/14 Modified TimeOut Register value to 0xFF
182 * 12/06/14 Implemented Repeated start feature.
183 * 01/31/15 Modified the code according to MISRAC 2012 Compliant.
184 * 02/18/15 Implemented larger data transfer using repeated start
185 * in Zynq UltraScale MP.
189 ******************************************************************************/
191 #ifndef XIICPS_H /* prevent circular inclusions */
192 #define XIICPS_H /* by using protection macros */
198 /***************************** Include Files *********************************/
200 #include "xil_types.h"
201 #include "xil_assert.h"
203 #include "xiicps_hw.h"
204 #include "xplatform_info.h"
206 /************************** Constant Definitions *****************************/
208 /** @name Configuration options
210 * The following options may be specified or retrieved for the device and
211 * enable/disable additional features of the IIC. Each of the options
212 * are bit fields, so more than one may be specified.
216 #define XIICPS_7_BIT_ADDR_OPTION 0x01U /**< 7-bit address mode */
217 #define XIICPS_10_BIT_ADDR_OPTION 0x02U /**< 10-bit address mode */
218 #define XIICPS_SLAVE_MON_OPTION 0x04U /**< Slave monitor mode */
219 #define XIICPS_REP_START_OPTION 0x08U /**< Repeated Start */
222 /** @name Callback events
224 * These constants specify the handler events that are passed to an application
225 * event handler from the driver. These constants are bit masks such that
226 * more than one event can be passed to the handler.
230 #define XIICPS_EVENT_COMPLETE_SEND 0x0001U /**< Transmit Complete Event*/
231 #define XIICPS_EVENT_COMPLETE_RECV 0x0002U /**< Receive Complete Event*/
232 #define XIICPS_EVENT_TIME_OUT 0x0004U /**< Transfer timed out */
233 #define XIICPS_EVENT_ERROR 0x0008U /**< Receive error */
234 #define XIICPS_EVENT_ARB_LOST 0x0010U /**< Arbitration lost */
235 #define XIICPS_EVENT_NACK 0x0020U /**< NACK Received */
236 #define XIICPS_EVENT_SLAVE_RDY 0x0040U /**< Slave ready */
237 #define XIICPS_EVENT_RX_OVR 0x0080U /**< RX overflow */
238 #define XIICPS_EVENT_TX_OVR 0x0100U /**< TX overflow */
239 #define XIICPS_EVENT_RX_UNF 0x0200U /**< RX underflow */
242 /** @name Role constants
244 * These constants are used to pass into the device setup routines to
245 * set up the device according to transfer direction.
247 #define SENDING_ROLE 1 /**< Transfer direction is sending */
248 #define RECVING_ROLE 0 /**< Transfer direction is receiving */
250 /* Maximum transfer size */
251 #define XIICPS_MAX_TRANSFER_SIZE (u32)(255U - 3U)
253 /**************************** Type Definitions *******************************/
256 * The handler data type allows the user to define a callback function to
257 * respond to interrupt events in the system. This function is executed
258 * in interrupt context, so amount of processing should be minimized.
260 * @param CallBackRef is the callback reference passed in by the upper
261 * layer when setting the callback functions, and passed back to
262 * the upper layer when the callback is invoked. Its type is
263 * not important to the driver, so it is a void pointer.
264 * @param StatusEvent indicates one or more status events that occurred.
266 typedef void (*XIicPs_IntrHandler) (void *CallBackRef, u32 StatusEvent);
269 * This typedef contains configuration information for the device.
272 u16 DeviceId; /**< Unique ID of device */
273 u32 BaseAddress; /**< Base address of the device */
274 u32 InputClockHz; /**< Input clock frequency */
278 * The XIicPs driver instance data. The user is required to allocate a
279 * variable of this type for each IIC device in the system. A pointer
280 * to a variable of this type is then passed to the driver API functions.
283 XIicPs_Config Config; /* Configuration structure */
284 u32 IsReady; /* Device is initialized and ready */
285 u32 Options; /* Options set in the device */
287 u8 *SendBufferPtr; /* Pointer to send buffer */
288 u8 *RecvBufferPtr; /* Pointer to recv buffer */
289 s32 SendByteCount; /* Number of bytes still expected to send */
290 s32 RecvByteCount; /* Number of bytes still expected to receive */
291 s32 CurrByteCount; /* No. of bytes expected in current transfer */
293 s32 UpdateTxSize; /* If tx size register has to be updated */
294 s32 IsSend; /* Whether master is sending or receiving */
295 s32 IsRepeatedStart; /* Indicates if user set repeated start */
297 XIicPs_IntrHandler StatusHandler; /* Event handler function */
298 void *CallBackRef; /* Callback reference for event handler */
301 /***************** Macros (Inline Functions) Definitions *********************/
302 /****************************************************************************/
305 * Place one byte into the transmit FIFO.
307 * @param InstancePtr is the instance of IIC
311 * @note C-Style signature:
312 * void XIicPs_SendByte(XIicPs *InstancePtr)
314 *****************************************************************************/
315 #define XIicPs_SendByte(InstancePtr) \
318 Data = *((InstancePtr)->SendBufferPtr); \
319 XIicPs_Out32((InstancePtr)->Config.BaseAddress \
320 + (u32)(XIICPS_DATA_OFFSET), \
322 (InstancePtr)->SendBufferPtr += 1; \
323 (InstancePtr)->SendByteCount -= 1;\
326 /****************************************************************************/
329 * Receive one byte from FIFO.
331 * @param InstancePtr is the instance of IIC
335 * @note C-Style signature:
336 * u8 XIicPs_RecvByte(XIicPs *InstancePtr)
338 *****************************************************************************/
339 #define XIicPs_RecvByte(InstancePtr) \
342 Value = (u8)(XIicPs_In32((InstancePtr)->Config.BaseAddress \
343 + (u32)XIICPS_DATA_OFFSET)); \
345 *(InstancePtr)->RecvBufferPtr = *Data; \
346 (InstancePtr)->RecvBufferPtr += 1; \
347 (InstancePtr)->RecvByteCount --; \
350 /************************** Function Prototypes ******************************/
353 * Function for configuration lookup, in xiicps_sinit.c
355 XIicPs_Config *XIicPs_LookupConfig(u16 DeviceId);
358 * Functions for general setup, in xiicps.c
360 s32 XIicPs_CfgInitialize(XIicPs *InstancePtr, XIicPs_Config * ConfigPtr,
363 void XIicPs_Abort(XIicPs *InstancePtr);
364 void XIicPs_Reset(XIicPs *InstancePtr);
366 s32 XIicPs_BusIsBusy(XIicPs *InstancePtr);
367 s32 TransmitFifoFill(XIicPs *InstancePtr);
370 * Functions for interrupts, in xiicps_intr.c
372 void XIicPs_SetStatusHandler(XIicPs *InstancePtr, void *CallBackRef,
373 XIicPs_IntrHandler FunctionPtr);
376 * Functions for device as master, in xiicps_master.c
378 void XIicPs_MasterSend(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
380 void XIicPs_MasterRecv(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
382 s32 XIicPs_MasterSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
384 s32 XIicPs_MasterRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
386 void XIicPs_EnableSlaveMonitor(XIicPs *InstancePtr, u16 SlaveAddr);
387 void XIicPs_DisableSlaveMonitor(XIicPs *InstancePtr);
388 void XIicPs_MasterInterruptHandler(XIicPs *InstancePtr);
391 * Functions for device as slave, in xiicps_slave.c
393 void XIicPs_SetupSlave(XIicPs *InstancePtr, u16 SlaveAddr);
394 void XIicPs_SlaveSend(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
395 void XIicPs_SlaveRecv(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
396 s32 XIicPs_SlaveSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
397 s32 XIicPs_SlaveRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
398 void XIicPs_SlaveInterruptHandler(XIicPs *InstancePtr);
401 * Functions for selftest, in xiicps_selftest.c
403 s32 XIicPs_SelfTest(XIicPs *InstancePtr);
406 * Functions for setting and getting data rate, in xiicps_options.c
408 s32 XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options);
409 s32 XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options);
410 u32 XIicPs_GetOptions(XIicPs *InstancePtr);
412 s32 XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz);
413 u32 XIicPs_GetSClk(XIicPs *InstancePtr);
419 #endif /* end of protection macro */