this is testing

Committer:
pmallick
Date:
Thu Jan 14 19:12:57 2021 +0530
Revision:
0:e8a1ba50c46b
this is testing

Who changed what in which revision?

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