AD7124 no-OS library files

Committer:
MitchAD
Date:
Thu Sep 05 20:45:41 2019 +0000
Revision:
1:22ed45e195a9
Initial Commit for the AD7124 no-OS drivers

Who changed what in which revision?

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