Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/fmcomms1/Common/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 "xparameters.h"
49 #include "xil_io.h"
50 #include "timer.h"
51
52 #ifndef XPAR_AXI_IIC_MAIN_BASEADDR
53 #ifndef XPAR_AXI_IIC_0_BASEADDR
54 #define XPAR_AXI_IIC_0_BASEADDR 0
55 #endif
56 #define AXI_IIC_BASEADDR_0 XPAR_AXI_IIC_0_BASEADDR
57 #ifdef XPAR_AXI_IIC_1_BASEADDR
58 #define AXI_IIC_BASEADDR_1 XPAR_AXI_IIC_1_BASEADDR
59 #else
60 #define AXI_IIC_BASEADDR_1 XPAR_AXI_IIC_0_BASEADDR
61 #endif
62 #else
63 #define AXI_IIC_BASEADDR_0 XPAR_AXI_IIC_MAIN_BASEADDR
64 #ifdef XPAR_AXI_IIC_FMC_BASEADDR
65 #define AXI_IIC_BASEADDR_1 XPAR_AXI_IIC_FMC_BASEADDR
66 #else
67 #define AXI_IIC_BASEADDR_1 XPAR_AXI_IIC_MAIN_BASEADDR
68 #endif
69 #endif
70
71 /*****************************************************************************/
72 /******************* I2C Registers Definitions *******************************/
73 /*****************************************************************************/
74 #define GIE 0x01C
75 #define ISR 0x020
76 #define IER 0x028
77 #define SOFTR 0x040
78 #define CR 0x100
79 #define SR 0x104
80 #define TX_FIFO 0x108
81 #define RX_FIFO 0x10C
82 #define ADR 0x110
83 #define TX_FIFO_OCY 0x114
84 #define RX_FIFO_OCY 0x118
85 #define TEN_ADDR 0x11C
86 #define RX_FIFO_PIRQ 0x120
87 #define GPO 0x124
88
89 /*****************************************************************************/
90 /************************ Variables/Constants Definitions ********************/
91 /*****************************************************************************/
92 #define I2C_DELAY 200 //delay in us between I2C operations
93 #define I2C_TIMEOUT 0xFFFFFF //timeout for I2C operations
94
95 static uint32_t axi_iic_baseaddr;
96
97 /**************************************************************************//**
98 * @brief Delays the program execution with the specified number of us.
99 *
100 * @param us_count Number of us with which the program must be delayed.
101 * @return None.
102 ******************************************************************************/
103 void delay_us(uint32_t us_count)
104 {
105 #ifdef _XPARAMETERS_PS_H_
106 volatile uint32_t i;
107 for(i = 0; i < us_count*500; i++);
108 #else
109 #ifdef XPAR_AXI_TIMER_0_BASEADDR
110 TIMER0_WAIT(XPAR_AXI_TIMER_0_BASEADDR, us_count*1000);
111 #else
112 TIMER0_WAIT(XPAR_AXI_TIMER_BASEADDR, us_count*1000);
113 #endif
114 #endif
115 }
116 /**************************************************************************//**
117 * @brief Initializes the I2C communication multiplexer.
118 *
119 * @param sel - Multiplexer selection value.
120 *
121 * @return Returns 0 or negative error code.
122 ******************************************************************************/
123 uint32_t I2C_EnableMux_axi(uint8_t sel)
124 {
125 uint32_t ret;
126 uint8_t retSel;
127
128 // Reset I2C mux
129 Xil_Out32((axi_iic_baseaddr + GPO), 0x000);
130 Xil_Out32((axi_iic_baseaddr + GPO), 0x001);
131
132 // Set the MUX selection
133 ret = I2C_Write_axi(I2C_MUX_ADDR, -1, 1, &sel);
134 if(!ret)
135 return -1;
136
137 // Read back the MUX selection
138 ret = I2C_Read_axi(I2C_MUX_ADDR, -1, 1, &retSel);
139 if(!ret || (sel != retSel))
140 return -1;
141
142 return 0;
143 }
144
145 /**************************************************************************//**
146 * @brief Initializes the communication with the Microblaze I2C peripheral.
147 *
148 * @param i2cAddr - The address of the I2C slave.
149 * @param fmcPort - Set to 0 for LPC, set to 1 for HPC
150 * @param enableCommMux - Set to 1 if the carrier board has an I2C multiplexer,
151 * set to 0 otherwise
152 *
153 * @return Returns 0 or negative error code.
154 ******************************************************************************/
155 uint32_t I2C_Init_axi(uint32_t i2cAddr, uint32_t fmcPort, uint32_t enableCommMux,
156 uint32_t carrierBoard)
157 {
158 uint32_t ret = 0;
159
160 //set the I2C core AXI address
161 axi_iic_baseaddr = fmcPort == 0 ? AXI_IIC_BASEADDR_0 : AXI_IIC_BASEADDR_1;
162 //disable the I2C core
163 Xil_Out32((axi_iic_baseaddr + CR), 0x00);
164 //set the Rx FIFO depth to maximum
165 Xil_Out32((axi_iic_baseaddr + RX_FIFO_PIRQ), 0x0F);
166 //reset the I2C core and flush the Tx fifo
167 Xil_Out32((axi_iic_baseaddr + CR), 0x02);
168 //enable the I2C core
169 Xil_Out32((axi_iic_baseaddr + CR), 0x01);
170 //enable the I2C mux
171 if(enableCommMux)
172 {
173 if(carrierBoard == 4)
174 ret = I2C_EnableMux_axi(0x20);
175 else
176 if(carrierBoard == 5)
177 ret = I2C_EnableMux_axi(0x40);
178 else
179 ret = I2C_EnableMux_axi(fmcPort == 0 ? (uint8_t)I2C_LPC_AXI :
180 (uint8_t)I2C_HPC_AXI);
181 }
182
183 return ret;
184 }
185
186 /**************************************************************************//**
187 * @brief Reads data from an I2C slave.
188 *
189 * @param i2cAddr - The address of the I2C slave.
190 * @param regAddr - Address of the I2C register to be read.
191 * Must be set to -1 if it is not used.
192 * @param rxSize - Number of bytes to read from the slave.
193 * @param rxBuf - Buffer to store the read data.
194 *
195 * @return Returns the number of bytes read.
196 ******************************************************************************/
197 uint32_t I2C_Read_axi(uint32_t i2cAddr, uint32_t regAddr,
198 uint32_t rxSize, uint8_t* rxBuf)
199 {
200 uint32_t rxCnt = 0;
201 uint32_t timeout = I2C_TIMEOUT;
202
203 // Reset tx fifo
204 Xil_Out32((axi_iic_baseaddr + CR), 0x002);
205 // Enable iic
206 Xil_Out32((axi_iic_baseaddr + CR), 0x001);
207 delay_us(I2C_DELAY);
208
209 if(regAddr != -1)
210 {
211 // Set the slave I2C address
212 Xil_Out32((axi_iic_baseaddr + TX_FIFO), (0x100 | (i2cAddr << 1)));
213 // Set the slave register address
214 Xil_Out32((axi_iic_baseaddr + TX_FIFO), regAddr);
215 }
216
217 // Set the slave I2C address
218 Xil_Out32((axi_iic_baseaddr + TX_FIFO), (0x101 | (i2cAddr << 1)));
219 // Start a read transaction
220 Xil_Out32((axi_iic_baseaddr + TX_FIFO), 0x200 + rxSize);
221
222 // Read data from the I2C slave
223 while(rxCnt < rxSize)
224 {
225 //wait for data to be available in the RxFifo
226 while((Xil_In32(axi_iic_baseaddr + SR) & 0x00000040) && (timeout--));
227 if(timeout == -1)
228 {
229 //disable the I2C core
230 Xil_Out32((axi_iic_baseaddr + CR), 0x00);
231 //set the Rx FIFO depth to maximum
232 Xil_Out32((axi_iic_baseaddr + RX_FIFO_PIRQ), 0x0F);
233 //reset the I2C core and flush the Tx fifo
234 Xil_Out32((axi_iic_baseaddr + CR), 0x02);
235 //enable the I2C core
236 Xil_Out32((axi_iic_baseaddr + CR), 0x01);
237 return rxCnt;
238 }
239 timeout = I2C_TIMEOUT;
240
241 //read the data
242 rxBuf[rxCnt] = Xil_In32(axi_iic_baseaddr + RX_FIFO) & 0xFFFF;
243
244 //increment the receive counter
245 rxCnt++;
246 }
247
248 delay_us(I2C_DELAY);
249
250 return rxCnt;
251 }
252
253 /**************************************************************************//**
254 * @brief Writes data to an I2C slave.
255 *
256 * @param i2cAddr - The address of the I2C slave.
257 * @param regAddr - Address of the I2C register to be read.
258 * Must be set to -1 if it is not used.
259 * @param txSize - Number of bytes to write to the slave.
260 * @param txBuf - Buffer to store the data to be transmitted.
261 *
262 * @return Returns the number of bytes written.
263 ******************************************************************************/
264 uint32_t I2C_Write_axi(uint32_t i2cAddr, uint32_t regAddr,
265 uint32_t txSize, uint8_t* txBuf)
266 {
267 uint32_t txCnt = 0;
268 uint32_t timeout = I2C_TIMEOUT;
269
270 // Reset tx fifo
271 Xil_Out32((axi_iic_baseaddr + CR), 0x002);
272 // enable iic
273 Xil_Out32((axi_iic_baseaddr + CR), 0x001);
274 delay_us(I2C_DELAY);
275
276 // Set the I2C address
277 Xil_Out32((axi_iic_baseaddr + TX_FIFO), (0x100 | (i2cAddr << 1)));
278 if(regAddr != -1)
279 {
280 // Set the slave register address
281 Xil_Out32((axi_iic_baseaddr + TX_FIFO), regAddr);
282 }
283
284 // Write data to the I2C slave
285 while((txCnt < txSize) && (timeout))
286 {
287 timeout = I2C_TIMEOUT;
288 // put the Tx data into the Tx FIFO
289 Xil_Out32((axi_iic_baseaddr + TX_FIFO), (txCnt == txSize - 1) ? (0x200 | txBuf[txCnt]) : txBuf[txCnt]);
290 while (((Xil_In32(axi_iic_baseaddr + SR) & 0x80) == 0x00) && (--timeout));
291 txCnt++;
292 }
293 delay_us(I2C_DELAY);
294
295 return (timeout ? txCnt : 0);
296 }