| 1 | /***************************************************************************//** |
| 2 | * @file AD7124.h |
| 3 | * @brief AD7124 header 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 | #ifndef __AD7124_H__ |
| 42 | #define __AD7124_H__ |
| 43 | |
| 44 | /******************************************************************************/ |
| 45 | /***************************** Include Files **********************************/ |
| 46 | /******************************************************************************/ |
| 47 | #include <stdint.h> |
| 48 | #include "AD7124_regs.h" |
| 49 | |
| 50 | typedef struct ad7124_device ad7124_device; |
| 51 | |
| 52 | /* |
| 53 | * The structure describes the device and is used with the ad7124 driver. |
| 54 | * @slave_select_id: The ID of the Slave Select to be passed to the SPI calls. |
| 55 | * @regs: A reference to the register list of the device that the user must |
| 56 | * provide when calling the Setup() function. |
| 57 | * @userCRC: Whether to do or not a cyclic redundancy check on SPI transfers. |
| 58 | * @check_ready: When enabled all register read and write calls will first wait |
| 59 | * until the device is ready to accept user requests. |
| 60 | * @spi_rdy_poll_cnt: Number of times the driver should read the Error register |
| 61 | * to check if the device is ready to accept user requests, |
| 62 | * before a timeout error will be issued. |
| 63 | */ |
| 64 | struct ad7124_device { |
| 65 | int slave_select_id; |
| 66 | ad7124_st_reg *regs; |
| 67 | int useCRC; |
| 68 | int check_ready; |
| 69 | int spi_rdy_poll_cnt; |
| 70 | }; |
| 71 | |
| 72 | /******************************************************************************/ |
| 73 | /******************* AD7124 Constants *****************************************/ |
| 74 | /******************************************************************************/ |
| 75 | #define AD7124_CRC8_POLYNOMIAL_REPRESENTATION 0x07 /* x8 + x2 + x + 1 */ |
| 76 | #define AD7124_DISABLE_CRC 0 |
| 77 | #define AD7124_USE_CRC 1 |
| 78 | |
| 79 | /******************************************************************************/ |
| 80 | /************************ Functions Declarations ******************************/ |
| 81 | /******************************************************************************/ |
| 82 | /*! Reads the value of the specified register. */ |
| 83 | int32_t AD7124_ReadRegister(ad7124_device *device, ad7124_st_reg* pReg); |
| 84 | |
| 85 | /*! Writes the value of the specified register. */ |
| 86 | int32_t AD7124_WriteRegister(ad7124_device *device, ad7124_st_reg reg); |
| 87 | |
| 88 | /*! Reads the value of the specified register without a device state check. */ |
| 89 | int32_t AD7124_NoCheckReadRegister(ad7124_device *device, ad7124_st_reg* pReg); |
| 90 | |
| 91 | /*! Writes the value of the specified register without a device state check. */ |
| 92 | int32_t AD7124_NoCheckWriteRegister(ad7124_device *device, ad7124_st_reg reg); |
| 93 | |
| 94 | /*! Resets the device. */ |
| 95 | int32_t AD7124_Reset(ad7124_device *device); |
| 96 | |
| 97 | /*! Waits until the device can accept read and write user actions. */ |
| 98 | int32_t AD7124_WaitForSpiReady(ad7124_device *device, uint32_t timeout); |
| 99 | |
| 100 | /*! Waits until a new conversion result is available. */ |
| 101 | int32_t AD7124_WaitForConvReady(ad7124_device *device, uint32_t timeout); |
| 102 | |
| 103 | /*! Reads the conversion result from the device. */ |
| 104 | int32_t AD7124_ReadData(ad7124_device *device, int32_t* pData); |
| 105 | |
| 106 | /*! Computes the CRC checksum for a data buffer. */ |
| 107 | uint8_t AD7124_ComputeCRC8(uint8_t* pBuf, uint8_t bufSize); |
| 108 | |
| 109 | /*! Updates the CRC settings. */ |
| 110 | void AD7124_UpdateCRCSetting(ad7124_device *device); |
| 111 | |
| 112 | /*! Updates the device SPI interface settings. */ |
| 113 | void AD7124_UpdateDevSpiSettings(ad7124_device *device); |
| 114 | |
| 115 | /*! Initializes the AD7124. */ |
| 116 | int32_t AD7124_Setup(ad7124_device *device, int slave_select, |
| 117 | ad7124_st_reg *regs); |
| 118 | |
| 119 | #endif /* __AD7124_H__ */ |