this is testing

Committer:
pmallick
Date:
Thu Jan 14 19:12:57 2021 +0530
Revision:
0:e8a1ba50c46b
this is testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 0:e8a1ba50c46b 1 /***************************************************************************//**
pmallick 0:e8a1ba50c46b 2 * @file ad5593r.c
pmallick 0:e8a1ba50c46b 3 * @brief Implementation of AD5593R driver.
pmallick 0:e8a1ba50c46b 4 * @author Mircea Caprioru (mircea.caprioru@analog.com)
pmallick 0:e8a1ba50c46b 5 ********************************************************************************
pmallick 0:e8a1ba50c46b 6 * Copyright 2018, 2020(c) Analog Devices, Inc.
pmallick 0:e8a1ba50c46b 7 *
pmallick 0:e8a1ba50c46b 8 * All rights reserved.
pmallick 0:e8a1ba50c46b 9 *
pmallick 0:e8a1ba50c46b 10 * Redistribution and use in source and binary forms, with or without
pmallick 0:e8a1ba50c46b 11 * modification, are permitted provided that the following conditions are met:
pmallick 0:e8a1ba50c46b 12 * - Redistributions of source code must retain the above copyright
pmallick 0:e8a1ba50c46b 13 * notice, this list of conditions and the following disclaimer.
pmallick 0:e8a1ba50c46b 14 * - Redistributions in binary form must reproduce the above copyright
pmallick 0:e8a1ba50c46b 15 * notice, this list of conditions and the following disclaimer in
pmallick 0:e8a1ba50c46b 16 * the documentation and/or other materials provided with the
pmallick 0:e8a1ba50c46b 17 * distribution.
pmallick 0:e8a1ba50c46b 18 * - Neither the name of Analog Devices, Inc. nor the names of its
pmallick 0:e8a1ba50c46b 19 * contributors may be used to endorse or promote products derived
pmallick 0:e8a1ba50c46b 20 * from this software without specific prior written permission.
pmallick 0:e8a1ba50c46b 21 * - The use of this software may or may not infringe the patent rights
pmallick 0:e8a1ba50c46b 22 * of one or more patent holders. This license does not release you
pmallick 0:e8a1ba50c46b 23 * from the requirement that you obtain separate licenses from these
pmallick 0:e8a1ba50c46b 24 * patent holders to use this software.
pmallick 0:e8a1ba50c46b 25 * - Use of the software either in source or binary form, must be run
pmallick 0:e8a1ba50c46b 26 * on or directly connected to an Analog Devices Inc. component.
pmallick 0:e8a1ba50c46b 27 *
pmallick 0:e8a1ba50c46b 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
pmallick 0:e8a1ba50c46b 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
pmallick 0:e8a1ba50c46b 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
pmallick 0:e8a1ba50c46b 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
pmallick 0:e8a1ba50c46b 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
pmallick 0:e8a1ba50c46b 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
pmallick 0:e8a1ba50c46b 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
pmallick 0:e8a1ba50c46b 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
pmallick 0:e8a1ba50c46b 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
pmallick 0:e8a1ba50c46b 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pmallick 0:e8a1ba50c46b 38 *******************************************************************************/
pmallick 0:e8a1ba50c46b 39
pmallick 0:e8a1ba50c46b 40 #include "ad5592r-base.h"
pmallick 0:e8a1ba50c46b 41 #include "error.h"
pmallick 0:e8a1ba50c46b 42 #include "ad5593r.h"
pmallick 0:e8a1ba50c46b 43
pmallick 0:e8a1ba50c46b 44 #define AD5593R_MODE_CONF (0 << 4)
pmallick 0:e8a1ba50c46b 45 #define AD5593R_MODE_DAC_WRITE (1 << 4)
pmallick 0:e8a1ba50c46b 46 #define AD5593R_MODE_ADC_READBACK (4 << 4)
pmallick 0:e8a1ba50c46b 47 #define AD5593R_MODE_DAC_READBACK (5 << 4)
pmallick 0:e8a1ba50c46b 48 #define AD5593R_MODE_GPIO_READBACK (6 << 4)
pmallick 0:e8a1ba50c46b 49 #define AD5593R_MODE_REG_READBACK (7 << 4)
pmallick 0:e8a1ba50c46b 50
pmallick 0:e8a1ba50c46b 51 #define STOP_BIT 1
pmallick 0:e8a1ba50c46b 52 #define RESTART_BIT 0
pmallick 0:e8a1ba50c46b 53 #define AD5593R_ADC_VALUES_BUFF_SIZE 18
pmallick 0:e8a1ba50c46b 54
pmallick 0:e8a1ba50c46b 55 const struct ad5592r_rw_ops ad5593r_rw_ops = {
pmallick 0:e8a1ba50c46b 56 .write_dac = ad5593r_write_dac,
pmallick 0:e8a1ba50c46b 57 .read_adc = ad5593r_read_adc,
pmallick 0:e8a1ba50c46b 58 .multi_read_adc= ad5593r_multi_read_adc,
pmallick 0:e8a1ba50c46b 59 .reg_write = ad5593r_reg_write,
pmallick 0:e8a1ba50c46b 60 .reg_read = ad5593r_reg_read,
pmallick 0:e8a1ba50c46b 61 .gpio_read = ad5593r_gpio_read,
pmallick 0:e8a1ba50c46b 62 };
pmallick 0:e8a1ba50c46b 63
pmallick 0:e8a1ba50c46b 64 /**
pmallick 0:e8a1ba50c46b 65 * Write DAC channel.
pmallick 0:e8a1ba50c46b 66 *
pmallick 0:e8a1ba50c46b 67 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 68 * @param chan - The channel number.
pmallick 0:e8a1ba50c46b 69 * @param value - DAC value
pmallick 0:e8a1ba50c46b 70 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 71 */
pmallick 0:e8a1ba50c46b 72 int32_t ad5593r_write_dac(struct ad5592r_dev *dev, uint8_t chan,
pmallick 0:e8a1ba50c46b 73 uint16_t value)
pmallick 0:e8a1ba50c46b 74 {
pmallick 0:e8a1ba50c46b 75 uint8_t data[3];
pmallick 0:e8a1ba50c46b 76
pmallick 0:e8a1ba50c46b 77 if (!dev)
pmallick 0:e8a1ba50c46b 78 return FAILURE;
pmallick 0:e8a1ba50c46b 79
pmallick 0:e8a1ba50c46b 80 data[0] = AD5593R_MODE_DAC_WRITE | chan;
pmallick 0:e8a1ba50c46b 81 data[1] = (value >> 8) & 0xF ;
pmallick 0:e8a1ba50c46b 82 data[2] = value & 0xFF;
pmallick 0:e8a1ba50c46b 83
pmallick 0:e8a1ba50c46b 84 return i2c_write(dev->i2c, data, sizeof(data), STOP_BIT);
pmallick 0:e8a1ba50c46b 85 }
pmallick 0:e8a1ba50c46b 86
pmallick 0:e8a1ba50c46b 87 /**
pmallick 0:e8a1ba50c46b 88 * Read ADC channel.
pmallick 0:e8a1ba50c46b 89 *
pmallick 0:e8a1ba50c46b 90 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 91 * @param chan - The channel number.
pmallick 0:e8a1ba50c46b 92 * @param value - ADC value
pmallick 0:e8a1ba50c46b 93 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 94 */
pmallick 0:e8a1ba50c46b 95 int32_t ad5593r_read_adc(struct ad5592r_dev *dev, uint8_t chan,
pmallick 0:e8a1ba50c46b 96 uint16_t *value)
pmallick 0:e8a1ba50c46b 97 {
pmallick 0:e8a1ba50c46b 98 int32_t ret;
pmallick 0:e8a1ba50c46b 99 uint8_t data[3];
pmallick 0:e8a1ba50c46b 100 uint16_t temp;
pmallick 0:e8a1ba50c46b 101
pmallick 0:e8a1ba50c46b 102 if (!dev)
pmallick 0:e8a1ba50c46b 103 return FAILURE;
pmallick 0:e8a1ba50c46b 104
pmallick 0:e8a1ba50c46b 105 temp = BIT(chan);
pmallick 0:e8a1ba50c46b 106
pmallick 0:e8a1ba50c46b 107 data[0] = AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ;
pmallick 0:e8a1ba50c46b 108 data[1] = temp >> 8;
pmallick 0:e8a1ba50c46b 109 data[2] = temp & 0xFF;
pmallick 0:e8a1ba50c46b 110
pmallick 0:e8a1ba50c46b 111 ret = i2c_write(dev->i2c, data, sizeof(data), STOP_BIT);
pmallick 0:e8a1ba50c46b 112 if (ret < 0)
pmallick 0:e8a1ba50c46b 113 return ret;
pmallick 0:e8a1ba50c46b 114
pmallick 0:e8a1ba50c46b 115 data[0] = AD5593R_MODE_ADC_READBACK;
pmallick 0:e8a1ba50c46b 116 ret = i2c_write(dev->i2c, data, 1, STOP_BIT);
pmallick 0:e8a1ba50c46b 117 if (ret < 0)
pmallick 0:e8a1ba50c46b 118 return ret;
pmallick 0:e8a1ba50c46b 119
pmallick 0:e8a1ba50c46b 120 ret = i2c_read(dev->i2c, data, 2, STOP_BIT);
pmallick 0:e8a1ba50c46b 121 if (ret < 0)
pmallick 0:e8a1ba50c46b 122 return ret;
pmallick 0:e8a1ba50c46b 123
pmallick 0:e8a1ba50c46b 124 *value = (uint16_t)((data[0] & 0x0F) << 8) + data[1];
pmallick 0:e8a1ba50c46b 125
pmallick 0:e8a1ba50c46b 126 return 0;
pmallick 0:e8a1ba50c46b 127 }
pmallick 0:e8a1ba50c46b 128
pmallick 0:e8a1ba50c46b 129 /**
pmallick 0:e8a1ba50c46b 130 * Read Multiple ADC Channels.
pmallick 0:e8a1ba50c46b 131 *
pmallick 0:e8a1ba50c46b 132 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 133 * @param chans - The ADC channels to be readback
pmallick 0:e8a1ba50c46b 134 * @param values - ADC value array
pmallick 0:e8a1ba50c46b 135 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 136 */
pmallick 0:e8a1ba50c46b 137 int32_t ad5593r_multi_read_adc(struct ad5592r_dev *dev, uint16_t chans,
pmallick 0:e8a1ba50c46b 138 uint16_t *values)
pmallick 0:e8a1ba50c46b 139 {
pmallick 0:e8a1ba50c46b 140 uint8_t data[AD5593R_ADC_VALUES_BUFF_SIZE], i;
pmallick 0:e8a1ba50c46b 141 int32_t ret;
pmallick 0:e8a1ba50c46b 142 uint8_t samples;
pmallick 0:e8a1ba50c46b 143
pmallick 0:e8a1ba50c46b 144 if (!dev)
pmallick 0:e8a1ba50c46b 145 return FAILURE;
pmallick 0:e8a1ba50c46b 146
pmallick 0:e8a1ba50c46b 147 samples = hweight8(chans);
pmallick 0:e8a1ba50c46b 148
pmallick 0:e8a1ba50c46b 149 data[0] = AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ;
pmallick 0:e8a1ba50c46b 150 data[1] = chans >> 8;
pmallick 0:e8a1ba50c46b 151 data[2] = chans & 0xFF;
pmallick 0:e8a1ba50c46b 152
pmallick 0:e8a1ba50c46b 153 ret = i2c_write(dev->i2c, data, 3, STOP_BIT);
pmallick 0:e8a1ba50c46b 154 if (ret < 0)
pmallick 0:e8a1ba50c46b 155 return ret;
pmallick 0:e8a1ba50c46b 156
pmallick 0:e8a1ba50c46b 157 data[0] = AD5593R_MODE_ADC_READBACK;
pmallick 0:e8a1ba50c46b 158 ret = i2c_write(dev->i2c, data, 1, RESTART_BIT);
pmallick 0:e8a1ba50c46b 159 if (ret < 0)
pmallick 0:e8a1ba50c46b 160 return ret;
pmallick 0:e8a1ba50c46b 161
pmallick 0:e8a1ba50c46b 162 ret = i2c_read(dev->i2c, data, (2 * samples), STOP_BIT);
pmallick 0:e8a1ba50c46b 163 if (ret < 0)
pmallick 0:e8a1ba50c46b 164 return ret;
pmallick 0:e8a1ba50c46b 165
pmallick 0:e8a1ba50c46b 166 for (i = 0; i < samples; i++) {
pmallick 0:e8a1ba50c46b 167 values[i] = ((uint16_t)(((data[2 * i] & 0xFF) << 8) + data[(2 * i) + 1]));
pmallick 0:e8a1ba50c46b 168 }
pmallick 0:e8a1ba50c46b 169
pmallick 0:e8a1ba50c46b 170 return 0;
pmallick 0:e8a1ba50c46b 171 }
pmallick 0:e8a1ba50c46b 172
pmallick 0:e8a1ba50c46b 173 /**
pmallick 0:e8a1ba50c46b 174 * Write register.
pmallick 0:e8a1ba50c46b 175 *
pmallick 0:e8a1ba50c46b 176 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 177 * @param reg - The register address.
pmallick 0:e8a1ba50c46b 178 * @param value - register value
pmallick 0:e8a1ba50c46b 179 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 180 */
pmallick 0:e8a1ba50c46b 181 int32_t ad5593r_reg_write(struct ad5592r_dev *dev, uint8_t reg,
pmallick 0:e8a1ba50c46b 182 uint16_t value)
pmallick 0:e8a1ba50c46b 183 {
pmallick 0:e8a1ba50c46b 184 int32_t ret;
pmallick 0:e8a1ba50c46b 185 uint8_t data[3];
pmallick 0:e8a1ba50c46b 186
pmallick 0:e8a1ba50c46b 187 if (!dev)
pmallick 0:e8a1ba50c46b 188 return FAILURE;
pmallick 0:e8a1ba50c46b 189
pmallick 0:e8a1ba50c46b 190 data[0] = AD5593R_MODE_CONF | reg;
pmallick 0:e8a1ba50c46b 191 data[1] = value >> 8;
pmallick 0:e8a1ba50c46b 192 data[2] = value;
pmallick 0:e8a1ba50c46b 193
pmallick 0:e8a1ba50c46b 194 ret = i2c_write(dev->i2c, data,sizeof(data), STOP_BIT);
pmallick 0:e8a1ba50c46b 195
pmallick 0:e8a1ba50c46b 196 if (reg == AD5592R_REG_RESET && ret < 0) {
pmallick 0:e8a1ba50c46b 197 return SUCCESS;
pmallick 0:e8a1ba50c46b 198 }
pmallick 0:e8a1ba50c46b 199
pmallick 0:e8a1ba50c46b 200 return ret;
pmallick 0:e8a1ba50c46b 201 }
pmallick 0:e8a1ba50c46b 202
pmallick 0:e8a1ba50c46b 203 /**
pmallick 0:e8a1ba50c46b 204 * Read register.
pmallick 0:e8a1ba50c46b 205 *
pmallick 0:e8a1ba50c46b 206 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 207 * @param reg - The register address.
pmallick 0:e8a1ba50c46b 208 * @param value - register value
pmallick 0:e8a1ba50c46b 209 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 210 */
pmallick 0:e8a1ba50c46b 211 int32_t ad5593r_reg_read(struct ad5592r_dev *dev, uint8_t reg,
pmallick 0:e8a1ba50c46b 212 uint16_t *value)
pmallick 0:e8a1ba50c46b 213 {
pmallick 0:e8a1ba50c46b 214 int32_t ret;
pmallick 0:e8a1ba50c46b 215 uint8_t data[2];
pmallick 0:e8a1ba50c46b 216
pmallick 0:e8a1ba50c46b 217 if (!dev)
pmallick 0:e8a1ba50c46b 218 return FAILURE;
pmallick 0:e8a1ba50c46b 219
pmallick 0:e8a1ba50c46b 220 data[0] = AD5593R_MODE_REG_READBACK | reg;
pmallick 0:e8a1ba50c46b 221
pmallick 0:e8a1ba50c46b 222 ret = i2c_write(dev->i2c, data, 1, STOP_BIT);
pmallick 0:e8a1ba50c46b 223 if (ret < 0)
pmallick 0:e8a1ba50c46b 224 return ret;
pmallick 0:e8a1ba50c46b 225
pmallick 0:e8a1ba50c46b 226 ret = i2c_read(dev->i2c, data, sizeof(data), STOP_BIT);
pmallick 0:e8a1ba50c46b 227 if (ret < 0)
pmallick 0:e8a1ba50c46b 228 return ret;
pmallick 0:e8a1ba50c46b 229
pmallick 0:e8a1ba50c46b 230 *value = (uint16_t) (data[0] << 8) + data[1];
pmallick 0:e8a1ba50c46b 231
pmallick 0:e8a1ba50c46b 232 return 0;
pmallick 0:e8a1ba50c46b 233 }
pmallick 0:e8a1ba50c46b 234
pmallick 0:e8a1ba50c46b 235 /**
pmallick 0:e8a1ba50c46b 236 * Read GPIOs.
pmallick 0:e8a1ba50c46b 237 *
pmallick 0:e8a1ba50c46b 238 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 239 * @param value - GPIOs value.
pmallick 0:e8a1ba50c46b 240 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 241 */
pmallick 0:e8a1ba50c46b 242 int32_t ad5593r_gpio_read(struct ad5592r_dev *dev, uint8_t *value)
pmallick 0:e8a1ba50c46b 243 {
pmallick 0:e8a1ba50c46b 244 int32_t ret;
pmallick 0:e8a1ba50c46b 245 uint8_t data[2];
pmallick 0:e8a1ba50c46b 246
pmallick 0:e8a1ba50c46b 247 if (!dev)
pmallick 0:e8a1ba50c46b 248 return FAILURE;
pmallick 0:e8a1ba50c46b 249
pmallick 0:e8a1ba50c46b 250 data[0] = AD5593R_MODE_GPIO_READBACK;
pmallick 0:e8a1ba50c46b 251 ret = i2c_write(dev->i2c, data, 1, STOP_BIT);
pmallick 0:e8a1ba50c46b 252 if (ret < 0)
pmallick 0:e8a1ba50c46b 253 return ret;
pmallick 0:e8a1ba50c46b 254
pmallick 0:e8a1ba50c46b 255 ret = i2c_read(dev->i2c, data, sizeof(data), STOP_BIT);
pmallick 0:e8a1ba50c46b 256 if (ret < 0)
pmallick 0:e8a1ba50c46b 257 return ret;
pmallick 0:e8a1ba50c46b 258
pmallick 0:e8a1ba50c46b 259 *value = data[1];
pmallick 0:e8a1ba50c46b 260
pmallick 0:e8a1ba50c46b 261 return 0;
pmallick 0:e8a1ba50c46b 262 }
pmallick 0:e8a1ba50c46b 263
pmallick 0:e8a1ba50c46b 264 /**
pmallick 0:e8a1ba50c46b 265 * Initialize AD5593r device.
pmallick 0:e8a1ba50c46b 266 *
pmallick 0:e8a1ba50c46b 267 * @param dev - The device structure.
pmallick 0:e8a1ba50c46b 268 * @param init_param - The initial parameters of the device.
pmallick 0:e8a1ba50c46b 269 * @return 0 in case of success, negative error code otherwise
pmallick 0:e8a1ba50c46b 270 */
pmallick 0:e8a1ba50c46b 271 int32_t ad5593r_init(struct ad5592r_dev *dev,
pmallick 0:e8a1ba50c46b 272 struct ad5592r_init_param *init_param)
pmallick 0:e8a1ba50c46b 273 {
pmallick 0:e8a1ba50c46b 274 int32_t ret;
pmallick 0:e8a1ba50c46b 275 uint16_t temp_reg_val;
pmallick 0:e8a1ba50c46b 276
pmallick 0:e8a1ba50c46b 277 if (!dev)
pmallick 0:e8a1ba50c46b 278 return FAILURE;
pmallick 0:e8a1ba50c46b 279
pmallick 0:e8a1ba50c46b 280 dev->ops = &ad5593r_rw_ops;
pmallick 0:e8a1ba50c46b 281
pmallick 0:e8a1ba50c46b 282 ret = ad5592r_software_reset(dev);
pmallick 0:e8a1ba50c46b 283 if (ret < 0)
pmallick 0:e8a1ba50c46b 284 return ret;
pmallick 0:e8a1ba50c46b 285
pmallick 0:e8a1ba50c46b 286 ret = ad5592r_set_channel_modes(dev);
pmallick 0:e8a1ba50c46b 287 if (ret < 0)
pmallick 0:e8a1ba50c46b 288 return ret;
pmallick 0:e8a1ba50c46b 289
pmallick 0:e8a1ba50c46b 290 if(init_param->int_ref) {
pmallick 0:e8a1ba50c46b 291 ret = ad5593r_reg_read(dev, AD5592R_REG_PD, &temp_reg_val);
pmallick 0:e8a1ba50c46b 292 if (ret < 0)
pmallick 0:e8a1ba50c46b 293 return ret;
pmallick 0:e8a1ba50c46b 294 temp_reg_val |= AD5592R_REG_PD_EN_REF;
pmallick 0:e8a1ba50c46b 295
pmallick 0:e8a1ba50c46b 296 return ad5593r_reg_write(dev, AD5592R_REG_PD, temp_reg_val);
pmallick 0:e8a1ba50c46b 297 }
pmallick 0:e8a1ba50c46b 298
pmallick 0:e8a1ba50c46b 299 return ret;
pmallick 0:e8a1ba50c46b 300 }