| 1 | /***************************************************************************//** |
| 2 | * @file AD74xx.c |
| 3 | * @brief Implementation of AD74xx Driver. |
| 4 | * @author DNechita([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2012(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 | * SVN Revision: $WCREV$ |
| 41 | *******************************************************************************/ |
| 42 | |
| 43 | /******************************************************************************/ |
| 44 | /***************************** Include Files **********************************/ |
| 45 | /******************************************************************************/ |
| 46 | #include "AD74xx.h" // AD74xx definitions. |
| 47 | |
| 48 | /*****************************************************************************/ |
| 49 | /************************ Variables Definitions ******************************/ |
| 50 | /*****************************************************************************/ |
| 51 | char deviceBitsNumber = 0; |
| 52 | |
| 53 | /******************************************************************************/ |
| 54 | /************************ Functions Definitions *******************************/ |
| 55 | /******************************************************************************/ |
| 56 | |
| 57 | /***************************************************************************//** |
| 58 | * @brief Initializes device. |
| 59 | * |
| 60 | * @param partNumber - The name of one of the supported devices. |
| 61 | * Example: AD7466 |
| 62 | * AD7467 |
| 63 | * AD7468 |
| 64 | * AD7475 |
| 65 | * AD7476 |
| 66 | * AD7476A |
| 67 | * AD7477 |
| 68 | * AD7477A |
| 69 | * AD7478 |
| 70 | * AD7478A |
| 71 | * AD7495 |
| 72 | * |
| 73 | * @return status - Result of the initialization procedure. |
| 74 | * Example: -1 - SPI peripheral was not initialized. |
| 75 | * 0 - SPI peripheral is initialized. |
| 76 | *******************************************************************************/ |
| 77 | char AD74xx_Init(char partNumber) |
| 78 | { |
| 79 | unsigned char status = -1; |
| 80 | |
| 81 | status = SPI_Init(0, 1000000, 1, 0); |
| 82 | deviceBitsNumber = partNumber; |
| 83 | |
| 84 | return status; |
| 85 | } |
| 86 | |
| 87 | /***************************************************************************//** |
| 88 | * @brief Powers down the device. This function is supported only by: |
| 89 | * AD7475(partial power down), AD7476, AD7477, AD7478, AD7476A, AD7477A, |
| 90 | * AD7478A, AD7479(partial power down). |
| 91 | * |
| 92 | * @return None. |
| 93 | *******************************************************************************/ |
| 94 | void AD74xx_PowerDown(void) |
| 95 | { |
| 96 | unsigned char dummyValue = 0; |
| 97 | |
| 98 | AD74XX_CS_LOW; |
| 99 | SPI_Read(0, &dummyValue, 1); |
| 100 | AD74XX_CS_HIGH; // CS is brought "High" between 2nd falling edge of SCLK |
| 101 | // and 10th falling edge of SCLK(8th falling edge, here) |
| 102 | SPI_Read(0, &dummyValue, 1); |
| 103 | } |
| 104 | |
| 105 | /***************************************************************************//** |
| 106 | * @brief Powers up the device by performing a dummy read. This function is |
| 107 | * supported only by: AD7475(partial power down), AD7476, AD7477, AD7478, |
| 108 | * AD7476A, AD7477A, AD7478A, AD7479(partial power down). |
| 109 | * |
| 110 | * @return None. |
| 111 | *******************************************************************************/ |
| 112 | void AD74xx_PowerUp(void) |
| 113 | { |
| 114 | unsigned char dummyValue[2] = {0, 0}; |
| 115 | |
| 116 | SPI_Read(AD74XX_SLAVE_ID, dummyValue, 2); |
| 117 | } |
| 118 | |
| 119 | /***************************************************************************//** |
| 120 | * @brief Reads the conversion value. |
| 121 | * |
| 122 | * @return convResult - conversion data. |
| 123 | *******************************************************************************/ |
| 124 | unsigned short AD74xx_GetRegisterValue(void) |
| 125 | { |
| 126 | unsigned char dataWord[2] = {0, 0}; |
| 127 | unsigned short convResult = 0; |
| 128 | |
| 129 | SPI_Read(AD74XX_SLAVE_ID, dataWord, 2); |
| 130 | switch(deviceBitsNumber){ |
| 131 | case 8: |
| 132 | convResult = (((unsigned short)(dataWord[0] & 0x1F)) << 8) + |
| 133 | (dataWord[1] & 0xE0); |
| 134 | convResult = convResult >> 5; |
| 135 | break; |
| 136 | case 10: |
| 137 | convResult = (((unsigned short)(dataWord[0] & 0x1F)) << 8) + |
| 138 | (dataWord[1] & 0xF8); |
| 139 | convResult = convResult >> 3; |
| 140 | break; |
| 141 | case 12: |
| 142 | convResult = (((unsigned short)(dataWord[0] & 0x1F)) << 8) + |
| 143 | (dataWord[1] & 0xFE); |
| 144 | convResult = convResult >> 1; |
| 145 | break; |
| 146 | default: // 16 bits |
| 147 | convResult = (((unsigned short)(dataWord[0] & 0xFF)) << 8) + |
| 148 | dataWord[1]; |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return convResult; |
| 153 | } |
| 154 | |
| 155 | /***************************************************************************//** |
| 156 | * @brief Converts a raw sample to volts. |
| 157 | * |
| 158 | * @param rawValue - The data sample. |
| 159 | * @param vRef - The value of the voltage reference used by the device. |
| 160 | * |
| 161 | * @return voltage - The result of the conversion expressed as volts. |
| 162 | *******************************************************************************/ |
| 163 | float AD74xx_ConvertToVolts(unsigned short rawValue, float vRef) |
| 164 | { |
| 165 | float voltage = 0; |
| 166 | |
| 167 | voltage = (rawValue) * vRef / (1 << deviceBitsNumber); |
| 168 | |
| 169 | return voltage; |
| 170 | } |