Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/fmcomms1/Common/i2c_ps7.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file i2c_ps7.c
3 * @brief ZYNQ Hardware I2C functions implementation.
4 * @author ATofan ([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_ps7.h"
48 #include "xparameters.h"
49 #include "xil_io.h"
50 #include "timer.h"
51
52 #ifndef XPAR_PS7_I2C_0_BASEADDR
53 #define XPAR_PS7_I2C_0_BASEADDR 0
54 #endif
55 #define AXI_IIC_BASEADDR_0 XPAR_PS7_I2C_0_BASEADDR
56 #ifdef XPAR_PS7_I2C_1_BASEADDR
57 #define AXI_IIC_BASEADDR_1 XPAR_PS7_I2C_1_BASEADDR
58 #else
59 #define AXI_IIC_BASEADDR_1 XPAR_PS7_I2C_0_BASEADDR
60 #endif
61
62 /*****************************************************************************/
63 /************************ Variables/Constants Definitions ********************/
64 /*****************************************************************************/
65 #define I2C_DELAY 500 //delay in us between I2C operations
66 #define I2C_TIMEOUT 0xFFFFFF //timeout for I2C operations
67
68 static uint32_t axi_iic_baseaddr;
69
70 /**************************************************************************//**
71 * @brief Delays the program execution with the specified number of us.
72 *
73 * @param us_count Number of us with which the program must be delayed.
74 * @return None.
75 ******************************************************************************/
76 extern void delay_us(uint32_t us_count);
77
78 /**************************************************************************//**
79 * @brief Initializes the I2C communication multiplexer.
80 *
81 * @param sel - Multiplexer selection value.
82 *
83 * @return Returns 0 or negative error code.
84 ******************************************************************************/
85 uint32_t I2C_EnableMux_ps7(uint8_t sel)
86 {
87 uint32_t ret;
88 uint8_t retSel;
89
90 // Set the MUX selection
91 ret = I2C_Write_ps7(I2C_MUX_ADDR, -1, 1, &sel);
92
93 if(!ret)
94 return -1;
95
96 // Read back the MUX selection
97 ret = I2C_Read_ps7(I2C_MUX_ADDR, -1, 1, &retSel);
98
99 if(!ret || (sel != retSel))
100 return -1;
101
102 return 0;
103 }
104
105 /**************************************************************************//**
106 * @brief Initializes the communication with the Microblaze I2C peripheral.
107 *
108 * @param i2cAddr - The address of the I2C slave.
109 * @param fmcPort - Set to 0 for LPC, set to 1 for HPC
110 * @param enableCommMux - Set to 1 if the carrier board has an I2C multiplexer,
111 * set to 0 otherwise
112 *
113 * @return Returns 0 or negative error code.
114 ******************************************************************************/
115 uint32_t I2C_Init_ps7(uint32_t i2cAddr, uint32_t fmcPort, uint32_t enableCommMux,
116 uint32_t carrierBoard)
117 {
118 uint32_t ret = 0;
119
120 //set the I2C core AXI address
121 axi_iic_baseaddr = fmcPort == 0 ? AXI_IIC_BASEADDR_0 : AXI_IIC_BASEADDR_1;
122 //enable the I2C mux
123 if(enableCommMux)
124 ret = I2C_EnableMux_ps7(fmcPort == 0 ? (uint8_t)I2C_LPC_PS7 :
125 (uint8_t)I2C_HPC_PS7);
126
127 return ret;
128 }
129
130 /**************************************************************************//**
131 * @brief Reads data from an I2C slave.
132 *
133 * @param i2cAddr - The address of the I2C slave.
134 * @param regAddr - Address of the I2C register to be read.
135 * Must be set to -1 if it is not used.
136 * @param rxSize - Number of bytes to read from the slave.
137 * @param rxBuf - Buffer to store the read data.
138 *
139 * @return Returns the number of bytes read.
140 ******************************************************************************/
141 uint32_t I2C_Read_ps7(uint32_t i2cAddr, uint32_t regAddr,
142 uint32_t rxSize, uint8_t* rxBuf)
143 {
144 uint32_t timeout = I2C_TIMEOUT;
145 uint32_t cfgValue = 0x00;
146 uint32_t rxBufIndex = 0x00;
147
148 // Write the desired register address if required
149 if(regAddr != -1)
150 {
151 I2C_Write_ps7(i2cAddr, regAddr, 0, NULL);
152 }
153 /* Write to the Control Register to set up SCL Speed and addressing mode
154 Set the MS, ACKEN, CLR_FIFO and RW bit */
155 cfgValue = (0 << DIV_A) |
156 (0x3f << DIV_B) |
157 (1 << CLR_FIFO) |
158 (0 << SLVMON) |
159 (0 << HOLD) |
160 (1 << ACK_EN) |
161 (1 << NEA) |
162 (1 << MS) |
163 (1 << RW);
164 Xil_Out32(axi_iic_baseaddr + HW_I2C_CONTROL_REG, cfgValue);
165 // Clear all Interrupts
166 Xil_Out32(axi_iic_baseaddr + HW_I2C_INTR_STATUS_REG, 0xFF);
167 // Write the slave address into the I2C address register. This initiates the I2C Transfer.
168 Xil_Out32(axi_iic_baseaddr + HW_I2C_ADDRESS_REG, i2cAddr);
169 // Write the number of requested bytes
170 Xil_Out32(axi_iic_baseaddr + HW_I2C_TX_SIZE_REG, rxSize & 0xFF);
171 // Write zeros to TXDATA Register
172 Xil_Out32(axi_iic_baseaddr + HW_I2C_DATA_REG, 0x00);
173 delay_us(I2C_DELAY);
174 // Read data from FIFO
175 while(rxBufIndex < rxSize)
176 {
177 // Wait for data to be available
178 while (((Xil_In32(axi_iic_baseaddr + HW_I2C_STATUS_REG) & 0x20) == 0x00) && (timeout--));
179 if(timeout == - 1)
180 {
181 return(rxBufIndex);
182 }
183 timeout = I2C_TIMEOUT;
184
185 rxBuf[rxBufIndex] = Xil_In32(axi_iic_baseaddr + HW_I2C_DATA_REG) & 0xFF;
186 rxBufIndex += 1;
187 }
188
189 return(rxBufIndex);
190 }
191
192 /**************************************************************************//**
193 * @brief Writes data to an I2C slave.
194 *
195 * @param i2cAddr - The address of the I2C slave.
196 * @param regAddr - Address of the I2C register to be read.
197 * Must be set to -1 if it is not used.
198 * @param txSize - Number of bytes to write to the slave.
199 * @param txBuf - Buffer to store the data to be transmitted.
200 *
201 * @return Returns the number of bytes written.
202 ******************************************************************************/
203 uint32_t I2C_Write_ps7(uint32_t i2cAddr, uint32_t regAddr,
204 uint32_t txSize, uint8_t* txBuf)
205 {
206 uint32_t timeout = I2C_TIMEOUT;
207 uint32_t cfgValue = 0x00;
208 uint32_t txBufIndex = 0x00;
209
210 /* Write to the Control Register to set up SCL Speed and addressing mode
211 Set the MS, ACKEN, CLR_FIFO bits and clear the RW bit */
212 cfgValue = (0 << DIV_A) |
213 (0x3f << DIV_B) |
214 (1 << CLR_FIFO) |
215 (0 << SLVMON) |
216 (0 << HOLD) |
217 (1 << ACK_EN) |
218 (1 << NEA) |
219 (1 << MS) |
220 (0 << RW);
221 Xil_Out32(axi_iic_baseaddr + HW_I2C_CONTROL_REG, cfgValue);
222 // Clear all Interrupts
223 Xil_Out32(axi_iic_baseaddr + HW_I2C_INTR_STATUS_REG, 0xFF);
224 // Set the transfer size
225 if(regAddr != -1)
226 {
227 Xil_Out32(axi_iic_baseaddr + HW_I2C_TX_SIZE_REG, txSize + 1);
228 }
229 else
230 {
231 Xil_Out32(axi_iic_baseaddr + HW_I2C_TX_SIZE_REG, txSize);
232 }
233 // Write the first byte of data to the I2C Data Register
234 if(regAddr != -1)
235 {
236 Xil_Out32(axi_iic_baseaddr + HW_I2C_DATA_REG, regAddr);
237 }
238 else
239 {
240 Xil_Out32(axi_iic_baseaddr + HW_I2C_DATA_REG, txBuf[txBufIndex]);
241 txBufIndex += 1;
242 }
243 // Write the rest of the data into the FIFO
244 while(txBufIndex < txSize)
245 {
246 Xil_Out32(axi_iic_baseaddr + HW_I2C_DATA_REG, txBuf[txBufIndex]);
247 txBufIndex += 1;
248 }
249 // Write the slave address into the I2C address register. This initiates the I2C Transfer.
250 Xil_Out32(axi_iic_baseaddr + HW_I2C_ADDRESS_REG, i2cAddr);
251 // Wait for data transmission to be complete
252 while(((Xil_In32(axi_iic_baseaddr + HW_I2C_INTR_STATUS_REG) & 0x01) == 0x00) && (--timeout));
253
254 delay_us(I2C_DELAY);
255
256 return(timeout ? txBufIndex : 0);
257 }