| 1 | /**************************************************************************//** |
| 2 | * @file spi_interface.c |
| 3 | * @brief SPI interface 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_ps7.h" |
| 48 | #include "i2c_axi.h" |
| 49 | #include "spi_interface.h" |
| 50 | |
| 51 | /* Pointer to functions - used depending on the type of I2C Core used (Hardware or Softcore) */ |
| 52 | uint32_t (*I2C_Write)(uint32_t, uint32_t, uint32_t, uint8_t*); |
| 53 | uint32_t (*I2C_Read)(uint32_t, uint32_t, uint32_t, uint8_t*); |
| 54 | uint32_t (*I2C_Init)(uint32_t, uint32_t, uint32_t, uint32_t); |
| 55 | |
| 56 | /*****************************************************************************/ |
| 57 | /************************ Constants Definitions ******************************/ |
| 58 | /*****************************************************************************/ |
| 59 | static const stDevConfig devConfig[] = |
| 60 | { |
| 61 | /* AD9122 */ |
| 62 | { |
| 63 | 8, 8, |
| 64 | (SPI_4_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 65 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 66 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 67 | (1 << SPI_SEL_AD9122) |
| 68 | }, |
| 69 | /* AD9643 */ |
| 70 | { |
| 71 | 16, 8, |
| 72 | (SPI_3_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 73 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 74 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 75 | (1 << SPI_SEL_AD9643) |
| 76 | }, |
| 77 | /* AD9548 */ |
| 78 | { |
| 79 | 16, 8, |
| 80 | (SPI_3_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 81 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 82 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 83 | (1 << SPI_SEL_AD9548) |
| 84 | }, |
| 85 | /* AD9523 */ |
| 86 | { |
| 87 | 16, 8, |
| 88 | (SPI_3_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 89 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 90 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 91 | (1 << SPI_SEL_AD9523) |
| 92 | }, |
| 93 | /* ADF4351_RX */ |
| 94 | { |
| 95 | 0, 32, |
| 96 | (SPI_4_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 97 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 98 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 99 | (1 << SPI_SEL_ADF4351_RX) |
| 100 | }, |
| 101 | /* ADF4351_TX */ |
| 102 | { |
| 103 | 0, 32, |
| 104 | (SPI_4_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 105 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 106 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 107 | (1 << SPI_SEL_ADF4351_TX) |
| 108 | }, |
| 109 | /* AD8366_TX */ |
| 110 | { |
| 111 | 0, 16, |
| 112 | (SPI_4_WIRE_MODE | SPI_CS_LOW_AT_TRANFER_END | |
| 113 | SPI_SAMPLE_AT_CLK_MIDDLE | SPI_TX_ON_CLK_FALL | |
| 114 | SPI_CLK_IDLE_LOW | SPI_CLK_FOSC_DIV_4), |
| 115 | (1 << SPI_SEL_AD8366) |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | /*****************************************************************************/ |
| 120 | /************************ Variables Definitions ******************************/ |
| 121 | /*****************************************************************************/ |
| 122 | static uint32_t picI2cAddr; |
| 123 | |
| 124 | /**************************************************************************//** |
| 125 | * @brief Configures the PIC for the next data transfer with the device |
| 126 | * |
| 127 | * @param spiSel - SPI CS number |
| 128 | * @param rxCnt - Number of bytes to read from the device |
| 129 | * @param csState - State of CS line at the end of the transfer |
| 130 | * |
| 131 | * @return None |
| 132 | ******************************************************************************/ |
| 133 | void PIC_Config(uint32_t spiSel, uint32_t rxCnt, uint8_t csState) |
| 134 | { |
| 135 | uint8_t wrSize; |
| 136 | uint8_t wrBuf[8]; |
| 137 | uint32_t spiConfig = devConfig[spiSel].spiConfig; |
| 138 | |
| 139 | /* Add to the SPI configuration the Rx size and the CS state */ |
| 140 | spiConfig += SPI_RX_TRANSFER_CNT(rxCnt); |
| 141 | spiConfig |= csState; |
| 142 | |
| 143 | /* Build the PIC configuration command */ |
| 144 | wrSize = 5; |
| 145 | wrBuf[0] = CTRL_WRITE; |
| 146 | wrBuf[1] = ((spiConfig >> 8) & 0xFF); |
| 147 | wrBuf[2] = (spiConfig) & 0xFF; |
| 148 | wrBuf[3] = (devConfig[spiSel].spiCS >> 8) & 0xFF; |
| 149 | wrBuf[4] = (devConfig[spiSel].spiCS) & 0xFF; |
| 150 | |
| 151 | /* Write data to the PIC */ |
| 152 | I2C_Write(picI2cAddr, -1, wrSize, wrBuf); |
| 153 | } |
| 154 | |
| 155 | /**************************************************************************//** |
| 156 | * @brief Writes data to the PIC |
| 157 | * |
| 158 | * @param spiSel - SPI CS number |
| 159 | * @param size - Number of bytes to be written |
| 160 | * @param data - Data to be written |
| 161 | * |
| 162 | * @return Returns the number of written bytes |
| 163 | ******************************************************************************/ |
| 164 | uint32_t PIC_Write(uint32_t spiSel, uint8_t size, uint32_t data) |
| 165 | { |
| 166 | uint8_t wrSize; |
| 167 | uint8_t wrBuf[8]; |
| 168 | |
| 169 | /* Build the write buffer */ |
| 170 | wrSize = size + 1; |
| 171 | wrBuf[0] = DATA_WRITE; |
| 172 | while(size) |
| 173 | { |
| 174 | wrBuf[size] = data & 0xFF; |
| 175 | data >>= 8; |
| 176 | size--; |
| 177 | } |
| 178 | |
| 179 | /* Write data to the PIC */ |
| 180 | return (I2C_Write(picI2cAddr, -1, wrSize, wrBuf) - 1); |
| 181 | } |
| 182 | |
| 183 | /**************************************************************************//** |
| 184 | * @brief Reads data from the PIC |
| 185 | * |
| 186 | * @param spiSel - SPI CS number |
| 187 | * @param size - The number of bytes to be read |
| 188 | * @param data - Variable to store the read data |
| 189 | * |
| 190 | * @return Returns the number of bytes read from the device |
| 191 | ******************************************************************************/ |
| 192 | uint32_t PIC_Read(uint32_t spiSel, uint8_t size, uint32_t* data) |
| 193 | { |
| 194 | int32_t i = 0; |
| 195 | uint32_t rSize = size; |
| 196 | uint8_t rdBuf[8]; |
| 197 | |
| 198 | /* Read data from the PIC */ |
| 199 | rSize = I2C_Read(picI2cAddr, -1, size, rdBuf); |
| 200 | |
| 201 | /* Build the result from the read data */ |
| 202 | *data = 0; |
| 203 | for(i = 0; i < size; i++) |
| 204 | { |
| 205 | *data = (*data << 8) | rdBuf[i]; |
| 206 | } |
| 207 | |
| 208 | return rSize; |
| 209 | } |
| 210 | |
| 211 | /**************************************************************************//** |
| 212 | * @brief Reads the PIC firmware version |
| 213 | * |
| 214 | * @return Returns the PIC firmware version number or -1 in case of error |
| 215 | ******************************************************************************/ |
| 216 | int32_t PIC_ReadFwVersion() |
| 217 | { |
| 218 | int32_t i; |
| 219 | int32_t ret; |
| 220 | uint8_t wrBuf[1] = {REV_READ}; |
| 221 | uint8_t rdBuf[PIC_FW_REV_LEN]; |
| 222 | |
| 223 | ret = I2C_Write(picI2cAddr, -1, 1, wrBuf); |
| 224 | if(ret == 0) |
| 225 | return -1; |
| 226 | |
| 227 | ret = I2C_Read(picI2cAddr, -1, PIC_FW_REV_LEN, rdBuf); |
| 228 | if(ret != PIC_FW_REV_LEN) |
| 229 | return -1; |
| 230 | |
| 231 | for(i = 0; i < PIC_FW_REV_LEN - 1; i++) |
| 232 | { |
| 233 | if(rdBuf[i] == 'F' && rdBuf[i+1] == 'W') |
| 234 | break; |
| 235 | } |
| 236 | if(i < PIC_FW_REV_LEN - 5) |
| 237 | ret = (rdBuf[i+2] - 0x30)*100 + (rdBuf[i+3] - 0x30)*10 + (rdBuf[i+4] - 0x30)*1; |
| 238 | else |
| 239 | ret = 0; |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | /**************************************************************************//** |
| 245 | * @brief Initializes the communication with the PIC |
| 246 | * |
| 247 | * @param fmcPort - The FMC port on which the daughter board is connected |
| 248 | * @param enableCommMux - Set to 1 if the carrier board has an I2C multiplexer, |
| 249 | * set to 0 otherwise |
| 250 | * @param ps7I2C - Set to 1 if PS7 I2C Core is used |
| 251 | * set to 0 if AXI I2C Core is used |
| 252 | * |
| 253 | * @return Returns -1 in case of error, 0 for success |
| 254 | ******************************************************************************/ |
| 255 | int32_t SPI_Init(uint32_t fmcPort, uint32_t enableCommMux, uint32_t ps7I2C, |
| 256 | uint32_t carrierBoard) |
| 257 | { |
| 258 | uint32_t ret; |
| 259 | uint8_t wrBuf[1] = {0x02}; |
| 260 | |
| 261 | picI2cAddr = (enableCommMux || (fmcPort == 1)) ? |
| 262 | IICSEL_PIC_1 : IICSEL_PIC_0; |
| 263 | |
| 264 | // Assign I2C Functions according to type of I2C Core used (Hardware or Softcore) |
| 265 | if(ps7I2C == 1) |
| 266 | { |
| 267 | I2C_Write = &I2C_Write_ps7; |
| 268 | I2C_Read = &I2C_Read_ps7; |
| 269 | I2C_Init = &I2C_Init_ps7; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | I2C_Write = &I2C_Write_axi; |
| 274 | I2C_Read = &I2C_Read_axi; |
| 275 | I2C_Init = &I2C_Init_axi; |
| 276 | } |
| 277 | |
| 278 | ret = I2C_Init(picI2cAddr, fmcPort, enableCommMux, carrierBoard); |
| 279 | |
| 280 | if(ret) |
| 281 | return -1; |
| 282 | |
| 283 | ret = I2C_Write(picI2cAddr, -1, |
| 284 | sizeof(wrBuf)/sizeof(unsigned char), wrBuf); |
| 285 | |
| 286 | return (ret == 0 ? -1 : 0); |
| 287 | } |
| 288 | |
| 289 | /**************************************************************************//** |
| 290 | * @brief Reads data from the selected device |
| 291 | * |
| 292 | * @param spiSel - SPI CS number |
| 293 | * @param regAddr - The adress of the register to be read |
| 294 | * @param data - Buffer to store the read data |
| 295 | * |
| 296 | * @return Returns -1 in case of error, 0 for success |
| 297 | ******************************************************************************/ |
| 298 | int32_t SPI_Read(uint32_t spiSel, uint32_t regAddr, uint32_t* data) |
| 299 | { |
| 300 | uint32_t addr; |
| 301 | uint32_t rSize; |
| 302 | |
| 303 | /* Write the address */ |
| 304 | if (devConfig[spiSel].addrWidth) |
| 305 | { |
| 306 | addr = regAddr; |
| 307 | |
| 308 | PIC_Config(spiSel, 0, SPI_CS_LOW_AT_TRANFER_END); |
| 309 | PIC_Write(spiSel, devConfig[spiSel].addrWidth / 8, addr); |
| 310 | } |
| 311 | |
| 312 | /* Configure the PIC for a read operation */ |
| 313 | PIC_Config(spiSel, devConfig[spiSel].dataWidth / 8, SPI_CS_HIGH_AT_TRANFER_END); |
| 314 | |
| 315 | /* Read data from the device */ |
| 316 | rSize = PIC_Read(spiSel, devConfig[spiSel].dataWidth / 8, data); |
| 317 | |
| 318 | return ((rSize != devConfig[spiSel].dataWidth / 8) ? -1 : 0); |
| 319 | } |
| 320 | |
| 321 | /**************************************************************************//** |
| 322 | * @brief Writes data to the selected device |
| 323 | * |
| 324 | * @param spiSel - SPI CS number |
| 325 | * @param regAddr - The adress of the register to be written |
| 326 | * @param data - Data to be written to the specified register |
| 327 | * |
| 328 | * @return Returns -1 in case of error, 0 for success |
| 329 | ******************************************************************************/ |
| 330 | int32_t SPI_Write(uint32_t spiSel, uint32_t regAddr, uint32_t data) |
| 331 | { |
| 332 | uint32_t wData; |
| 333 | uint32_t wSize; |
| 334 | |
| 335 | wData = (regAddr << devConfig[spiSel].dataWidth) | data; |
| 336 | |
| 337 | /* Configure the PIC */ |
| 338 | PIC_Config(spiSel, 0, SPI_CS_HIGH_AT_TRANFER_END); |
| 339 | |
| 340 | /* Write data to the device */ |
| 341 | wSize = PIC_Write(spiSel, (devConfig[spiSel].dataWidth + devConfig[spiSel].addrWidth) / 8, wData); |
| 342 | |
| 343 | return ((wSize != (devConfig[spiSel].dataWidth + devConfig[spiSel].addrWidth) / 8) ? -1 : 0); |
| 344 | } |