Analog Devices AD7124-8 - 8-Channel, Low Noise, Low Power, 24-Bit, Sigma-Delta ADC with PGA and Reference

Dependents:   CN0398 CN0391 CN0398_arduino

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AD7124.cpp Source File

AD7124.cpp

Go to the documentation of this file.
00001 /**
00002 *   @file     AD7124.cpp
00003 *   @brief    Source file for AD7124 ADC
00004 *   @author   Analog Devices Inc.
00005 *
00006 * For support please go to:
00007 * Github: https://github.com/analogdevicesinc/mbed-adi
00008 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
00009 * Product: http://www.analog.com/ad7124
00010 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
00011 
00012 ********************************************************************************
00013 * Copyright 2016(c) Analog Devices, Inc.
00014 *
00015 * All rights reserved.
00016 *
00017 * Redistribution and use in source and binary forms, with or without
00018 * modification, are permitted provided that the following conditions are met:
00019 *  - Redistributions of source code must retain the above copyright
00020 *    notice, this list of conditions and the following disclaimer.
00021 *  - Redistributions in binary form must reproduce the above copyright
00022 *    notice, this list of conditions and the following disclaimer in
00023 *    the documentation and/or other materials provided with the
00024 *    distribution.
00025 *  - Neither the name of Analog Devices, Inc. nor the names of its
00026 *    contributors may be used to endorse or promote products derived
00027 *    from this software without specific prior written permission.
00028 *  - The use of this software may or may not infringe the patent rights
00029 *    of one or more patent holders.  This license does not release you
00030 *    from the requirement that you obtain separate licenses from these
00031 *    patent holders to use this software.
00032 *  - Use of the software either in source or binary form, must be run
00033 *    on or directly connected to an Analog Devices Inc. component.
00034 *
00035 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00036 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00037 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00038 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00039 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00040 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00041 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00042 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00043 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00044 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045 *
00046 ********************************************************************************/
00047 
00048 #include <stdint.h>
00049 #include "mbed.h"
00050 #include "AD7124.h"
00051 
00052 
00053 /**
00054  * @brief AD7790 constructor, sets CS pin and SPI format
00055  * @param CS - (optional)chip select of the AD7790
00056  * @param MOSI - (optional)pin of the SPI interface
00057  * @param MISO - (optional)pin of the SPI interface
00058  * @param SCK  - (optional)pin of the SPI interface
00059  */
00060     
00061 const static AD7124::ad7124_st_reg  ad7124_regs_init[57] = { 
00062         {0x00, 0x00,   1, 2}, /* AD7124_Status */
00063         {0x01, 0x0000, 2, 1}, /* AD7124_ADC_Control */
00064         {0x02, 0x0000, 3, 2}, /* AD7124_Data */
00065         {0x03, 0x0000, 3, 1}, /* AD7124_IOCon1 */
00066         {0x04, 0x0000, 2, 1}, /* AD7124_IOCon2 */
00067         {0x05, 0x12,   1, 2}, /* AD7124_ID */
00068         {0x06, 0x0000, 3, 2}, /* AD7124_Error */
00069         {0x07, 0x0400, 3, 1}, /* AD7124_Error_En */
00070         {0x08, 0x00,   1, 2}, /* AD7124_Mclk_Count */
00071         {0x09, 0x8001, 2, 1}, /* AD7124_Channel_0 */
00072         {0x0A, 0x0001, 2, 1}, /* AD7124_Channel_1 */
00073         {0x0B, 0x0001, 2, 1}, /* AD7124_Channel_2 */
00074         {0x0C, 0x0001, 2, 1}, /* AD7124_Channel_3 */
00075         {0x0D, 0x0001, 2, 1}, /* AD7124_Channel_4 */
00076         {0x0E, 0x0001, 2, 1}, /* AD7124_Channel_5 */
00077         {0x0F, 0x0001, 2, 1}, /* AD7124_Channel_6 */
00078         {0x10, 0x0001, 2, 1}, /* AD7124_Channel_7 */
00079         {0x11, 0x0001, 2, 1}, /* AD7124_Channel_8 */
00080         {0x12, 0x0001, 2, 1}, /* AD7124_Channel_9 */
00081         {0x13, 0x0001, 2, 1}, /* AD7124_Channel_10 */
00082         {0x14, 0x0001, 2, 1}, /* AD7124_Channel_11 */
00083         {0x15, 0x0001, 2, 1}, /* AD7124_Channel_12 */
00084         {0x16, 0x0001, 2, 1}, /* AD7124_Channel_13 */
00085         {0x17, 0x0001, 2, 1}, /* AD7124_Channel_14 */
00086         {0x18, 0x0001, 2, 1}, /* AD7124_Channel_15 */
00087         {0x19, 0x0860, 2, 1}, /* AD7124_Config_0 */
00088         {0x1A, 0x0860, 2, 1}, /* AD7124_Config_1 */
00089         {0x1B, 0x0860, 2, 1}, /* AD7124_Config_2 */
00090         {0x1C, 0x0860, 2, 1}, /* AD7124_Config_3 */
00091         {0x1D, 0x0860, 2, 1}, /* AD7124_Config_4 */
00092         {0x1E, 0x0860, 2, 1}, /* AD7124_Config_5 */
00093         {0x1F, 0x0860, 2, 1}, /* AD7124_Config_6 */
00094         {0x20, 0x0860, 2, 1}, /* AD7124_Config_7 */
00095         {0x21, 0x060180, 3, 1}, /* AD7124_Filter_0 */
00096         {0x22, 0x060180, 3, 1}, /* AD7124_Filter_1 */
00097         {0x23, 0x060180, 3, 1}, /* AD7124_Filter_2 */
00098         {0x24, 0x060180, 3, 1}, /* AD7124_Filter_3 */
00099         {0x25, 0x060180, 3, 1}, /* AD7124_Filter_4 */
00100         {0x26, 0x060180, 3, 1}, /* AD7124_Filter_5 */
00101         {0x27, 0x060180, 3, 1}, /* AD7124_Filter_6 */
00102         {0x28, 0x060180, 3, 1}, /* AD7124_Filter_7 */
00103         {0x29, 0x800000, 3, 1}, /* AD7124_Offset_0 */
00104         {0x2A, 0x800000, 3, 1}, /* AD7124_Offset_1 */
00105         {0x2B, 0x800000, 3, 1}, /* AD7124_Offset_2 */
00106         {0x2C, 0x800000, 3, 1}, /* AD7124_Offset_3 */
00107         {0x2D, 0x800000, 3, 1}, /* AD7124_Offset_4 */
00108         {0x2E, 0x800000, 3, 1}, /* AD7124_Offset_5 */
00109         {0x2F, 0x800000, 3, 1}, /* AD7124_Offset_6 */
00110         {0x30, 0x800000, 3, 1}, /* AD7124_Offset_7 */
00111         {0x31, 0x500000, 3, 1}, /* AD7124_Gain_0 */
00112         {0x32, 0x500000, 3, 1}, /* AD7124_Gain_1 */
00113         {0x33, 0x500000, 3, 1}, /* AD7124_Gain_2 */
00114         {0x34, 0x500000, 3, 1}, /* AD7124_Gain_3 */
00115         {0x35, 0x500000, 3, 1}, /* AD7124_Gain_4 */
00116         {0x36, 0x500000, 3, 1}, /* AD7124_Gain_5 */
00117         {0x37, 0x500000, 3, 1}, /* AD7124_Gain_6 */
00118         {0x38, 0x500000, 3, 1}, /* AD7124_Gain_7 */
00119     }; 
00120  
00121 AD7124::AD7124(PinName CS,
00122                PinName MOSI,
00123                PinName MISO,
00124                PinName SCK) :
00125     miso(MISO), ad7124(MOSI, MISO, SCK), cs(CS)
00126     
00127 {
00128     cs = true; // cs is active low
00129     ad7124.format(8, _SPI_MODE);     
00130     memcpy(ad7124_regs , ad7124_regs_init, 57 * sizeof(ad7124_st_reg ));
00131     this->regs = ad7124_regs ;
00132     this->useCRC = false;
00133     
00134     }
00135 
00136 /**
00137  * @brief Set AD7790 SPI frequency
00138  * @param hz - SPI bus frequency in hz
00139  * @return none
00140  */
00141 void AD7124::frequency(int hz)
00142 {
00143     ad7124.frequency(hz);
00144 }
00145 
00146 /**
00147  * @brief Resets the AD7790
00148  * @return none
00149  */
00150 /*void AD7124::reset()
00151 {
00152     ad7124.format(8, _SPI_MODE);
00153     cs = false;
00154     wait_us(_DELAY_TIMING);
00155     ad7124.write(_RESET);
00156     ad7124.write(_RESET);
00157     ad7124.write(_RESET);
00158     ad7124.write(_RESET);
00159     ad7124.write(_RESET);
00160     ad7124.write(_RESET);
00161     ad7124.write(_RESET);
00162     ad7124.write(_RESET);
00163     wait_us(_DELAY_TIMING);
00164     cs = true;
00165     //_continous_conversion = true;
00166 }*/
00167 /**
00168  * @brief Reads a register of the AD7790
00169  * @param  address - address of the register
00170  * @return value of the register
00171  */
00172 uint16_t AD7124::read_reg(uint8_t address)
00173 {
00174     uint16_t data = address << 8;
00175     data |= _DUMMY_BYTE;
00176     data |= _READ_FLAG;
00177     return  write_spi(data);
00178 }
00179 
00180 /**
00181  * @brief Writes a register of the AD7790
00182  * @param address - address of the register
00183  * @param reg_val - value to be written
00184  * @return none
00185  *
00186  */
00187 void AD7124::write_reg(uint8_t address, uint8_t reg_val)
00188 {
00189     uint16_t spi_data = address << 8;
00190     spi_data |= reg_val;
00191     write_spi(spi_data);
00192 }
00193 
00194 /**
00195  * @brief Writes 16bit data to the AD7790 SPI interface
00196  * @param reg_val to be written
00197  * @return data returned by the AD7790
00198  */
00199 uint16_t AD7124::write_spi(uint16_t reg_val)
00200 {
00201     uint16_t data_result;
00202     uint8_t upper_byte = (reg_val >> 8) & 0xFF;
00203     uint8_t lower_byte = reg_val & 0xFF;
00204     ad7124.format(8, _SPI_MODE);
00205     cs = false;
00206     data_result  =  (ad7124.write(upper_byte) << 8);
00207     data_result |=   ad7124.write(lower_byte);
00208     cs = true;
00209     return data_result;
00210 }
00211 
00212 
00213 
00214 /***************************************************************************//**
00215 * @brief Reads the value of the specified register without checking if the
00216 *        device is ready to accept user requests.
00217 *
00218 * @param device - The handler of the instance of the driver.
00219 * @param pReg - Pointer to the register structure holding info about the
00220 *               register to be read. The read value is stored inside the
00221 *               register structure.
00222 *
00223 * @return Returns 0 for success or negative error code.
00224 *******************************************************************************/
00225 int32_t AD7124::NoCheckReadRegister(ad7124_st_reg * pReg)
00226 {
00227     int32_t ret       = 0;
00228     uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
00229     uint8_t i         = 0;
00230     uint8_t check8    = 0;
00231     uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
00232 
00233 
00234     check8 = useCRC;
00235 
00236     /* Build the Command word */
00237     buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
00238                 AD7124_COMM_REG_RA(pReg->addr);
00239 
00240     /* Read data from the device */
00241     ret = SPI_Read(buffer,
00242                    ((useCRC != AD7124_DISABLE_CRC) ? pReg->size + 1
00243                     : pReg->size) + 1);
00244     if(ret < 0)
00245         return ret;
00246 
00247     /* Check the CRC */
00248     if(check8 == AD7124_USE_CRC) {
00249         msgBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
00250                     AD7124_COMM_REG_RA(pReg->addr);
00251         for(i = 1; i < pReg->size + 2; ++i) {
00252             msgBuf[i] = buffer[i];
00253         }
00254         check8 = ComputeCRC8 (msgBuf, pReg->size + 2);
00255     }
00256 
00257     if(check8 != 0) {
00258         /* ReadRegister checksum failed. */
00259         return COMM_ERR;
00260     }
00261 
00262     /* Build the result */
00263     pReg->value = 0;
00264     for(i = 1; i < pReg->size + 1; i++) {
00265         pReg->value <<= 8;
00266         pReg->value += buffer[i];
00267     }
00268 
00269     return ret;
00270 }
00271 
00272 /***************************************************************************//**
00273 * @brief Writes the value of the specified register without checking if the
00274 *        device is ready to accept user requests.
00275 *
00276 * @param device - The handler of the instance of the driver.
00277 * @param reg - Register structure holding info about the register to be written
00278 *
00279 * @return Returns 0 for success or negative error code.
00280 *******************************************************************************/
00281 int32_t AD7124::NoCheckWriteRegister(ad7124_st_reg  reg)
00282 {
00283     int32_t ret      = 0;
00284     int32_t regValue = 0;
00285     uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
00286     uint8_t i        = 0;
00287     uint8_t crc8     = 0;
00288 
00289 
00290     /* Build the Command word */
00291     wrBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_WR |
00292                AD7124_COMM_REG_RA(reg.addr);
00293 
00294     /* Fill the write buffer */
00295     regValue = reg.value;
00296     for(i = 0; i < reg.size; i++) {
00297         wrBuf[reg.size - i] = regValue & 0xFF;
00298         regValue >>= 8;
00299     }
00300 
00301     /* Compute the CRC */
00302     if(useCRC != AD7124_DISABLE_CRC) {
00303         crc8 = ComputeCRC8 (wrBuf, reg.size + 1);
00304         wrBuf[reg.size + 1] = crc8;
00305     }
00306 
00307     /* Write data to the device */
00308     ret = SPI_Write(wrBuf,
00309                     (useCRC != AD7124_DISABLE_CRC) ? reg.size + 2
00310                     : reg.size + 1);
00311 
00312     return ret;
00313 }
00314 
00315 /***************************************************************************//**
00316 * @brief Reads the value of the specified register only when the device is ready
00317 *        to accept user requests. If the device ready flag is deactivated the
00318 *        read operation will be executed without checking the device state.
00319 *
00320 * @param device - The handler of the instance of the driver.
00321 * @param pReg - Pointer to the register structure holding info about the
00322 *               register to be read. The read value is stored inside the
00323 *               register structure.
00324 *
00325 * @return Returns 0 for success or negative error code.
00326 *******************************************************************************/
00327 int32_t AD7124::ReadRegister(ad7124_st_reg * pReg)
00328 {
00329     int32_t ret;
00330 
00331     if (pReg->addr != ERR_REG && check_ready) {
00332         ret = WaitForSpiReady (spi_rdy_poll_cnt);
00333         if (ret < 0)
00334             return ret;
00335     }
00336     ret = NoCheckReadRegister(pReg);
00337 
00338     return ret;
00339 }
00340 
00341 /***************************************************************************//**
00342 * @brief Writes the value of the specified register only when the device is
00343 *        ready to accept user requests. If the device ready flag is deactivated
00344 *        the write operation will be executed without checking the device state.
00345 *
00346 * @param device - The handler of the instance of the driver.
00347 * @param reg - Register structure holding info about the register to be written
00348 *
00349 * @return Returns 0 for success or negative error code.
00350 *******************************************************************************/
00351 int32_t AD7124::WriteRegister(ad7124_st_reg  pReg)
00352 {
00353     int32_t ret;
00354 
00355     if (check_ready) {
00356         ret = WaitForSpiReady (spi_rdy_poll_cnt);
00357         if (ret < 0)
00358             return ret;
00359     }
00360     ret = NoCheckWriteRegister(pReg);
00361 
00362     return ret;
00363 }
00364 
00365 /***************************************************************************//**
00366 * @brief Reads and returns the value of a device register. The read value is
00367 *        also stored in software register list of the device.
00368 *
00369 * @param device - The handler of the instance of the driver.
00370 * @param reg - Which register to read from.
00371 * @param pError - Pointer to the location where to store the error code if an
00372 *                 error occurs. Stores 0 for success or negative error code.
00373 *                 Does not store anything if pErorr = NULL;
00374 *
00375 * @return Returns the value read from the specified register.
00376 *******************************************************************************/
00377 uint32_t AD7124::ReadDeviceRegister(enum ad7124_registers reg)
00378 {
00379     ReadRegister(&regs[reg]);
00380     return (regs[reg].value);
00381 }
00382 
00383 /***************************************************************************//**
00384 * @brief Writes the specified value to a device register. The value to be
00385 *        written is also stored in the software register list of the device.
00386 *
00387 * @param device - The handler of the instance of the driver.
00388 * @param reg - Which register to write to.
00389 * @param value - The value to be written to the reigster of the device.
00390 *
00391 * @return Returns 0 for success or negative error code.
00392 *******************************************************************************/
00393 int32_t AD7124::WriteDeviceRegister(enum ad7124_registers reg, uint32_t value)
00394 {
00395     regs[reg].value = value;
00396     return(WriteRegister(regs[reg]));
00397 }
00398 
00399 /***************************************************************************//**
00400 * @brief Resets the device.
00401 *
00402 * @param device - The handler of the instance of the driver.
00403 *
00404 * @return Returns 0 for success or negative error code.
00405 *******************************************************************************/
00406 int32_t AD7124::Reset()
00407 {
00408     int32_t ret = 0;
00409     uint8_t wrBuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
00410 
00411     ret = SPI_Write( wrBuf, 8);
00412 
00413 
00414     return ret;
00415 }
00416 
00417 /***************************************************************************//**
00418 * @brief Waits until the device can accept read and write user actions.
00419 *
00420 * @param device - The handler of the instance of the driver.
00421 * @param timeout - Count representing the number of polls to be done until the
00422 *                  function returns.
00423 *
00424 * @return Returns 0 for success or negative error code.
00425 *******************************************************************************/
00426 int32_t AD7124::WaitForSpiReady (uint32_t timeout)
00427 {
00428     ad7124_st_reg  *regs;
00429     int32_t ret;
00430     int8_t ready = 0;
00431 
00432     regs = this->regs;
00433 
00434     while(!ready && --timeout) {
00435         /* Read the value of the Error Register */
00436         ret = ReadRegister(&regs[AD7124_Error]);
00437         if(ret < 0)
00438             return ret;
00439 
00440         /* Check the SPI IGNORE Error bit in the Error Register */
00441         ready = (regs[AD7124_Error].value &
00442                  AD7124_ERR_REG_SPI_IGNORE_ERR) == 0;
00443     }
00444 
00445     return timeout ? 0 : TIMEOUT;
00446 }
00447 
00448 /***************************************************************************//**
00449 * @brief Waits until a new conversion result is available.
00450 *
00451 * @param device - The handler of the instance of the driver.
00452 * @param timeout - Count representing the number of polls to be done until the
00453 *                  function returns if no new data is available.
00454 *
00455 * @return Returns 0 for success or negative error code.
00456 *******************************************************************************/
00457 int32_t AD7124::WaitForConvReady (uint32_t timeout)
00458 {
00459     ad7124_st_reg  *regs;
00460     int32_t ret;
00461     int8_t ready = 0;
00462 
00463     regs = this->regs;
00464 
00465     while(!ready && --timeout) {
00466         /* Read the value of the Status Register */
00467         ret = ReadRegister(&regs[AD7124_Status]);
00468         if(ret < 0)
00469             return ret;
00470 
00471         /* Check the RDY bit in the Status Register */
00472         ready = (regs[AD7124_Status].value &
00473                  AD7124_STATUS_REG_RDY) == 0;
00474         wait_ms(1);
00475     }
00476 
00477     return timeout ? 0 : TIMEOUT;
00478 }
00479 
00480 bool AD7124::get_miso()
00481 {
00482     return miso.read();
00483 }
00484 
00485 /***************************************************************************//**
00486 * @brief Reads the conversion result from the device.
00487 *
00488 * @param device - The handler of the instance of the driver.
00489 * @param pData - Pointer to store the read data.
00490 *
00491 * @return Returns 0 for success or negative error code.
00492 *******************************************************************************/
00493 int32_t AD7124::ReadData ( int32_t* pData)
00494 {
00495     int32_t ret       = 0;
00496     uint8_t check8    = 0;
00497     uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
00498     uint8_t i         = 0;
00499     uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
00500     ad7124_st_reg  *pReg;
00501 
00502     if( !pData)
00503         return INVALID_VAL;
00504 
00505     pReg = &regs[AD7124_Data];
00506 
00507     /* Build the Command word */
00508     buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
00509                 AD7124_COMM_REG_RA(pReg->addr);
00510 
00511 
00512     /* Read data from the device */
00513     ret = SPI_Read(buffer,
00514                    ((useCRC != AD7124_DISABLE_CRC) ? pReg->size + 1
00515                     : pReg->size) + 2);
00516 
00517     if(ret < 0)
00518         return ret;
00519 
00520     /* Check the CRC */
00521     if(check8 == AD7124_USE_CRC) {
00522         msgBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
00523                     AD7124_COMM_REG_RA(pReg->addr);
00524         for(i = 1; i < pReg->size + 2; ++i) {
00525             msgBuf[i] = buffer[i];
00526         }
00527         check8 = ComputeCRC8 (msgBuf, pReg->size + 3);
00528     }
00529 
00530     if(check8 != 0) {
00531         /* ReadRegister checksum failed. */
00532         return COMM_ERR;
00533     }
00534 
00535     /* Build the result */
00536     *pData = 0;
00537     for(i = 1; i < pReg->size + 2; i++) {
00538         *pData <<= 8;
00539         *pData += buffer[i];
00540     }
00541     wait_ms(500);
00542     return ret;
00543 }
00544 
00545 /***************************************************************************//**
00546 * @brief Computes the CRC checksum for a data buffer.
00547 *
00548 * @param pBuf - Data buffer
00549 * @param bufSize - Data buffer size in bytes
00550 *
00551 * @return Returns the computed CRC checksum.
00552 *******************************************************************************/
00553 uint8_t AD7124::ComputeCRC8 (uint8_t * pBuf, uint8_t bufSize)
00554 {
00555     uint8_t i   = 0;
00556     uint8_t crc = 0;
00557 
00558     while(bufSize) {
00559         for(i = 0x80; i != 0; i >>= 1) {
00560             if(((crc & 0x80) != 0) != ((*pBuf & i) != 0)) { /* MSB of CRC register XOR input Bit from Data */
00561                 crc <<= 1;
00562                 crc ^= AD7124_CRC8_POLYNOMIAL_REPRESENTATION;
00563             } else {
00564                 crc <<= 1;
00565             }
00566         }
00567         pBuf++;
00568         bufSize--;
00569     }
00570     return crc;
00571 }
00572 
00573 
00574 /***************************************************************************//**
00575 * @brief Updates the device SPI interface settings.
00576 *
00577 * @param device - The handler of the instance of the driver.
00578 *
00579 * @return None.
00580 *******************************************************************************/
00581 void AD7124::UpdateDevSpiSettings ()
00582 {
00583     ad7124_st_reg  *regs;
00584 
00585     regs = this->regs;
00586 
00587     if (regs[AD7124_Error_En].value & AD7124_ERREN_REG_SPI_IGNORE_ERR_EN) {
00588         check_ready = 1;
00589     } else {
00590         check_ready = 0;
00591     }
00592 }
00593 
00594 /***************************************************************************//**
00595 * @brief Initializes the AD7124.
00596 *
00597 * @param device - The handler of the instance of the driver.
00598 * @param slave_select - The Slave Chip Select Id to be passed to the SPI calls.
00599 * @param regs - The list of registers of the device (initialized or not) to be
00600 *               added to the instance of the driver.
00601 *
00602 * @return Returns 0 for success or negative error code.
00603 *******************************************************************************/
00604 int32_t AD7124::Setup ()
00605 {
00606     int32_t ret;
00607     uint8_t regNr;
00608 
00609 
00610     spi_rdy_poll_cnt = 25000;
00611 
00612     /* Initialize the SPI communication. */
00613     /*ret = SPI_Init(0, 2500000, 1, 0);
00614     if (ret < 0)
00615         return ret;*/
00616 
00617     /*  Reset the device interface.*/
00618     ret = Reset();
00619     if (ret < 0)
00620         return ret;
00621 
00622     check_ready = 1;
00623 
00624     /* Initialize registers AD7124_ADC_Control through AD7124_Filter_7. */
00625     for(regNr = static_cast<uint8_t>(AD7124_Status); (regNr < static_cast<uint8_t>(AD7124_Offset_0)) && !(ret < 0);
00626             regNr++) {
00627         if (regs[regNr].rw == AD7124_RW) {
00628             ret = WriteRegister(regs[regNr]);
00629             if (ret < 0)
00630                 break;
00631         }
00632 
00633         /* Get CRC State and device SPI interface settings */
00634         if (regNr == AD7124_Error_En) {
00635             UpdateDevSpiSettings ();
00636         }
00637     }
00638 
00639     return ret;
00640 }
00641 
00642 uint8_t AD7124::SPI_Read(uint8_t *data, uint8_t bytes_number)
00643 {
00644     cs = false;
00645     for(uint8_t byte = 0; byte < bytes_number; byte++) {
00646         data[byte] = ad7124.write(data[byte]);
00647     }
00648     cs = true;
00649     return bytes_number;
00650 }
00651 
00652 uint8_t AD7124::SPI_Write(uint8_t *data, uint8_t bytes_number)
00653 {
00654     cs = false;
00655     for(uint8_t byte = 0; byte < bytes_number; byte++) {
00656         ad7124.write(data[byte]);
00657     }
00658 
00659     cs = true;
00660     return bytes_number;
00661 
00662 }