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