thirdparty/analogdevicesinc/no-OS/platform_drivers/Xilinx/SDP_Common/Communication.c
raw symbols SCIP| 1 | /***************************************************************************//** |
| 2 | * @file Communication.c |
| 3 | * @brief Implementation of Communication Driver for Xilinx FPGAs. |
| 4 | * @author Istvan Csomortani ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2014(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | *******************************************************************************/ |
| 40 | |
| 41 | /******************************************************************************/ |
| 42 | /************************ Include Files ***************************************/ |
| 43 | /******************************************************************************/ |
| 44 | #include "Communication.h" |
| 45 | #include "xil_io.h" |
| 46 | #include "TIME.h" |
| 47 | |
| 48 | /******************************************************************************/ |
| 49 | /************************* Variables Definitions ******************************/ |
| 50 | /******************************************************************************/ |
| 51 | static u32 configValue = 0; |
| 52 | |
| 53 | /******************************************************************************/ |
| 54 | /*********************** Functions Definitions ********************************/ |
| 55 | /******************************************************************************/ |
| 56 | |
| 57 | /***************************************************************************//** |
| 58 | * @brief Define preprocessor directives to activate some board specific |
| 59 | * setups, like I2C or SPI ports, or board specific GPIO pins. |
| 60 | * Note that this function is local / protected! |
| 61 | * |
| 62 | * @param - None |
| 63 | * |
| 64 | * @return - None |
| 65 | *******************************************************************************/ |
| 66 | void DefinePorts(void) |
| 67 | { |
| 68 | /*********** Custom setups for Digital-to-Analog Converter Boards *************/ |
| 69 | #if(defined(AD5541A) || defined(AD5542A)) |
| 70 | /* Activate DAC buffer */ |
| 71 | GPIO2_PIN_OUT; |
| 72 | GPIO2_LOW; |
| 73 | /* Deactivate ADC buffer */ |
| 74 | GPIO3_PIN_OUT; |
| 75 | GPIO3_HIGH; |
| 76 | #endif |
| 77 | #if(defined(AD5443)) |
| 78 | /* Select the SDO line for the device */ |
| 79 | GPIO0_PIN_OUT; |
| 80 | GPIO0_HIGH; |
| 81 | #endif |
| 82 | #if(defined(AD5570) || defined(AD5453) || defined(AD5446) || defined(AD5443)) |
| 83 | SPI_BASEADDR = XPAR_AXI_SPI_0_BASEADDR; |
| 84 | #endif |
| 85 | #if(defined(AD5629R) || defined(AD5669R) || defined(AD5694R) || defined(AD5696R)) |
| 86 | I2C_BASEADDR = XPAR_AXI_IIC_1_BASEADDR; |
| 87 | #endif |
| 88 | #if(defined(AD5668) || defined(AD5648) || defined(AD5628)) |
| 89 | /* Activate DAC buffer */ |
| 90 | GPIO2_PIN_OUT; |
| 91 | GPIO2_LOW; |
| 92 | /* Deactivate ADC buffer */ |
| 93 | GPIO3_PIN_OUT; |
| 94 | GPIO3_HIGH; |
| 95 | /* Deactivate MUX */ |
| 96 | GPIO4_PIN_OUT; |
| 97 | GPIO4_HIGH; |
| 98 | #endif |
| 99 | #if(defined(AD5686R) || defined(AD5684R)) |
| 100 | GPIO0_PIN_OUT; |
| 101 | GPIO0_HIGH; |
| 102 | #endif |
| 103 | |
| 104 | /**************** Custom setups for Digital Potentiometers ********************/ |
| 105 | /* Select the AD8403 SPI line (SYNC-1) as output of SYNC lines MUX */ |
| 106 | #if(defined(AD8403)) |
| 107 | /* Define the level of MUX-AO|CS */ |
| 108 | GPIO5_PIN_OUT; |
| 109 | GPIO5_LOW; |
| 110 | /* Define the level of MUX-A1|DACSEL */ |
| 111 | GPIO6_PIN_OUT; |
| 112 | GPIO6_LOW; |
| 113 | /* Define the level of MUX-A2|U/D */ |
| 114 | GPIO7_PIN_OUT; |
| 115 | GPIO7_LOW; |
| 116 | #endif |
| 117 | /* Select the AD5162 SPI line (SYNC-4) as output of SYNC lines MUX */ |
| 118 | #if(defined(AD5162)) |
| 119 | /* Define the level of MUX-AO|CS */ |
| 120 | GPIO5_PIN_OUT; |
| 121 | GPIO5_HIGH; |
| 122 | /* Define the level of MUX-A1|DACSEL */ |
| 123 | GPIO6_PIN_OUT; |
| 124 | GPIO6_HIGH; |
| 125 | /* Define the level of MUX-A2|U/D */ |
| 126 | GPIO7_PIN_OUT; |
| 127 | GPIO7_LOW; |
| 128 | #endif |
| 129 | /* Select the AD5232 SPI line (SYNC-5) as output of SYNC lines MUX */ |
| 130 | #if(defined(AD5232)) |
| 131 | /* Define the level of MUX-AO|CS */ |
| 132 | GPIO5_PIN_OUT; |
| 133 | GPIO5_LOW; |
| 134 | /* Define the level of MUX-A1|DACSEL */ |
| 135 | GPIO6_PIN_OUT; |
| 136 | GPIO6_LOW; |
| 137 | /* Define the level of MUX-A2|U/D */ |
| 138 | GPIO7_PIN_OUT; |
| 139 | GPIO7_HIGH; |
| 140 | #endif |
| 141 | /* Select the ADN2850 SPI line (SYNC-6) as output of SYNC lines MUX */ |
| 142 | #if(defined(ADN2850)) |
| 143 | /* Define the level of MUX-AO|CS */ |
| 144 | GPIO5_PIN_OUT; |
| 145 | GPIO5_HIGH; |
| 146 | /* Define the level of MUX-A1|DACSEL */ |
| 147 | GPIO6_PIN_OUT; |
| 148 | GPIO6_LOW; |
| 149 | /* Define the level of MUX-A2|U/D */ |
| 150 | GPIO7_PIN_OUT; |
| 151 | GPIO7_HIGH; |
| 152 | #endif |
| 153 | /* Select the AD5235 SPI line (SYNC-7) as output of SYNC lines MUX */ |
| 154 | #if(defined(AD5235)) |
| 155 | /* Define the level of MUX-AO|CS */ |
| 156 | GPIO5_PIN_OUT; |
| 157 | GPIO5_LOW; |
| 158 | /* Define the level of MUX-A1|DACSEL */ |
| 159 | GPIO6_PIN_OUT; |
| 160 | GPIO6_HIGH; |
| 161 | /* Define the level of MUX-A2|U/D */ |
| 162 | GPIO7_PIN_OUT; |
| 163 | GPIO7_HIGH; |
| 164 | #endif |
| 165 | |
| 166 | #if(defined(AD5270) || defined(AD5271) || defined(AD5272) || defined(AD5274)) |
| 167 | SPI_BASEADDR = XPAR_AXI_SPI_0_BASEADDR; |
| 168 | #endif |
| 169 | /**************** Custom setups for PLL Synthesizers **************************/ |
| 170 | #if(defined(ADF4001) || defined(ADF4002) || defined(ADF4106) || \ |
| 171 | defined(ADF4153) || defined(ADF4156) || defined(ADF4157)) |
| 172 | SPI_BASEADDR = XPAR_AXI_SPI_0_BASEADDR; |
| 173 | #endif |
| 174 | /**************** Custom setups for Circuits from the Lab *********************/ |
| 175 | #if(defined(CN0150) || defined(CN0178) || defined(CN0187) || \ |
| 176 | defined(CN0188) || defined(CN0189) || defined(CN0202) || \ |
| 177 | defined(CN0203) || defined(CN0204) || defined(CN0216) || \ |
| 178 | defined(CN0218) || defined(CN0235) || defined(CN0240) || \ |
| 179 | defined(CN0241) || defined(CN0271)) |
| 180 | SPI_BASEADDR = XPAR_AXI_SPI_0_BASEADDR; |
| 181 | #endif |
| 182 | #if (defined(CN0235)) |
| 183 | /* Define the output pins referring to AD8280. */ |
| 184 | GPIO0_PIN_OUT; // TEST_IN |
| 185 | GPIO0_LOW; |
| 186 | GPIO3_PIN_OUT; // ENBL_IN |
| 187 | GPIO3_LOW; |
| 188 | /* Define the input pins referring to AD8280. */ |
| 189 | GPIO1_PIN_IN; // ALRM_OV |
| 190 | GPIO4_PIN_IN; // ALRM_UV |
| 191 | |
| 192 | #endif |
| 193 | } |
| 194 | |
| 195 | /***************************************************************************//** |
| 196 | * @brief Initializes the selected platform. |
| 197 | * |
| 198 | * @return - The result of the initialization. |
| 199 | * Example: -1 - the platform was not initialized or the specified |
| 200 | * platform is not present. |
| 201 | * 0 - the platform was initialized and the specified |
| 202 | * platform is present. |
| 203 | *******************************************************************************/ |
| 204 | char PLATFORM_Init(platformBoard platform) |
| 205 | { |
| 206 | switch (platform ) |
| 207 | { |
| 208 | case XILINX_KC705: |
| 209 | #ifdef XPAR_AXI_SPORT_0_DEVICE_ID |
| 210 | SPI_BASEADDR = XPAR_AXI_SPORT_0_BASEADDR; |
| 211 | #endif |
| 212 | #ifdef XPAR_AXI_IIC_0_DEVICE_ID |
| 213 | I2C_BASEADDR = XPAR_AXI_IIC_0_BASEADDR; |
| 214 | #endif |
| 215 | #ifdef XPAR_AXI_GPIO_0_DEVICE_ID |
| 216 | GPIO_BASEADDR = XPAR_AXI_GPIO_0_BASEADDR; |
| 217 | #endif |
| 218 | #ifdef XPAR_AXI_PAR_0_DEVICE_ID |
| 219 | PAR_BASEADDR = XPAR_AXI_PAR_0_BASEADDR; |
| 220 | #endif |
| 221 | UART_BAUDRATE = 115200; |
| 222 | break; |
| 223 | default : |
| 224 | xil_printf("Selected platform is not supported\n"); |
| 225 | return -1; |
| 226 | }; |
| 227 | |
| 228 | /* Define and activate board specific setups */ |
| 229 | DefinePorts(); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | /***************************************************************************//** |
| 234 | * @brief Initializes the SPI communication peripheral. |
| 235 | * |
| 236 | * @param lsbFirst - Transfer format (0 or 1). |
| 237 | * Example: 0x0 - MSB first. |
| 238 | * 0x1 - LSB first. |
| 239 | * @param clockFreq - SPI clock frequency (Hz). |
| 240 | * The primary and secondary prescalers have to be modified |
| 241 | * in order to change the SPI clock frequency |
| 242 | * @param clockPol - SPI clock polarity (0 or 1). |
| 243 | * Example: 0x0 - Idle state for clock is a low level; active |
| 244 | * state is a high level; |
| 245 | * 0x1 - Idle state for clock is a high level; active |
| 246 | * state is a low level. |
| 247 | * @param clockEdg - SPI clock edge (0 or 1). |
| 248 | * Example: 0x0 - Serial output data changes on transition |
| 249 | * from idle clock state to active clock |
| 250 | * state; |
| 251 | * 0x1 - Serial output data changes on transition |
| 252 | * from active clock state to idle clock state |
| 253 | * |
| 254 | * @return status - Result of the initialization procedure. |
| 255 | * Example: 0 - if initialization was successful; |
| 256 | * -1 - if initialization was unsuccessful. |
| 257 | ******************************************************************************/ |
| 258 | char SPI_Init(unsigned char lsbFirst, |
| 259 | unsigned long clockFreq, |
| 260 | unsigned char clockPol, |
| 261 | unsigned char clockEdg) |
| 262 | { |
| 263 | /* Configuration Register Settings */ |
| 264 | configValue = (lsbFirst << LSBFirst) | // MSB First transfer format |
| 265 | (1 << MasterTranInh) | // Master transactions disabled |
| 266 | (1 << ManualSlaveAssEn) | // Slave select output follows data in slave select register |
| 267 | (1 << RxFifoReset) | // Receive FIFO normal operation |
| 268 | (0 << TxFifoReset) | // Transmit FIFO normal operation |
| 269 | (!clockEdg << CHPA) | // Data valid on first SCK edge |
| 270 | (clockPol << CPOL) | // Active high clock, SCK idles low |
| 271 | (1 << Master) | // SPI in Master configuration mode |
| 272 | (1 << SPE) | // SPI enabled |
| 273 | (0 << LOOP); // Normal operation |
| 274 | |
| 275 | /* Set the slave select register to all ones */ |
| 276 | Xil_Out32(SPI_BASEADDR + SPISSR, 0xFFFFFFFF); |
| 277 | |
| 278 | /* Set corresponding value to the Configuration Register */ |
| 279 | Xil_Out32(SPI_BASEADDR + SPICR, configValue); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /***************************************************************************//** |
| 285 | * @brief Writes multiple bytes to SPI. |
| 286 | * |
| 287 | * @param slaveDeviceId - The ID of the selected slave device. |
| 288 | * @param data - Data represents the write buffer. |
| 289 | * @param bytesNumber - Number of bytes to write. |
| 290 | * |
| 291 | * @return : number of written bytes, if the write was successfulness |
| 292 | -1, if the write was unsuccessful |
| 293 | *******************************************************************************/ |
| 294 | char SPI_Write(unsigned char slaveDeviceId, |
| 295 | unsigned char* data, |
| 296 | unsigned char bytesNumber) |
| 297 | { |
| 298 | u32 cfgValue = configValue; |
| 299 | u32 SPIStatus = 0; |
| 300 | u32 rxCnt = 0; |
| 301 | u32 txCnt = 0; |
| 302 | u32 timeout = 0xFFFF; |
| 303 | |
| 304 | /* Write configuration data to master SPI device SPICR */ |
| 305 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 306 | |
| 307 | /* Write to SPISSR to manually assert SSn */ |
| 308 | Xil_Out32(SPI_BASEADDR + SPISSR, ~(0x00000001 << (slaveDeviceId-1))); |
| 309 | |
| 310 | /* Write initial data to master SPIDTR register */ |
| 311 | Xil_Out32(SPI_BASEADDR + SPIDTR, data[0]); |
| 312 | |
| 313 | /* Enable the master transactions */ |
| 314 | cfgValue &= ~(1 << MasterTranInh); |
| 315 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 316 | |
| 317 | /* Send and receive the data */ |
| 318 | while(txCnt < bytesNumber) |
| 319 | { |
| 320 | /* Poll status for completion */ |
| 321 | do |
| 322 | { |
| 323 | SPIStatus = Xil_In32(SPI_BASEADDR + SPISR); |
| 324 | } |
| 325 | while(((SPIStatus & 0x01) == 1) && timeout--); |
| 326 | if(timeout == -1) |
| 327 | { |
| 328 | /* Disable the master transactions */ |
| 329 | cfgValue |= (1 << MasterTranInh); |
| 330 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 331 | |
| 332 | /* Reset the SPI core */ |
| 333 | Xil_Out32(SPI_BASEADDR + SRR, 0x0000000A); |
| 334 | |
| 335 | /* Set the slave select register to all ones */ |
| 336 | Xil_Out32(SPI_BASEADDR + SPISSR, 0xFFFFFFFF); |
| 337 | |
| 338 | /* Set corresponding value to the Configuration Register */ |
| 339 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 340 | |
| 341 | /* Return error */ |
| 342 | return -1; |
| 343 | } |
| 344 | timeout = 0xFFFF; |
| 345 | |
| 346 | /* Read received data from SPI Core buffer */ |
| 347 | if(rxCnt < bytesNumber) |
| 348 | { |
| 349 | data[rxCnt] = Xil_In32(SPI_BASEADDR + SPIDRR); |
| 350 | rxCnt++; |
| 351 | } |
| 352 | /* Send next data */ |
| 353 | txCnt++; |
| 354 | if(txCnt < bytesNumber) |
| 355 | { |
| 356 | /* Disable the master transactions */ |
| 357 | cfgValue |= (1 << MasterTranInh); |
| 358 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 359 | |
| 360 | /* Write data */ |
| 361 | Xil_Out32(SPI_BASEADDR + SPIDTR, data[txCnt]); |
| 362 | |
| 363 | /* Enable the master transactions */ |
| 364 | cfgValue &= ~(1 << MasterTranInh); |
| 365 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* Disable the master transactions */ |
| 370 | cfgValue |= (1 << MasterTranInh); |
| 371 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 372 | |
| 373 | /* Write all ones to SPISSR */ |
| 374 | Xil_Out32(SPI_BASEADDR + SPISSR, 0xFFFFFFFF); |
| 375 | |
| 376 | /* Return the number of bytes written */ |
| 377 | return bytesNumber; |
| 378 | } |
| 379 | |
| 380 | /***************************************************************************//** |
| 381 | * @brief Reads multiple bytes from SPI. |
| 382 | * |
| 383 | * @param slaveDeviceId - The ID of the selected slave device. |
| 384 | * @param data - Data represents the read buffer. |
| 385 | * @param bytesNumber - Number of bytes to read. |
| 386 | * |
| 387 | * @return Number of read bytes. |
| 388 | *******************************************************************************/ |
| 389 | char SPI_Read(unsigned char slaveDeviceId, |
| 390 | unsigned char* data, |
| 391 | unsigned char bytesNumber) |
| 392 | { |
| 393 | u32 cfgValue = configValue; |
| 394 | u32 SPIStatus = 0; |
| 395 | u32 rxCnt = 0; |
| 396 | u32 txCnt = 0; |
| 397 | u32 timeout = 0xFFFF; |
| 398 | |
| 399 | /* Write configuration data to master SPI device SPICR */ |
| 400 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 401 | |
| 402 | /* Write to SPISSR to manually assert SSn */ |
| 403 | Xil_Out32(SPI_BASEADDR + SPISSR, ~(0x00000001 << (slaveDeviceId-1))); |
| 404 | |
| 405 | /* Write initial data to master SPIDTR register */ |
| 406 | Xil_Out32(SPI_BASEADDR + SPIDTR, data[0]); |
| 407 | |
| 408 | /* Enable the master transactions */ |
| 409 | cfgValue &= ~(1 << MasterTranInh); |
| 410 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 411 | |
| 412 | /* Send and receive the data */ |
| 413 | while(txCnt < bytesNumber) |
| 414 | { |
| 415 | /* Poll status for completion */ |
| 416 | do |
| 417 | { |
| 418 | SPIStatus = Xil_In32(SPI_BASEADDR + SPISR); |
| 419 | } |
| 420 | while(((SPIStatus & 0x01) == 1) && timeout--); |
| 421 | if(timeout == -1) |
| 422 | { |
| 423 | /* Disable the master transactions */ |
| 424 | cfgValue |= (1 << MasterTranInh); |
| 425 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 426 | |
| 427 | /* Reset the SPI core */ |
| 428 | Xil_Out32(SPI_BASEADDR + SRR, 0x0000000A); |
| 429 | |
| 430 | /* Set the slave select register to all ones */ |
| 431 | Xil_Out32(SPI_BASEADDR + SPISSR, 0xFFFFFFFF); |
| 432 | |
| 433 | /* Set corresponding value to the Configuration Register */ |
| 434 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 435 | |
| 436 | /* Return error */ |
| 437 | return -1; |
| 438 | } |
| 439 | timeout = 0xFFFF; |
| 440 | |
| 441 | /* Read received data from SPI Core buffer */ |
| 442 | if(rxCnt < bytesNumber) |
| 443 | { |
| 444 | data[rxCnt] = Xil_In32(SPI_BASEADDR + SPIDRR); |
| 445 | rxCnt++; |
| 446 | } |
| 447 | /* Send next data */ |
| 448 | txCnt++; |
| 449 | if(txCnt < bytesNumber) |
| 450 | { |
| 451 | /* Disable the master transactions */ |
| 452 | cfgValue |= (1 << MasterTranInh); |
| 453 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 454 | |
| 455 | /* Write data */ |
| 456 | Xil_Out32(SPI_BASEADDR + SPIDTR, data[txCnt]); |
| 457 | |
| 458 | /* Enable the master transactions */ |
| 459 | cfgValue &= ~(1 << MasterTranInh); |
| 460 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* Disable the master transactions */ |
| 465 | cfgValue |= (1 << MasterTranInh); |
| 466 | Xil_Out32(SPI_BASEADDR + SPICR, cfgValue); |
| 467 | |
| 468 | /* Write all ones to SPISSR */ |
| 469 | Xil_Out32(SPI_BASEADDR + SPISSR, 0xFFFFFFFF); |
| 470 | |
| 471 | /* Return the number of bytes read */ |
| 472 | return bytesNumber; |
| 473 | } |
| 474 | |
| 475 | /***************************************************************************//** |
| 476 | * @brief Initializes the UART communication peripheral. If the value of the |
| 477 | * baud rate is not equal with the ipcore's baud rate the |
| 478 | * initialization going to FAIL. |
| 479 | * |
| 480 | * @param baudRate - Baud rate value. |
| 481 | * Example: 9600 - 9600 bps. |
| 482 | * |
| 483 | * @return status - Result of the initialization procedure. |
| 484 | * Example: 0 - if initialization was successful; |
| 485 | * -1 - if initialization was unsuccessful. |
| 486 | * |
| 487 | *******************************************************************************/ |
| 488 | char UART_Init(unsigned long baudRate) |
| 489 | { |
| 490 | /* Disable interrupt and reset the Rx and Tx FIFO */ |
| 491 | Xil_Out32(UART_CNTRL, ( UART_RST_TX | \ |
| 492 | UART_RST_RX | \ |
| 493 | ~UART_EN_INTR) ); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /***************************************************************************//** |
| 499 | * @brief Writes one character to UART. |
| 500 | * |
| 501 | * @param data - Character to write. |
| 502 | * |
| 503 | * @return None. |
| 504 | *******************************************************************************/ |
| 505 | void UART_WriteChar(char data) |
| 506 | { |
| 507 | /* Wait the Tx FIFO empty indication */ |
| 508 | while( (Xil_In32(UART_STAT) & 0x4) == 0x0); |
| 509 | /* Put the data to the FIFO */ |
| 510 | Xil_Out32(UART_TX, data); |
| 511 | /* Wait the Tx FIFO empty indication */ |
| 512 | while( (Xil_In32(UART_STAT) & 0x4) == 0x0); |
| 513 | } |
| 514 | |
| 515 | /***************************************************************************//** |
| 516 | * @brief Reads one character from UART. |
| 517 | * |
| 518 | * @return receivedChar - Read character. |
| 519 | * |
| 520 | * Note: Blocking function - waits until get a valid data |
| 521 | *******************************************************************************/ |
| 522 | void UART_ReadChar(char * data) |
| 523 | { |
| 524 | /* Wait for a valid data */ |
| 525 | while((Xil_In32(UART_STAT) & UART_RX_VALID) == 0x00); |
| 526 | /* Read the character */ |
| 527 | *data = (char)(Xil_In32(UART_RX) & 0xFF); |
| 528 | /* Flush the Rx FIFO at carriage return */ |
| 529 | if((*data == '\n') || (*data == '\r')) |
| 530 | { |
| 531 | Xil_Out32(UART_CNTRL, UART_RST_RX | UART_RST_TX); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /***************************************************************************//** |
| 536 | * @brief Writes one character string to UART. |
| 537 | * |
| 538 | * @param data - Character to write. |
| 539 | * |
| 540 | * @return None. |
| 541 | *******************************************************************************/ |
| 542 | void UART_WriteString(const char* string) |
| 543 | { |
| 544 | while(*string) |
| 545 | { |
| 546 | UART_WriteChar(*string++); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /***************************************************************************//** |
| 551 | * @brief Initializes the I2C communication peripheral. |
| 552 | * |
| 553 | * @param clockFreq - I2C clock frequency (Hz). |
| 554 | * Example: 100000 - I2C clock frequency is 100 kHz. |
| 555 | * @return status - Result of the initialization procedure. |
| 556 | * Example: 0 - if initialization was successful; |
| 557 | * -1 - if initialization was unsuccessful. |
| 558 | *******************************************************************************/ |
| 559 | unsigned char I2C_Init(unsigned long clockFreq) |
| 560 | { |
| 561 | /* Disable the I2C core */ |
| 562 | Xil_Out32((I2C_BASEADDR + CR), 0x00); |
| 563 | /* Set the Rx FIFO depth to maximum */ |
| 564 | Xil_Out32((I2C_BASEADDR + RX_FIFO_PIRQ), 0x0F); |
| 565 | /* Reset the I2C core and flush the Tx fifo */ |
| 566 | Xil_Out32((I2C_BASEADDR + CR), 0x02); |
| 567 | /* Enable the I2C core */ |
| 568 | Xil_Out32((I2C_BASEADDR + CR), 0x01); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /***************************************************************************//** |
| 574 | * @brief Reads data from a slave device. |
| 575 | * |
| 576 | * @param slaveAddress - Adress of the slave device. |
| 577 | * @param dataBuffer - Pointer to a buffer that will store the received data. |
| 578 | * @param bytesNumber - Number of bytes to read. |
| 579 | * @param stopBit - Stop condition control. |
| 580 | * Example: 0 - A stop condition will not be sent; |
| 581 | * 1 - A stop condition will be sent. |
| 582 | * |
| 583 | * @return status - Number of read bytes or 0xFF if the slave address was not |
| 584 | * acknowledged by the device. |
| 585 | *******************************************************************************/ |
| 586 | unsigned char I2C_Read(unsigned char slaveAddress, |
| 587 | unsigned char* dataBuffer, |
| 588 | unsigned char bytesNumber, |
| 589 | unsigned char stopBit) |
| 590 | { |
| 591 | u32 rxCnt = 0; |
| 592 | u32 timeout = 0xFFFFFF; |
| 593 | |
| 594 | /* Reset tx fifo */ |
| 595 | Xil_Out32((I2C_BASEADDR + CR), 0x002); |
| 596 | /* Enable iic */ |
| 597 | Xil_Out32((I2C_BASEADDR + CR), 0x001); |
| 598 | TIME_DelayMs(10); |
| 599 | |
| 600 | /* Set the slave I2C address */ |
| 601 | Xil_Out32((I2C_BASEADDR + TX_FIFO), (0x101 | (slaveAddress << 1))); |
| 602 | /* Start a read transaction */ |
| 603 | Xil_Out32((I2C_BASEADDR + TX_FIFO), 0x200 + bytesNumber); |
| 604 | |
| 605 | /* Read data from the I2C slave */ |
| 606 | while(rxCnt < bytesNumber) |
| 607 | { |
| 608 | /* Wait for data to be available in the RxFifo */ |
| 609 | while((Xil_In32(I2C_BASEADDR + SR) & 0x00000040) && (timeout--)); |
| 610 | if(timeout == -1) |
| 611 | { |
| 612 | /* Disable the I2C core */ |
| 613 | Xil_Out32((I2C_BASEADDR + CR), 0x00); |
| 614 | /* Set the Rx FIFO depth to maximum */ |
| 615 | Xil_Out32((I2C_BASEADDR + RX_FIFO_PIRQ), 0x0F); |
| 616 | /* Reset the I2C core and flush the Tx fifo */ |
| 617 | Xil_Out32((I2C_BASEADDR + CR), 0x02); |
| 618 | /* Enable the I2C core */ |
| 619 | Xil_Out32((I2C_BASEADDR + CR), 0x01); |
| 620 | return rxCnt; |
| 621 | } |
| 622 | timeout = 0xFFFFFF; |
| 623 | |
| 624 | /* Read the data */ |
| 625 | dataBuffer[rxCnt] = Xil_In32(I2C_BASEADDR + RX_FIFO) & 0xFFFF; |
| 626 | |
| 627 | /* Increment the receive counter */ |
| 628 | rxCnt++; |
| 629 | } |
| 630 | |
| 631 | TIME_DelayMs(10); |
| 632 | |
| 633 | return rxCnt; |
| 634 | } |
| 635 | |
| 636 | /***************************************************************************//** |
| 637 | * @brief Writes data to a slave device. |
| 638 | * |
| 639 | * @param slaveAddress - Adress of the slave device. |
| 640 | * @param dataBuffer - Pointer to a buffer storing the transmission data. |
| 641 | * @param bytesNumber - Number of bytes to write. |
| 642 | * @param stopBit - Stop condition control. |
| 643 | * Example: 0 - A stop condition will not be sent; |
| 644 | * 1 - A stop condition will be sent. |
| 645 | * |
| 646 | * @return status - Number of read bytes or 0xFF if the slave address was not |
| 647 | * acknowledged by the device. |
| 648 | *******************************************************************************/ |
| 649 | unsigned char I2C_Write(unsigned char slaveAddress, |
| 650 | unsigned char* dataBuffer, |
| 651 | unsigned char bytesNumber, |
| 652 | unsigned char stopBit) |
| 653 | { |
| 654 | u32 txCnt = 0; |
| 655 | |
| 656 | /* Reset tx fifo */ |
| 657 | Xil_Out32((I2C_BASEADDR + CR), 0x002); |
| 658 | /* Enable iic */ |
| 659 | Xil_Out32((I2C_BASEADDR + CR), 0x001); |
| 660 | TIME_DelayMs(10); |
| 661 | |
| 662 | /* Set the I2C address */ |
| 663 | Xil_Out32((I2C_BASEADDR + TX_FIFO), (0x100 | (slaveAddress << 1))); |
| 664 | |
| 665 | /* Write data to the I2C slave */ |
| 666 | while(txCnt < bytesNumber) |
| 667 | { |
| 668 | /* Put the Tx data into the Tx FIFO */ |
| 669 | Xil_Out32((I2C_BASEADDR + TX_FIFO), (txCnt == bytesNumber - 1) ? |
| 670 | (0x200 | dataBuffer[txCnt]) : dataBuffer[txCnt]); |
| 671 | txCnt++; |
| 672 | } |
| 673 | TIME_DelayMs(10); |
| 674 | |
| 675 | return txCnt; |
| 676 | } |