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