Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/Common/sw/i2c_axi.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file i2c_axi.c
3 * @brief I2C functions implementation.
4 * @author acozma ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2011(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************
41 * SVN Revision: $WCREV$
42 ******************************************************************************/
43
44 /*****************************************************************************/
45 /***************************** Include Files *********************************/
46 /*****************************************************************************/
47 #include "i2c_axi.h"
48 #include "xil_io.h"
49
50 /*****************************************************************************/
51 /************************ Variables Definitions ******************************/
52 /*****************************************************************************/
53
54 /**************************************************************************//**
55 * @brief Delays the program execution with the specified number of ms.
56 *
57 * @param ns_count Number of ms with which the program must be delayed.
58 * @return None.
59 ******************************************************************************/
60 void delay_ms(u32 ms_count)
61 {
62 u32 count;
63 for (count = 0; count < ((ms_count * 100000) + 1); count++)
64 {
65 asm("nop");
66 }
67 }
68
69 /**************************************************************************//**
70 * @brief Initializes the communication with the Microblaze I2C peripheral.
71 *
72 * @param axiBaseAddr - Microblaze I2C peripheral AXI base address.
73 * @param i2cAddr - The address of the I2C slave.
74 * @return TRUE.
75 ******************************************************************************/
76 u32 I2C_Init_axi(u32 axiBaseAddr, u32 i2cAddr)
77 {
78 //disable the I2C core
79 Xil_Out32((axiBaseAddr + CR), 0x00);
80 //set the Rx FIFO depth to maximum
81 Xil_Out32((axiBaseAddr + RX_FIFO_PIRQ), 0x0F);
82 //reset the I2C core and flush the Tx fifo
83 Xil_Out32((axiBaseAddr + CR), 0x02);
84 //enable the I2C core
85 Xil_Out32((axiBaseAddr + CR), 0x01);
86
87 return TRUE;
88 }
89
90 /**************************************************************************//**
91 * @brief Reads data from an I2C slave.
92 *
93 * @param axiBaseAddr - Microblaze I2C peripheral AXI base address.
94 * @param i2cAddr - The address of the I2C slave.
95 * @param regAddr - Address of the I2C register to be read.
96 * Must be set to -1 if it is not used.
97 * @param rxSize - Number of bytes to read from the slave.
98 * @param rxBuf - Buffer to store the read data.
99 * @return Returns the number of bytes read.
100 ******************************************************************************/
101 u32 I2C_Read_axi(u32 axiBaseAddr, u32 i2cAddr, u32 regAddr, u32 rxSize, unsigned char* rxBuf)
102 {
103 u32 rxCnt = 0;
104 u32 timeout = 0xFFFFFF;
105
106 // Reset tx fifo
107 Xil_Out32((axiBaseAddr + CR), 0x002);
108 // Enable iic
109 Xil_Out32((axiBaseAddr + CR), 0x001);
110 delay_ms(10);
111
112 if(regAddr != -1)
113 {
114 // Set the slave I2C address
115 Xil_Out32((axiBaseAddr + TX_FIFO), (0x100 | (i2cAddr << 1)));
116 // Set the slave register address
117 Xil_Out32((axiBaseAddr + TX_FIFO), regAddr);
118 }
119
120 // Set the slave I2C address
121 Xil_Out32((axiBaseAddr + TX_FIFO), (0x101 | (i2cAddr << 1)));
122 // Start a read transaction
123 Xil_Out32((axiBaseAddr + TX_FIFO), 0x200 + rxSize);
124
125 // Read data from the I2C slave
126 while(rxCnt < rxSize)
127 {
128 //wait for data to be available in the RxFifo
129 while((Xil_In32(axiBaseAddr + SR) & 0x00000040) && (timeout--));
130 if(timeout == -1)
131 {
132 //disable the I2C core
133 Xil_Out32((axiBaseAddr + CR), 0x00);
134 //set the Rx FIFO depth to maximum
135 Xil_Out32((axiBaseAddr + RX_FIFO_PIRQ), 0x0F);
136 //reset the I2C core and flush the Tx fifo
137 Xil_Out32((axiBaseAddr + CR), 0x02);
138 //enable the I2C core
139 Xil_Out32((axiBaseAddr + CR), 0x01);
140 return rxCnt;
141 }
142 timeout = 0xFFFFFF;
143
144 //read the data
145 rxBuf[rxCnt] = Xil_In32(axiBaseAddr + RX_FIFO) & 0xFFFF;
146
147 //increment the receive counter
148 rxCnt++;
149 }
150
151 delay_ms(10);
152
153 return rxCnt;
154 }
155
156 /**************************************************************************//**
157 * @brief Writes data to an I2C slave.
158 *
159 * @param axiBaseAddr - Microblaze I2C peripheral AXI base address.
160 * @param i2cAddr - The address of the I2C slave.
161 * @param regAddr - Address of the I2C register to be read.
162 * Must be set to -1 if it is not used.
163 * @param txSize - Number of bytes to write to the slave.
164 * @param txBuf - Buffer to store the data to be transmitted.
165 * @return Returns the number of bytes written.
166 ******************************************************************************/
167 u32 I2C_Write_axi(u32 axiBaseAddr, u32 i2cAddr, u32 regAddr, u32 txSize, unsigned char* txBuf)
168 {
169 u32 txCnt = 0;
170
171 // Reset tx fifo
172 Xil_Out32((axiBaseAddr + CR), 0x002);
173 // enable iic
174 Xil_Out32((axiBaseAddr + CR), 0x001);
175 delay_ms(10);
176
177 // Set the I2C address
178 Xil_Out32((axiBaseAddr + TX_FIFO), (0x100 | (i2cAddr << 1)));
179 if(regAddr != -1)
180 {
181 // Set the slave register address
182 Xil_Out32((axiBaseAddr + TX_FIFO), regAddr);
183 }
184
185 // Write data to the I2C slave
186 while(txCnt < txSize)
187 {
188 // put the Tx data into the Tx FIFO
189 Xil_Out32((axiBaseAddr + TX_FIFO), (txCnt == txSize - 1) ? (0x200 | txBuf[txCnt]) : txBuf[txCnt]);
190 txCnt++;
191 }
192
193 delay_ms(10);
194
195 return txCnt;
196 }