]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5_bsp/psu_cortexr5_0/libsrc/iicps_v3_6/src/xiicps.h
Update Zynq, MPSoc Cortex-A53 and MPSoc Cortex-R5 demo projects to build with the...
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5_bsp / psu_cortexr5_0 / libsrc / iicps_v3_6 / src / xiicps.h
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2016 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 xiicps.h
36 * @addtogroup iicps_v3_5
37 * @{
38 * @details
39 *
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.
44 *
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.
49 *
50 * The higher level software must implement a higher layer protocol to inform
51 * the slave what to send to the master.
52 *
53 * <b>Initialization & Configuration</b>
54 *
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
57 * build properties.
58 *
59 * To support multiple runtime loading and initialization strategies employed by
60 * various operating systems, the driver instance can be initialized in the
61 * following way:
62 *
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.
67 *
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
72 *     structure.
73 *
74 * <b>Multiple Masters</b>
75 *
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.
78 *
79 * <b>Multiple Slaves</b>
80 *
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
83 * unique addresses.
84 *
85 * <b>Addressing</b>
86 *
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
89 * slave device.
90 *
91 * <b>FIFO Size </b>
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.
95 *
96 * <b>Data Rates</b>
97 *
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))
101 *
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.
105 *
106 * <b>Polled Mode Operation</b>
107 *
108 * This driver supports polled mode transfers.
109 *
110 * <b>Interrupts</b>
111 *
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.
116 *
117 * The driver handles the following interrupts:
118 * - Transfer complete
119 * - More Data
120 * - Transfer not Acknowledged
121 * - Transfer Time out
122 * - Monitored slave ready - master mode only
123 * - Receive Overflow
124 * - Transmit FIFO overflow
125 * - Receive FIFO underflow
126 * - Arbitration lost
127 *
128 * <b>Bus Busy</b>
129 *
130 * Bus busy is checked before the setup of a master mode device, to avoid
131 * unnecessary arbitration loss interrupt.
132 *
133 * <b>RTOS Independence</b>
134 *
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.
139 *
140 *<b>Repeated Start</b>
141 *
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.
145 *
146 * <pre> MODIFICATION HISTORY:
147 *
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
164 *                        CR #674195
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
181 *                                        in XIicPs_Reset.
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.
186 * 3.3   kvn 05/05/16 Modified latest code for MISRA-C:2012 Compliance.
187 *       ms  03/17/17 Added readme.txt file in examples folder for doxygen
188 *                    generation.
189 *
190 * </pre>
191 *
192 ******************************************************************************/
193
194 #ifndef XIICPS_H       /* prevent circular inclusions */
195 #define XIICPS_H       /* by using protection macros */
196
197 #ifdef __cplusplus
198 extern "C" {
199 #endif
200
201 /***************************** Include Files *********************************/
202
203 #include "xil_types.h"
204 #include "xil_assert.h"
205 #include "xstatus.h"
206 #include "xiicps_hw.h"
207 #include "xplatform_info.h"
208
209 /************************** Constant Definitions *****************************/
210
211 /** @name Configuration options
212  *
213  * The following options may be specified or retrieved for the device and
214  * enable/disable additional features of the IIC.  Each of the options
215  * are bit fields, so more than one may be specified.
216  *
217  * @{
218  */
219 #define XIICPS_7_BIT_ADDR_OPTION        0x01U  /**< 7-bit address mode */
220 #define XIICPS_10_BIT_ADDR_OPTION       0x02U  /**< 10-bit address mode */
221 #define XIICPS_SLAVE_MON_OPTION         0x04U  /**< Slave monitor mode */
222 #define XIICPS_REP_START_OPTION         0x08U  /**< Repeated Start */
223 /*@}*/
224
225 /** @name Callback events
226  *
227  * These constants specify the handler events that are passed to an application
228  * event handler from the driver.  These constants are bit masks such that
229  * more than one event can be passed to the handler.
230  *
231  * @{
232  */
233 #define XIICPS_EVENT_COMPLETE_SEND      0x0001U  /**< Transmit Complete Event*/
234 #define XIICPS_EVENT_COMPLETE_RECV      0x0002U  /**< Receive Complete Event*/
235 #define XIICPS_EVENT_TIME_OUT           0x0004U  /**< Transfer timed out */
236 #define XIICPS_EVENT_ERROR                      0x0008U  /**< Receive error */
237 #define XIICPS_EVENT_ARB_LOST           0x0010U  /**< Arbitration lost */
238 #define XIICPS_EVENT_NACK                       0x0020U  /**< NACK Received */
239 #define XIICPS_EVENT_SLAVE_RDY          0x0040U  /**< Slave ready */
240 #define XIICPS_EVENT_RX_OVR                     0x0080U  /**< RX overflow */
241 #define XIICPS_EVENT_TX_OVR                     0x0100U  /**< TX overflow */
242 #define XIICPS_EVENT_RX_UNF                     0x0200U  /**< RX underflow */
243 /*@}*/
244
245 /** @name Role constants
246  *
247  * These constants are used to pass into the device setup routines to
248  * set up the device according to transfer direction.
249  */
250 #define SENDING_ROLE            1  /**< Transfer direction is sending */
251 #define RECVING_ROLE            0  /**< Transfer direction is receiving */
252
253 /* Maximum transfer size */
254 #define XIICPS_MAX_TRANSFER_SIZE        (u32)(255U - 3U)
255
256 /**************************** Type Definitions *******************************/
257
258 /**
259 * The handler data type allows the user to define a callback function to
260 * respond to interrupt events in the system. This function is executed
261 * in interrupt context, so amount of processing should be minimized.
262 *
263 * @param        CallBackRef is the callback reference passed in by the upper
264 *               layer when setting the callback functions, and passed back to
265 *               the upper layer when the callback is invoked. Its type is
266 *               not important to the driver, so it is a void pointer.
267 * @param        StatusEvent indicates one or more status events that occurred.
268 */
269 typedef void (*XIicPs_IntrHandler) (void *CallBackRef, u32 StatusEvent);
270
271 /**
272  * This typedef contains configuration information for the device.
273  */
274 typedef struct {
275         u16 DeviceId;     /**< Unique ID  of device */
276         u32 BaseAddress;  /**< Base address of the device */
277         u32 InputClockHz; /**< Input clock frequency */
278 } XIicPs_Config;
279
280 /**
281  * The XIicPs driver instance data. The user is required to allocate a
282  * variable of this type for each IIC device in the system. A pointer
283  * to a variable of this type is then passed to the driver API functions.
284  */
285 typedef struct {
286         XIicPs_Config Config;   /* Configuration structure */
287         u32 IsReady;            /* Device is initialized and ready */
288         u32 Options;            /* Options set in the device */
289
290         u8 *SendBufferPtr;      /* Pointer to send buffer */
291         u8 *RecvBufferPtr;      /* Pointer to recv buffer */
292         s32 SendByteCount;      /* Number of bytes still expected to send */
293         s32 RecvByteCount;      /* Number of bytes still expected to receive */
294         s32 CurrByteCount;      /* No. of bytes expected in current transfer */
295
296         s32 UpdateTxSize;       /* If tx size register has to be updated */
297         s32 IsSend;             /* Whether master is sending or receiving */
298         s32 IsRepeatedStart;    /* Indicates if user set repeated start */
299
300         XIicPs_IntrHandler StatusHandler;  /* Event handler function */
301         void *CallBackRef;      /* Callback reference for event handler */
302 } XIicPs;
303
304 /***************** Macros (Inline Functions) Definitions *********************/
305 /****************************************************************************/
306 /*
307 *
308 * Place one byte into the transmit FIFO.
309 *
310 * @param        InstancePtr is the instance of IIC
311 *
312 * @return       None.
313 *
314 * @note         C-Style signature:
315 *               void XIicPs_SendByte(XIicPs *InstancePtr)
316 *
317 *****************************************************************************/
318 #define XIicPs_SendByte(InstancePtr)                                    \
319 {                                                                       \
320         u8 Data;                                                        \
321         Data = *((InstancePtr)->SendBufferPtr);                         \
322          XIicPs_Out32((InstancePtr)->Config.BaseAddress                 \
323                          + (u32)(XIICPS_DATA_OFFSET),                   \
324                                         (u32)(Data));                   \
325         (InstancePtr)->SendBufferPtr += 1;                              \
326         (InstancePtr)->SendByteCount -= 1;\
327 }
328
329 /****************************************************************************/
330 /*
331 *
332 * Receive one byte from FIFO.
333 *
334 * @param        InstancePtr is the instance of IIC
335 *
336 * @return       None.
337 *
338 * @note         C-Style signature:
339 *               u8 XIicPs_RecvByte(XIicPs *InstancePtr)
340 *
341 *****************************************************************************/
342 #define XIicPs_RecvByte(InstancePtr)                                    \
343 {                                                                       \
344         u8 *Data, Value;                                                \
345         Value = (u8)(XIicPs_In32((InstancePtr)->Config.BaseAddress      \
346                   + (u32)XIICPS_DATA_OFFSET));                          \
347         Data = &Value;                                                  \
348         *(InstancePtr)->RecvBufferPtr = *Data;                          \
349         (InstancePtr)->RecvBufferPtr += 1;                              \
350          (InstancePtr)->RecvByteCount --;                               \
351 }
352
353 /************************** Function Prototypes ******************************/
354
355 /*
356  * Function for configuration lookup, in xiicps_sinit.c
357  */
358 XIicPs_Config *XIicPs_LookupConfig(u16 DeviceId);
359
360 /*
361  * Functions for general setup, in xiicps.c
362  */
363 s32 XIicPs_CfgInitialize(XIicPs *InstancePtr, XIicPs_Config * ConfigPtr,
364                                   u32 EffectiveAddr);
365
366 void XIicPs_Abort(XIicPs *InstancePtr);
367 void XIicPs_Reset(XIicPs *InstancePtr);
368
369 s32 XIicPs_BusIsBusy(XIicPs *InstancePtr);
370 s32 TransmitFifoFill(XIicPs *InstancePtr);
371
372 /*
373  * Functions for interrupts, in xiicps_intr.c
374  */
375 void XIicPs_SetStatusHandler(XIicPs *InstancePtr, void *CallBackRef,
376                                   XIicPs_IntrHandler FunctionPtr);
377
378 /*
379  * Functions for device as master, in xiicps_master.c
380  */
381 void XIicPs_MasterSend(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
382                 u16 SlaveAddr);
383 void XIicPs_MasterRecv(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
384                 u16 SlaveAddr);
385 s32 XIicPs_MasterSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
386                 u16 SlaveAddr);
387 s32 XIicPs_MasterRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
388                 u16 SlaveAddr);
389 void XIicPs_EnableSlaveMonitor(XIicPs *InstancePtr, u16 SlaveAddr);
390 void XIicPs_DisableSlaveMonitor(XIicPs *InstancePtr);
391 void XIicPs_MasterInterruptHandler(XIicPs *InstancePtr);
392
393 /*
394  * Functions for device as slave, in xiicps_slave.c
395  */
396 void XIicPs_SetupSlave(XIicPs *InstancePtr, u16 SlaveAddr);
397 void XIicPs_SlaveSend(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
398 void XIicPs_SlaveRecv(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
399 s32 XIicPs_SlaveSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
400 s32 XIicPs_SlaveRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
401 void XIicPs_SlaveInterruptHandler(XIicPs *InstancePtr);
402
403 /*
404  * Functions for selftest, in xiicps_selftest.c
405  */
406 s32 XIicPs_SelfTest(XIicPs *InstancePtr);
407
408 /*
409  * Functions for setting and getting data rate, in xiicps_options.c
410  */
411 s32 XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options);
412 s32 XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options);
413 u32 XIicPs_GetOptions(XIicPs *InstancePtr);
414
415 s32 XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz);
416 u32 XIicPs_GetSClk(XIicPs *InstancePtr);
417
418 #ifdef __cplusplus
419 }
420 #endif
421
422 #endif /* end of protection macro */
423 /** @} */