Library files for AD5770R

Committer:
mbradley
Date:
Fri Mar 27 19:05:55 2020 +0000
Revision:
3:e2c56c903ec8
Parent:
2:a22a3d5d9f41
removing license file, as the noos already have a license in the header.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbradley 1:bcc701b1fd33 1 /***************************************************************************//**
mbradley 1:bcc701b1fd33 2 * @file ad5770r.c
mbradley 1:bcc701b1fd33 3 * @brief Implementation of ad5770r Driver.
mbradley 1:bcc701b1fd33 4 * @author Mircea Caprioru (mircea.caprioru@analog.com)
mbradley 1:bcc701b1fd33 5 ********************************************************************************
mbradley 1:bcc701b1fd33 6 * Copyright 2018, 2020(c) Analog Devices, Inc.
mbradley 1:bcc701b1fd33 7 *
mbradley 1:bcc701b1fd33 8 * All rights reserved.
mbradley 1:bcc701b1fd33 9 *
mbradley 1:bcc701b1fd33 10 * Redistribution and use in source and binary forms, with or without
mbradley 1:bcc701b1fd33 11 * modification, are permitted provided that the following conditions are met:
mbradley 1:bcc701b1fd33 12 * - Redistributions of source code must retain the above copyright
mbradley 1:bcc701b1fd33 13 * notice, this list of conditions and the following disclaimer.
mbradley 1:bcc701b1fd33 14 * - Redistributions in binary form must reproduce the above copyright
mbradley 1:bcc701b1fd33 15 * notice, this list of conditions and the following disclaimer in
mbradley 1:bcc701b1fd33 16 * the documentation and/or other materials provided with the
mbradley 1:bcc701b1fd33 17 * distribution.
mbradley 1:bcc701b1fd33 18 * - Neither the name of Analog Devices, Inc. nor the names of its
mbradley 1:bcc701b1fd33 19 * contributors may be used to endorse or promote products derived
mbradley 1:bcc701b1fd33 20 * from this software without specific prior written permission.
mbradley 1:bcc701b1fd33 21 * - The use of this software may or may not infringe the patent rights
mbradley 1:bcc701b1fd33 22 * of one or more patent holders. This license does not release you
mbradley 1:bcc701b1fd33 23 * from the requirement that you obtain separate licenses from these
mbradley 1:bcc701b1fd33 24 * patent holders to use this software.
mbradley 1:bcc701b1fd33 25 * - Use of the software either in source or binary form, must be run
mbradley 1:bcc701b1fd33 26 * on or directly connected to an Analog Devices Inc. component.
mbradley 1:bcc701b1fd33 27 *
mbradley 1:bcc701b1fd33 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
mbradley 1:bcc701b1fd33 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
mbradley 1:bcc701b1fd33 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
mbradley 1:bcc701b1fd33 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
mbradley 1:bcc701b1fd33 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
mbradley 1:bcc701b1fd33 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
mbradley 1:bcc701b1fd33 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbradley 1:bcc701b1fd33 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbradley 1:bcc701b1fd33 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbradley 1:bcc701b1fd33 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbradley 1:bcc701b1fd33 38 *******************************************************************************/
mbradley 1:bcc701b1fd33 39
mbradley 1:bcc701b1fd33 40 /******************************************************************************/
mbradley 1:bcc701b1fd33 41 /***************************** Include Files **********************************/
mbradley 1:bcc701b1fd33 42 /******************************************************************************/
mbradley 1:bcc701b1fd33 43 #include <stdio.h>
mbradley 1:bcc701b1fd33 44 #include <stdlib.h>
mbradley 1:bcc701b1fd33 45 #include <stdbool.h>
mbradley 1:bcc701b1fd33 46 #include <string.h>
mbradley 1:bcc701b1fd33 47 #include "platform_drivers.h"
mbradley 1:bcc701b1fd33 48 #include "ad5770r.h"
mbradley 1:bcc701b1fd33 49
mbradley 1:bcc701b1fd33 50 /******************************************************************************/
mbradley 1:bcc701b1fd33 51 /************************** Functions Implementation **************************/
mbradley 1:bcc701b1fd33 52 /******************************************************************************/
mbradley 1:bcc701b1fd33 53 /**
mbradley 1:bcc701b1fd33 54 * Read from device.
mbradley 1:bcc701b1fd33 55 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 56 * @param reg_addr - The register address.
mbradley 1:bcc701b1fd33 57 * @param reg_data - The register data.
mbradley 1:bcc701b1fd33 58 * @return 0 in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 59 */
mbradley 1:bcc701b1fd33 60 int32_t ad5770r_spi_reg_read(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 61 uint8_t reg_addr,
mbradley 1:bcc701b1fd33 62 uint8_t *reg_data)
mbradley 1:bcc701b1fd33 63 {
mbradley 1:bcc701b1fd33 64 uint8_t buf[2];
mbradley 1:bcc701b1fd33 65 int32_t ret;
mbradley 1:bcc701b1fd33 66
mbradley 1:bcc701b1fd33 67 if (!dev | !reg_data)
mbradley 1:bcc701b1fd33 68 return FAILURE;
mbradley 1:bcc701b1fd33 69
mbradley 1:bcc701b1fd33 70 buf[0] = AD5770R_REG_READ(reg_addr);
mbradley 1:bcc701b1fd33 71 buf[1] = 0x00;
mbradley 1:bcc701b1fd33 72
mbradley 1:bcc701b1fd33 73 ret = spi_write_and_read(dev->spi_desc, buf, sizeof(buf));
mbradley 1:bcc701b1fd33 74
mbradley 1:bcc701b1fd33 75 if (ret)
mbradley 1:bcc701b1fd33 76 return ret;
mbradley 1:bcc701b1fd33 77
mbradley 1:bcc701b1fd33 78 *reg_data = buf[1];
mbradley 1:bcc701b1fd33 79
mbradley 1:bcc701b1fd33 80 return ret;
mbradley 1:bcc701b1fd33 81 }
mbradley 1:bcc701b1fd33 82
mbradley 1:bcc701b1fd33 83 /**
mbradley 1:bcc701b1fd33 84 * Multibyte read from device. A register read begins with the address
mbradley 1:bcc701b1fd33 85 * and autoincrements for each aditional byte in the transfer.
mbradley 1:bcc701b1fd33 86 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 87 * @param reg_addr - The register address.
mbradley 1:bcc701b1fd33 88 * @param reg_data - The register data.
mbradley 1:bcc701b1fd33 89 * @param count - Number of bytes to read.
mbradley 1:bcc701b1fd33 90 * @return 0 in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 91 */
mbradley 1:bcc701b1fd33 92 int32_t ad5770r_spi_reg_read_multiple(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 93 uint8_t reg_addr,
mbradley 1:bcc701b1fd33 94 uint8_t *reg_data,
mbradley 1:bcc701b1fd33 95 uint16_t count)
mbradley 1:bcc701b1fd33 96 {
mbradley 1:bcc701b1fd33 97 uint8_t *buf;
mbradley 1:bcc701b1fd33 98 int32_t ret;
mbradley 1:bcc701b1fd33 99
mbradley 1:bcc701b1fd33 100 if (!dev || !reg_data)
mbradley 1:bcc701b1fd33 101 return FAILURE;
mbradley 1:bcc701b1fd33 102
mbradley 1:bcc701b1fd33 103 buf = (uint8_t*)calloc(count + 1, sizeof(uint8_t));
mbradley 1:bcc701b1fd33 104
mbradley 1:bcc701b1fd33 105 buf[0] = AD5770R_REG_READ(reg_addr);
mbradley 1:bcc701b1fd33 106
mbradley 1:bcc701b1fd33 107 ret = spi_write_and_read(dev->spi_desc, buf, count + 1);
mbradley 1:bcc701b1fd33 108
mbradley 1:bcc701b1fd33 109 if (ret) {
mbradley 1:bcc701b1fd33 110 free(buf);
mbradley 1:bcc701b1fd33 111 return ret;
mbradley 1:bcc701b1fd33 112 }
mbradley 1:bcc701b1fd33 113
mbradley 1:bcc701b1fd33 114 memcpy(reg_data, buf + 1, count);
mbradley 1:bcc701b1fd33 115 free(buf);
mbradley 1:bcc701b1fd33 116
mbradley 1:bcc701b1fd33 117 return ret;
mbradley 1:bcc701b1fd33 118 }
mbradley 1:bcc701b1fd33 119 /**
mbradley 1:bcc701b1fd33 120 * Write to device.
mbradley 1:bcc701b1fd33 121 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 122 * @param reg_addr - The register address.
mbradley 1:bcc701b1fd33 123 * @param reg_data - The register data.
mbradley 1:bcc701b1fd33 124 * @return 0 in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 125 */
mbradley 1:bcc701b1fd33 126 int32_t ad5770r_spi_reg_write(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 127 uint8_t reg_addr,
mbradley 1:bcc701b1fd33 128 uint8_t reg_data)
mbradley 1:bcc701b1fd33 129 {
mbradley 1:bcc701b1fd33 130 uint8_t buf[2];
mbradley 1:bcc701b1fd33 131
mbradley 1:bcc701b1fd33 132 if (!dev)
mbradley 1:bcc701b1fd33 133 return FAILURE;
mbradley 1:bcc701b1fd33 134
mbradley 1:bcc701b1fd33 135 buf[0] = AD5770R_REG_WRITE(reg_addr);
mbradley 1:bcc701b1fd33 136 buf[1] = reg_data;
mbradley 1:bcc701b1fd33 137
mbradley 1:bcc701b1fd33 138 return spi_write_and_read(dev->spi_desc, buf, sizeof(buf));
mbradley 1:bcc701b1fd33 139 }
mbradley 1:bcc701b1fd33 140
mbradley 1:bcc701b1fd33 141 /**
mbradley 1:bcc701b1fd33 142 * Multibyte write from device. A register write begins with the address
mbradley 1:bcc701b1fd33 143 * and autoincrements for each additional byte in the transfer.
mbradley 1:bcc701b1fd33 144 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 145 * @param reg_addr - The register address.
mbradley 1:bcc701b1fd33 146 * @param reg_data - The register data.
mbradley 1:bcc701b1fd33 147 * @param count - Number of bytes to read.
mbradley 1:bcc701b1fd33 148 * @return 0 in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 149 */
mbradley 1:bcc701b1fd33 150 int32_t ad5770r_spi_reg_write_multiple(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 151 uint8_t reg_addr,
mbradley 1:bcc701b1fd33 152 uint8_t *reg_data,
mbradley 1:bcc701b1fd33 153 uint16_t count)
mbradley 1:bcc701b1fd33 154 {
mbradley 1:bcc701b1fd33 155 int32_t ret;
mbradley 1:bcc701b1fd33 156 uint8_t *data;
mbradley 1:bcc701b1fd33 157
mbradley 1:bcc701b1fd33 158 if (!dev | !reg_data)
mbradley 1:bcc701b1fd33 159 return FAILURE;
mbradley 1:bcc701b1fd33 160
mbradley 1:bcc701b1fd33 161 data = (uint8_t*)calloc(count + 1, sizeof(uint8_t));
mbradley 1:bcc701b1fd33 162
mbradley 1:bcc701b1fd33 163 data[0] = AD5770R_REG_WRITE(reg_addr);
mbradley 1:bcc701b1fd33 164 memcpy(&data[1], reg_data, count);
mbradley 1:bcc701b1fd33 165
mbradley 1:bcc701b1fd33 166 ret = spi_write_and_read(dev->spi_desc, data, count + 1);
mbradley 1:bcc701b1fd33 167 free(data);
mbradley 1:bcc701b1fd33 168
mbradley 1:bcc701b1fd33 169 return ret;
mbradley 1:bcc701b1fd33 170 }
mbradley 1:bcc701b1fd33 171
mbradley 1:bcc701b1fd33 172 /**
mbradley 1:bcc701b1fd33 173 * SPI write to device using a mask.
mbradley 1:bcc701b1fd33 174 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 175 * @param reg_addr - The register address.
mbradley 1:bcc701b1fd33 176 * @param mask - The mask.
mbradley 1:bcc701b1fd33 177 * @param data - The register data.
mbradley 1:bcc701b1fd33 178 * @return 0 in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 179 */
mbradley 1:bcc701b1fd33 180 int32_t ad5770r_spi_write_mask(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 181 uint8_t reg_addr,
mbradley 1:bcc701b1fd33 182 uint32_t mask,
mbradley 1:bcc701b1fd33 183 uint8_t data)
mbradley 1:bcc701b1fd33 184 {
mbradley 1:bcc701b1fd33 185 uint8_t reg_data;
mbradley 1:bcc701b1fd33 186 int32_t ret;
mbradley 1:bcc701b1fd33 187
mbradley 1:bcc701b1fd33 188 if (!dev)
mbradley 1:bcc701b1fd33 189 return FAILURE;
mbradley 1:bcc701b1fd33 190
mbradley 1:bcc701b1fd33 191 ret = ad5770r_spi_reg_read(dev, reg_addr, &reg_data);
mbradley 1:bcc701b1fd33 192 reg_data &= ~mask;
mbradley 1:bcc701b1fd33 193 reg_data |= data;
mbradley 1:bcc701b1fd33 194 ret |= ad5770r_spi_reg_write(dev, reg_addr, reg_data);
mbradley 1:bcc701b1fd33 195
mbradley 1:bcc701b1fd33 196 return ret;
mbradley 1:bcc701b1fd33 197 }
mbradley 1:bcc701b1fd33 198
mbradley 1:bcc701b1fd33 199 /**
mbradley 1:bcc701b1fd33 200 * Set device spi settings.
mbradley 1:bcc701b1fd33 201 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 202 * @param spi_settings - The structure that contains the device spi
mbradley 1:bcc701b1fd33 203 * parameters.
mbradley 1:bcc701b1fd33 204 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 205 */
mbradley 1:bcc701b1fd33 206 int32_t ad5770r_set_device_spi(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 207 const struct ad5770r_device_spi_settings *spi_settings)
mbradley 1:bcc701b1fd33 208 {
mbradley 1:bcc701b1fd33 209 int32_t ret;
mbradley 1:bcc701b1fd33 210
mbradley 1:bcc701b1fd33 211 if (!dev || !spi_settings)
mbradley 1:bcc701b1fd33 212 return FAILURE;
mbradley 1:bcc701b1fd33 213
mbradley 1:bcc701b1fd33 214 ret = ad5770r_spi_write_mask(dev,
mbradley 1:bcc701b1fd33 215 AD5770R_INTERFACE_CONFIG_A,
mbradley 1:bcc701b1fd33 216 AD5770R_INTERFACE_CONFIG_A_ADDR_ASCENSION_MSB_MSK,
mbradley 1:bcc701b1fd33 217 AD5770R_INTERFACE_CONFIG_A_ADDR_ASCENSION_MSB(spi_settings->addr_ascension));
mbradley 1:bcc701b1fd33 218
mbradley 1:bcc701b1fd33 219 ret |= ad5770r_spi_write_mask(dev,
mbradley 1:bcc701b1fd33 220 AD5770R_INTERFACE_CONFIG_B,
mbradley 1:bcc701b1fd33 221 AD5770R_INTERFACE_CONFIG_B_SINGLE_INST_MSK,
mbradley 1:bcc701b1fd33 222 AD5770R_INTERFACE_CONFIG_B_SINGLE_INST(spi_settings->single_instruction));
mbradley 1:bcc701b1fd33 223
mbradley 1:bcc701b1fd33 224 ret |= ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 225 AD5770R_STREAM_MODE,
mbradley 1:bcc701b1fd33 226 spi_settings->stream_mode_length);
mbradley 1:bcc701b1fd33 227
mbradley 1:bcc701b1fd33 228 if (ret)
mbradley 1:bcc701b1fd33 229 return ret;
mbradley 1:bcc701b1fd33 230
mbradley 1:bcc701b1fd33 231 dev->dev_spi_settings.addr_ascension =
mbradley 1:bcc701b1fd33 232 spi_settings->addr_ascension;
mbradley 1:bcc701b1fd33 233 dev->dev_spi_settings.single_instruction =
mbradley 1:bcc701b1fd33 234 spi_settings->single_instruction;
mbradley 1:bcc701b1fd33 235 dev->dev_spi_settings.stream_mode_length =
mbradley 1:bcc701b1fd33 236 spi_settings->stream_mode_length;
mbradley 1:bcc701b1fd33 237
mbradley 1:bcc701b1fd33 238 return SUCCESS;
mbradley 1:bcc701b1fd33 239 }
mbradley 1:bcc701b1fd33 240
mbradley 1:bcc701b1fd33 241 /**
mbradley 1:bcc701b1fd33 242 * Set the channel configuration.
mbradley 1:bcc701b1fd33 243 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 244 * @param channel_config - The structure that contains the channel
mbradley 1:bcc701b1fd33 245 * configuration.
mbradley 1:bcc701b1fd33 246 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 247 */
mbradley 1:bcc701b1fd33 248 int32_t ad5770r_channel_config(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 249 const struct ad5770r_channel_switches *channel_config)
mbradley 1:bcc701b1fd33 250 {
mbradley 1:bcc701b1fd33 251 int32_t ret;
mbradley 1:bcc701b1fd33 252
mbradley 1:bcc701b1fd33 253 if (!dev || !channel_config)
mbradley 1:bcc701b1fd33 254 return FAILURE;
mbradley 1:bcc701b1fd33 255
mbradley 1:bcc701b1fd33 256 ret = ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 257 AD5770R_CHANNEL_CONFIG,
mbradley 1:bcc701b1fd33 258 AD5770R_CHANNEL_CONFIG_CH0_SHUTDOWN_B(channel_config->en0) |
mbradley 1:bcc701b1fd33 259 AD5770R_CHANNEL_CONFIG_CH1_SHUTDOWN_B(channel_config->en1) |
mbradley 1:bcc701b1fd33 260 AD5770R_CHANNEL_CONFIG_CH2_SHUTDOWN_B(channel_config->en2) |
mbradley 1:bcc701b1fd33 261 AD5770R_CHANNEL_CONFIG_CH3_SHUTDOWN_B(channel_config->en3) |
mbradley 1:bcc701b1fd33 262 AD5770R_CHANNEL_CONFIG_CH4_SHUTDOWN_B(channel_config->en4) |
mbradley 1:bcc701b1fd33 263 AD5770R_CHANNEL_CONFIG_CH5_SHUTDOWN_B(channel_config->en5) |
mbradley 1:bcc701b1fd33 264 AD5770R_CHANNEL_CONFIG_CH0_SINK_EN(channel_config->sink0));
mbradley 1:bcc701b1fd33 265
mbradley 1:bcc701b1fd33 266 if (ret)
mbradley 1:bcc701b1fd33 267 return ret;
mbradley 1:bcc701b1fd33 268
mbradley 1:bcc701b1fd33 269 dev->channel_config = *channel_config;
mbradley 1:bcc701b1fd33 270
mbradley 1:bcc701b1fd33 271 return ret;
mbradley 1:bcc701b1fd33 272 }
mbradley 1:bcc701b1fd33 273
mbradley 1:bcc701b1fd33 274 /**
mbradley 1:bcc701b1fd33 275 * Set the channel configuration.
mbradley 1:bcc701b1fd33 276 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 277 * @param output_mode - The structure that contains the channel output
mbradley 1:bcc701b1fd33 278 * parameters.
mbradley 1:bcc701b1fd33 279 * @param channel - The channel number.
mbradley 1:bcc701b1fd33 280 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 281 */
mbradley 1:bcc701b1fd33 282 int32_t ad5770r_set_output_mode(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 283 const struct ad5770r_output_range *output_mode,
mbradley 1:bcc701b1fd33 284 enum ad5770r_channels channel)
mbradley 1:bcc701b1fd33 285 {
mbradley 1:bcc701b1fd33 286
mbradley 1:bcc701b1fd33 287 if (!dev || !output_mode)
mbradley 1:bcc701b1fd33 288 return FAILURE;
mbradley 1:bcc701b1fd33 289
mbradley 1:bcc701b1fd33 290 return ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 291 AD5770R_OUTPUT_RANGE_CH0 + channel,
mbradley 1:bcc701b1fd33 292 AD5770R_OUTPUT_RANGE_OUTPUT_SCALING(output_mode->output_scale) |
mbradley 1:bcc701b1fd33 293 AD5770R_OUTPUT_RANGE_MODE(output_mode->output_range_mode));
mbradley 1:bcc701b1fd33 294 };
mbradley 1:bcc701b1fd33 295
mbradley 1:bcc701b1fd33 296 /**
mbradley 1:bcc701b1fd33 297 * Set reference configuration.
mbradley 1:bcc701b1fd33 298 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 299 * @param external_reference - Enable external reference.
mbradley 1:bcc701b1fd33 300 * @param reference_selector - The reference to be selected.
mbradley 1:bcc701b1fd33 301 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 302 */
mbradley 1:bcc701b1fd33 303 int32_t ad5770r_set_reference(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 304 bool external_reference,
mbradley 1:bcc701b1fd33 305 enum ad5770r_reference_voltage reference_selector)
mbradley 1:bcc701b1fd33 306 {
mbradley 1:bcc701b1fd33 307 int32_t ret;
mbradley 1:bcc701b1fd33 308
mbradley 1:bcc701b1fd33 309 if (!dev)
mbradley 1:bcc701b1fd33 310 return FAILURE;
mbradley 1:bcc701b1fd33 311
mbradley 1:bcc701b1fd33 312 ret = ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 313 AD5770R_REFERENCE,
mbradley 1:bcc701b1fd33 314 AD5770R_REFERENCE_RESISTOR_SEL(external_reference) |
mbradley 1:bcc701b1fd33 315 AD5770R_REFERENCE_VOLTATE_SEL(reference_selector));
mbradley 1:bcc701b1fd33 316
mbradley 1:bcc701b1fd33 317 if (ret)
mbradley 1:bcc701b1fd33 318 return ret;
mbradley 1:bcc701b1fd33 319
mbradley 1:bcc701b1fd33 320 dev->reference_selector = reference_selector;
mbradley 1:bcc701b1fd33 321 dev->external_reference = external_reference;
mbradley 1:bcc701b1fd33 322
mbradley 1:bcc701b1fd33 323 return ret;
mbradley 1:bcc701b1fd33 324 };
mbradley 1:bcc701b1fd33 325
mbradley 1:bcc701b1fd33 326 /**
mbradley 1:bcc701b1fd33 327 * Set reference configuration.
mbradley 1:bcc701b1fd33 328 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 329 * @param alarm_config - Pointer to the array that contains the alarms
mbradley 1:bcc701b1fd33 330 * that will be enabled.
mbradley 1:bcc701b1fd33 331 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 332 */
mbradley 1:bcc701b1fd33 333 int32_t ad5770r_set_alarm(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 334 const struct ad5770r_alarm_cfg *const alarm_config)
mbradley 1:bcc701b1fd33 335 {
mbradley 1:bcc701b1fd33 336 int32_t ret;
mbradley 1:bcc701b1fd33 337
mbradley 1:bcc701b1fd33 338 if (!dev || !alarm_config)
mbradley 1:bcc701b1fd33 339 return FAILURE;
mbradley 1:bcc701b1fd33 340
mbradley 1:bcc701b1fd33 341 ret = ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 342 AD5770R_ALARM_CONFIG,
mbradley 1:bcc701b1fd33 343 AD5770R_ALARM_CONFIG_OPEN_DRAIN_EN(alarm_config->open_drain_en) |
mbradley 1:bcc701b1fd33 344 AD5770R_ALARM_CONFIG_THERMAL_SHUTDOWN_EN(alarm_config->thermal_shutdown_en) |
mbradley 1:bcc701b1fd33 345 AD5770R_ALARM_CONFIG_BACKGROUND_CRC_EN(alarm_config->background_crc_en) |
mbradley 1:bcc701b1fd33 346 AD5770R_ALARM_CONFIG_TEMP_WARNING_ALARM_MASK(alarm_config->temp_warning_msk) |
mbradley 1:bcc701b1fd33 347 AD5770R_ALARM_CONFIG_OVER_TEMP_ALARM_MASK(alarm_config->over_temp_msk) |
mbradley 1:bcc701b1fd33 348 AD5770R_ALARM_CONFIG_NEGATIVE_CHANNEL0_ALARM_MASK(alarm_config->neg_ch0_msk) |
mbradley 1:bcc701b1fd33 349 AD5770R_ALARM_CONFIG_IREF_FAULT_ALARM_MASK(alarm_config->iref_fault_msk) |
mbradley 1:bcc701b1fd33 350 AD5770R_ALARM_CONFIG_BACKGROUND_CRC_ALARM_MASK(
mbradley 1:bcc701b1fd33 351 alarm_config->background_crc_msk));
mbradley 1:bcc701b1fd33 352
mbradley 1:bcc701b1fd33 353 if (ret)
mbradley 1:bcc701b1fd33 354 return ret;
mbradley 1:bcc701b1fd33 355
mbradley 1:bcc701b1fd33 356 dev->alarm_config = *alarm_config;
mbradley 1:bcc701b1fd33 357
mbradley 1:bcc701b1fd33 358 return ret;
mbradley 1:bcc701b1fd33 359 };
mbradley 1:bcc701b1fd33 360
mbradley 1:bcc701b1fd33 361 /**
mbradley 1:bcc701b1fd33 362 * Set the channel output filter resistor.
mbradley 1:bcc701b1fd33 363 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 364 * @param output_filter - The structure that contains the channel output
mbradley 1:bcc701b1fd33 365 * filter resistor values.
mbradley 1:bcc701b1fd33 366 * @param channel - The channel number.
mbradley 1:bcc701b1fd33 367 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 368 */
mbradley 1:bcc701b1fd33 369 int32_t ad5770r_set_output_filter(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 370 enum ad5770r_output_filter_resistor output_filter,
mbradley 1:bcc701b1fd33 371 enum ad5770r_channels channel)
mbradley 1:bcc701b1fd33 372 {
mbradley 1:bcc701b1fd33 373 if (!dev)
mbradley 1:bcc701b1fd33 374 return FAILURE;
mbradley 1:bcc701b1fd33 375
mbradley 1:bcc701b1fd33 376 return ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 377 AD5770R_OUTPUT_FILTER_CH0 + channel,
mbradley 1:bcc701b1fd33 378 AD5770R_OUTPUT_FILTER_CH(output_filter));
mbradley 1:bcc701b1fd33 379 };
mbradley 1:bcc701b1fd33 380
mbradley 1:bcc701b1fd33 381 /**
mbradley 1:bcc701b1fd33 382 * Set the hardware ldac configuration.
mbradley 1:bcc701b1fd33 383 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 384 * @param mask_hw_ldac - The array contains HW LDAC channel masks.
mbradley 1:bcc701b1fd33 385 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 386 */
mbradley 1:bcc701b1fd33 387 int32_t ad5770r_set_hw_ldac(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 388 const struct ad5770r_channel_switches *mask_hw_ldac)
mbradley 1:bcc701b1fd33 389 {
mbradley 1:bcc701b1fd33 390 int32_t ret;
mbradley 1:bcc701b1fd33 391
mbradley 1:bcc701b1fd33 392 if (!dev || !mask_hw_ldac)
mbradley 1:bcc701b1fd33 393 return FAILURE;
mbradley 1:bcc701b1fd33 394
mbradley 1:bcc701b1fd33 395 ret = ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 396 AD5770R_HW_LDAC,
mbradley 1:bcc701b1fd33 397 AD5770R_HW_LDAC_MASK_CH(mask_hw_ldac->en0, AD5770R_CH0) |
mbradley 1:bcc701b1fd33 398 AD5770R_HW_LDAC_MASK_CH(mask_hw_ldac->en1, AD5770R_CH1) |
mbradley 1:bcc701b1fd33 399 AD5770R_HW_LDAC_MASK_CH(mask_hw_ldac->en2, AD5770R_CH2) |
mbradley 1:bcc701b1fd33 400 AD5770R_HW_LDAC_MASK_CH(mask_hw_ldac->en3, AD5770R_CH3) |
mbradley 1:bcc701b1fd33 401 AD5770R_HW_LDAC_MASK_CH(mask_hw_ldac->en4, AD5770R_CH4) |
mbradley 1:bcc701b1fd33 402 AD5770R_HW_LDAC_MASK_CH(mask_hw_ldac->en5, AD5770R_CH5));
mbradley 1:bcc701b1fd33 403
mbradley 1:bcc701b1fd33 404 if (ret)
mbradley 1:bcc701b1fd33 405 return ret;
mbradley 1:bcc701b1fd33 406
mbradley 1:bcc701b1fd33 407 dev->mask_hw_ldac = *mask_hw_ldac;
mbradley 1:bcc701b1fd33 408
mbradley 1:bcc701b1fd33 409 return ret;
mbradley 1:bcc701b1fd33 410 };
mbradley 1:bcc701b1fd33 411
mbradley 1:bcc701b1fd33 412 /**
mbradley 1:bcc701b1fd33 413 * Set dac value.
mbradley 1:bcc701b1fd33 414 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 415 * @param dac_value - value that will be set in the register.
mbradley 1:bcc701b1fd33 416 * @param channel - the selected channel.
mbradley 1:bcc701b1fd33 417 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 418 */
mbradley 1:bcc701b1fd33 419 int32_t ad5770r_set_dac_value(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 420 uint16_t dac_value, enum ad5770r_channels channel)
mbradley 1:bcc701b1fd33 421 {
mbradley 1:bcc701b1fd33 422 uint8_t data[2];
mbradley 1:bcc701b1fd33 423 int32_t ret;
mbradley 1:bcc701b1fd33 424
mbradley 1:bcc701b1fd33 425 if (!dev)
mbradley 1:bcc701b1fd33 426 return FAILURE;
mbradley 1:bcc701b1fd33 427
mbradley 1:bcc701b1fd33 428 data[1] = (uint8_t)AD5770R_CH_DAC_DATA_LSB(dac_value);
mbradley 1:bcc701b1fd33 429 data[0] = (uint8_t)((dac_value & 0x3FC0) >> 6);
mbradley 1:bcc701b1fd33 430
mbradley 1:bcc701b1fd33 431 ret = ad5770r_spi_reg_write_multiple(dev,
mbradley 1:bcc701b1fd33 432 AD5770R_CH0_DAC_MSB + 2 * channel,
mbradley 1:bcc701b1fd33 433 data, sizeof(data));
mbradley 1:bcc701b1fd33 434
mbradley 1:bcc701b1fd33 435 if (ret)
mbradley 1:bcc701b1fd33 436 return ret;
mbradley 1:bcc701b1fd33 437
mbradley 1:bcc701b1fd33 438 dev->dac_value[channel] = dac_value;
mbradley 1:bcc701b1fd33 439 dev->input_value[channel] = dac_value;
mbradley 1:bcc701b1fd33 440
mbradley 1:bcc701b1fd33 441 return ret;
mbradley 1:bcc701b1fd33 442 };
mbradley 1:bcc701b1fd33 443
mbradley 1:bcc701b1fd33 444 /**
mbradley 1:bcc701b1fd33 445 * Set dac value.
mbradley 1:bcc701b1fd33 446 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 447 * @param dac_input - value that will be set in the register.
mbradley 1:bcc701b1fd33 448 * @param channel - the selected channel.
mbradley 1:bcc701b1fd33 449 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 450 */
mbradley 1:bcc701b1fd33 451 int32_t ad5770r_set_dac_input(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 452 uint16_t dac_input, enum ad5770r_channels channel)
mbradley 1:bcc701b1fd33 453 {
mbradley 1:bcc701b1fd33 454 uint8_t data[2];
mbradley 1:bcc701b1fd33 455 int32_t ret;
mbradley 1:bcc701b1fd33 456
mbradley 1:bcc701b1fd33 457 if (!dev)
mbradley 1:bcc701b1fd33 458 return FAILURE;
mbradley 1:bcc701b1fd33 459
mbradley 1:bcc701b1fd33 460 data[1] = (uint8_t)AD5770R_CH_DAC_DATA_LSB(dac_input);
mbradley 1:bcc701b1fd33 461 data[0] = (uint8_t)((dac_input & 0x3FC0) >> 6);
mbradley 1:bcc701b1fd33 462
mbradley 1:bcc701b1fd33 463 ret = ad5770r_spi_reg_write_multiple(dev,
mbradley 1:bcc701b1fd33 464 AD5770R_CH0_INPUT_MSB + 2 * channel,
mbradley 1:bcc701b1fd33 465 data, sizeof(data));
mbradley 1:bcc701b1fd33 466 if (ret)
mbradley 1:bcc701b1fd33 467 return ret;
mbradley 1:bcc701b1fd33 468
mbradley 1:bcc701b1fd33 469 dev->input_value[channel] = dac_input;
mbradley 1:bcc701b1fd33 470
mbradley 1:bcc701b1fd33 471 return ret;
mbradley 1:bcc701b1fd33 472 };
mbradley 1:bcc701b1fd33 473
mbradley 1:bcc701b1fd33 474 /**
mbradley 1:bcc701b1fd33 475 * Set page mask for dac value and input.
mbradley 1:bcc701b1fd33 476 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 477 * @param page_mask - The structure contains the page mask values.
mbradley 1:bcc701b1fd33 478 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 479 */
mbradley 1:bcc701b1fd33 480 int32_t ad5770r_set_page_mask(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 481 const struct ad5770r_dac_page_mask *page_mask)
mbradley 1:bcc701b1fd33 482 {
mbradley 1:bcc701b1fd33 483 int32_t ret;
mbradley 1:bcc701b1fd33 484 uint8_t data[2];
mbradley 1:bcc701b1fd33 485
mbradley 1:bcc701b1fd33 486 if (!dev || !page_mask)
mbradley 1:bcc701b1fd33 487 return FAILURE;
mbradley 1:bcc701b1fd33 488
mbradley 1:bcc701b1fd33 489 data[1] = (uint8_t)AD5770R_CH_DAC_DATA_LSB(page_mask->dac_data_page_mask);
mbradley 1:bcc701b1fd33 490 data[0] = (uint8_t)((page_mask->dac_data_page_mask & 0x3FC0) >> 6);
mbradley 1:bcc701b1fd33 491
mbradley 1:bcc701b1fd33 492 ret = ad5770r_spi_reg_write_multiple(dev,
mbradley 1:bcc701b1fd33 493 AD5770R_DAC_PAGE_MASK_MSB,
mbradley 1:bcc701b1fd33 494 data, sizeof(data));
mbradley 1:bcc701b1fd33 495
mbradley 1:bcc701b1fd33 496 if (ret)
mbradley 1:bcc701b1fd33 497 return ret;
mbradley 1:bcc701b1fd33 498
mbradley 1:bcc701b1fd33 499 data[1] = (uint8_t)AD5770R_CH_DAC_DATA_LSB(page_mask->input_page_mask);
mbradley 1:bcc701b1fd33 500 data[0] = (uint8_t)((page_mask->input_page_mask & 0x3FC0) >> 6);
mbradley 1:bcc701b1fd33 501
mbradley 1:bcc701b1fd33 502 return ad5770r_spi_reg_write_multiple(dev,
mbradley 1:bcc701b1fd33 503 AD5770R_INPUT_PAGE_MASK_MSB,
mbradley 1:bcc701b1fd33 504 data, sizeof(data));
mbradley 1:bcc701b1fd33 505 }
mbradley 1:bcc701b1fd33 506
mbradley 1:bcc701b1fd33 507 /**
mbradley 1:bcc701b1fd33 508 * Set channel select.
mbradley 1:bcc701b1fd33 509 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 510 * @param mask_channel_sel - The array contains channel selection options.
mbradley 1:bcc701b1fd33 511 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 512 */
mbradley 1:bcc701b1fd33 513 int32_t ad5770r_set_mask_channel(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 514 const struct ad5770r_channel_switches *mask_channel_sel)
mbradley 1:bcc701b1fd33 515 {
mbradley 1:bcc701b1fd33 516 int32_t ret;
mbradley 1:bcc701b1fd33 517
mbradley 1:bcc701b1fd33 518 if (!dev || !mask_channel_sel)
mbradley 1:bcc701b1fd33 519 return FAILURE;
mbradley 1:bcc701b1fd33 520
mbradley 1:bcc701b1fd33 521 ret = ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 522 AD5770R_CH_SELECT,
mbradley 1:bcc701b1fd33 523 AD5770R_CH_SELECT_SEL_CH(mask_channel_sel->en0, AD5770R_CH0) |
mbradley 1:bcc701b1fd33 524 AD5770R_CH_SELECT_SEL_CH(mask_channel_sel->en1, AD5770R_CH1) |
mbradley 1:bcc701b1fd33 525 AD5770R_CH_SELECT_SEL_CH(mask_channel_sel->en2, AD5770R_CH2) |
mbradley 1:bcc701b1fd33 526 AD5770R_CH_SELECT_SEL_CH(mask_channel_sel->en3, AD5770R_CH3) |
mbradley 1:bcc701b1fd33 527 AD5770R_CH_SELECT_SEL_CH(mask_channel_sel->en4, AD5770R_CH4) |
mbradley 1:bcc701b1fd33 528 AD5770R_CH_SELECT_SEL_CH(mask_channel_sel->en5, AD5770R_CH5));
mbradley 1:bcc701b1fd33 529
mbradley 1:bcc701b1fd33 530 if (ret)
mbradley 1:bcc701b1fd33 531 return ret;
mbradley 1:bcc701b1fd33 532
mbradley 1:bcc701b1fd33 533 dev->mask_channel_sel = *mask_channel_sel;
mbradley 1:bcc701b1fd33 534
mbradley 1:bcc701b1fd33 535 return ret;
mbradley 1:bcc701b1fd33 536 }
mbradley 1:bcc701b1fd33 537
mbradley 1:bcc701b1fd33 538 /**
mbradley 1:bcc701b1fd33 539 * Set software LDAC.
mbradley 1:bcc701b1fd33 540 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 541 * @param sw_ldac - The array contains channel selection options.
mbradley 1:bcc701b1fd33 542 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 543 */
mbradley 1:bcc701b1fd33 544 int32_t ad5770r_set_sw_ldac(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 545 const struct ad5770r_channel_switches *sw_ldac)
mbradley 1:bcc701b1fd33 546 {
mbradley 1:bcc701b1fd33 547 int32_t ret;
mbradley 1:bcc701b1fd33 548 uint8_t sw_ldac_value;
mbradley 1:bcc701b1fd33 549 uint8_t i;
mbradley 1:bcc701b1fd33 550
mbradley 1:bcc701b1fd33 551 if (!dev || !sw_ldac)
mbradley 1:bcc701b1fd33 552 return FAILURE;
mbradley 1:bcc701b1fd33 553
mbradley 1:bcc701b1fd33 554 sw_ldac_value = AD5770R_SW_LDAC_CH(sw_ldac->en0, AD5770R_CH0) |
mbradley 1:bcc701b1fd33 555 AD5770R_SW_LDAC_CH(sw_ldac->en1, AD5770R_CH1) |
mbradley 1:bcc701b1fd33 556 AD5770R_SW_LDAC_CH(sw_ldac->en2, AD5770R_CH2) |
mbradley 1:bcc701b1fd33 557 AD5770R_SW_LDAC_CH(sw_ldac->en3, AD5770R_CH3) |
mbradley 1:bcc701b1fd33 558 AD5770R_SW_LDAC_CH(sw_ldac->en4, AD5770R_CH4) |
mbradley 1:bcc701b1fd33 559 AD5770R_SW_LDAC_CH(sw_ldac->en5, AD5770R_CH5);
mbradley 1:bcc701b1fd33 560
mbradley 1:bcc701b1fd33 561 ret = ad5770r_spi_reg_write(dev, AD5770R_SW_LDAC, sw_ldac_value);
mbradley 1:bcc701b1fd33 562
mbradley 1:bcc701b1fd33 563 if (ret)
mbradley 1:bcc701b1fd33 564 return ret;
mbradley 1:bcc701b1fd33 565
mbradley 1:bcc701b1fd33 566 dev->sw_ldac = *sw_ldac;
mbradley 1:bcc701b1fd33 567
mbradley 1:bcc701b1fd33 568 for (i = 0; i <= AD5770R_CH5; i++) {
mbradley 1:bcc701b1fd33 569 if ((sw_ldac_value >> i) & BIT(0)) {
mbradley 1:bcc701b1fd33 570 dev->dac_value[i] = dev->input_value[i];
mbradley 1:bcc701b1fd33 571 }
mbradley 1:bcc701b1fd33 572 }
mbradley 1:bcc701b1fd33 573
mbradley 1:bcc701b1fd33 574 return ret;
mbradley 1:bcc701b1fd33 575 }
mbradley 1:bcc701b1fd33 576
mbradley 1:bcc701b1fd33 577 /**
mbradley 1:bcc701b1fd33 578 * Get status value.
mbradley 1:bcc701b1fd33 579 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 580 * @param status - The status of the device.
mbradley 1:bcc701b1fd33 581 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 582 */
mbradley 1:bcc701b1fd33 583 int32_t ad5770r_get_status(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 584 uint8_t *status)
mbradley 1:bcc701b1fd33 585 {
mbradley 1:bcc701b1fd33 586 return ad5770r_spi_reg_read(dev,
mbradley 1:bcc701b1fd33 587 AD5770R_STATUS,
mbradley 1:bcc701b1fd33 588 status);
mbradley 1:bcc701b1fd33 589 };
mbradley 1:bcc701b1fd33 590
mbradley 1:bcc701b1fd33 591 /**
mbradley 1:bcc701b1fd33 592 * Get interface status value.
mbradley 1:bcc701b1fd33 593 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 594 * @param status - The INTERFACE STATUS A register value.
mbradley 1:bcc701b1fd33 595 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 596 */
mbradley 1:bcc701b1fd33 597 int32_t ad5770r_get_interface_status(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 598 uint8_t *status)
mbradley 1:bcc701b1fd33 599 {
mbradley 1:bcc701b1fd33 600 return ad5770r_spi_reg_read(dev,
mbradley 1:bcc701b1fd33 601 AD5770R_INTERFACE_STATUS_A,
mbradley 1:bcc701b1fd33 602 status);
mbradley 1:bcc701b1fd33 603 };
mbradley 1:bcc701b1fd33 604
mbradley 1:bcc701b1fd33 605 /**
mbradley 1:bcc701b1fd33 606 * Set the channel monitor configuration.
mbradley 1:bcc701b1fd33 607 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 608 * @param mon_setup - The structure that contains the monitor setup
mbradley 1:bcc701b1fd33 609 * values.
mbradley 1:bcc701b1fd33 610 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 611 */
mbradley 1:bcc701b1fd33 612 int32_t ad5770r_set_monitor_setup(struct ad5770r_dev *dev,
mbradley 1:bcc701b1fd33 613 const struct ad5770r_monitor_setup *mon_setup)
mbradley 1:bcc701b1fd33 614 {
mbradley 1:bcc701b1fd33 615 int32_t ret;
mbradley 1:bcc701b1fd33 616
mbradley 1:bcc701b1fd33 617 if (!dev || !mon_setup)
mbradley 1:bcc701b1fd33 618 return FAILURE;
mbradley 1:bcc701b1fd33 619
mbradley 1:bcc701b1fd33 620 ret = ad5770r_spi_reg_write(dev,
mbradley 1:bcc701b1fd33 621 AD5770R_MONITOR_SETUP,
mbradley 1:bcc701b1fd33 622 AD5770R_MONITOR_SETUP_MON_CH(mon_setup->monitor_channel) |
mbradley 1:bcc701b1fd33 623 AD5770R_MONITOR_SETUP_IB_EXT_EN(mon_setup->ib_ext_en) |
mbradley 1:bcc701b1fd33 624 AD5770R_MONITOR_SETUP_MUX_BUFFER(mon_setup->mux_buffer) |
mbradley 1:bcc701b1fd33 625 AD5770R_MONITOR_SETUP_MON_FUNCTION(mon_setup->monitor_function));
mbradley 1:bcc701b1fd33 626
mbradley 1:bcc701b1fd33 627 if (ret)
mbradley 1:bcc701b1fd33 628 return ret;
mbradley 1:bcc701b1fd33 629
mbradley 1:bcc701b1fd33 630 dev->mon_setup = *mon_setup;
mbradley 1:bcc701b1fd33 631
mbradley 1:bcc701b1fd33 632 return ret;
mbradley 1:bcc701b1fd33 633 };
mbradley 1:bcc701b1fd33 634
mbradley 1:bcc701b1fd33 635 /**
mbradley 1:bcc701b1fd33 636 * Initialize the device.
mbradley 1:bcc701b1fd33 637 * @param device - The device structure.
mbradley 1:bcc701b1fd33 638 * @param init_param - The structure that contains the device initial
mbradley 1:bcc701b1fd33 639 * parameters.
mbradley 1:bcc701b1fd33 640 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 641 */
mbradley 1:bcc701b1fd33 642 int32_t ad5770r_init(struct ad5770r_dev **device,
mbradley 1:bcc701b1fd33 643 const struct ad5770r_init_param *init_param)
mbradley 1:bcc701b1fd33 644 {
mbradley 1:bcc701b1fd33 645 struct ad5770r_dev *dev;
mbradley 1:bcc701b1fd33 646 uint8_t product_id_l, product_id_h;
mbradley 1:bcc701b1fd33 647 int32_t ret;
mbradley 1:bcc701b1fd33 648 enum ad5770r_channels i;
mbradley 1:bcc701b1fd33 649
mbradley 1:bcc701b1fd33 650 if (!device || !init_param)
mbradley 1:bcc701b1fd33 651 return FAILURE;
mbradley 1:bcc701b1fd33 652
mbradley 1:bcc701b1fd33 653 dev = (struct ad5770r_dev *)calloc(1, sizeof(*dev));
mbradley 1:bcc701b1fd33 654 if (!dev)
mbradley 1:bcc701b1fd33 655 return FAILURE;
mbradley 1:bcc701b1fd33 656
mbradley 1:bcc701b1fd33 657 /* SPI */
mbradley 1:bcc701b1fd33 658 ret = spi_init(&dev->spi_desc, &init_param->spi_init);
mbradley 1:bcc701b1fd33 659
mbradley 1:bcc701b1fd33 660 /* Query device presence */
mbradley 1:bcc701b1fd33 661 ad5770r_spi_reg_read(dev, AD5770R_PRODUCT_ID_L, &product_id_l);
mbradley 1:bcc701b1fd33 662 ad5770r_spi_reg_read(dev, AD5770R_PRODUCT_ID_H, &product_id_h);
mbradley 1:bcc701b1fd33 663
mbradley 1:bcc701b1fd33 664 if (product_id_l != 0x04 || product_id_h != 0x40) {
mbradley 1:bcc701b1fd33 665 printf("failed to read id (0x%X : 0x%X)\n", product_id_l,
mbradley 1:bcc701b1fd33 666 product_id_h);
mbradley 1:bcc701b1fd33 667 return FAILURE;
mbradley 1:bcc701b1fd33 668 }
mbradley 1:bcc701b1fd33 669
mbradley 1:bcc701b1fd33 670 /* Device settings */
mbradley 1:bcc701b1fd33 671 ret = ad5770r_set_device_spi(dev,
mbradley 1:bcc701b1fd33 672 &init_param->dev_spi_settings);
mbradley 1:bcc701b1fd33 673
mbradley 1:bcc701b1fd33 674 ret |= ad5770r_channel_config(dev,
mbradley 1:bcc701b1fd33 675 &init_param->channel_config);
mbradley 1:bcc701b1fd33 676
mbradley 1:bcc701b1fd33 677 ret |= ad5770r_set_reference(dev,
mbradley 1:bcc701b1fd33 678 init_param->external_reference,
mbradley 1:bcc701b1fd33 679 init_param->reference_selector);
mbradley 1:bcc701b1fd33 680 ret |= ad5770r_set_alarm(dev, &init_param->alarm_config);
mbradley 1:bcc701b1fd33 681
mbradley 1:bcc701b1fd33 682 ret |= ad5770r_set_hw_ldac(dev,
mbradley 1:bcc701b1fd33 683 &init_param->mask_hw_ldac);
mbradley 1:bcc701b1fd33 684
mbradley 1:bcc701b1fd33 685 for( i = AD5770R_CH0; i <= AD5770R_CH5; i++) {
mbradley 1:bcc701b1fd33 686 ret |= ad5770r_set_output_mode(dev,
mbradley 1:bcc701b1fd33 687 &init_param->output_mode[i], i);
mbradley 1:bcc701b1fd33 688 ret |= ad5770r_set_output_filter(dev,
mbradley 1:bcc701b1fd33 689 init_param->output_filter[i], i);
mbradley 1:bcc701b1fd33 690 ret |= ad5770r_set_dac_value(dev,
mbradley 1:bcc701b1fd33 691 init_param->dac_value[i], i);
mbradley 1:bcc701b1fd33 692 ret |= ad5770r_set_dac_input(dev,
mbradley 1:bcc701b1fd33 693 init_param->input_value[i], i);
mbradley 1:bcc701b1fd33 694
mbradley 1:bcc701b1fd33 695 dev->output_mode[i] = init_param->output_mode[i];
mbradley 1:bcc701b1fd33 696 dev->output_filter[i] = init_param->output_filter[i];
mbradley 1:bcc701b1fd33 697 dev->dac_value[i] = init_param->dac_value[i];
mbradley 1:bcc701b1fd33 698 dev->input_value[i] = init_param->input_value[i];
mbradley 1:bcc701b1fd33 699 }
mbradley 1:bcc701b1fd33 700
mbradley 1:bcc701b1fd33 701 ret |= ad5770r_set_page_mask(dev,
mbradley 1:bcc701b1fd33 702 &init_param->page_mask);
mbradley 1:bcc701b1fd33 703 ret |= ad5770r_set_mask_channel(dev,
mbradley 1:bcc701b1fd33 704 &init_param->mask_channel_sel);
mbradley 1:bcc701b1fd33 705 ret |= ad5770r_set_sw_ldac(dev,
mbradley 1:bcc701b1fd33 706 &init_param->sw_ldac);
mbradley 1:bcc701b1fd33 707 ret |= ad5770r_set_monitor_setup(dev,
mbradley 1:bcc701b1fd33 708 &init_param->mon_setup);
mbradley 1:bcc701b1fd33 709
mbradley 1:bcc701b1fd33 710 *device = dev;
mbradley 1:bcc701b1fd33 711
mbradley 1:bcc701b1fd33 712 if (!ret)
mbradley 1:bcc701b1fd33 713 printf("ad5770r successfully initialized\n");
mbradley 1:bcc701b1fd33 714 else
mbradley 1:bcc701b1fd33 715 printf("ad5770r initialization error (%d)\n", ret);
mbradley 1:bcc701b1fd33 716
mbradley 1:bcc701b1fd33 717 return ret;
mbradley 1:bcc701b1fd33 718 }
mbradley 1:bcc701b1fd33 719
mbradley 1:bcc701b1fd33 720 /**
mbradley 1:bcc701b1fd33 721 * Delete and remove the device.
mbradley 2:a22a3d5d9f41 722 * @param dev - The device structure.
mbradley 1:bcc701b1fd33 723 * @return SUCCESS in case of success, negative error code otherwise.
mbradley 1:bcc701b1fd33 724 */
mbradley 1:bcc701b1fd33 725 int32_t ad5770r_remove(struct ad5770r_dev *dev)
mbradley 1:bcc701b1fd33 726 {
mbradley 1:bcc701b1fd33 727 int32_t ret;
mbradley 1:bcc701b1fd33 728
mbradley 1:bcc701b1fd33 729 if (!dev)
mbradley 1:bcc701b1fd33 730 return FAILURE;
mbradley 1:bcc701b1fd33 731
mbradley 1:bcc701b1fd33 732 ret = spi_remove(dev->spi_desc);
mbradley 1:bcc701b1fd33 733
mbradley 1:bcc701b1fd33 734 free(dev);
mbradley 1:bcc701b1fd33 735
mbradley 1:bcc701b1fd33 736 return ret;
mbradley 1:bcc701b1fd33 737 }