this is testing

Committer:
pmallick
Date:
Thu Jan 14 18:54:16 2021 +0530
Revision:
0:3afcd581558d
this is testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 0:3afcd581558d 1 /***************************************************************************//**
pmallick 0:3afcd581558d 2 * @file AD717X.c
pmallick 0:3afcd581558d 3 * @brief AD717X implementation file.
pmallick 0:3afcd581558d 4 * Devices: AD7172-2, AD7172-4, AD7173-8, AD7175-2, AD7175-8, AD7176-2
pmallick 0:3afcd581558d 5 * AD7177-2, AD4111, AD4112, AD4114, AD4115
pmallick 0:3afcd581558d 6 * @author acozma (andrei.cozma@analog.com)
pmallick 0:3afcd581558d 7 * dnechita (dan.nechita@analog.com)
pmallick 0:3afcd581558d 8 *
pmallick 0:3afcd581558d 9 ********************************************************************************
pmallick 0:3afcd581558d 10 * Copyright 2015(c) Analog Devices, Inc.
pmallick 0:3afcd581558d 11 *
pmallick 0:3afcd581558d 12 * All rights reserved.
pmallick 0:3afcd581558d 13 *
pmallick 0:3afcd581558d 14 * Redistribution and use in source and binary forms, with or without modification,
pmallick 0:3afcd581558d 15 * are permitted provided that the following conditions are met:
pmallick 0:3afcd581558d 16 * - Redistributions of source code must retain the above copyright
pmallick 0:3afcd581558d 17 * notice, this list of conditions and the following disclaimer.
pmallick 0:3afcd581558d 18 * - Redistributions in binary form must reproduce the above copyright
pmallick 0:3afcd581558d 19 * notice, this list of conditions and the following disclaimer in
pmallick 0:3afcd581558d 20 * the documentation and/or other materials provided with the
pmallick 0:3afcd581558d 21 * distribution.
pmallick 0:3afcd581558d 22 * - Neither the name of Analog Devices, Inc. nor the names of its
pmallick 0:3afcd581558d 23 * contributors may be used to endorse or promote products derived
pmallick 0:3afcd581558d 24 * from this software without specific prior written permission.
pmallick 0:3afcd581558d 25 * - The use of this software may or may not infringe the patent rights
pmallick 0:3afcd581558d 26 * of one or more patent holders. This license does not release you
pmallick 0:3afcd581558d 27 * from the requirement that you obtain separate licenses from these
pmallick 0:3afcd581558d 28 * patent holders to use this software.
pmallick 0:3afcd581558d 29 * - Use of the software either in source or binary form, must be run
pmallick 0:3afcd581558d 30 * on or directly connected to an Analog Devices Inc. component.
pmallick 0:3afcd581558d 31 *
pmallick 0:3afcd581558d 32 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
pmallick 0:3afcd581558d 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
pmallick 0:3afcd581558d 34 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
pmallick 0:3afcd581558d 35 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
pmallick 0:3afcd581558d 36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
pmallick 0:3afcd581558d 37 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
pmallick 0:3afcd581558d 38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
pmallick 0:3afcd581558d 39 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
pmallick 0:3afcd581558d 40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
pmallick 0:3afcd581558d 41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pmallick 0:3afcd581558d 42 *******************************************************************************/
pmallick 0:3afcd581558d 43
pmallick 0:3afcd581558d 44 /******************************************************************************/
pmallick 0:3afcd581558d 45 /***************************** Include Files **********************************/
pmallick 0:3afcd581558d 46 /******************************************************************************/
pmallick 0:3afcd581558d 47 #include <stdlib.h>
pmallick 0:3afcd581558d 48 #include "ad717x.h"
pmallick 0:3afcd581558d 49
pmallick 0:3afcd581558d 50 /* Error codes */
pmallick 0:3afcd581558d 51 #define INVALID_VAL -1 /* Invalid argument */
pmallick 0:3afcd581558d 52 #define COMM_ERR -2 /* Communication error on receive */
pmallick 0:3afcd581558d 53 #define TIMEOUT -3 /* A timeout has occured */
pmallick 0:3afcd581558d 54
pmallick 0:3afcd581558d 55 /***************************************************************************//**
pmallick 0:3afcd581558d 56 * @brief Searches through the list of registers of the driver instance and
pmallick 0:3afcd581558d 57 * retrieves a pointer to the register that matches the given address.
pmallick 0:3afcd581558d 58 *
pmallick 0:3afcd581558d 59 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 60 * @param reg_address - The address to be used to find the register.
pmallick 0:3afcd581558d 61 *
pmallick 0:3afcd581558d 62 * @return A pointer to the register if found or 0.
pmallick 0:3afcd581558d 63 *******************************************************************************/
pmallick 0:3afcd581558d 64 ad717x_st_reg *AD717X_GetReg(ad717x_dev *device,
pmallick 0:3afcd581558d 65 uint8_t reg_address)
pmallick 0:3afcd581558d 66 {
pmallick 0:3afcd581558d 67 uint8_t i;
pmallick 0:3afcd581558d 68 ad717x_st_reg *reg = 0;
pmallick 0:3afcd581558d 69
pmallick 0:3afcd581558d 70 if (!device || !device->regs)
pmallick 0:3afcd581558d 71 return 0;
pmallick 0:3afcd581558d 72
pmallick 0:3afcd581558d 73 for (i = 0; i < device->num_regs; i++) {
pmallick 0:3afcd581558d 74 if (device->regs[i].addr == reg_address) {
pmallick 0:3afcd581558d 75 reg = &device->regs[i];
pmallick 0:3afcd581558d 76 break;
pmallick 0:3afcd581558d 77 }
pmallick 0:3afcd581558d 78 }
pmallick 0:3afcd581558d 79
pmallick 0:3afcd581558d 80 return reg;
pmallick 0:3afcd581558d 81 }
pmallick 0:3afcd581558d 82
pmallick 0:3afcd581558d 83 /***************************************************************************//**
pmallick 0:3afcd581558d 84 * @brief Reads the value of the specified register.
pmallick 0:3afcd581558d 85 *
pmallick 0:3afcd581558d 86 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 87 * @param addr - The address of the register to be read. The value will be stored
pmallick 0:3afcd581558d 88 * inside the register structure that holds info about this register.
pmallick 0:3afcd581558d 89 *
pmallick 0:3afcd581558d 90 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 91 *******************************************************************************/
pmallick 0:3afcd581558d 92 int32_t AD717X_ReadRegister(ad717x_dev *device,
pmallick 0:3afcd581558d 93 uint8_t addr)
pmallick 0:3afcd581558d 94 {
pmallick 0:3afcd581558d 95 int32_t ret = 0;
pmallick 0:3afcd581558d 96 uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
pmallick 0:3afcd581558d 97 uint8_t i = 0;
pmallick 0:3afcd581558d 98 uint8_t check8 = 0;
pmallick 0:3afcd581558d 99 uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
pmallick 0:3afcd581558d 100 ad717x_st_reg *pReg;
pmallick 0:3afcd581558d 101
pmallick 0:3afcd581558d 102 if(!device)
pmallick 0:3afcd581558d 103 return INVALID_VAL;
pmallick 0:3afcd581558d 104
pmallick 0:3afcd581558d 105 pReg = AD717X_GetReg(device, addr);
pmallick 0:3afcd581558d 106 if (!pReg)
pmallick 0:3afcd581558d 107 return INVALID_VAL;
pmallick 0:3afcd581558d 108
pmallick 0:3afcd581558d 109 /* Build the Command word */
pmallick 0:3afcd581558d 110 buffer[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD |
pmallick 0:3afcd581558d 111 AD717X_COMM_REG_RA(pReg->addr);
pmallick 0:3afcd581558d 112
pmallick 0:3afcd581558d 113 /* Read data from the device */
pmallick 0:3afcd581558d 114 ret = spi_write_and_read(device->spi_desc,
pmallick 0:3afcd581558d 115 buffer,
pmallick 0:3afcd581558d 116 ((device->useCRC != AD717X_DISABLE) ? pReg->size + 1
pmallick 0:3afcd581558d 117 : pReg->size) + 1);
pmallick 0:3afcd581558d 118 if(ret < 0)
pmallick 0:3afcd581558d 119 return ret;
pmallick 0:3afcd581558d 120
pmallick 0:3afcd581558d 121 /* Check the CRC */
pmallick 0:3afcd581558d 122 if(device->useCRC == AD717X_USE_CRC) {
pmallick 0:3afcd581558d 123 msgBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD |
pmallick 0:3afcd581558d 124 AD717X_COMM_REG_RA(pReg->addr);
pmallick 0:3afcd581558d 125 for(i = 1; i < pReg->size + 2; ++i) {
pmallick 0:3afcd581558d 126 msgBuf[i] = buffer[i];
pmallick 0:3afcd581558d 127 }
pmallick 0:3afcd581558d 128 check8 = AD717X_ComputeCRC8(msgBuf, pReg->size + 2);
pmallick 0:3afcd581558d 129 }
pmallick 0:3afcd581558d 130 if(device->useCRC == AD717X_USE_XOR) {
pmallick 0:3afcd581558d 131 msgBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD |
pmallick 0:3afcd581558d 132 AD717X_COMM_REG_RA(pReg->addr);
pmallick 0:3afcd581558d 133 for(i = 1; i < pReg->size + 2; ++i) {
pmallick 0:3afcd581558d 134 msgBuf[i] = buffer[i];
pmallick 0:3afcd581558d 135 }
pmallick 0:3afcd581558d 136 check8 = AD717X_ComputeXOR8(msgBuf, pReg->size + 2);
pmallick 0:3afcd581558d 137 }
pmallick 0:3afcd581558d 138
pmallick 0:3afcd581558d 139 if(check8 != 0) {
pmallick 0:3afcd581558d 140 /* ReadRegister checksum failed. */
pmallick 0:3afcd581558d 141 return COMM_ERR;
pmallick 0:3afcd581558d 142 }
pmallick 0:3afcd581558d 143
pmallick 0:3afcd581558d 144 /* Build the result */
pmallick 0:3afcd581558d 145 pReg->value = 0;
pmallick 0:3afcd581558d 146 for(i = 1; i < pReg->size + 1; i++) {
pmallick 0:3afcd581558d 147 pReg->value <<= 8;
pmallick 0:3afcd581558d 148 pReg->value += buffer[i];
pmallick 0:3afcd581558d 149 }
pmallick 0:3afcd581558d 150
pmallick 0:3afcd581558d 151 return ret;
pmallick 0:3afcd581558d 152 }
pmallick 0:3afcd581558d 153
pmallick 0:3afcd581558d 154 /***************************************************************************//**
pmallick 0:3afcd581558d 155 * @brief Writes the value of the specified register.
pmallick 0:3afcd581558d 156 *
pmallick 0:3afcd581558d 157 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 158 * @param addr - The address of the register to be written with the value stored
pmallick 0:3afcd581558d 159 * inside the register structure that holds info about this
pmallick 0:3afcd581558d 160 * register.
pmallick 0:3afcd581558d 161 *
pmallick 0:3afcd581558d 162 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 163 *******************************************************************************/
pmallick 0:3afcd581558d 164 int32_t AD717X_WriteRegister(ad717x_dev *device,
pmallick 0:3afcd581558d 165 uint8_t addr)
pmallick 0:3afcd581558d 166 {
pmallick 0:3afcd581558d 167 int32_t ret = 0;
pmallick 0:3afcd581558d 168 int32_t regValue = 0;
pmallick 0:3afcd581558d 169 uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
pmallick 0:3afcd581558d 170 uint8_t i = 0;
pmallick 0:3afcd581558d 171 uint8_t crc8 = 0;
pmallick 0:3afcd581558d 172 ad717x_st_reg *preg;
pmallick 0:3afcd581558d 173
pmallick 0:3afcd581558d 174 if(!device)
pmallick 0:3afcd581558d 175 return INVALID_VAL;
pmallick 0:3afcd581558d 176
pmallick 0:3afcd581558d 177 preg = AD717X_GetReg(device, addr);
pmallick 0:3afcd581558d 178 if (!preg)
pmallick 0:3afcd581558d 179 return INVALID_VAL;
pmallick 0:3afcd581558d 180
pmallick 0:3afcd581558d 181 /* Build the Command word */
pmallick 0:3afcd581558d 182 wrBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_WR |
pmallick 0:3afcd581558d 183 AD717X_COMM_REG_RA(preg->addr);
pmallick 0:3afcd581558d 184
pmallick 0:3afcd581558d 185 /* Fill the write buffer */
pmallick 0:3afcd581558d 186 regValue = preg->value;
pmallick 0:3afcd581558d 187 for(i = 0; i < preg->size; i++) {
pmallick 0:3afcd581558d 188 wrBuf[preg->size - i] = regValue & 0xFF;
pmallick 0:3afcd581558d 189 regValue >>= 8;
pmallick 0:3afcd581558d 190 }
pmallick 0:3afcd581558d 191
pmallick 0:3afcd581558d 192 /* Compute the CRC */
pmallick 0:3afcd581558d 193 if(device->useCRC != AD717X_DISABLE) {
pmallick 0:3afcd581558d 194 crc8 = AD717X_ComputeCRC8(wrBuf, preg->size + 1);
pmallick 0:3afcd581558d 195 wrBuf[preg->size + 1] = crc8;
pmallick 0:3afcd581558d 196 }
pmallick 0:3afcd581558d 197
pmallick 0:3afcd581558d 198 /* Write data to the device */
pmallick 0:3afcd581558d 199 ret = spi_write_and_read(device->spi_desc,
pmallick 0:3afcd581558d 200 wrBuf,
pmallick 0:3afcd581558d 201 (device->useCRC != AD717X_DISABLE) ?
pmallick 0:3afcd581558d 202 preg->size + 2 : preg->size + 1);
pmallick 0:3afcd581558d 203
pmallick 0:3afcd581558d 204 return ret;
pmallick 0:3afcd581558d 205 }
pmallick 0:3afcd581558d 206
pmallick 0:3afcd581558d 207 /***************************************************************************//**
pmallick 0:3afcd581558d 208 * @brief Resets the device.
pmallick 0:3afcd581558d 209 *
pmallick 0:3afcd581558d 210 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 211 *
pmallick 0:3afcd581558d 212 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 213 *******************************************************************************/
pmallick 0:3afcd581558d 214 int32_t AD717X_Reset(ad717x_dev *device)
pmallick 0:3afcd581558d 215 {
pmallick 0:3afcd581558d 216 int32_t ret = 0;
pmallick 0:3afcd581558d 217 uint8_t wrBuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
pmallick 0:3afcd581558d 218
pmallick 0:3afcd581558d 219 if(!device)
pmallick 0:3afcd581558d 220 return INVALID_VAL;
pmallick 0:3afcd581558d 221
pmallick 0:3afcd581558d 222 ret = spi_write_and_read(device->spi_desc,
pmallick 0:3afcd581558d 223 wrBuf,
pmallick 0:3afcd581558d 224 8);
pmallick 0:3afcd581558d 225
pmallick 0:3afcd581558d 226 return ret;
pmallick 0:3afcd581558d 227 }
pmallick 0:3afcd581558d 228
pmallick 0:3afcd581558d 229 /***************************************************************************//**
pmallick 0:3afcd581558d 230 * @brief Waits until a new conversion result is available.
pmallick 0:3afcd581558d 231 *
pmallick 0:3afcd581558d 232 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 233 * @param timeout - Count representing the number of polls to be done until the
pmallick 0:3afcd581558d 234 * function returns if no new data is available.
pmallick 0:3afcd581558d 235 *
pmallick 0:3afcd581558d 236 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 237 *******************************************************************************/
pmallick 0:3afcd581558d 238 int32_t AD717X_WaitForReady(ad717x_dev *device,
pmallick 0:3afcd581558d 239 uint32_t timeout)
pmallick 0:3afcd581558d 240 {
pmallick 0:3afcd581558d 241 ad717x_st_reg *statusReg;
pmallick 0:3afcd581558d 242 int32_t ret;
pmallick 0:3afcd581558d 243 int8_t ready = 0;
pmallick 0:3afcd581558d 244
pmallick 0:3afcd581558d 245 if(!device || !device->regs)
pmallick 0:3afcd581558d 246 return INVALID_VAL;
pmallick 0:3afcd581558d 247
pmallick 0:3afcd581558d 248 statusReg = AD717X_GetReg(device, AD717X_STATUS_REG);
pmallick 0:3afcd581558d 249 if (!statusReg)
pmallick 0:3afcd581558d 250 return INVALID_VAL;
pmallick 0:3afcd581558d 251
pmallick 0:3afcd581558d 252 while(!ready && --timeout) {
pmallick 0:3afcd581558d 253 /* Read the value of the Status Register */
pmallick 0:3afcd581558d 254 ret = AD717X_ReadRegister(device, AD717X_STATUS_REG);
pmallick 0:3afcd581558d 255 if(ret < 0)
pmallick 0:3afcd581558d 256 return ret;
pmallick 0:3afcd581558d 257
pmallick 0:3afcd581558d 258 /* Check the RDY bit in the Status Register */
pmallick 0:3afcd581558d 259 ready = (statusReg->value & AD717X_STATUS_REG_RDY) == 0;
pmallick 0:3afcd581558d 260 }
pmallick 0:3afcd581558d 261
pmallick 0:3afcd581558d 262 return timeout ? 0 : TIMEOUT;
pmallick 0:3afcd581558d 263 }
pmallick 0:3afcd581558d 264
pmallick 0:3afcd581558d 265 /***************************************************************************//**
pmallick 0:3afcd581558d 266 * @brief Reads the conversion result from the device.
pmallick 0:3afcd581558d 267 *
pmallick 0:3afcd581558d 268 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 269 * @param pData - Pointer to store the read data.
pmallick 0:3afcd581558d 270 *
pmallick 0:3afcd581558d 271 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 272 *******************************************************************************/
pmallick 0:3afcd581558d 273 int32_t AD717X_ReadData(ad717x_dev *device,
pmallick 0:3afcd581558d 274 int32_t* pData)
pmallick 0:3afcd581558d 275 {
pmallick 0:3afcd581558d 276 ad717x_st_reg *dataReg;
pmallick 0:3afcd581558d 277 int32_t ret;
pmallick 0:3afcd581558d 278
pmallick 0:3afcd581558d 279 if(!device || !device->regs)
pmallick 0:3afcd581558d 280 return INVALID_VAL;
pmallick 0:3afcd581558d 281
pmallick 0:3afcd581558d 282 dataReg = AD717X_GetReg(device, AD717X_DATA_REG);
pmallick 0:3afcd581558d 283 if (!dataReg)
pmallick 0:3afcd581558d 284 return INVALID_VAL;
pmallick 0:3afcd581558d 285
pmallick 0:3afcd581558d 286 /* Update the data register length with respect to device and options */
pmallick 0:3afcd581558d 287 ret = AD717X_ComputeDataregSize(device);
pmallick 0:3afcd581558d 288
pmallick 0:3afcd581558d 289 /* Read the value of the Status Register */
pmallick 0:3afcd581558d 290 ret |= AD717X_ReadRegister(device, AD717X_DATA_REG);
pmallick 0:3afcd581558d 291
pmallick 0:3afcd581558d 292 /* Get the read result */
pmallick 0:3afcd581558d 293 *pData = dataReg->value;
pmallick 0:3afcd581558d 294
pmallick 0:3afcd581558d 295 return ret;
pmallick 0:3afcd581558d 296 }
pmallick 0:3afcd581558d 297
pmallick 0:3afcd581558d 298 /***************************************************************************//**
pmallick 0:3afcd581558d 299 * @brief Computes data register read size to account for bit number and status
pmallick 0:3afcd581558d 300 * read.
pmallick 0:3afcd581558d 301 *
pmallick 0:3afcd581558d 302 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 303 *
pmallick 0:3afcd581558d 304 * @return 0in case of success or negative code in case of failure.
pmallick 0:3afcd581558d 305 *******************************************************************************/
pmallick 0:3afcd581558d 306 int32_t AD717X_ComputeDataregSize(ad717x_dev *device)
pmallick 0:3afcd581558d 307 {
pmallick 0:3afcd581558d 308 ad717x_st_reg *reg_ptr;
pmallick 0:3afcd581558d 309 ad717x_st_reg *datareg_ptr;
pmallick 0:3afcd581558d 310 uint16_t case_var;
pmallick 0:3afcd581558d 311
pmallick 0:3afcd581558d 312 /* Get interface mode register pointer */
pmallick 0:3afcd581558d 313 reg_ptr = AD717X_GetReg(device, AD717X_IFMODE_REG);
pmallick 0:3afcd581558d 314 /* Get data register pointer */
pmallick 0:3afcd581558d 315 datareg_ptr = AD717X_GetReg(device, AD717X_DATA_REG);
pmallick 0:3afcd581558d 316 case_var = reg_ptr->value & (AD717X_IFMODE_REG_DATA_STAT |
pmallick 0:3afcd581558d 317 AD717X_IFMODE_REG_DATA_WL16);
pmallick 0:3afcd581558d 318
pmallick 0:3afcd581558d 319 /* Compute data register size */
pmallick 0:3afcd581558d 320 datareg_ptr->size = 3;
pmallick 0:3afcd581558d 321 if ((case_var & AD717X_IFMODE_REG_DATA_WL16) == AD717X_IFMODE_REG_DATA_WL16)
pmallick 0:3afcd581558d 322 datareg_ptr->size--;
pmallick 0:3afcd581558d 323 if ((case_var & AD717X_IFMODE_REG_DATA_STAT) == AD717X_IFMODE_REG_DATA_STAT)
pmallick 0:3afcd581558d 324 datareg_ptr->size++;
pmallick 0:3afcd581558d 325
pmallick 0:3afcd581558d 326 /* Get ID register pointer */
pmallick 0:3afcd581558d 327 reg_ptr = AD717X_GetReg(device, AD717X_ID_REG);
pmallick 0:3afcd581558d 328
pmallick 0:3afcd581558d 329 /* If the part is 32/24 bit wide add a byte to the read */
pmallick 0:3afcd581558d 330 if((reg_ptr->value & AD717X_ID_REG_MASK) == AD7177_2_ID_REG_VALUE)
pmallick 0:3afcd581558d 331 datareg_ptr->size++;
pmallick 0:3afcd581558d 332
pmallick 0:3afcd581558d 333 return 0;
pmallick 0:3afcd581558d 334 }
pmallick 0:3afcd581558d 335
pmallick 0:3afcd581558d 336 /***************************************************************************//**
pmallick 0:3afcd581558d 337 * @brief Computes the CRC checksum for a data buffer.
pmallick 0:3afcd581558d 338 *
pmallick 0:3afcd581558d 339 * @param pBuf - Data buffer
pmallick 0:3afcd581558d 340 * @param bufSize - Data buffer size in bytes
pmallick 0:3afcd581558d 341 *
pmallick 0:3afcd581558d 342 * @return Returns the computed CRC checksum.
pmallick 0:3afcd581558d 343 *******************************************************************************/
pmallick 0:3afcd581558d 344 uint8_t AD717X_ComputeCRC8(uint8_t * pBuf,
pmallick 0:3afcd581558d 345 uint8_t bufSize)
pmallick 0:3afcd581558d 346 {
pmallick 0:3afcd581558d 347 uint8_t i = 0;
pmallick 0:3afcd581558d 348 uint8_t crc = 0;
pmallick 0:3afcd581558d 349
pmallick 0:3afcd581558d 350 while(bufSize) {
pmallick 0:3afcd581558d 351 for(i = 0x80; i != 0; i >>= 1) {
pmallick 0:3afcd581558d 352 if(((crc & 0x80) != 0) != ((*pBuf & i) !=
pmallick 0:3afcd581558d 353 0)) { /* MSB of CRC register XOR input Bit from Data */
pmallick 0:3afcd581558d 354 crc <<= 1;
pmallick 0:3afcd581558d 355 crc ^= AD717X_CRC8_POLYNOMIAL_REPRESENTATION;
pmallick 0:3afcd581558d 356 } else {
pmallick 0:3afcd581558d 357 crc <<= 1;
pmallick 0:3afcd581558d 358 }
pmallick 0:3afcd581558d 359 }
pmallick 0:3afcd581558d 360 pBuf++;
pmallick 0:3afcd581558d 361 bufSize--;
pmallick 0:3afcd581558d 362 }
pmallick 0:3afcd581558d 363 return crc;
pmallick 0:3afcd581558d 364 }
pmallick 0:3afcd581558d 365
pmallick 0:3afcd581558d 366 /***************************************************************************//**
pmallick 0:3afcd581558d 367 * @brief Computes the XOR checksum for a data buffer.
pmallick 0:3afcd581558d 368 *
pmallick 0:3afcd581558d 369 * @param pBuf - Data buffer
pmallick 0:3afcd581558d 370 * @param bufSize - Data buffer size in bytes
pmallick 0:3afcd581558d 371 *
pmallick 0:3afcd581558d 372 * @return Returns the computed XOR checksum.
pmallick 0:3afcd581558d 373 *******************************************************************************/
pmallick 0:3afcd581558d 374 uint8_t AD717X_ComputeXOR8(uint8_t * pBuf,
pmallick 0:3afcd581558d 375 uint8_t bufSize)
pmallick 0:3afcd581558d 376 {
pmallick 0:3afcd581558d 377 uint8_t xor = 0;
pmallick 0:3afcd581558d 378
pmallick 0:3afcd581558d 379 while(bufSize) {
pmallick 0:3afcd581558d 380 xor ^= *pBuf;
pmallick 0:3afcd581558d 381 pBuf++;
pmallick 0:3afcd581558d 382 bufSize--;
pmallick 0:3afcd581558d 383 }
pmallick 0:3afcd581558d 384 return xor;
pmallick 0:3afcd581558d 385 }
pmallick 0:3afcd581558d 386
pmallick 0:3afcd581558d 387 /***************************************************************************//**
pmallick 0:3afcd581558d 388 * @brief Updates the CRC settings.
pmallick 0:3afcd581558d 389 *
pmallick 0:3afcd581558d 390 * @param device - The handler of the instance of the driver.
pmallick 0:3afcd581558d 391 *
pmallick 0:3afcd581558d 392 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 393 *******************************************************************************/
pmallick 0:3afcd581558d 394 int32_t AD717X_UpdateCRCSetting(ad717x_dev *device)
pmallick 0:3afcd581558d 395 {
pmallick 0:3afcd581558d 396 ad717x_st_reg *interfaceReg;
pmallick 0:3afcd581558d 397
pmallick 0:3afcd581558d 398 if(!device || !device->regs)
pmallick 0:3afcd581558d 399 return INVALID_VAL;
pmallick 0:3afcd581558d 400
pmallick 0:3afcd581558d 401 interfaceReg = AD717X_GetReg(device, AD717X_IFMODE_REG);
pmallick 0:3afcd581558d 402 if (!interfaceReg)
pmallick 0:3afcd581558d 403 return INVALID_VAL;
pmallick 0:3afcd581558d 404
pmallick 0:3afcd581558d 405 /* Get CRC State. */
pmallick 0:3afcd581558d 406 if(AD717X_IFMODE_REG_CRC_STAT(interfaceReg->value)) {
pmallick 0:3afcd581558d 407 device->useCRC = AD717X_USE_CRC;
pmallick 0:3afcd581558d 408 } else if(AD717X_IFMODE_REG_XOR_STAT(interfaceReg->value)) {
pmallick 0:3afcd581558d 409 device->useCRC = AD717X_USE_XOR;
pmallick 0:3afcd581558d 410 } else {
pmallick 0:3afcd581558d 411 device->useCRC = AD717X_DISABLE;
pmallick 0:3afcd581558d 412 }
pmallick 0:3afcd581558d 413
pmallick 0:3afcd581558d 414 return 0;
pmallick 0:3afcd581558d 415 }
pmallick 0:3afcd581558d 416
pmallick 0:3afcd581558d 417 /***************************************************************************//**
pmallick 0:3afcd581558d 418 * @brief Initializes the AD717X.
pmallick 0:3afcd581558d 419 *
pmallick 0:3afcd581558d 420 * @param device - The device structure.
pmallick 0:3afcd581558d 421 * @param init_param - The structure that contains the device initial
pmallick 0:3afcd581558d 422 * parameters.
pmallick 0:3afcd581558d 423 *
pmallick 0:3afcd581558d 424 * @return Returns 0 for success or negative error code.
pmallick 0:3afcd581558d 425 *******************************************************************************/
pmallick 0:3afcd581558d 426 int32_t AD717X_Init(ad717x_dev **device,
pmallick 0:3afcd581558d 427 ad717x_init_param init_param)
pmallick 0:3afcd581558d 428 {
pmallick 0:3afcd581558d 429 ad717x_dev *dev;
pmallick 0:3afcd581558d 430 int32_t ret;
pmallick 0:3afcd581558d 431 ad717x_st_reg *preg;
pmallick 0:3afcd581558d 432
pmallick 0:3afcd581558d 433 dev = (ad717x_dev *)malloc(sizeof(*dev));
pmallick 0:3afcd581558d 434 if (!dev)
pmallick 0:3afcd581558d 435 return -1;
pmallick 0:3afcd581558d 436
pmallick 0:3afcd581558d 437 dev->regs = init_param.regs;
pmallick 0:3afcd581558d 438 dev->num_regs = init_param.num_regs;
pmallick 0:3afcd581558d 439
pmallick 0:3afcd581558d 440 /* Initialize the SPI communication. */
pmallick 0:3afcd581558d 441 ret = spi_init(&dev->spi_desc, &init_param.spi_init);
pmallick 0:3afcd581558d 442 if (ret < 0)
pmallick 0:3afcd581558d 443 return ret;
pmallick 0:3afcd581558d 444
pmallick 0:3afcd581558d 445 /* Reset the device interface.*/
pmallick 0:3afcd581558d 446 ret = AD717X_Reset(dev);
pmallick 0:3afcd581558d 447 if (ret < 0)
pmallick 0:3afcd581558d 448 return ret;
pmallick 0:3afcd581558d 449
pmallick 0:3afcd581558d 450 /* Initialize ADC mode register. */
pmallick 0:3afcd581558d 451 ret = AD717X_WriteRegister(dev, AD717X_ADCMODE_REG);
pmallick 0:3afcd581558d 452 if(ret < 0)
pmallick 0:3afcd581558d 453 return ret;
pmallick 0:3afcd581558d 454
pmallick 0:3afcd581558d 455 /* Initialize Interface mode register. */
pmallick 0:3afcd581558d 456 ret = AD717X_WriteRegister(dev, AD717X_IFMODE_REG);
pmallick 0:3afcd581558d 457 if(ret < 0)
pmallick 0:3afcd581558d 458 return ret;
pmallick 0:3afcd581558d 459
pmallick 0:3afcd581558d 460 /* Get CRC State */
pmallick 0:3afcd581558d 461 ret = AD717X_UpdateCRCSetting(dev);
pmallick 0:3afcd581558d 462 if(ret < 0)
pmallick 0:3afcd581558d 463 return ret;
pmallick 0:3afcd581558d 464
pmallick 0:3afcd581558d 465 /* Initialize registers AD717X_GPIOCON_REG through AD717X_OFFSET0_REG */
pmallick 0:3afcd581558d 466 preg = AD717X_GetReg(dev, AD717X_GPIOCON_REG);
pmallick 0:3afcd581558d 467 if (!preg)
pmallick 0:3afcd581558d 468 return INVALID_VAL;
pmallick 0:3afcd581558d 469
pmallick 0:3afcd581558d 470 while (preg && preg->addr != AD717X_OFFSET0_REG) {
pmallick 0:3afcd581558d 471 if (preg->addr == AD717X_ID_REG) {
pmallick 0:3afcd581558d 472 preg ++;
pmallick 0:3afcd581558d 473 continue;
pmallick 0:3afcd581558d 474 }
pmallick 0:3afcd581558d 475
pmallick 0:3afcd581558d 476 ret = AD717X_WriteRegister(dev, preg->addr);
pmallick 0:3afcd581558d 477 if (ret < 0)
pmallick 0:3afcd581558d 478 break;
pmallick 0:3afcd581558d 479 preg ++;
pmallick 0:3afcd581558d 480 }
pmallick 0:3afcd581558d 481
pmallick 0:3afcd581558d 482 /* Read ID register to identify the part */
pmallick 0:3afcd581558d 483 ret = AD717X_ReadRegister(dev, AD717X_ID_REG);
pmallick 0:3afcd581558d 484 if(ret < 0)
pmallick 0:3afcd581558d 485 return ret;
pmallick 0:3afcd581558d 486
pmallick 0:3afcd581558d 487 *device = dev;
pmallick 0:3afcd581558d 488
pmallick 0:3afcd581558d 489 return ret;
pmallick 0:3afcd581558d 490 }
pmallick 0:3afcd581558d 491
pmallick 0:3afcd581558d 492 /***************************************************************************//**
pmallick 0:3afcd581558d 493 * @brief Free the resources allocated by AD717X_Init().
pmallick 0:3afcd581558d 494 * @param dev - The device structure.
pmallick 0:3afcd581558d 495 * @return SUCCESS in case of success, negative error code otherwise.
pmallick 0:3afcd581558d 496 *******************************************************************************/
pmallick 0:3afcd581558d 497 int32_t AD717X_remove(ad717x_dev *dev)
pmallick 0:3afcd581558d 498 {
pmallick 0:3afcd581558d 499 int32_t ret;
pmallick 0:3afcd581558d 500
pmallick 0:3afcd581558d 501 ret = spi_remove(dev->spi_desc);
pmallick 0:3afcd581558d 502
pmallick 0:3afcd581558d 503 free(dev);
pmallick 0:3afcd581558d 504
pmallick 0:3afcd581558d 505 return ret;
pmallick 0:3afcd581558d 506 }