Library files for AD717x and AD411x family of products.

Dependents:  

Committer:
mahphalke
Date:
Mon Aug 03 09:18:41 2020 +0000
Revision:
2:0780c234e572
Parent:
1:1d54b9448386
Added no-os driver support for AD4114/15 devices

Who changed what in which revision?

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