| 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 | #include "system_config.h" |
| 52 | |
| 53 | #if(USE_PS7 == 0) |
| 54 | /*****************************************************************************/ |
| 55 | /************************ Variables/Constants Definitions ********************/ |
| 56 | /*****************************************************************************/ |
| 57 | #define I2C_DELAY 500 //delay in us between I2C operations |
| 58 | #define I2C_TIMEOUT 0xFFFFFF //timeout for I2C operations |
| 59 | |
| 60 | /**************************************************************************//** |
| 61 | * @brief Delays the program execution with the specified number of us. |
| 62 | * |
| 63 | * @param us_count Number of us with which the program must be delayed. |
| 64 | * @return None. |
| 65 | ******************************************************************************/ |
| 66 | extern void delay_us(uint32_t us_count); |
| 67 | |
| 68 | /**************************************************************************//** |
| 69 | * @brief Initializes the communication with the Microblaze I2C peripheral. |
| 70 | * |
| 71 | * @param i2cAddr - The address of the I2C slave. |
| 72 | * @param fmcPort - Set to 0 for LPC, set to 1 for HPC |
| 73 | * @param enableCommMux - Set to 1 if the carrier board has an I2C multiplexer, |
| 74 | * set to 0 otherwise |
| 75 | * |
| 76 | * @return Returns 0 or negative error code. |
| 77 | ******************************************************************************/ |
| 78 | uint32_t I2C_Init_ps7(int i2cBaseAddr, uint32_t i2cAddr) |
| 79 | { |
| 80 | return(0); |
| 81 | } |
| 82 | |
| 83 | /**************************************************************************//** |
| 84 | * @brief Reads data from an I2C slave. |
| 85 | * |
| 86 | * @param i2cAddr - The address of the I2C slave. |
| 87 | * @param regAddr - Address of the I2C register to be read. |
| 88 | * Must be set to -1 if it is not used. |
| 89 | * @param rxSize - Number of bytes to read from the slave. |
| 90 | * @param rxBuf - Buffer to store the read data. |
| 91 | * |
| 92 | * @return Returns the number of bytes read. |
| 93 | ******************************************************************************/ |
| 94 | uint32_t I2C_Read_ps7(int i2cBaseAddr, uint32_t i2cAddr, uint32_t regAddr, uint32_t rxSize, uint8_t* rxBuf) |
| 95 | { |
| 96 | uint32_t timeout = I2C_TIMEOUT; |
| 97 | uint32_t cfgValue = 0x00; |
| 98 | uint32_t rxBufIndex = 0x00; |
| 99 | |
| 100 | // Write the desired register address if required |
| 101 | if(regAddr != -1) |
| 102 | { |
| 103 | I2C_Write_ps7(i2cBaseAddr, i2cAddr, regAddr, 0, NULL); |
| 104 | } |
| 105 | /* Write to the Control Register to set up SCL Speed and addressing mode |
| 106 | Set the MS, ACKEN, CLR_FIFO and RW bit */ |
| 107 | cfgValue = (0 << DIV_A) | |
| 108 | (0x3f << DIV_B) | |
| 109 | (1 << CLR_FIFO) | |
| 110 | (0 << SLVMON) | |
| 111 | (0 << HOLD) | |
| 112 | (1 << ACK_EN) | |
| 113 | (1 << NEA) | |
| 114 | (1 << MS) | |
| 115 | (1 << RW); |
| 116 | Xil_Out32(i2cBaseAddr + HW_I2C_CONTROL_REG, cfgValue); |
| 117 | // Clear all Interrupts |
| 118 | Xil_Out32(i2cBaseAddr + HW_I2C_INTR_STATUS_REG, 0xFF); |
| 119 | // Write the slave address into the I2C address register. This initiates the I2C Transfer. |
| 120 | Xil_Out32(i2cBaseAddr + HW_I2C_ADDRESS_REG, i2cAddr); |
| 121 | // Write the number of requested bytes |
| 122 | Xil_Out32(i2cBaseAddr + HW_I2C_TX_SIZE_REG, rxSize & 0xFF); |
| 123 | // Write zeros to TXDATA Register |
| 124 | Xil_Out32(i2cBaseAddr + HW_I2C_DATA_REG, 0x00); |
| 125 | delay_us(I2C_DELAY); |
| 126 | // Read data from FIFO |
| 127 | while(rxBufIndex < rxSize) |
| 128 | { |
| 129 | // Wait for data to be available |
| 130 | while (((Xil_In32(i2cBaseAddr + HW_I2C_STATUS_REG) & 0x20) == 0x00) && (timeout--)); |
| 131 | if(timeout == - 1) |
| 132 | { |
| 133 | return(rxBufIndex); |
| 134 | } |
| 135 | timeout = I2C_TIMEOUT; |
| 136 | |
| 137 | rxBuf[rxBufIndex] = Xil_In32(i2cBaseAddr + HW_I2C_DATA_REG) & 0xFF; |
| 138 | rxBufIndex += 1; |
| 139 | } |
| 140 | |
| 141 | return(rxBufIndex); |
| 142 | } |
| 143 | |
| 144 | /**************************************************************************//** |
| 145 | * @brief Writes data to an I2C slave. |
| 146 | * |
| 147 | * @param i2cAddr - The address of the I2C slave. |
| 148 | * @param regAddr - Address of the I2C register to be read. |
| 149 | * Must be set to -1 if it is not used. |
| 150 | * @param txSize - Number of bytes to write to the slave. |
| 151 | * @param txBuf - Buffer to store the data to be transmitted. |
| 152 | * |
| 153 | * @return Returns the number of bytes written. |
| 154 | ******************************************************************************/ |
| 155 | uint32_t I2C_Write_ps7(int i2cBaseAddr, uint32_t i2cAddr, uint32_t regAddr, uint32_t txSize, uint8_t* txBuf) |
| 156 | { |
| 157 | uint32_t timeout = I2C_TIMEOUT; |
| 158 | uint32_t cfgValue = 0x00; |
| 159 | uint32_t txBufIndex = 0x00; |
| 160 | |
| 161 | /* Write to the Control Register to set up SCL Speed and addressing mode |
| 162 | Set the MS, ACKEN, CLR_FIFO bits and clear the RW bit */ |
| 163 | cfgValue = (0 << DIV_A) | |
| 164 | (0x3f << DIV_B) | |
| 165 | (1 << CLR_FIFO) | |
| 166 | (0 << SLVMON) | |
| 167 | (0 << HOLD) | |
| 168 | (1 << ACK_EN) | |
| 169 | (1 << NEA) | |
| 170 | (1 << MS) | |
| 171 | (0 << RW); |
| 172 | Xil_Out32(i2cBaseAddr + HW_I2C_CONTROL_REG, cfgValue); |
| 173 | // Clear all Interrupts |
| 174 | Xil_Out32(i2cBaseAddr + HW_I2C_INTR_STATUS_REG, 0xFF); |
| 175 | // Set the transfer size |
| 176 | if(regAddr != -1) |
| 177 | { |
| 178 | Xil_Out32(i2cBaseAddr + HW_I2C_TX_SIZE_REG, txSize + 1); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | Xil_Out32(i2cBaseAddr + HW_I2C_TX_SIZE_REG, txSize); |
| 183 | } |
| 184 | // Write the first byte of data to the I2C Data Register |
| 185 | if(regAddr != -1) |
| 186 | { |
| 187 | Xil_Out32(i2cBaseAddr + HW_I2C_DATA_REG, regAddr); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | Xil_Out32(i2cBaseAddr + HW_I2C_DATA_REG, txBuf[txBufIndex]); |
| 192 | txBufIndex += 1; |
| 193 | } |
| 194 | // Write the rest of the data into the FIFO |
| 195 | while(txBufIndex < txSize) |
| 196 | { |
| 197 | Xil_Out32(i2cBaseAddr + HW_I2C_DATA_REG, txBuf[txBufIndex]); |
| 198 | txBufIndex += 1; |
| 199 | } |
| 200 | // Write the slave address into the I2C address register. This initiates the I2C Transfer. |
| 201 | Xil_Out32(i2cBaseAddr + HW_I2C_ADDRESS_REG, i2cAddr); |
| 202 | // Wait for data transmission to be complete |
| 203 | while(((Xil_In32(i2cBaseAddr+ HW_I2C_INTR_STATUS_REG) & 0x01) == 0x00) && (--timeout)); |
| 204 | |
| 205 | delay_us(I2C_DELAY); |
| 206 | |
| 207 | return(timeout ? txBufIndex : 0); |
| 208 | } |
| 209 | #endif |