| 1 | /***************************************************************************//** |
| 2 | * @file AD5791.c |
| 3 | * @brief Implementation of AD5791 Driver. |
| 4 | * @author DNechita ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2013(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 "AD5791.h" // AD5791 definitions. |
| 45 | #include "TIME.h" // TIME definitions. |
| 46 | |
| 47 | /******************************************************************************/ |
| 48 | /************************ Variables Definitions *******************************/ |
| 49 | /******************************************************************************/ |
| 50 | struct ad5791_chip_info { |
| 51 | unsigned int resolution; |
| 52 | }; |
| 53 | |
| 54 | static const struct ad5791_chip_info ad5791_chip_info[] = { |
| 55 | [ID_AD5760] = { |
| 56 | .resolution = 16, |
| 57 | }, |
| 58 | [ID_AD5780] = { |
| 59 | .resolution = 18, |
| 60 | }, |
| 61 | [ID_AD5781] = { |
| 62 | .resolution = 18, |
| 63 | }, |
| 64 | [ID_AD5790] = { |
| 65 | .resolution = 20, |
| 66 | }, |
| 67 | [ID_AD5791] = { |
| 68 | .resolution = 20, |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | AD5791_type act_device; |
| 73 | |
| 74 | |
| 75 | /***************************************************************************//** |
| 76 | * @brief Initializes the communication with the device. |
| 77 | * |
| 78 | * @return status - Result of the initialization procedure. |
| 79 | * Example: 0 - if initialization was successful; |
| 80 | * -1 - if initialization was unsuccessful. |
| 81 | *******************************************************************************/ |
| 82 | long AD5791_Init(AD5791_type device) |
| 83 | { |
| 84 | long status = -1; |
| 85 | |
| 86 | act_device = device; |
| 87 | |
| 88 | AD5791_RESET_OUT; |
| 89 | AD5791_RESET_HIGH; |
| 90 | AD5791_LDAC_OUT; |
| 91 | AD5791_LDAC_HIGH; |
| 92 | AD5791_CLR_OUT; |
| 93 | AD5791_CLR_HIGH; |
| 94 | |
| 95 | status = (long)SPI_Init(0, // transfer format(MSB first) |
| 96 | 1000000, // SPI clock frequency(1 MHz) |
| 97 | 1, // SPI clock polarity(Idle High) |
| 98 | 1); // SPI clock edge(active->idle edge) |
| 99 | |
| 100 | return status; |
| 101 | } |
| 102 | |
| 103 | /***************************************************************************//** |
| 104 | * @brief Writes data into a register. |
| 105 | * |
| 106 | * @param registerAddress - Address of the register. |
| 107 | * Example: |
| 108 | * AD5791_REG_DAC - DAC register |
| 109 | * AD5791_REG_CTRL - Control register |
| 110 | * AD5791_REG_CLR_CODE - Clearcode register |
| 111 | * AD5791_CMD_WR_SOFT_CTRL - Software control register |
| 112 | * @param registerValue - Value of the register. |
| 113 | * |
| 114 | * @return Returns 0 in case of success or negative error code. |
| 115 | *******************************************************************************/ |
| 116 | long AD5791_SetRegisterValue(unsigned char registerAddress, |
| 117 | unsigned long registerValue) |
| 118 | { |
| 119 | unsigned char writeCommand[3] = {0, 0, 0}; |
| 120 | unsigned long spiWord = 0; |
| 121 | char status = 0; |
| 122 | |
| 123 | spiWord = AD5791_WRITE | |
| 124 | AD5791_ADDR_REG(registerAddress) | |
| 125 | (registerValue & 0xFFFFF); |
| 126 | writeCommand[0] = (spiWord >> 16) & 0x0000FF; |
| 127 | writeCommand[1] = (spiWord >> 8) & 0x0000FF; |
| 128 | writeCommand[2] = (spiWord >> 0) & 0x0000FF; |
| 129 | status = SPI_Write(AD5791_SLAVE_ID, writeCommand, 3); |
| 130 | if(status != 3) |
| 131 | { |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /***************************************************************************//** |
| 139 | * @brief Reads the value of a register. |
| 140 | * |
| 141 | * @param registerAddress - Address of the register. |
| 142 | * Example: |
| 143 | * AD5791_REG_DAC - DAC register |
| 144 | * AD5791_REG_CTRL - Control register |
| 145 | * AD5791_REG_CLR_CODE - Clearcode register |
| 146 | * AD5791_CMD_WR_SOFT_CTRL - Software control register |
| 147 | * |
| 148 | * @return dataRead - The register's value or negative error code. |
| 149 | *******************************************************************************/ |
| 150 | long AD5791_GetRegisterValue(unsigned char registerAddress) |
| 151 | { |
| 152 | unsigned char registerWord[3] = {0, 0, 0}; |
| 153 | unsigned long dataRead = 0x0; |
| 154 | char status = 0; |
| 155 | |
| 156 | registerWord[0] = (AD5791_READ | AD5791_ADDR_REG(registerAddress)) >> 16; |
| 157 | status = SPI_Write(AD5791_SLAVE_ID, registerWord, 3); |
| 158 | if(status != 3) |
| 159 | { |
| 160 | return -1; |
| 161 | } |
| 162 | registerWord[0] = 0x00; |
| 163 | registerWord[1] = 0x00; |
| 164 | registerWord[2] = 0x00; |
| 165 | status = SPI_Read(AD5791_SLAVE_ID, registerWord, 3); |
| 166 | if(status != 3) |
| 167 | { |
| 168 | return -1; |
| 169 | } |
| 170 | dataRead = ((long)registerWord[0] << 16) | |
| 171 | ((long)registerWord[1] << 8) | |
| 172 | ((long)registerWord[2] << 0); |
| 173 | |
| 174 | return dataRead; |
| 175 | } |
| 176 | |
| 177 | /***************************************************************************//** |
| 178 | * @brief Sets the DAC output in one of the three states. |
| 179 | * |
| 180 | * @param state - The output state. |
| 181 | * Example: |
| 182 | * AD5791_OUT_NORMAL - normal operation mode |
| 183 | * AD5791_OUT_CLAMPED_6K - output is clamped via ~6KOhm to AGND |
| 184 | * AD5791_OUT_TRISTATE - output is in tristate |
| 185 | * |
| 186 | * @return Negative error code or 0 in case of success. |
| 187 | *******************************************************************************/ |
| 188 | long AD5791_DacOuputState(unsigned char state) |
| 189 | { |
| 190 | unsigned long oldCtrl = 0; |
| 191 | unsigned long newCtrl = 0; |
| 192 | long status = 0; |
| 193 | |
| 194 | status = AD5791_GetRegisterValue(AD5791_REG_CTRL); |
| 195 | if(status < 0) |
| 196 | { |
| 197 | return status; |
| 198 | } |
| 199 | oldCtrl = status; |
| 200 | /* Clear DACTRI and OPGND bits. */ |
| 201 | oldCtrl = oldCtrl & ~(AD5791_CTRL_DACTRI | AD5791_CTRL_OPGND); |
| 202 | /* Sets the new state provided by the user. */ |
| 203 | newCtrl = oldCtrl | |
| 204 | ((state << 2) & (AD5791_CTRL_DACTRI | AD5791_CTRL_OPGND)); |
| 205 | status = AD5791_SetRegisterValue(AD5791_REG_CTRL, newCtrl); |
| 206 | |
| 207 | return status; |
| 208 | } |
| 209 | |
| 210 | /***************************************************************************//** |
| 211 | * @brief Writes to the DAC register. |
| 212 | * |
| 213 | * @param value - The value to be written to DAC. |
| 214 | * |
| 215 | * @return Negative error code or 0 in case of success. |
| 216 | *******************************************************************************/ |
| 217 | long AD5791_SetDacValue(unsigned long value) |
| 218 | { |
| 219 | long status = 0; |
| 220 | |
| 221 | value = value << (MAX_RESOLUTION - ad5791_chip_info[act_device].resolution); |
| 222 | AD5791_LDAC_LOW; |
| 223 | status = AD5791_SetRegisterValue(AD5791_REG_DAC, value); |
| 224 | AD5791_LDAC_HIGH; |
| 225 | |
| 226 | return status; |
| 227 | } |
| 228 | |
| 229 | /***************************************************************************//** |
| 230 | * @brief Asserts RESET, CLR or LDAC in a software manner. |
| 231 | * |
| 232 | * @param instructionBit - A Software Control Register bit. |
| 233 | * Example: |
| 234 | * AD5791_SOFT_CTRL_LDAC - Load DAC |
| 235 | * AD5791_SOFT_CTRL_CLR - Clear |
| 236 | * AD5791_SOFT_CTRL_RESET - Reset |
| 237 | * |
| 238 | * @return Negative error code or 0 in case of success. |
| 239 | *******************************************************************************/ |
| 240 | long AD5791_SoftInstruction(unsigned char instructionBit) |
| 241 | { |
| 242 | long status = 0; |
| 243 | |
| 244 | status = AD5791_SetRegisterValue(AD5791_CMD_WR_SOFT_CTRL, instructionBit); |
| 245 | if(status < 0) |
| 246 | { |
| 247 | return status; |
| 248 | } |
| 249 | TIME_DelayMs(1); // Wait for the instruction to take effect. |
| 250 | |
| 251 | return status; |
| 252 | } |
| 253 | |
| 254 | /***************************************************************************//** |
| 255 | * @brief Configures the output amplifier, DAC coding, SDO state and the |
| 256 | * linearity error compensation. |
| 257 | * |
| 258 | * @param setupWord - Is a 24-bit value that sets or clears the Control Register |
| 259 | * bits : RBUF bit(AD5791_CTRL_RBUF), |
| 260 | * BIN/2sC bit(AD5791_CTRL_BIN2SC), |
| 261 | * SDODIS bit(AD5791_CTRL_SDODIS) and |
| 262 | * LINCOMP bits(AD5791_CTRL_LINCOMP(x)). |
| 263 | * Example: AD5791_CTRL_BIN2SC | AD5791_CTRL_RBUF - sets |
| 264 | * the DAC register to use offset binary coding and |
| 265 | * powers down the internal output amplifier. |
| 266 | * |
| 267 | * @return Negative error code or 0 in case of success. |
| 268 | *******************************************************************************/ |
| 269 | long AD5791_Setup(unsigned long setupWord) |
| 270 | { |
| 271 | unsigned long oldCtrl = 0; |
| 272 | unsigned long newCtrl = 0; |
| 273 | long status = 0; |
| 274 | |
| 275 | /* Reads the control register in order to save the options related to the |
| 276 | DAC output state that may have been configured previously. */ |
| 277 | status = AD5791_GetRegisterValue(AD5791_REG_CTRL); |
| 278 | if(status < 0) |
| 279 | { |
| 280 | return status; |
| 281 | } |
| 282 | oldCtrl = status; |
| 283 | /* Clear LINCOMP, SDODIS, BIN2SC and RBUF bits. */ |
| 284 | oldCtrl = oldCtrl & ~(AD5791_CTRL_LINCOMP(-1) | |
| 285 | AD5791_CTRL_SDODIS | |
| 286 | AD5791_CTRL_BIN2SC | |
| 287 | AD5791_CTRL_RBUF); |
| 288 | /* Sets the new state provided by the user. */ |
| 289 | newCtrl = oldCtrl | setupWord; |
| 290 | status = AD5791_SetRegisterValue(AD5791_REG_CTRL, newCtrl); |
| 291 | |
| 292 | return status; |
| 293 | } |