| 1 | /**************************************************************************//** |
| 2 | * @file AD5421.h |
| 3 | * @brief Header file of AD5421 Driver for Microblaze processor. |
| 4 | * @author Lucian Sin ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2013(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, |
| 13 | * are permitted provided that the following conditions are met: |
| 14 | * - Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * - Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in |
| 18 | * the documentation and/or other materials provided with the |
| 19 | * distribution. |
| 20 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 21 | * contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * - The use of this software may or may not infringe the patent rights |
| 24 | * of one or more patent holders. This license does not release you |
| 25 | * from the requirement that you obtain separate licenses from these |
| 26 | * patent holders to use this software. |
| 27 | * - Use of the software either in source or binary form, must be run |
| 28 | * on or directly connected to an Analog Devices Inc. component. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 32 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 33 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 34 | * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 35 | * * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS |
| 36 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 37 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 38 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 39 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 40 | * DAMAGE. |
| 41 | * |
| 42 | ******************************************************************************/ |
| 43 | |
| 44 | /*****************************************************************************/ |
| 45 | /***************************** Include Files *********************************/ |
| 46 | /*****************************************************************************/ |
| 47 | #include "AD5421.h" |
| 48 | #include "Communication.h" |
| 49 | |
| 50 | /*****************************************************************************/ |
| 51 | /************************* Functions Definitions *****************************/ |
| 52 | /*****************************************************************************/ |
| 53 | |
| 54 | /**************************************************************************//** |
| 55 | * @brief Initialize SPI and Initial Values for AD5421 Board. |
| 56 | * |
| 57 | * @param None. |
| 58 | * |
| 59 | * @return retValue - Result of the initialization. |
| 60 | * Example: 0 - if initialization was successful; |
| 61 | * -1 - if initialization was unsuccessful. |
| 62 | ******************************************************************************/ |
| 63 | int AD5421_Init(void) |
| 64 | { |
| 65 | int spiData = 0; |
| 66 | /* Set bits for initialization process. */ |
| 67 | int cmdCheck = (CTRL_AUTO_FAULT_RDBK | |
| 68 | CTRL_SEL_ADC_INPUT | |
| 69 | CTRL_ONCHIP_ADC | |
| 70 | !CTRL_SPI_WATCHDOG); |
| 71 | int retValue = -1; |
| 72 | |
| 73 | /* Setup the SPI interface. */ |
| 74 | SPI_Init(0, 1000000, 0, 0); |
| 75 | /* Setup AD5421 control register. */ |
| 76 | /* Write to the control register. */ |
| 77 | spiData = AD5421_CMD(AD5421_CMDWRCTRL); |
| 78 | /* Set certain bits. */ |
| 79 | spiData += (CTRL_AUTO_FAULT_RDBK | |
| 80 | CTRL_SEL_ADC_INPUT | |
| 81 | CTRL_ONCHIP_ADC | |
| 82 | CTRL_SPI_WATCHDOG); |
| 83 | /* Send data via SPI. */ |
| 84 | AD5421_Set(&spiData); |
| 85 | /* Read from the control register. */ |
| 86 | spiData = AD5421_CMD(AD5421_CMDRDCTRL); |
| 87 | AD5421_Set(&spiData); |
| 88 | /* Receive data via SPI. */ |
| 89 | spiData = AD5421_Get(); |
| 90 | |
| 91 | if (spiData & cmdCheck) |
| 92 | { |
| 93 | /* Check the echo. */ |
| 94 | retValue = 0; |
| 95 | } |
| 96 | /* Set GPIO pins */ |
| 97 | AD5421_LDAC_OUT; |
| 98 | AD5421_LDAC_LOW; |
| 99 | AD5421_FAULT_IN; |
| 100 | |
| 101 | return(retValue); |
| 102 | } |
| 103 | |
| 104 | /**************************************************************************//** |
| 105 | * @brief Write to the DAC register. |
| 106 | * |
| 107 | * @param dacValue - desired value to be written in register. |
| 108 | * |
| 109 | * @return None. |
| 110 | ******************************************************************************/ |
| 111 | void AD5421_SetDac(int dacValue) |
| 112 | { |
| 113 | int spiData = 0; |
| 114 | |
| 115 | /* Load data in DAC register. */ |
| 116 | spiData = AD5421_CMD(AD5421_CMDWRDAC); |
| 117 | spiData += (dacValue); |
| 118 | AD5421_Set(&spiData); |
| 119 | } |
| 120 | |
| 121 | /**************************************************************************//** |
| 122 | * @brief Write to the Offset Adjust register. |
| 123 | * |
| 124 | * @param offsetValue - desired value to be written in register. |
| 125 | * |
| 126 | * @return None. |
| 127 | ******************************************************************************/ |
| 128 | void AD5421_SetOffset(int offsetValue) |
| 129 | { |
| 130 | int spiData = 0; |
| 131 | |
| 132 | /* Load data in OFFSET register. */ |
| 133 | spiData = AD5421_CMD(AD5421_CMDWROFFSET); |
| 134 | spiData += (offsetValue); |
| 135 | AD5421_Set(&spiData); |
| 136 | } |
| 137 | |
| 138 | /**************************************************************************//** |
| 139 | * @brief Write to the Gain Adjust register |
| 140 | * |
| 141 | * @param gainValue - desired value to be written in register. |
| 142 | * |
| 143 | * @return None. |
| 144 | ******************************************************************************/ |
| 145 | void AD5421_SetGain(int gainValue) |
| 146 | { |
| 147 | int spiData = 0; |
| 148 | |
| 149 | /* Load data in GAIN register. */ |
| 150 | spiData = AD5421_CMD(AD5421_CMDWRGAIN); |
| 151 | spiData += (gainValue); |
| 152 | AD5421_Set(&spiData); |
| 153 | } |
| 154 | |
| 155 | /**************************************************************************//** |
| 156 | * @brief Read from the DAC register. |
| 157 | * |
| 158 | * @param None. |
| 159 | * |
| 160 | * @return dacValue - value read from the register. |
| 161 | ******************************************************************************/ |
| 162 | int AD5421_GetDac(void) |
| 163 | { |
| 164 | int spiData = 0; |
| 165 | int dacValue = 0; |
| 166 | |
| 167 | /* Read data from DAC register. */ |
| 168 | spiData = AD5421_CMD(AD5421_CMDRDDAC); |
| 169 | AD5421_Set(&spiData); |
| 170 | dacValue = AD5421_Get(); |
| 171 | |
| 172 | return(dacValue); |
| 173 | } |
| 174 | |
| 175 | /**************************************************************************//** |
| 176 | * @brief Read from the Offset Adjust register. |
| 177 | * |
| 178 | * @param None. |
| 179 | * |
| 180 | * @return offsetValue - value calculated according to the datasheet formula. |
| 181 | ******************************************************************************/ |
| 182 | int AD5421_GetOffset(void) |
| 183 | { |
| 184 | int spiData = 0; |
| 185 | int offsetValue = 0; |
| 186 | |
| 187 | /* Read data from OFFSET register. */ |
| 188 | spiData = AD5421_CMD(AD5421_CMDRDOFFSET); |
| 189 | AD5421_Set(&spiData); |
| 190 | offsetValue = AD5421_Get(); |
| 191 | |
| 192 | return(offsetValue); |
| 193 | } |
| 194 | |
| 195 | /**************************************************************************//** |
| 196 | * @brief Read from the Gain Adjust register |
| 197 | * |
| 198 | * @param None. |
| 199 | * |
| 200 | * @return gainValue - value calculated according to the datasheet formula |
| 201 | ******************************************************************************/ |
| 202 | int AD5421_GetGain(void) |
| 203 | { |
| 204 | int spiData = 0; |
| 205 | int gainValue = 0; |
| 206 | |
| 207 | /* Read from GAIN register. */ |
| 208 | spiData = AD5421_CMD(AD5421_CMDRDGAIN); |
| 209 | AD5421_Set(&spiData); |
| 210 | gainValue = AD5421_Get(); |
| 211 | |
| 212 | return(gainValue); |
| 213 | } |
| 214 | |
| 215 | /**************************************************************************//** |
| 216 | * @brief Read from the Fault register |
| 217 | * |
| 218 | * @param None. |
| 219 | * |
| 220 | * @return value - register content |
| 221 | ******************************************************************************/ |
| 222 | int AD5421_GetFault(void) |
| 223 | { |
| 224 | int spiData = 0; |
| 225 | int value = 0; |
| 226 | |
| 227 | /* Read from GAIN register. */ |
| 228 | spiData = AD5421_CMD(AD5421_CMDRDFAULT); |
| 229 | AD5421_Set(&spiData); |
| 230 | value = AD5421_Get(); |
| 231 | |
| 232 | return(value); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /**************************************************************************//** |
| 237 | * @brief Read the temperature from Fault register. |
| 238 | * |
| 239 | * @param None. |
| 240 | * |
| 241 | * @return tempValue - temperature value in Celsius degrees. |
| 242 | ******************************************************************************/ |
| 243 | int AD5421_GetTemp(void) |
| 244 | { |
| 245 | int spiData = 0; |
| 246 | int tempValue = 0; |
| 247 | /* Constant taken from datasheet formula */ |
| 248 | float tempConstant1 = -1.559; |
| 249 | /* Constant taken from datasheet formula */ |
| 250 | int tempConstant2 = 312; |
| 251 | |
| 252 | /* Set CONTROL Register to measure temperature. */ |
| 253 | spiData = AD5421_CMD(AD5421_CMDWRCTRL); |
| 254 | spiData += (CTRL_AUTO_FAULT_RDBK | |
| 255 | CTRL_SEL_ADC_INPUT | |
| 256 | CTRL_ONCHIP_ADC | |
| 257 | CTRL_SPI_WATCHDOG); |
| 258 | AD5421_Set(&spiData); |
| 259 | /* Initiate temperature measurement. */ |
| 260 | spiData = 0; |
| 261 | spiData = AD5421_CMD(AD5421_CMDMEASVTEMP); |
| 262 | AD5421_Set(&spiData); |
| 263 | /* Read temperature from Fault register. */ |
| 264 | spiData = 0; |
| 265 | spiData = AD5421_CMD(AD5421_CMDRDFAULT); |
| 266 | AD5421_Set(&spiData); |
| 267 | tempValue = AD5421_Get() & 0x0FF; |
| 268 | /* Calculate temperature according to the datasheet formula. */ |
| 269 | tempValue = (int) (tempConstant1 * tempValue) + tempConstant2; |
| 270 | |
| 271 | return(tempValue); |
| 272 | } |
| 273 | |
| 274 | /**************************************************************************//** |
| 275 | * @brief Read VLoop-COM from Fault register. |
| 276 | * |
| 277 | * @param None. |
| 278 | * |
| 279 | * @return vlopValue - value of the voltage drop between Vloop and COM. |
| 280 | ******************************************************************************/ |
| 281 | float AD5421_GetVloop(void) |
| 282 | { |
| 283 | int spiData = 0; |
| 284 | float vloopValue = 0; |
| 285 | /* Constant taken from datasheet formula */ |
| 286 | float divide_constant_vloop = 0.009765625; |
| 287 | |
| 288 | /* Set CONTROL Register to measure Vloop. */ |
| 289 | spiData = AD5421_CMD(AD5421_CMDWRCTRL); |
| 290 | spiData += (CTRL_AUTO_FAULT_RDBK | |
| 291 | CTRL_ONCHIP_ADC | |
| 292 | CTRL_SPI_WATCHDOG); |
| 293 | AD5421_Set(&spiData); |
| 294 | /* Initiate VLOOP measurement. */ |
| 295 | spiData = 0; |
| 296 | spiData = AD5421_CMD(AD5421_CMDMEASVTEMP); |
| 297 | AD5421_Set(&spiData); |
| 298 | /* Read VLOOP from Fault register. */ |
| 299 | spiData = 0; |
| 300 | spiData = AD5421_CMD(AD5421_CMDRDFAULT); |
| 301 | AD5421_Set(&spiData); |
| 302 | /* Calculate Vloop according to the datasheet formula. */ |
| 303 | vloopValue = (float) (AD5421_Get() & 0x0FF); |
| 304 | vloopValue = divide_constant_vloop * vloopValue; |
| 305 | |
| 306 | return(vloopValue); |
| 307 | } |
| 308 | |
| 309 | /**************************************************************************//** |
| 310 | * @brief Send data via SPI |
| 311 | * |
| 312 | * @param value: value to be sent via SPI |
| 313 | * |
| 314 | * @return - Result of the initialization. |
| 315 | * Example: 0 - if initialization was successful; |
| 316 | * -1 - if initialization was unsuccessful. |
| 317 | ******************************************************************************/ |
| 318 | int AD5421_Set(int *value) |
| 319 | { |
| 320 | unsigned char txBuffer[3] = {0, 0, 0}; |
| 321 | char status = 0; |
| 322 | |
| 323 | /* Arrange the data to be sent in 8 bit packets. */ |
| 324 | txBuffer[0] = (unsigned char) ((*value & 0xff0000) >> 16); |
| 325 | txBuffer[1] = (unsigned char) ((*value & 0x00ff00) >> 8); |
| 326 | txBuffer[2] = (unsigned char) ((*value & 0x0000ff) >> 0); |
| 327 | /* Do a write operation via SPI. */ |
| 328 | status = SPI_Write(AD5421_SLAVE_ID, txBuffer, 3); |
| 329 | if (status != 3) |
| 330 | { |
| 331 | return -1; |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | return 0; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /**************************************************************************//** |
| 340 | * @brief Read data via SPI |
| 341 | * |
| 342 | * @param None. |
| 343 | * |
| 344 | * @return returnVal - Data read |
| 345 | ******************************************************************************/ |
| 346 | int AD5421_Get(void) |
| 347 | { |
| 348 | int returnVal = 0; |
| 349 | unsigned char rxBuffer[3] = {0, 0, 0}; |
| 350 | char status = 0; |
| 351 | |
| 352 | status = SPI_Read(AD5421_SLAVE_ID, rxBuffer, 3); |
| 353 | if (status != 3) |
| 354 | { |
| 355 | return -1; |
| 356 | } |
| 357 | returnVal |= (int) (rxBuffer[1] << 8); |
| 358 | returnVal |= (int) (rxBuffer[2] << 0); |
| 359 | |
| 360 | return(returnVal); |
| 361 | } |
| 362 | |
| 363 | /**************************************************************************//** |
| 364 | * @brief Resets the AD5421 device. |
| 365 | * |
| 366 | * @param None. |
| 367 | * |
| 368 | * @return None. |
| 369 | ******************************************************************************/ |
| 370 | void AD5421_Reset(void) |
| 371 | { |
| 372 | int spiData = 0; |
| 373 | |
| 374 | spiData = AD5421_CMD(AD5421_CMDRST); |
| 375 | AD5421_Set(&spiData); |
| 376 | } |