Example Program for EVAL-AD7606

Dependencies:   platform_drivers

Committer:
Kjansen
Date:
Tue Aug 03 11:54:49 2021 +0100
Revision:
7:054dbd5e1f45
Parent:
6:32de160dce43
Modified the ADC data capture module to remove dependancy on type of ADC and it's specific operations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 1:819ac9aa5667 1 /***************************************************************************//**
mahphalke 1:819ac9aa5667 2 * @file iio_ad7606.c
mahphalke 1:819ac9aa5667 3 * @brief Implementation of AD7606 IIO application interfaces
mahphalke 1:819ac9aa5667 4 * @details This module acts as an interface for AD7606 IIO application
mahphalke 1:819ac9aa5667 5 ********************************************************************************
Kjansen 6:32de160dce43 6 * Copyright (c) 2020-2021 Analog Devices, Inc.
mahphalke 1:819ac9aa5667 7 *
mahphalke 1:819ac9aa5667 8 * This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 1:819ac9aa5667 9 * By using this software you agree to the terms of the associated
mahphalke 1:819ac9aa5667 10 * Analog Devices Software License Agreement.
mahphalke 1:819ac9aa5667 11 *******************************************************************************/
mahphalke 1:819ac9aa5667 12
mahphalke 1:819ac9aa5667 13 /******************************************************************************/
mahphalke 1:819ac9aa5667 14 /***************************** Include Files **********************************/
mahphalke 1:819ac9aa5667 15 /******************************************************************************/
mahphalke 1:819ac9aa5667 16
mahphalke 1:819ac9aa5667 17 #include <inttypes.h>
mahphalke 1:819ac9aa5667 18 #include <string.h>
mahphalke 1:819ac9aa5667 19 #include <errno.h>
mahphalke 1:819ac9aa5667 20 #include <math.h>
mahphalke 1:819ac9aa5667 21
mahphalke 1:819ac9aa5667 22 #include "app_config.h"
mahphalke 1:819ac9aa5667 23 #include "iio_ad7606.h"
mahphalke 1:819ac9aa5667 24 #include "platform_support.h"
mahphalke 1:819ac9aa5667 25 #include "spi_extra.h"
mahphalke 1:819ac9aa5667 26 #include "gpio_extra.h"
mahphalke 1:819ac9aa5667 27 #include "uart_extra.h"
mahphalke 1:819ac9aa5667 28 #include "irq_extra.h"
Kjansen 6:32de160dce43 29 #include "error.h"
mahphalke 1:819ac9aa5667 30
mahphalke 1:819ac9aa5667 31 #include "ad7606.h"
Kjansen 7:054dbd5e1f45 32 #include "adc_data_capture.h"
mahphalke 1:819ac9aa5667 33 #include "ad7606_support.h"
mahphalke 1:819ac9aa5667 34 #include "ad7606_user_config.h"
mahphalke 1:819ac9aa5667 35
mahphalke 1:819ac9aa5667 36 /******************************************************************************/
mahphalke 1:819ac9aa5667 37 /************************ Macros/Constants ************************************/
mahphalke 1:819ac9aa5667 38 /******************************************************************************/
mahphalke 1:819ac9aa5667 39
mahphalke 1:819ac9aa5667 40 /* ADC data to Voltage conversion scale factor for IIO client */
mahphalke 1:819ac9aa5667 41 #define DEFAULT_SCALE ((DEFAULT_CHN_RANGE / ADC_MAX_COUNT_BIPOLAR) * 1000)
mahphalke 1:819ac9aa5667 42
mahphalke 3:83b3133f544a 43 /* LSB Threshold to entry into open circuit detection as per datasheet */
mahphalke 3:83b3133f544a 44 #define MANUAL_OPEN_DETECT_ENTRY_TRHLD 350
mahphalke 3:83b3133f544a 45
mahphalke 3:83b3133f544a 46 /* Manual open circuit detect LSB threshold @50K Rpd as per datasheet */
mahphalke 3:83b3133f544a 47 #define MANUAL_OPEN_DETECT_THRESHOLD_RPD50K 20
mahphalke 3:83b3133f544a 48
mahphalke 3:83b3133f544a 49 /* Number of consecutive conversions (N) in manual open circuit detection */
mahphalke 3:83b3133f544a 50 #define MANUAL_OPEN_DETECT_CONV_CNTS 10
mahphalke 1:819ac9aa5667 51
mahphalke 3:83b3133f544a 52 /* LSB Threshold b/w consecutive N conversions */
mahphalke 3:83b3133f544a 53 #define MANUAL_OPEN_DETECT_CONV_TRSHLD 10
mahphalke 3:83b3133f544a 54
mahphalke 3:83b3133f544a 55 /* Number of common mode conversions in manual open circuit detect */
mahphalke 3:83b3133f544a 56 #define MANUAL_OPEN_DETECT_CM_CNV_CNT 3
mahphalke 3:83b3133f544a 57
mahphalke 3:83b3133f544a 58 /* Max number of queue counts for auto mode open circuit detection */
mahphalke 3:83b3133f544a 59 #define AUTO_OPEN_DETECT_QUEUE_MAX_CNT 128
mahphalke 3:83b3133f544a 60 #define AUTO_OPEN_DETECT_QUEUE_EXTRA_CONV_CNT 15
mahphalke 1:819ac9aa5667 61
mahphalke 1:819ac9aa5667 62 /* Maximum ADC calibration gain value */
mahphalke 1:819ac9aa5667 63 #define ADC_CALIBRATION_GAIN_MAX 64.0
mahphalke 1:819ac9aa5667 64
mahphalke 3:83b3133f544a 65 #if defined(DEV_AD7606C_18)
mahphalke 3:83b3133f544a 66 #define OFFSET_REG_RESOLUTION 4
mahphalke 3:83b3133f544a 67 #else
mahphalke 3:83b3133f544a 68 #define OFFSET_REG_RESOLUTION 1
mahphalke 3:83b3133f544a 69 #endif
mahphalke 3:83b3133f544a 70
Kjansen 7:054dbd5e1f45 71 /* Bytes per sample. This count should divide the total 256 bytes into 'n' equivalent
Kjansen 7:054dbd5e1f45 72 * ADC samples as IIO library requests only 256bytes of data at a time in a given
Kjansen 7:054dbd5e1f45 73 * data read query.
Kjansen 7:054dbd5e1f45 74 * For 1 to 8-bit ADC, bytes per sample = 1 (2^0)
Kjansen 7:054dbd5e1f45 75 * For 9 to 16-bit ADC, bytes per sample = 2 (2^1)
Kjansen 7:054dbd5e1f45 76 * For 17 to 32-bit ADC, bytes per sample = 4 (2^2)
Kjansen 7:054dbd5e1f45 77 **/
Kjansen 7:054dbd5e1f45 78 #if (AD7606X_ADC_RESOLUTION == 18)
Kjansen 7:054dbd5e1f45 79 #define BYTES_PER_SAMPLE sizeof(uint32_t) // For ADC resolution of 18-bits
Kjansen 7:054dbd5e1f45 80 #else
Kjansen 7:054dbd5e1f45 81 #define BYTES_PER_SAMPLE sizeof(uint16_t) // For ADC resolution of 16-bits
Kjansen 7:054dbd5e1f45 82 #endif
Kjansen 7:054dbd5e1f45 83
Kjansen 7:054dbd5e1f45 84 /* Number of data storage bits (needed for IIO client to plot ADC data) */
Kjansen 7:054dbd5e1f45 85 #define CHN_STORAGE_BITS (BYTES_PER_SAMPLE * 8)
Kjansen 7:054dbd5e1f45 86
mahphalke 1:819ac9aa5667 87 /******************************************************************************/
mahphalke 1:819ac9aa5667 88 /*************************** Types Declarations *******************************/
mahphalke 1:819ac9aa5667 89 /******************************************************************************/
mahphalke 1:819ac9aa5667 90
Kjansen 6:32de160dce43 91 /* IIO interface descriptor */
Kjansen 6:32de160dce43 92 static struct iio_desc *p_ad7606_iio_desc;
mahphalke 1:819ac9aa5667 93
mahphalke 1:819ac9aa5667 94 /**
mahphalke 1:819ac9aa5667 95 * Device name.
mahphalke 1:819ac9aa5667 96 */
mahphalke 1:819ac9aa5667 97 static const char dev_name[] = ACTIVE_DEVICE_NAME;
mahphalke 1:819ac9aa5667 98
mahphalke 1:819ac9aa5667 99 /**
mahphalke 1:819ac9aa5667 100 * Pointer to the struct representing the AD7606 IIO device
mahphalke 1:819ac9aa5667 101 */
Kjansen 7:054dbd5e1f45 102 struct ad7606_dev *p_ad7606_dev_inst = NULL;
mahphalke 1:819ac9aa5667 103
mahphalke 1:819ac9aa5667 104
mahphalke 1:819ac9aa5667 105 /* Device attributes with default values */
mahphalke 1:819ac9aa5667 106
mahphalke 1:819ac9aa5667 107 /* Power down mode values string representation (possible values specified in datasheet) */
mahphalke 1:819ac9aa5667 108 static char *operating_mode_str[] = {
mahphalke 1:819ac9aa5667 109 "0 (Normal Mode)",
mahphalke 1:819ac9aa5667 110 "1 (Standby Mode)",
mahphalke 1:819ac9aa5667 111 "2 (Auto Standby Mode)",
mahphalke 1:819ac9aa5667 112 "3 (Shutdown Mode)"
mahphalke 1:819ac9aa5667 113 };
mahphalke 1:819ac9aa5667 114
mahphalke 1:819ac9aa5667 115 /* Bandwidth values string */
mahphalke 1:819ac9aa5667 116 static char *bandwidth_str[] = {
mahphalke 1:819ac9aa5667 117 "0 (Low)",
mahphalke 1:819ac9aa5667 118 "1 (High)"
mahphalke 1:819ac9aa5667 119 };
mahphalke 1:819ac9aa5667 120
mahphalke 1:819ac9aa5667 121 /* Channel range values string representation (possible values specified in datasheet) */
mahphalke 1:819ac9aa5667 122 static char *chn_range_str[] = {
mahphalke 1:819ac9aa5667 123 #if defined(DEV_AD7606B)
mahphalke 1:819ac9aa5667 124 "0 (+/-2.5V SE)", "1 (+/-5.0V SE)", "2 (+/-10.0V SE)", "3 (+/-10.0V SE)",
mahphalke 1:819ac9aa5667 125 "4 (+/-10.0V SE)", "5 (+/-10.0V SE)", "6 (+/-10.0V SE)", "7 (+/-10.0V SE)",
mahphalke 1:819ac9aa5667 126 "8 (+/-10.0V SE)", "9 (+/-10.0V SE)", "10 (+/-10.0V SE)", "11 (+/-10.0V SE)",
mahphalke 1:819ac9aa5667 127 #elif defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
mahphalke 1:819ac9aa5667 128 "0 (+/-2.5V SE)", "1 (+/-5.0V SE)", "2 (+/-6.25V SE)", "3 (+/-10.0V SE)",
mahphalke 3:83b3133f544a 129 "4 (+/-12.5V SE)", "5 (0 to 5V SE)", "6 (0 to 10V SE)", "7 (0 to 12.5V SE)",
mahphalke 3:83b3133f544a 130 "8 (+/-5.0V Diff)", "9 (+/-10.0V Diff)", "10 (+/-12.5V Diff)", "11 (+/-20.0V Diff)"
mahphalke 1:819ac9aa5667 131 #elif defined(DEV_AD7609)
mahphalke 1:819ac9aa5667 132 "0 (+/-10.0V SE)", "1 (+/-20.0V SE)"
mahphalke 1:819ac9aa5667 133 #else
mahphalke 1:819ac9aa5667 134 "0 (+/-5.0V SE)", "1 (+/-10.0V SE)"
mahphalke 1:819ac9aa5667 135 #endif
mahphalke 1:819ac9aa5667 136 };
mahphalke 1:819ac9aa5667 137
mahphalke 1:819ac9aa5667 138 /* Oversampling values string representation (possible values specified in datasheet) */
mahphalke 1:819ac9aa5667 139 static char *oversampling_val_str[] = {
mahphalke 1:819ac9aa5667 140 "0 (no oversampling)", "1 (oversampling by 2)", "2 (oversampling by 4)",
mahphalke 1:819ac9aa5667 141 "3 (oversampling by 8)", "4 (oversampling by 16)", "5 (oversampling by 32)",
mahphalke 1:819ac9aa5667 142 "6 (oversampling by 64)", "7 (oversampling by 128)", "8 (oversampling by 256)"
mahphalke 1:819ac9aa5667 143 };
mahphalke 1:819ac9aa5667 144
mahphalke 1:819ac9aa5667 145
mahphalke 1:819ac9aa5667 146 /* Channel range values string representation (possible values specified in datasheet) */
mahphalke 1:819ac9aa5667 147 static float chn_range_val[] = {
mahphalke 1:819ac9aa5667 148 #if defined(DEV_AD7606B)
mahphalke 1:819ac9aa5667 149 2.5, 5.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0
mahphalke 1:819ac9aa5667 150 #elif defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
mahphalke 1:819ac9aa5667 151 2.5, 5.0, 6.25, 10.0, 12.5, 5.0, 10.0, 12.5, 5.0, 10.0, 12.5, 20.0
mahphalke 1:819ac9aa5667 152 #elif defined(DEV_AD7609)
mahphalke 1:819ac9aa5667 153 10.0, 20.0
mahphalke 1:819ac9aa5667 154 #else
mahphalke 1:819ac9aa5667 155 5.0, 10.0
mahphalke 1:819ac9aa5667 156 #endif
mahphalke 1:819ac9aa5667 157 };
mahphalke 1:819ac9aa5667 158
mahphalke 1:819ac9aa5667 159 /* Range value per channel */
mahphalke 1:819ac9aa5667 160 static float attr_chn_range[AD7606X_ADC_CHANNELS] = {
mahphalke 1:819ac9aa5667 161 DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE,
mahphalke 1:819ac9aa5667 162 DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE
mahphalke 1:819ac9aa5667 163 };
mahphalke 1:819ac9aa5667 164
mahphalke 1:819ac9aa5667 165 /* Scale value per channel */
mahphalke 1:819ac9aa5667 166 static float attr_scale_val[AD7606X_ADC_CHANNELS] = {
mahphalke 1:819ac9aa5667 167 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE,
mahphalke 1:819ac9aa5667 168 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE
mahphalke 1:819ac9aa5667 169 };
mahphalke 1:819ac9aa5667 170
mahphalke 1:819ac9aa5667 171 /* Scale value per channel */
mahphalke 1:819ac9aa5667 172 static polarity_e attr_polarity_val[AD7606X_ADC_CHANNELS] = {
mahphalke 1:819ac9aa5667 173 BIPOLAR, BIPOLAR, BIPOLAR, BIPOLAR,
mahphalke 1:819ac9aa5667 174 BIPOLAR, BIPOLAR, BIPOLAR, BIPOLAR
mahphalke 1:819ac9aa5667 175 };
mahphalke 1:819ac9aa5667 176
mahphalke 1:819ac9aa5667 177 /* Channel range */
mahphalke 1:819ac9aa5667 178 typedef enum {
mahphalke 1:819ac9aa5667 179 LOW,
mahphalke 1:819ac9aa5667 180 HIGH
mahphalke 1:819ac9aa5667 181 } range_e;
mahphalke 1:819ac9aa5667 182
mahphalke 1:819ac9aa5667 183 /* Open detect auto mode QUEUE register count */
mahphalke 1:819ac9aa5667 184 static uint8_t open_detect_queue_cnts[AD7606X_ADC_CHANNELS] = {
mahphalke 1:819ac9aa5667 185 0
mahphalke 1:819ac9aa5667 186 };
mahphalke 1:819ac9aa5667 187
mahphalke 1:819ac9aa5667 188 /* ADC gain calibration Rfilter value (in Kohms) */
mahphalke 1:819ac9aa5667 189 static uint8_t gain_calibration_reg_val[AD7606X_ADC_CHANNELS] = {
mahphalke 1:819ac9aa5667 190 0
mahphalke 1:819ac9aa5667 191 };
mahphalke 1:819ac9aa5667 192
mahphalke 1:819ac9aa5667 193 /* Flag to trigger new background conversion and capture when READBUFF command is issued */
Kjansen 6:32de160dce43 194 static bool adc_data_capture_started = false;
mahphalke 1:819ac9aa5667 195
mahphalke 3:83b3133f544a 196 /* Gain calibration status */
mahphalke 3:83b3133f544a 197 static bool gain_calibration_done = false;
mahphalke 3:83b3133f544a 198
mahphalke 3:83b3133f544a 199 /* Open circuit mode detection flags */
mahphalke 3:83b3133f544a 200 static bool open_circuit_detection_done = false;
mahphalke 3:83b3133f544a 201 static bool open_circuit_detection_error = false;
mahphalke 3:83b3133f544a 202 static bool open_circuit_detect_read_done = false;
mahphalke 3:83b3133f544a 203
mahphalke 1:819ac9aa5667 204 /******************************************************************************/
mahphalke 1:819ac9aa5667 205 /************************ Functions Prototypes ********************************/
mahphalke 1:819ac9aa5667 206 /******************************************************************************/
mahphalke 1:819ac9aa5667 207
mahphalke 1:819ac9aa5667 208 static float get_vltg_conv_scale_factor(float chn_range, polarity_e polarity);
mahphalke 1:819ac9aa5667 209 static void save_local_attributes(void);
mahphalke 1:819ac9aa5667 210
mahphalke 1:819ac9aa5667 211 /******************************************************************************/
mahphalke 1:819ac9aa5667 212 /************************ Functions Definitions *******************************/
mahphalke 1:819ac9aa5667 213 /******************************************************************************/
mahphalke 1:819ac9aa5667 214
mahphalke 1:819ac9aa5667 215 /*!
mahphalke 1:819ac9aa5667 216 * @brief Getter/Setter for the scale attribute value
mahphalke 1:819ac9aa5667 217 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 218 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 219 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 220 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 221 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 222 */
Kjansen 6:32de160dce43 223 static ssize_t get_chn_scale(void *device,
Kjansen 6:32de160dce43 224 char *buf,
Kjansen 6:32de160dce43 225 size_t len,
Kjansen 6:32de160dce43 226 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 227 intptr_t id)
mahphalke 1:819ac9aa5667 228 {
Kjansen 6:32de160dce43 229 return (ssize_t) sprintf(buf, "%f", attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 230 }
mahphalke 1:819ac9aa5667 231
Kjansen 6:32de160dce43 232 static ssize_t set_chn_scale(void *device,
Kjansen 6:32de160dce43 233 char *buf,
Kjansen 6:32de160dce43 234 size_t len,
Kjansen 6:32de160dce43 235 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 236 intptr_t id)
mahphalke 1:819ac9aa5667 237 {
mahphalke 1:819ac9aa5667 238 float scale;
mahphalke 1:819ac9aa5667 239
mahphalke 1:819ac9aa5667 240 (void)sscanf(buf, "%f", &scale);
mahphalke 1:819ac9aa5667 241
mahphalke 1:819ac9aa5667 242 if (scale > 0.0) {
Kjansen 6:32de160dce43 243 attr_scale_val[channel->ch_num] = scale;
mahphalke 1:819ac9aa5667 244 return len;
mahphalke 1:819ac9aa5667 245 }
mahphalke 1:819ac9aa5667 246
mahphalke 1:819ac9aa5667 247 return -EINVAL;
mahphalke 1:819ac9aa5667 248 }
mahphalke 1:819ac9aa5667 249
mahphalke 1:819ac9aa5667 250
mahphalke 1:819ac9aa5667 251 /*!
mahphalke 3:83b3133f544a 252 * @brief Getter/Setter for the sampling frequency attribute value
mahphalke 3:83b3133f544a 253 * @param device- pointer to IIO device structure
mahphalke 3:83b3133f544a 254 * @param buf- pointer to buffer holding attribute value
mahphalke 3:83b3133f544a 255 * @param len- length of buffer string data
mahphalke 3:83b3133f544a 256 * @param channel- pointer to IIO channel structure
mahphalke 3:83b3133f544a 257 * @return Number of characters read/written
mahphalke 3:83b3133f544a 258 * @Note This attribute is used to define the timeout period in IIO
mahphalke 3:83b3133f544a 259 * client during data capture.
mahphalke 3:83b3133f544a 260 * Timeout = (number of requested samples * (1/sampling frequency)) + 1sec
mahphalke 3:83b3133f544a 261 * e.g. if sampling frequency = 1KSPS and requested samples = 400
mahphalke 3:83b3133f544a 262 * Timeout = (400 * 0.001) + 1 = 1.4sec
mahphalke 3:83b3133f544a 263 */
Kjansen 6:32de160dce43 264 static ssize_t get_sampling_frequency(void *device,
Kjansen 6:32de160dce43 265 char *buf,
Kjansen 6:32de160dce43 266 size_t len,
Kjansen 6:32de160dce43 267 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 268 intptr_t id)
mahphalke 3:83b3133f544a 269 {
Kjansen 6:32de160dce43 270 return (ssize_t) sprintf(buf, "%d", SAMPLING_RATE);
mahphalke 3:83b3133f544a 271 }
mahphalke 3:83b3133f544a 272
Kjansen 6:32de160dce43 273 static ssize_t set_sampling_frequency(void *device,
Kjansen 6:32de160dce43 274 char *buf,
Kjansen 6:32de160dce43 275 size_t len,
Kjansen 6:32de160dce43 276 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 277 intptr_t id)
mahphalke 3:83b3133f544a 278 {
mahphalke 3:83b3133f544a 279 /* NA- Can't set sampling frequency value */
mahphalke 3:83b3133f544a 280 return len;
mahphalke 3:83b3133f544a 281 }
mahphalke 3:83b3133f544a 282
mahphalke 3:83b3133f544a 283
mahphalke 3:83b3133f544a 284 /*!
mahphalke 1:819ac9aa5667 285 * @brief Getter/Setter for the raw attribute value
mahphalke 1:819ac9aa5667 286 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 287 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 288 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 289 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 290 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 291 */
Kjansen 6:32de160dce43 292 static ssize_t get_chn_raw(void *device,
Kjansen 6:32de160dce43 293 char *buf,
Kjansen 6:32de160dce43 294 size_t len,
Kjansen 6:32de160dce43 295 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 296 intptr_t id)
mahphalke 1:819ac9aa5667 297 {
mahphalke 1:819ac9aa5667 298 int32_t adc_data_raw;
mahphalke 1:819ac9aa5667 299
mahphalke 1:819ac9aa5667 300 /* Capture the raw adc data */
Kjansen 7:054dbd5e1f45 301 if (read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw) != SUCCESS) {
Kjansen 7:054dbd5e1f45 302 return -EINVAL;
Kjansen 7:054dbd5e1f45 303 }
mahphalke 1:819ac9aa5667 304
mahphalke 1:819ac9aa5667 305 return (ssize_t) sprintf(buf, "%d", adc_data_raw);
mahphalke 1:819ac9aa5667 306 }
mahphalke 1:819ac9aa5667 307
Kjansen 6:32de160dce43 308 static ssize_t set_chn_raw(void *device,
Kjansen 6:32de160dce43 309 char *buf,
Kjansen 6:32de160dce43 310 size_t len,
Kjansen 6:32de160dce43 311 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 312 intptr_t id)
mahphalke 1:819ac9aa5667 313 {
mahphalke 1:819ac9aa5667 314 /* NA- Can't set raw value */
mahphalke 1:819ac9aa5667 315 return len;
mahphalke 1:819ac9aa5667 316 }
mahphalke 1:819ac9aa5667 317
mahphalke 1:819ac9aa5667 318
mahphalke 1:819ac9aa5667 319 /*!
mahphalke 1:819ac9aa5667 320 * @brief Getter/Setter for the operating mode attribute value
mahphalke 1:819ac9aa5667 321 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 322 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 323 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 324 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 325 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 326 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 327 */
Kjansen 6:32de160dce43 328 static ssize_t get_operating_mode(void *device,
Kjansen 6:32de160dce43 329 char *buf,
Kjansen 6:32de160dce43 330 size_t len,
Kjansen 6:32de160dce43 331 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 332 intptr_t id)
mahphalke 1:819ac9aa5667 333 {
mahphalke 1:819ac9aa5667 334 uint8_t read_val;
mahphalke 1:819ac9aa5667 335 uint8_t operating_mode_value;
mahphalke 1:819ac9aa5667 336
mahphalke 1:819ac9aa5667 337 if (ad7606_spi_reg_read(device, AD7606_REG_CONFIG, &read_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 338 operating_mode_value = (read_val & AD7606_CONFIG_OPERATION_MODE_MSK);
mahphalke 1:819ac9aa5667 339
mahphalke 1:819ac9aa5667 340 if (operating_mode_value < sizeof(operating_mode_str) / sizeof(
mahphalke 1:819ac9aa5667 341 operating_mode_str[0])) {
mahphalke 1:819ac9aa5667 342 return (ssize_t)sprintf(buf, "%s", operating_mode_str[operating_mode_value]);
mahphalke 1:819ac9aa5667 343 }
mahphalke 1:819ac9aa5667 344 }
mahphalke 1:819ac9aa5667 345
mahphalke 1:819ac9aa5667 346 return -EINVAL;
mahphalke 1:819ac9aa5667 347 }
mahphalke 1:819ac9aa5667 348
Kjansen 6:32de160dce43 349 static ssize_t set_operating_mode(void *device,
Kjansen 6:32de160dce43 350 char *buf,
Kjansen 6:32de160dce43 351 size_t len,
Kjansen 6:32de160dce43 352 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 353 intptr_t id)
mahphalke 1:819ac9aa5667 354 {
mahphalke 1:819ac9aa5667 355 uint8_t operating_mode_value;
mahphalke 1:819ac9aa5667 356
mahphalke 1:819ac9aa5667 357 (void)sscanf(buf, "%d", &operating_mode_value);
mahphalke 1:819ac9aa5667 358
mahphalke 1:819ac9aa5667 359 if (operating_mode_value < sizeof(operating_mode_str) / sizeof(
mahphalke 1:819ac9aa5667 360 operating_mode_str[0])) {
mahphalke 1:819ac9aa5667 361 if (ad7606_spi_write_mask(device,
mahphalke 1:819ac9aa5667 362 AD7606_REG_CONFIG,
mahphalke 1:819ac9aa5667 363 AD7606_CONFIG_OPERATION_MODE_MSK,
mahphalke 1:819ac9aa5667 364 operating_mode_value) == SUCCESS) {
mahphalke 1:819ac9aa5667 365 return len;
mahphalke 1:819ac9aa5667 366 }
mahphalke 1:819ac9aa5667 367 }
mahphalke 1:819ac9aa5667 368
mahphalke 1:819ac9aa5667 369 return -EINVAL;
mahphalke 1:819ac9aa5667 370 }
mahphalke 1:819ac9aa5667 371
mahphalke 1:819ac9aa5667 372
mahphalke 1:819ac9aa5667 373 /*!
mahphalke 1:819ac9aa5667 374 * @brief Getter/Setter for the power down mode attribute value
mahphalke 1:819ac9aa5667 375 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 376 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 377 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 378 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 379 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 380 * @note Available for all devices except AD7606B and AD7606C
mahphalke 1:819ac9aa5667 381 */
Kjansen 6:32de160dce43 382 static ssize_t get_power_down_mode(void *device,
Kjansen 6:32de160dce43 383 char *buf,
Kjansen 6:32de160dce43 384 size_t len,
Kjansen 6:32de160dce43 385 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 386 intptr_t id)
mahphalke 1:819ac9aa5667 387 {
mahphalke 1:819ac9aa5667 388 uint8_t gpio_stby_val;
mahphalke 1:819ac9aa5667 389 uint8_t gpio_range_val;
mahphalke 1:819ac9aa5667 390
mahphalke 1:819ac9aa5667 391 if (gpio_get_value(((struct ad7606_dev *)device)->gpio_stby_n,
mahphalke 1:819ac9aa5667 392 &gpio_stby_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 393 if (gpio_get_value(((struct ad7606_dev *)device)->gpio_range,
mahphalke 1:819ac9aa5667 394 &gpio_range_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 395
mahphalke 1:819ac9aa5667 396 if (gpio_stby_val) {
mahphalke 1:819ac9aa5667 397 return sprintf(buf, "%s", operating_mode_str[AD7606_NORMAL]);
mahphalke 1:819ac9aa5667 398 } else {
mahphalke 1:819ac9aa5667 399 if (gpio_range_val) {
mahphalke 1:819ac9aa5667 400 return sprintf(buf, "%s", operating_mode_str[AD7606_STANDBY]);
mahphalke 1:819ac9aa5667 401 } else {
mahphalke 1:819ac9aa5667 402 return sprintf(buf, "%s", operating_mode_str[AD7606_SHUTDOWN]);
mahphalke 1:819ac9aa5667 403 }
mahphalke 1:819ac9aa5667 404 }
mahphalke 1:819ac9aa5667 405 }
mahphalke 1:819ac9aa5667 406 }
mahphalke 1:819ac9aa5667 407
mahphalke 1:819ac9aa5667 408 return -EINVAL;
mahphalke 1:819ac9aa5667 409 }
mahphalke 1:819ac9aa5667 410
Kjansen 6:32de160dce43 411 static ssize_t set_power_down_mode(void *device,
Kjansen 6:32de160dce43 412 char *buf,
Kjansen 6:32de160dce43 413 size_t len,
Kjansen 6:32de160dce43 414 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 415 intptr_t id)
mahphalke 1:819ac9aa5667 416 {
mahphalke 1:819ac9aa5667 417 uint8_t power_down_mode_value;
mahphalke 1:819ac9aa5667 418 static enum ad7606_op_mode prev_power_down_mode = AD7606_NORMAL;
mahphalke 1:819ac9aa5667 419 struct ad7606_config dev_config;
mahphalke 1:819ac9aa5667 420
mahphalke 1:819ac9aa5667 421 sscanf(buf, "%d", &power_down_mode_value);
mahphalke 1:819ac9aa5667 422
mahphalke 1:819ac9aa5667 423 if (power_down_mode_value < (sizeof(operating_mode_str) / sizeof(
mahphalke 1:819ac9aa5667 424 operating_mode_str[0]))) {
mahphalke 1:819ac9aa5667 425
mahphalke 1:819ac9aa5667 426 dev_config.op_mode = power_down_mode_value;
mahphalke 1:819ac9aa5667 427
mahphalke 1:819ac9aa5667 428 switch (power_down_mode_value) {
mahphalke 1:819ac9aa5667 429 case AD7606_NORMAL:
mahphalke 1:819ac9aa5667 430 if (ad7606_set_config(device, dev_config) == SUCCESS) {
mahphalke 1:819ac9aa5667 431 /* Reset the device if previous power down mode was either standby
mahphalke 1:819ac9aa5667 432 * or shutdown */
mahphalke 1:819ac9aa5667 433 if (prev_power_down_mode != AD7606_NORMAL) {
mahphalke 1:819ac9aa5667 434
mahphalke 1:819ac9aa5667 435 /* Power-up wait time */
mahphalke 1:819ac9aa5667 436 mdelay(1);
mahphalke 1:819ac9aa5667 437
mahphalke 1:819ac9aa5667 438 /* Toggle reset pin */
mahphalke 1:819ac9aa5667 439 if (gpio_set_value(((struct ad7606_dev *)device)->gpio_reset,
mahphalke 1:819ac9aa5667 440 GPIO_HIGH) == SUCCESS) {
mahphalke 1:819ac9aa5667 441 mdelay(1);
mahphalke 1:819ac9aa5667 442
mahphalke 1:819ac9aa5667 443 if (gpio_set_value(((struct ad7606_dev *)device)->gpio_reset,
mahphalke 1:819ac9aa5667 444 GPIO_LOW) == SUCCESS) {
mahphalke 1:819ac9aa5667 445 prev_power_down_mode = AD7606_NORMAL;
mahphalke 1:819ac9aa5667 446 return len;
mahphalke 1:819ac9aa5667 447 }
mahphalke 1:819ac9aa5667 448 }
mahphalke 1:819ac9aa5667 449 }
mahphalke 1:819ac9aa5667 450 }
mahphalke 1:819ac9aa5667 451 break;
mahphalke 1:819ac9aa5667 452
mahphalke 1:819ac9aa5667 453 case AD7606_STANDBY:
mahphalke 1:819ac9aa5667 454 if (ad7606_set_config(device, dev_config) == SUCCESS) {
mahphalke 1:819ac9aa5667 455 prev_power_down_mode = AD7606_STANDBY;
mahphalke 1:819ac9aa5667 456 return len;
mahphalke 1:819ac9aa5667 457 }
mahphalke 1:819ac9aa5667 458 break;
mahphalke 1:819ac9aa5667 459
mahphalke 1:819ac9aa5667 460 case AD7606_SHUTDOWN:
mahphalke 1:819ac9aa5667 461 if (ad7606_set_config(device, dev_config) == SUCCESS) {
mahphalke 1:819ac9aa5667 462 prev_power_down_mode = AD7606_SHUTDOWN;
mahphalke 1:819ac9aa5667 463 return len;
mahphalke 1:819ac9aa5667 464 }
mahphalke 1:819ac9aa5667 465 break;
mahphalke 1:819ac9aa5667 466
mahphalke 1:819ac9aa5667 467 default:
mahphalke 1:819ac9aa5667 468 break;
mahphalke 1:819ac9aa5667 469 }
mahphalke 1:819ac9aa5667 470 }
mahphalke 1:819ac9aa5667 471
mahphalke 1:819ac9aa5667 472 return -EINVAL;
mahphalke 1:819ac9aa5667 473 }
mahphalke 1:819ac9aa5667 474
mahphalke 1:819ac9aa5667 475
mahphalke 1:819ac9aa5667 476 /*!
mahphalke 1:819ac9aa5667 477 * @brief Getter/Setter for the range attribute value
mahphalke 1:819ac9aa5667 478 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 479 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 480 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 481 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 482 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 483 * @note Available for all devices except AD7606B and AD7606C
mahphalke 1:819ac9aa5667 484 */
Kjansen 6:32de160dce43 485 static ssize_t get_range(void *device,
Kjansen 6:32de160dce43 486 char *buf,
Kjansen 6:32de160dce43 487 size_t len,
Kjansen 6:32de160dce43 488 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 489 intptr_t id)
mahphalke 1:819ac9aa5667 490 {
mahphalke 1:819ac9aa5667 491 uint8_t gpio_range_val;
mahphalke 1:819ac9aa5667 492 struct ad7606_dev *dev = device;
mahphalke 1:819ac9aa5667 493
mahphalke 1:819ac9aa5667 494 if (gpio_get_value(dev->gpio_range, &gpio_range_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 495 if (gpio_range_val) {
mahphalke 1:819ac9aa5667 496 return sprintf(buf, "%s", chn_range_str[HIGH]);
mahphalke 1:819ac9aa5667 497 } else {
mahphalke 1:819ac9aa5667 498 return sprintf(buf, "%s", chn_range_str[LOW]);
mahphalke 1:819ac9aa5667 499 }
mahphalke 1:819ac9aa5667 500 }
mahphalke 1:819ac9aa5667 501
mahphalke 1:819ac9aa5667 502 return -EINVAL;
mahphalke 1:819ac9aa5667 503 }
mahphalke 1:819ac9aa5667 504
Kjansen 6:32de160dce43 505 static ssize_t set_range(void *device,
Kjansen 6:32de160dce43 506 char *buf,
Kjansen 6:32de160dce43 507 size_t len,
Kjansen 6:32de160dce43 508 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 509 intptr_t id)
mahphalke 1:819ac9aa5667 510 {
mahphalke 1:819ac9aa5667 511 uint8_t range_value;
mahphalke 1:819ac9aa5667 512 struct ad7606_dev *dev = device;
mahphalke 1:819ac9aa5667 513
mahphalke 1:819ac9aa5667 514 (void)sscanf(buf, "%d", &range_value);
mahphalke 1:819ac9aa5667 515
mahphalke 1:819ac9aa5667 516 if (range_value < (sizeof(chn_range_str) / sizeof(chn_range_str[0]))) {
mahphalke 1:819ac9aa5667 517 if (range_value == LOW) {
mahphalke 1:819ac9aa5667 518 if (gpio_set_value(dev->gpio_range, GPIO_LOW) == SUCCESS) {
mahphalke 1:819ac9aa5667 519 return len;
mahphalke 1:819ac9aa5667 520 }
mahphalke 1:819ac9aa5667 521 } else {
mahphalke 1:819ac9aa5667 522 if (gpio_set_value(dev->gpio_range, GPIO_HIGH) == SUCCESS) {
mahphalke 1:819ac9aa5667 523 return len;
mahphalke 1:819ac9aa5667 524 }
mahphalke 1:819ac9aa5667 525 }
mahphalke 1:819ac9aa5667 526 }
mahphalke 1:819ac9aa5667 527
mahphalke 1:819ac9aa5667 528 return -EINVAL;
mahphalke 1:819ac9aa5667 529 }
mahphalke 1:819ac9aa5667 530
mahphalke 1:819ac9aa5667 531
mahphalke 1:819ac9aa5667 532 /*!
mahphalke 1:819ac9aa5667 533 * @brief Getter/Setter for the oversampling attribute value
mahphalke 1:819ac9aa5667 534 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 535 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 536 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 537 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 538 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 539 * @note Available for all devices except AD7606B and AD7606C
mahphalke 1:819ac9aa5667 540 */
Kjansen 6:32de160dce43 541 static ssize_t get_oversampling(void *device,
Kjansen 6:32de160dce43 542 char *buf,
Kjansen 6:32de160dce43 543 size_t len,
Kjansen 6:32de160dce43 544 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 545 intptr_t id)
mahphalke 1:819ac9aa5667 546 {
mahphalke 1:819ac9aa5667 547 uint8_t oversampling_value;
mahphalke 1:819ac9aa5667 548 uint8_t read_val;
mahphalke 1:819ac9aa5667 549 uint8_t gpio_osr0_val;
mahphalke 1:819ac9aa5667 550 uint8_t gpio_osr1_val;
mahphalke 1:819ac9aa5667 551 uint8_t gpio_osr2_val;
mahphalke 1:819ac9aa5667 552 struct ad7606_dev *dev = device;
mahphalke 1:819ac9aa5667 553
mahphalke 1:819ac9aa5667 554 #if defined(DEV_AD7606B) || defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
mahphalke 1:819ac9aa5667 555 if (ad7606_spi_reg_read(device,
mahphalke 1:819ac9aa5667 556 AD7606_REG_OVERSAMPLING,
mahphalke 1:819ac9aa5667 557 &read_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 558 oversampling_value = (read_val & AD7606_OVERSAMPLING_MSK);
mahphalke 1:819ac9aa5667 559
mahphalke 1:819ac9aa5667 560 if (oversampling_value < sizeof(oversampling_val_str) / sizeof(
mahphalke 1:819ac9aa5667 561 oversampling_val_str[0])) {
mahphalke 1:819ac9aa5667 562 return (ssize_t)sprintf(buf, "%s", oversampling_val_str[oversampling_value]);
mahphalke 1:819ac9aa5667 563 }
mahphalke 1:819ac9aa5667 564 }
mahphalke 1:819ac9aa5667 565 #else
mahphalke 1:819ac9aa5667 566 if (gpio_get_value(dev->gpio_os0, &gpio_osr0_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 567 if (gpio_get_value(dev->gpio_os1, &gpio_osr1_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 568 if (gpio_get_value(dev->gpio_os2, &gpio_osr2_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 569 oversampling_value = (gpio_osr2_val << 2) | (gpio_osr1_val << 1) |
mahphalke 1:819ac9aa5667 570 gpio_osr0_val;
mahphalke 1:819ac9aa5667 571
mahphalke 1:819ac9aa5667 572 if (oversampling_value < (sizeof(oversampling_val_str) / sizeof(
mahphalke 1:819ac9aa5667 573 oversampling_val_str[0]))) {
mahphalke 1:819ac9aa5667 574 return sprintf(buf, "%s", oversampling_val_str[oversampling_value]);
mahphalke 1:819ac9aa5667 575 }
mahphalke 1:819ac9aa5667 576 }
mahphalke 1:819ac9aa5667 577 }
mahphalke 1:819ac9aa5667 578 }
mahphalke 1:819ac9aa5667 579 #endif
mahphalke 1:819ac9aa5667 580
mahphalke 1:819ac9aa5667 581 return -EINVAL;
mahphalke 1:819ac9aa5667 582 }
mahphalke 1:819ac9aa5667 583
Kjansen 6:32de160dce43 584 static ssize_t set_oversampling(void *device,
Kjansen 6:32de160dce43 585 char *buf,
Kjansen 6:32de160dce43 586 size_t len,
Kjansen 6:32de160dce43 587 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 588 intptr_t id)
mahphalke 1:819ac9aa5667 589 {
mahphalke 1:819ac9aa5667 590 uint8_t oversampling_value;
mahphalke 1:819ac9aa5667 591 struct ad7606_oversampling oversampling_cfg;
mahphalke 1:819ac9aa5667 592
mahphalke 1:819ac9aa5667 593 (void)sscanf(buf, "%d", &oversampling_value);
mahphalke 1:819ac9aa5667 594
mahphalke 1:819ac9aa5667 595 if (oversampling_value < (sizeof(oversampling_val_str) / sizeof(
mahphalke 1:819ac9aa5667 596 oversampling_val_str[0]))) {
mahphalke 1:819ac9aa5667 597
mahphalke 1:819ac9aa5667 598 oversampling_cfg.os_pad = 0;
mahphalke 1:819ac9aa5667 599 oversampling_cfg.os_ratio = oversampling_value;
mahphalke 1:819ac9aa5667 600
mahphalke 1:819ac9aa5667 601 ad7606_set_oversampling(device, oversampling_cfg);
mahphalke 1:819ac9aa5667 602
mahphalke 1:819ac9aa5667 603 return len;
mahphalke 1:819ac9aa5667 604 }
mahphalke 1:819ac9aa5667 605
mahphalke 1:819ac9aa5667 606 return -EINVAL;
mahphalke 1:819ac9aa5667 607 }
mahphalke 1:819ac9aa5667 608
mahphalke 1:819ac9aa5667 609
mahphalke 1:819ac9aa5667 610 /*!
mahphalke 1:819ac9aa5667 611 * @brief Getter/Setter for the bandwidth attribute value
mahphalke 1:819ac9aa5667 612 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 613 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 614 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 615 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 616 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 617 * @note Available for only AD7606C
mahphalke 1:819ac9aa5667 618 */
Kjansen 6:32de160dce43 619 static ssize_t get_bandwidth(void *device,
Kjansen 6:32de160dce43 620 char *buf,
Kjansen 6:32de160dce43 621 size_t len,
Kjansen 6:32de160dce43 622 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 623 intptr_t id)
mahphalke 1:819ac9aa5667 624 {
mahphalke 1:819ac9aa5667 625 uint8_t bw_value;
mahphalke 1:819ac9aa5667 626 uint8_t read_val;
mahphalke 1:819ac9aa5667 627
mahphalke 1:819ac9aa5667 628 if (ad7606_spi_reg_read(device,
mahphalke 1:819ac9aa5667 629 AD7606_REG_BANDWIDTH,
mahphalke 1:819ac9aa5667 630 &read_val) == SUCCESS) {
Kjansen 6:32de160dce43 631 bw_value = (read_val >> (channel->ch_num)) & 0x1;
mahphalke 1:819ac9aa5667 632
mahphalke 1:819ac9aa5667 633 if (bw_value < sizeof(bandwidth_str) / sizeof(
mahphalke 1:819ac9aa5667 634 bandwidth_str[0])) {
mahphalke 1:819ac9aa5667 635 return (ssize_t)sprintf(buf, "%s", bandwidth_str[bw_value]);
mahphalke 1:819ac9aa5667 636 }
mahphalke 1:819ac9aa5667 637 }
mahphalke 1:819ac9aa5667 638
mahphalke 1:819ac9aa5667 639 return -EINVAL;
mahphalke 1:819ac9aa5667 640 }
mahphalke 1:819ac9aa5667 641
Kjansen 6:32de160dce43 642 static ssize_t set_bandwidth(void *device,
Kjansen 6:32de160dce43 643 char *buf,
Kjansen 6:32de160dce43 644 size_t len,
Kjansen 6:32de160dce43 645 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 646 intptr_t id)
mahphalke 1:819ac9aa5667 647 {
mahphalke 1:819ac9aa5667 648 uint8_t bw_value;
mahphalke 1:819ac9aa5667 649 uint8_t read_val;
mahphalke 1:819ac9aa5667 650
mahphalke 1:819ac9aa5667 651 (void)sscanf(buf, "%d", &bw_value);
mahphalke 1:819ac9aa5667 652
mahphalke 1:819ac9aa5667 653 if (bw_value < sizeof(bandwidth_str) / sizeof(
mahphalke 1:819ac9aa5667 654 bandwidth_str[0])) {
mahphalke 1:819ac9aa5667 655 if (ad7606_spi_reg_read(device,
mahphalke 1:819ac9aa5667 656 AD7606_REG_BANDWIDTH,
mahphalke 1:819ac9aa5667 657 &read_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 658 if (bw_value) {
Kjansen 6:32de160dce43 659 read_val |= (1 << (channel->ch_num));
mahphalke 1:819ac9aa5667 660 } else {
Kjansen 6:32de160dce43 661 read_val &= (~(1 << (channel->ch_num)));
mahphalke 1:819ac9aa5667 662 }
mahphalke 1:819ac9aa5667 663
mahphalke 1:819ac9aa5667 664 if (ad7606_spi_reg_write(device,
mahphalke 1:819ac9aa5667 665 AD7606_REG_BANDWIDTH,
mahphalke 1:819ac9aa5667 666 read_val) == SUCCESS) {
mahphalke 1:819ac9aa5667 667 return len;
mahphalke 1:819ac9aa5667 668 }
mahphalke 1:819ac9aa5667 669 }
mahphalke 1:819ac9aa5667 670 }
mahphalke 1:819ac9aa5667 671
mahphalke 1:819ac9aa5667 672 return -EINVAL;
mahphalke 1:819ac9aa5667 673 }
mahphalke 1:819ac9aa5667 674
mahphalke 1:819ac9aa5667 675
mahphalke 1:819ac9aa5667 676 /*!
mahphalke 1:819ac9aa5667 677 * @brief Getter/Setter for the channel range attribute value
mahphalke 1:819ac9aa5667 678 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 679 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 680 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 681 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 682 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 683 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 684 */
Kjansen 6:32de160dce43 685 static ssize_t get_chn_range(void *device,
Kjansen 6:32de160dce43 686 char *buf,
Kjansen 6:32de160dce43 687 size_t len,
Kjansen 6:32de160dce43 688 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 689 intptr_t id)
mahphalke 1:819ac9aa5667 690 {
mahphalke 1:819ac9aa5667 691 uint8_t read_val;
mahphalke 1:819ac9aa5667 692 uint8_t chn_range;
mahphalke 1:819ac9aa5667 693
Kjansen 6:32de160dce43 694 if (ad7606_spi_reg_read(device, AD7606_REG_RANGE_CH_ADDR(channel->ch_num),
mahphalke 1:819ac9aa5667 695 &read_val) == SUCCESS) {
Kjansen 6:32de160dce43 696 if (((channel->ch_num) % 2) != 0) {
mahphalke 1:819ac9aa5667 697 read_val >>= CHANNEL_RANGE_MSK_OFFSET;
mahphalke 1:819ac9aa5667 698 chn_range = read_val;
mahphalke 1:819ac9aa5667 699 } else {
Kjansen 6:32de160dce43 700 chn_range = (read_val & AD7606_RANGE_CH_MSK(channel->ch_num));
mahphalke 1:819ac9aa5667 701 }
mahphalke 1:819ac9aa5667 702
mahphalke 1:819ac9aa5667 703 if (chn_range < sizeof(chn_range_str) / sizeof(chn_range_str[0])) {
Kjansen 6:32de160dce43 704 attr_chn_range[channel->ch_num] = chn_range_val[chn_range];
Kjansen 6:32de160dce43 705 attr_polarity_val[channel->ch_num] = ad7606_get_input_polarity(chn_range);
mahphalke 1:819ac9aa5667 706
mahphalke 1:819ac9aa5667 707 return (ssize_t)sprintf(buf, "%s", chn_range_str[chn_range]);
mahphalke 1:819ac9aa5667 708 }
mahphalke 1:819ac9aa5667 709 }
mahphalke 1:819ac9aa5667 710
mahphalke 1:819ac9aa5667 711 return -EINVAL;
mahphalke 1:819ac9aa5667 712 }
mahphalke 1:819ac9aa5667 713
Kjansen 6:32de160dce43 714 static ssize_t set_chn_range(void *device,
Kjansen 6:32de160dce43 715 char *buf,
Kjansen 6:32de160dce43 716 size_t len,
Kjansen 6:32de160dce43 717 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 718 intptr_t id)
mahphalke 1:819ac9aa5667 719 {
mahphalke 1:819ac9aa5667 720 uint8_t chn_range;
mahphalke 1:819ac9aa5667 721
mahphalke 1:819ac9aa5667 722 (void)sscanf(buf, "%d", &chn_range);
mahphalke 1:819ac9aa5667 723
mahphalke 1:819ac9aa5667 724 if (chn_range < sizeof(chn_range_val) / sizeof(chn_range_val[0])) {
mahphalke 1:819ac9aa5667 725
mahphalke 1:819ac9aa5667 726 /* Get the polarity of channel */
Kjansen 6:32de160dce43 727 attr_polarity_val[channel->ch_num] = ad7606_get_input_polarity(chn_range);
mahphalke 1:819ac9aa5667 728
Kjansen 6:32de160dce43 729 attr_chn_range[channel->ch_num] = chn_range_val[chn_range];
Kjansen 6:32de160dce43 730 attr_scale_val[channel->ch_num] = get_vltg_conv_scale_factor(
mahphalke 1:819ac9aa5667 731 chn_range_val[chn_range],
Kjansen 6:32de160dce43 732 attr_polarity_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 733
Kjansen 6:32de160dce43 734 if (((channel->ch_num) % 2) != 0) {
mahphalke 1:819ac9aa5667 735 chn_range <<= CHANNEL_RANGE_MSK_OFFSET;
mahphalke 1:819ac9aa5667 736 }
mahphalke 1:819ac9aa5667 737
mahphalke 1:819ac9aa5667 738 if (ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 739 AD7606_REG_RANGE_CH_ADDR(channel->ch_num),
Kjansen 6:32de160dce43 740 AD7606_RANGE_CH_MSK(channel->ch_num),
mahphalke 1:819ac9aa5667 741 chn_range) == SUCCESS) {
mahphalke 1:819ac9aa5667 742 return len;
mahphalke 1:819ac9aa5667 743 }
mahphalke 1:819ac9aa5667 744 }
mahphalke 1:819ac9aa5667 745
mahphalke 1:819ac9aa5667 746 return -EINVAL;
mahphalke 1:819ac9aa5667 747 }
mahphalke 1:819ac9aa5667 748
mahphalke 1:819ac9aa5667 749
mahphalke 1:819ac9aa5667 750 /*!
mahphalke 1:819ac9aa5667 751 * @brief Getter/Setter for the channel offset attribute value
mahphalke 1:819ac9aa5667 752 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 753 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 754 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 755 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 756 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 757 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 758 */
Kjansen 6:32de160dce43 759 static ssize_t get_chn_offset(void *device,
Kjansen 6:32de160dce43 760 char *buf,
Kjansen 6:32de160dce43 761 size_t len,
Kjansen 6:32de160dce43 762 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 763 intptr_t id)
mahphalke 1:819ac9aa5667 764 {
mahphalke 1:819ac9aa5667 765 uint8_t chn_offset_value;
mahphalke 1:819ac9aa5667 766
Kjansen 6:32de160dce43 767 if (ad7606_spi_reg_read(device, AD7606_REG_OFFSET_CH(channel->ch_num),
mahphalke 1:819ac9aa5667 768 &chn_offset_value) == SUCCESS) {
mahphalke 1:819ac9aa5667 769 return (ssize_t)sprintf(buf, "%d", chn_offset_value);
mahphalke 1:819ac9aa5667 770 }
mahphalke 1:819ac9aa5667 771
mahphalke 1:819ac9aa5667 772 return -EINVAL;
mahphalke 1:819ac9aa5667 773 }
mahphalke 1:819ac9aa5667 774
Kjansen 6:32de160dce43 775 static ssize_t set_chn_offset(void *device,
Kjansen 6:32de160dce43 776 char *buf,
Kjansen 6:32de160dce43 777 size_t len,
Kjansen 6:32de160dce43 778 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 779 intptr_t id)
mahphalke 1:819ac9aa5667 780 {
mahphalke 1:819ac9aa5667 781 uint8_t chn_offset_value = 0;
mahphalke 1:819ac9aa5667 782
mahphalke 1:819ac9aa5667 783 (void)sscanf(buf, "%d", &chn_offset_value);
mahphalke 1:819ac9aa5667 784
Kjansen 6:32de160dce43 785 if (ad7606_set_ch_offset(device, channel->ch_num,
mahphalke 1:819ac9aa5667 786 chn_offset_value) == SUCCESS) {
mahphalke 1:819ac9aa5667 787 return len;
mahphalke 1:819ac9aa5667 788 }
mahphalke 1:819ac9aa5667 789
mahphalke 1:819ac9aa5667 790 return -EINVAL;
mahphalke 1:819ac9aa5667 791 }
mahphalke 1:819ac9aa5667 792
mahphalke 1:819ac9aa5667 793
mahphalke 1:819ac9aa5667 794 /*!
mahphalke 1:819ac9aa5667 795 * @brief Getter/Setter for the channel pahse offset attribute value
mahphalke 1:819ac9aa5667 796 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 797 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 798 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 799 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 800 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 801 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 802 */
Kjansen 6:32de160dce43 803 static ssize_t get_chn_phase_offset(void *device,
Kjansen 6:32de160dce43 804 char *buf,
Kjansen 6:32de160dce43 805 size_t len,
Kjansen 6:32de160dce43 806 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 807 intptr_t id)
mahphalke 1:819ac9aa5667 808 {
mahphalke 1:819ac9aa5667 809 uint8_t chn_phase_offset_value;
mahphalke 1:819ac9aa5667 810
mahphalke 1:819ac9aa5667 811 if (ad7606_spi_reg_read(device,
Kjansen 6:32de160dce43 812 AD7606_REG_PHASE_CH(channel->ch_num),
mahphalke 1:819ac9aa5667 813 &chn_phase_offset_value) == SUCCESS) {
mahphalke 1:819ac9aa5667 814 return (ssize_t)sprintf(buf, "%d", chn_phase_offset_value);
mahphalke 1:819ac9aa5667 815 }
mahphalke 1:819ac9aa5667 816
mahphalke 1:819ac9aa5667 817 return -EINVAL;
mahphalke 1:819ac9aa5667 818 }
mahphalke 1:819ac9aa5667 819
Kjansen 6:32de160dce43 820 static ssize_t set_chn_phase_offset(void *device,
Kjansen 6:32de160dce43 821 char *buf,
Kjansen 6:32de160dce43 822 size_t len,
Kjansen 6:32de160dce43 823 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 824 intptr_t id)
mahphalke 1:819ac9aa5667 825 {
mahphalke 1:819ac9aa5667 826 uint8_t chn_phase_offset_value = 0;
mahphalke 1:819ac9aa5667 827
mahphalke 1:819ac9aa5667 828 (void)sscanf(buf, "%d", &chn_phase_offset_value);
mahphalke 1:819ac9aa5667 829
Kjansen 6:32de160dce43 830 if (ad7606_set_ch_phase(device, channel->ch_num,
mahphalke 1:819ac9aa5667 831 chn_phase_offset_value) == SUCCESS) {
mahphalke 1:819ac9aa5667 832 return len;
mahphalke 1:819ac9aa5667 833 }
mahphalke 1:819ac9aa5667 834
mahphalke 1:819ac9aa5667 835 return -EINVAL;
mahphalke 1:819ac9aa5667 836 }
mahphalke 1:819ac9aa5667 837
mahphalke 1:819ac9aa5667 838
mahphalke 1:819ac9aa5667 839 /*!
mahphalke 1:819ac9aa5667 840 * @brief Getter/Setter for the channel temperature attribute value
mahphalke 1:819ac9aa5667 841 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 842 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 843 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 844 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 845 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 846 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 847 */
Kjansen 6:32de160dce43 848 static ssize_t get_chn_temperature(void *device,
Kjansen 6:32de160dce43 849 char *buf,
Kjansen 6:32de160dce43 850 size_t len,
Kjansen 6:32de160dce43 851 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 852 intptr_t id)
mahphalke 1:819ac9aa5667 853 {
Kjansen 7:054dbd5e1f45 854 int32_t adc_data_raw = 0;
mahphalke 1:819ac9aa5667 855 float temperature;
mahphalke 1:819ac9aa5667 856 float voltage;
mahphalke 1:819ac9aa5667 857
mahphalke 1:819ac9aa5667 858 /* Configure the channel multiplexer to select temperature read */
mahphalke 1:819ac9aa5667 859 if (ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 860 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 861 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 862 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 863 TEMPERATURE_MUX)) == SUCCESS) {
mahphalke 1:819ac9aa5667 864
mahphalke 1:819ac9aa5667 865 /* Allow to settle Mux channel */
mahphalke 1:819ac9aa5667 866 udelay(100);
mahphalke 1:819ac9aa5667 867
mahphalke 1:819ac9aa5667 868 /* Sample the channel and read conversion result */
Kjansen 7:054dbd5e1f45 869 read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw);
mahphalke 1:819ac9aa5667 870
mahphalke 1:819ac9aa5667 871 /* Convert ADC data into equivalent voltage */
Kjansen 7:054dbd5e1f45 872 voltage = convert_adc_raw_to_voltage(adc_data_raw,
Kjansen 6:32de160dce43 873 attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 874
mahphalke 1:819ac9aa5667 875 /* Obtain the temperature using equation specified in device datasheet */
mahphalke 1:819ac9aa5667 876 temperature = ((voltage - 0.69068) / 0.019328) + 25.0;
mahphalke 1:819ac9aa5667 877
mahphalke 1:819ac9aa5667 878 /* Change channel mux back to analog input */
mahphalke 1:819ac9aa5667 879 (void)ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 880 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 881 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 882 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 883 ANALOG_INPUT_MUX));
mahphalke 1:819ac9aa5667 884
mahphalke 1:819ac9aa5667 885 return (ssize_t)sprintf(buf, "%f", temperature);
mahphalke 1:819ac9aa5667 886 }
mahphalke 1:819ac9aa5667 887
mahphalke 1:819ac9aa5667 888 return -EINVAL;
mahphalke 1:819ac9aa5667 889 }
mahphalke 1:819ac9aa5667 890
Kjansen 6:32de160dce43 891 static ssize_t set_chn_temperature(void *device,
Kjansen 6:32de160dce43 892 char *buf,
Kjansen 6:32de160dce43 893 size_t len,
Kjansen 6:32de160dce43 894 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 895 intptr_t id)
mahphalke 1:819ac9aa5667 896 {
mahphalke 1:819ac9aa5667 897 // NA- Can't set temperature
mahphalke 1:819ac9aa5667 898 return -EINVAL;
mahphalke 1:819ac9aa5667 899 }
mahphalke 1:819ac9aa5667 900
mahphalke 1:819ac9aa5667 901
mahphalke 1:819ac9aa5667 902 /*!
mahphalke 1:819ac9aa5667 903 * @brief Getter/Setter for the channel Vref attribute value
mahphalke 1:819ac9aa5667 904 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 905 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 906 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 907 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 908 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 909 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 910 */
Kjansen 6:32de160dce43 911 static ssize_t get_chn_vref(void *device,
Kjansen 6:32de160dce43 912 char *buf,
Kjansen 6:32de160dce43 913 size_t len,
Kjansen 6:32de160dce43 914 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 915 intptr_t id)
mahphalke 1:819ac9aa5667 916 {
mahphalke 1:819ac9aa5667 917 float vref_voltge;
Kjansen 7:054dbd5e1f45 918 int32_t adc_data_raw;
mahphalke 1:819ac9aa5667 919
mahphalke 1:819ac9aa5667 920 /* Configure the channel multiplexer to select Vref read */
mahphalke 1:819ac9aa5667 921 if (ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 922 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 923 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 924 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 925 VREF_MUX)) == SUCCESS) {
mahphalke 1:819ac9aa5667 926
mahphalke 1:819ac9aa5667 927 /* Allow to settle Mux channel */
mahphalke 1:819ac9aa5667 928 udelay(100);
mahphalke 1:819ac9aa5667 929
mahphalke 1:819ac9aa5667 930 /* Sample the channel and read conversion result */
Kjansen 7:054dbd5e1f45 931 read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw);
mahphalke 1:819ac9aa5667 932
mahphalke 1:819ac9aa5667 933 /* Convert ADC data into equivalent voltage */
Kjansen 7:054dbd5e1f45 934 vref_voltge = convert_adc_raw_to_voltage(adc_data_raw,
Kjansen 6:32de160dce43 935 attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 936
mahphalke 1:819ac9aa5667 937 /* Divide by 4 since Vref Mux has 4x multiplier on it */
mahphalke 1:819ac9aa5667 938 vref_voltge /= VREF_MUX_MULTIPLIER;
mahphalke 1:819ac9aa5667 939
mahphalke 1:819ac9aa5667 940 /* Change channel mux back to analog input */
mahphalke 1:819ac9aa5667 941 (void)ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 942 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 943 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 944 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 945 ANALOG_INPUT_MUX));
mahphalke 1:819ac9aa5667 946
mahphalke 1:819ac9aa5667 947 return (ssize_t)sprintf(buf, "%f", vref_voltge);
mahphalke 1:819ac9aa5667 948 }
mahphalke 1:819ac9aa5667 949
mahphalke 1:819ac9aa5667 950 return -EINVAL;
mahphalke 1:819ac9aa5667 951 }
mahphalke 1:819ac9aa5667 952
Kjansen 6:32de160dce43 953 static ssize_t set_chn_vref(void *device,
Kjansen 6:32de160dce43 954 char *buf,
Kjansen 6:32de160dce43 955 size_t len,
Kjansen 6:32de160dce43 956 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 957 intptr_t id)
mahphalke 1:819ac9aa5667 958 {
mahphalke 1:819ac9aa5667 959 // NA- Can't set Vref
mahphalke 1:819ac9aa5667 960 return - EINVAL;
mahphalke 1:819ac9aa5667 961 }
mahphalke 1:819ac9aa5667 962
mahphalke 1:819ac9aa5667 963
mahphalke 1:819ac9aa5667 964 /*!
mahphalke 1:819ac9aa5667 965 * @brief Getter/Setter for the channel Vdrive attribute value
mahphalke 1:819ac9aa5667 966 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 967 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 968 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 969 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 970 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 971 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 972 */
Kjansen 6:32de160dce43 973 static ssize_t get_chn_vdrive(void *device,
Kjansen 6:32de160dce43 974 char *buf,
Kjansen 6:32de160dce43 975 size_t len,
Kjansen 6:32de160dce43 976 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 977 intptr_t id)
mahphalke 1:819ac9aa5667 978 {
mahphalke 1:819ac9aa5667 979 float vdrive_voltge;
Kjansen 7:054dbd5e1f45 980 int32_t adc_data_raw;
mahphalke 1:819ac9aa5667 981
mahphalke 1:819ac9aa5667 982 /* Configure the channel multiplexer to select Vdrive read */
mahphalke 1:819ac9aa5667 983 if (ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 984 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 985 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 986 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 987 VDRIVE_MUX)) == SUCCESS) {
mahphalke 1:819ac9aa5667 988
mahphalke 1:819ac9aa5667 989 /* Allow to settle Mux channel */
mahphalke 1:819ac9aa5667 990 udelay(100);
mahphalke 1:819ac9aa5667 991
mahphalke 1:819ac9aa5667 992 /* Sample the channel and read conversion result */
Kjansen 7:054dbd5e1f45 993 read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw);
mahphalke 1:819ac9aa5667 994
mahphalke 1:819ac9aa5667 995 /* Convert ADC data into equivalent voltage */
Kjansen 7:054dbd5e1f45 996 vdrive_voltge = convert_adc_raw_to_voltage(adc_data_raw,
Kjansen 6:32de160dce43 997 attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 998
mahphalke 1:819ac9aa5667 999 /* Change channel mux back to analog input */
mahphalke 1:819ac9aa5667 1000 (void)ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 1001 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 1002 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 1003 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 1004 ANALOG_INPUT_MUX));
mahphalke 1:819ac9aa5667 1005
mahphalke 1:819ac9aa5667 1006 return (ssize_t)sprintf(buf, "%f", vdrive_voltge);
mahphalke 1:819ac9aa5667 1007 }
mahphalke 1:819ac9aa5667 1008
mahphalke 1:819ac9aa5667 1009 return -EINVAL;
mahphalke 1:819ac9aa5667 1010 }
mahphalke 1:819ac9aa5667 1011
Kjansen 6:32de160dce43 1012 static ssize_t set_chn_vdrive(void *device,
Kjansen 6:32de160dce43 1013 char *buf,
Kjansen 6:32de160dce43 1014 size_t len,
Kjansen 6:32de160dce43 1015 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1016 intptr_t id)
mahphalke 1:819ac9aa5667 1017 {
mahphalke 1:819ac9aa5667 1018 // NA- Can't set Vdrive
mahphalke 1:819ac9aa5667 1019 return - EINVAL;
mahphalke 1:819ac9aa5667 1020 }
mahphalke 1:819ac9aa5667 1021
mahphalke 1:819ac9aa5667 1022
mahphalke 1:819ac9aa5667 1023 /*!
mahphalke 1:819ac9aa5667 1024 * @brief Getter/Setter for the channel ALDO attribute value
mahphalke 1:819ac9aa5667 1025 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 1026 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 1027 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 1028 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 1029 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 1030 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 1031 */
Kjansen 6:32de160dce43 1032 static ssize_t get_chn_aldo(void *device,
Kjansen 6:32de160dce43 1033 char *buf,
Kjansen 6:32de160dce43 1034 size_t len,
Kjansen 6:32de160dce43 1035 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1036 intptr_t id)
mahphalke 1:819ac9aa5667 1037 {
mahphalke 1:819ac9aa5667 1038 float aldo_voltge;
Kjansen 7:054dbd5e1f45 1039 int32_t adc_data_raw;
mahphalke 1:819ac9aa5667 1040
mahphalke 1:819ac9aa5667 1041 /* Configure the channel multiplexer to select ALDO read */
mahphalke 1:819ac9aa5667 1042 if (ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 1043 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 1044 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 1045 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 1046 ALDO_MUX)) == SUCCESS) {
mahphalke 1:819ac9aa5667 1047
mahphalke 1:819ac9aa5667 1048 /* Allow to settle Mux channel */
mahphalke 1:819ac9aa5667 1049 udelay(100);
mahphalke 1:819ac9aa5667 1050
mahphalke 1:819ac9aa5667 1051 /* Sample the channel and read conversion result */
Kjansen 7:054dbd5e1f45 1052 read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw);
mahphalke 1:819ac9aa5667 1053
mahphalke 1:819ac9aa5667 1054 /* Convert ADC data into equivalent voltage */
Kjansen 7:054dbd5e1f45 1055 aldo_voltge = convert_adc_raw_to_voltage(adc_data_raw,
Kjansen 6:32de160dce43 1056 attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 1057
mahphalke 1:819ac9aa5667 1058 /* Divide by 4 since ALDO Mux has 4x multiplier on it */
mahphalke 1:819ac9aa5667 1059 aldo_voltge /= VREF_MUX_MULTIPLIER;
mahphalke 1:819ac9aa5667 1060
mahphalke 1:819ac9aa5667 1061 /* Change channel mux back to analog input */
mahphalke 1:819ac9aa5667 1062 (void)ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 1063 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 1064 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 1065 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 1066 ANALOG_INPUT_MUX));
mahphalke 1:819ac9aa5667 1067
mahphalke 1:819ac9aa5667 1068 return (ssize_t)sprintf(buf, "%f", aldo_voltge);
mahphalke 1:819ac9aa5667 1069 }
mahphalke 1:819ac9aa5667 1070
mahphalke 1:819ac9aa5667 1071 return -EINVAL;
mahphalke 1:819ac9aa5667 1072 }
mahphalke 1:819ac9aa5667 1073
Kjansen 6:32de160dce43 1074 static ssize_t set_chn_aldo(void *device,
Kjansen 6:32de160dce43 1075 char *buf,
Kjansen 6:32de160dce43 1076 size_t len,
Kjansen 6:32de160dce43 1077 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1078 intptr_t id)
mahphalke 1:819ac9aa5667 1079 {
mahphalke 1:819ac9aa5667 1080 // NA- Can't set ALDO
mahphalke 1:819ac9aa5667 1081 return - EINVAL;
mahphalke 1:819ac9aa5667 1082 }
mahphalke 1:819ac9aa5667 1083
mahphalke 1:819ac9aa5667 1084
mahphalke 1:819ac9aa5667 1085 /*!
mahphalke 1:819ac9aa5667 1086 * @brief Getter/Setter for the channel DLDO attribute value
mahphalke 1:819ac9aa5667 1087 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 1088 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 1089 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 1090 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 1091 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 1092 * @note Available only for AD7606B and AD7606C
mahphalke 1:819ac9aa5667 1093 */
Kjansen 6:32de160dce43 1094 static ssize_t get_chn_dldo(void *device,
Kjansen 6:32de160dce43 1095 char *buf,
Kjansen 6:32de160dce43 1096 size_t len,
Kjansen 6:32de160dce43 1097 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1098 intptr_t id)
mahphalke 1:819ac9aa5667 1099 {
mahphalke 1:819ac9aa5667 1100 float dldo_voltge;
Kjansen 7:054dbd5e1f45 1101 int32_t adc_data_raw;
mahphalke 1:819ac9aa5667 1102
mahphalke 1:819ac9aa5667 1103 /* Configure the channel multiplexer to select DLDO read */
mahphalke 1:819ac9aa5667 1104 if (ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 1105 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 1106 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 1107 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 1108 DLDO_MUX)) == SUCCESS) {
mahphalke 1:819ac9aa5667 1109
mahphalke 1:819ac9aa5667 1110 /* Allow to settle Mux channel */
mahphalke 1:819ac9aa5667 1111 udelay(100);
mahphalke 1:819ac9aa5667 1112
mahphalke 1:819ac9aa5667 1113 /* Sample the channel and read conversion result */
Kjansen 7:054dbd5e1f45 1114 read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw);
mahphalke 1:819ac9aa5667 1115
mahphalke 1:819ac9aa5667 1116 /* Convert ADC data into equivalent voltage */
Kjansen 7:054dbd5e1f45 1117 dldo_voltge = convert_adc_raw_to_voltage(adc_data_raw,
Kjansen 6:32de160dce43 1118 attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 1119
mahphalke 1:819ac9aa5667 1120 /* Divide by 4 since ALDO Mux has 4x multiplier on it */
mahphalke 1:819ac9aa5667 1121 dldo_voltge /= VREF_MUX_MULTIPLIER;
mahphalke 1:819ac9aa5667 1122
mahphalke 1:819ac9aa5667 1123 /* Change channel mux back to analog input */
mahphalke 1:819ac9aa5667 1124 (void)ad7606_spi_write_mask(device,
Kjansen 6:32de160dce43 1125 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num),
Kjansen 6:32de160dce43 1126 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num),
Kjansen 6:32de160dce43 1127 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num),
mahphalke 1:819ac9aa5667 1128 ANALOG_INPUT_MUX));
mahphalke 1:819ac9aa5667 1129
mahphalke 1:819ac9aa5667 1130 return (ssize_t)sprintf(buf, "%f", dldo_voltge);
mahphalke 1:819ac9aa5667 1131 }
mahphalke 1:819ac9aa5667 1132
mahphalke 1:819ac9aa5667 1133 return -EINVAL;
mahphalke 1:819ac9aa5667 1134 }
mahphalke 1:819ac9aa5667 1135
Kjansen 6:32de160dce43 1136 static ssize_t set_chn_dldo(void *device,
Kjansen 6:32de160dce43 1137 char *buf,
Kjansen 6:32de160dce43 1138 size_t len,
Kjansen 6:32de160dce43 1139 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1140 intptr_t id)
mahphalke 1:819ac9aa5667 1141 {
mahphalke 1:819ac9aa5667 1142 // NA- Can't set DLDO
mahphalke 1:819ac9aa5667 1143 return - EINVAL;
mahphalke 1:819ac9aa5667 1144 }
mahphalke 1:819ac9aa5667 1145
mahphalke 1:819ac9aa5667 1146
mahphalke 1:819ac9aa5667 1147 /*!
mahphalke 3:83b3133f544a 1148 * @brief Getter/Setter for the channel open circuit detect manual attribute value
mahphalke 1:819ac9aa5667 1149 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 1150 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 1151 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 1152 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 1153 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 1154 */
Kjansen 6:32de160dce43 1155 static ssize_t get_chn_open_circuit_detect_manual(void *device,
mahphalke 3:83b3133f544a 1156 char *buf,
mahphalke 3:83b3133f544a 1157 size_t len,
Kjansen 6:32de160dce43 1158 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1159 intptr_t id)
mahphalke 1:819ac9aa5667 1160 {
mahphalke 3:83b3133f544a 1161 int32_t prev_adc_code, curr_adc_code;
mahphalke 3:83b3133f544a 1162 bool open_detect_flag = false;
mahphalke 3:83b3133f544a 1163 bool open_detect_done = false;
mahphalke 3:83b3133f544a 1164 uint8_t cnt;
mahphalke 1:819ac9aa5667 1165
mahphalke 3:83b3133f544a 1166 /* Enter into manual open circuit detection mode */
mahphalke 3:83b3133f544a 1167 do {
mahphalke 3:83b3133f544a 1168 if (ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE, 1) == SUCCESS) {
mahphalke 3:83b3133f544a 1169 /* Read the ADC on selected chnnel (first reading post open circuit detection start) */
Kjansen 7:054dbd5e1f45 1170 read_single_sample(channel->ch_num, (uint32_t *)&prev_adc_code);
mahphalke 1:819ac9aa5667 1171
mahphalke 3:83b3133f544a 1172 /* Perform N conversions and monitor the code delta */
mahphalke 3:83b3133f544a 1173 for (cnt = 0; cnt < MANUAL_OPEN_DETECT_CONV_CNTS; cnt++) {
mahphalke 3:83b3133f544a 1174 /* Check if code is within 350LSB (nearest ZS code) */
mahphalke 3:83b3133f544a 1175 if (prev_adc_code >= 0 && prev_adc_code < MANUAL_OPEN_DETECT_ENTRY_TRHLD) {
mahphalke 3:83b3133f544a 1176 /* Perform next conversion and read the result */
Kjansen 7:054dbd5e1f45 1177 read_single_sample(channel->ch_num, (uint32_t *)&curr_adc_code);
mahphalke 1:819ac9aa5667 1178
mahphalke 3:83b3133f544a 1179 /* Check if delta b/w current and previus reading is within 10 LSB code */
mahphalke 3:83b3133f544a 1180 if (abs(curr_adc_code - prev_adc_code) > MANUAL_OPEN_DETECT_CONV_TRSHLD) {
mahphalke 3:83b3133f544a 1181 open_detect_done = true;
mahphalke 3:83b3133f544a 1182 break;
mahphalke 3:83b3133f544a 1183 }
mahphalke 1:819ac9aa5667 1184
mahphalke 3:83b3133f544a 1185 /* Get the previous code */
mahphalke 3:83b3133f544a 1186 prev_adc_code = curr_adc_code;
mahphalke 3:83b3133f544a 1187 } else {
mahphalke 3:83b3133f544a 1188 open_detect_done = true;
mahphalke 3:83b3133f544a 1189 break;
mahphalke 1:819ac9aa5667 1190 }
mahphalke 1:819ac9aa5667 1191 }
mahphalke 1:819ac9aa5667 1192
mahphalke 3:83b3133f544a 1193 /* Break if open circuit detection aborted (in case above conditions not met) */
mahphalke 3:83b3133f544a 1194 if (open_detect_done)
mahphalke 3:83b3133f544a 1195 break;
mahphalke 3:83b3133f544a 1196
mahphalke 3:83b3133f544a 1197 /* Set common mode high (enabling open circuit detect on selected channel) */
mahphalke 3:83b3133f544a 1198 if (ad7606_spi_reg_write(device,
mahphalke 3:83b3133f544a 1199 AD7606_REG_OPEN_DETECT_ENABLE,
Kjansen 6:32de160dce43 1200 (1 << (channel->ch_num))) == SUCCESS) {
mahphalke 3:83b3133f544a 1201
mahphalke 3:83b3133f544a 1202 /* Perform next conversions (~2-3) and read the result (with common mode set high) */
mahphalke 3:83b3133f544a 1203 for (cnt = 0; cnt < MANUAL_OPEN_DETECT_CM_CNV_CNT; cnt++) {
mahphalke 3:83b3133f544a 1204 udelay(100);
Kjansen 7:054dbd5e1f45 1205 read_single_sample(channel->ch_num, (uint32_t *)&curr_adc_code);
mahphalke 3:83b3133f544a 1206 }
mahphalke 3:83b3133f544a 1207
mahphalke 3:83b3133f544a 1208 /* Check if delta b/w common mode high code and previous N conversion code is > threshold */
mahphalke 3:83b3133f544a 1209 if ((curr_adc_code - prev_adc_code) < MANUAL_OPEN_DETECT_THRESHOLD_RPD50K) {
mahphalke 3:83b3133f544a 1210 open_detect_done = true;
mahphalke 3:83b3133f544a 1211 break;
mahphalke 3:83b3133f544a 1212 }
mahphalke 3:83b3133f544a 1213 } else {
mahphalke 3:83b3133f544a 1214 return -EINVAL;
mahphalke 3:83b3133f544a 1215 }
mahphalke 3:83b3133f544a 1216
mahphalke 3:83b3133f544a 1217 /* Break if open circuit detection aborted (in case above conditions not met) */
mahphalke 3:83b3133f544a 1218 if (open_detect_done)
mahphalke 3:83b3133f544a 1219 break;
mahphalke 3:83b3133f544a 1220
mahphalke 3:83b3133f544a 1221 /* Set common mode low (disabling open circuit detect on channel) */
mahphalke 3:83b3133f544a 1222 if (ad7606_spi_reg_write(device,
mahphalke 3:83b3133f544a 1223 AD7606_REG_OPEN_DETECT_ENABLE,
mahphalke 3:83b3133f544a 1224 0) == SUCCESS) {
mahphalke 3:83b3133f544a 1225 /* Perform next conversion and read the result (with common mode set low) */
Kjansen 7:054dbd5e1f45 1226 read_single_sample(channel->ch_num, (uint32_t *)&curr_adc_code);
mahphalke 3:83b3133f544a 1227
mahphalke 3:83b3133f544a 1228 /* Check if delta b/w common mode low code and previous N conversion code is < threshold */
mahphalke 3:83b3133f544a 1229 if (abs(curr_adc_code - prev_adc_code) < MANUAL_OPEN_DETECT_THRESHOLD_RPD50K) {
mahphalke 3:83b3133f544a 1230 open_detect_flag = true;
mahphalke 3:83b3133f544a 1231 open_detect_done = true;
mahphalke 3:83b3133f544a 1232 }
mahphalke 3:83b3133f544a 1233 } else {
mahphalke 3:83b3133f544a 1234 return -EINVAL;
mahphalke 3:83b3133f544a 1235 }
mahphalke 3:83b3133f544a 1236 } else {
mahphalke 3:83b3133f544a 1237 return -EINVAL;
mahphalke 1:819ac9aa5667 1238 }
mahphalke 3:83b3133f544a 1239 } while (0);
mahphalke 1:819ac9aa5667 1240
mahphalke 1:819ac9aa5667 1241 /* Disable open detect mode */
mahphalke 1:819ac9aa5667 1242 (void)ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE, 0);
mahphalke 1:819ac9aa5667 1243
mahphalke 3:83b3133f544a 1244 if (open_detect_done) {
mahphalke 1:819ac9aa5667 1245 if (open_detect_flag) {
mahphalke 3:83b3133f544a 1246 strcpy(buf, "Open Circuit Detected");
mahphalke 1:819ac9aa5667 1247 } else {
mahphalke 3:83b3133f544a 1248 strcpy(buf, "Open Circuit Not Detected");
mahphalke 1:819ac9aa5667 1249 }
mahphalke 1:819ac9aa5667 1250
mahphalke 1:819ac9aa5667 1251 return len;
mahphalke 1:819ac9aa5667 1252 }
mahphalke 1:819ac9aa5667 1253
mahphalke 1:819ac9aa5667 1254 return -EINVAL;
mahphalke 1:819ac9aa5667 1255 }
mahphalke 1:819ac9aa5667 1256
Kjansen 6:32de160dce43 1257 static ssize_t set_chn_open_circuit_detect_manual(void *device,
mahphalke 3:83b3133f544a 1258 char *buf,
mahphalke 3:83b3133f544a 1259 size_t len,
Kjansen 6:32de160dce43 1260 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1261 intptr_t id)
mahphalke 1:819ac9aa5667 1262 {
mahphalke 3:83b3133f544a 1263 // NA- Can't set open circuit detect
mahphalke 1:819ac9aa5667 1264 return - EINVAL;
mahphalke 1:819ac9aa5667 1265 }
mahphalke 1:819ac9aa5667 1266
mahphalke 1:819ac9aa5667 1267
mahphalke 1:819ac9aa5667 1268 /*!
mahphalke 3:83b3133f544a 1269 * @brief Getter/Setter for the channel open circuit detect auto attribute value
mahphalke 1:819ac9aa5667 1270 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 1271 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 1272 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 1273 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 1274 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 1275 */
Kjansen 6:32de160dce43 1276 static ssize_t get_chn_open_circuit_detect_auto(void *device,
mahphalke 3:83b3133f544a 1277 char *buf,
mahphalke 3:83b3133f544a 1278 size_t len,
Kjansen 6:32de160dce43 1279 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1280 intptr_t id)
mahphalke 1:819ac9aa5667 1281 {
mahphalke 3:83b3133f544a 1282 if (open_circuit_detect_read_done) {
mahphalke 3:83b3133f544a 1283 open_circuit_detect_read_done = false;
mahphalke 3:83b3133f544a 1284
mahphalke 3:83b3133f544a 1285 if (open_circuit_detection_error) {
mahphalke 3:83b3133f544a 1286 strcpy(buf, "Error!!");
mahphalke 3:83b3133f544a 1287 }
mahphalke 1:819ac9aa5667 1288
mahphalke 3:83b3133f544a 1289 if (open_circuit_detection_done) {
mahphalke 3:83b3133f544a 1290 strcpy(buf, "Open Circuit Detected");
mahphalke 3:83b3133f544a 1291 } else {
mahphalke 3:83b3133f544a 1292 strcpy(buf, "Open Circuit Not Detected");
mahphalke 3:83b3133f544a 1293 }
mahphalke 3:83b3133f544a 1294
mahphalke 1:819ac9aa5667 1295 return len;
mahphalke 1:819ac9aa5667 1296 }
mahphalke 1:819ac9aa5667 1297
mahphalke 3:83b3133f544a 1298 return (ssize_t)sprintf(buf, "OPEN_DETECT_QUEUE: %d",
Kjansen 6:32de160dce43 1299 open_detect_queue_cnts[channel->ch_num]);
mahphalke 3:83b3133f544a 1300 }
mahphalke 3:83b3133f544a 1301
Kjansen 6:32de160dce43 1302 static ssize_t set_chn_open_circuit_detect_auto(void *device,
mahphalke 3:83b3133f544a 1303 char *buf,
mahphalke 3:83b3133f544a 1304 size_t len,
Kjansen 6:32de160dce43 1305 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1306 intptr_t id)
mahphalke 3:83b3133f544a 1307 {
mahphalke 3:83b3133f544a 1308 uint8_t data;
mahphalke 3:83b3133f544a 1309 uint8_t open_detect_flag = false;
mahphalke 3:83b3133f544a 1310 int32_t rw_status = FAILURE;
mahphalke 3:83b3133f544a 1311 uint16_t conv_cnts;
mahphalke 3:83b3133f544a 1312
mahphalke 3:83b3133f544a 1313 (void)sscanf(buf, "%d", &data);
mahphalke 3:83b3133f544a 1314 open_circuit_detection_error = false;
mahphalke 3:83b3133f544a 1315
mahphalke 3:83b3133f544a 1316 if ((data > 1 && data <= AUTO_OPEN_DETECT_QUEUE_MAX_CNT) && (buf[0] >= '0'
mahphalke 3:83b3133f544a 1317 && buf[0] <= '9')) {
Kjansen 6:32de160dce43 1318 open_detect_queue_cnts[channel->ch_num] = data;
mahphalke 1:819ac9aa5667 1319
mahphalke 3:83b3133f544a 1320 /* Enter into open circuit auto open detect mode */
mahphalke 3:83b3133f544a 1321 if (ad7606_spi_reg_write(device,
mahphalke 3:83b3133f544a 1322 AD7606_REG_OPEN_DETECT_QUEUE,
Kjansen 6:32de160dce43 1323 open_detect_queue_cnts[channel->ch_num]) == SUCCESS) {
mahphalke 3:83b3133f544a 1324 /* Enable open circuit detection on selected channel */
mahphalke 3:83b3133f544a 1325 if (ad7606_spi_reg_write(device,
mahphalke 3:83b3133f544a 1326 AD7606_REG_OPEN_DETECT_ENABLE,
Kjansen 6:32de160dce43 1327 (1 << (channel->ch_num))) == SUCCESS) {
mahphalke 3:83b3133f544a 1328 /* Monitor the open detect flag for max N+15 (open detect queue count) conversions.
mahphalke 3:83b3133f544a 1329 * Note: In ideal scenario, the open detect flash should be monitored continuously while
mahphalke 3:83b3133f544a 1330 * background N conversions are in progress */
mahphalke 3:83b3133f544a 1331 for (conv_cnts = 0;
Kjansen 6:32de160dce43 1332 conv_cnts < (open_detect_queue_cnts[channel->ch_num] +
mahphalke 3:83b3133f544a 1333 AUTO_OPEN_DETECT_QUEUE_EXTRA_CONV_CNT);
mahphalke 3:83b3133f544a 1334 conv_cnts++) {
mahphalke 3:83b3133f544a 1335 if (ad7606_convst(device) == SUCCESS) {
mahphalke 3:83b3133f544a 1336 udelay(100);
mahphalke 1:819ac9aa5667 1337
mahphalke 3:83b3133f544a 1338 /* Monitor the open detect flag */
mahphalke 3:83b3133f544a 1339 if (ad7606_spi_reg_read(device,
mahphalke 3:83b3133f544a 1340 AD7606_REG_OPEN_DETECTED,
mahphalke 3:83b3133f544a 1341 &open_detect_flag) == SUCCESS) {
Kjansen 6:32de160dce43 1342 open_detect_flag >>= (channel->ch_num);
mahphalke 3:83b3133f544a 1343 open_detect_flag &= 0x1;
mahphalke 3:83b3133f544a 1344
mahphalke 3:83b3133f544a 1345 rw_status = SUCCESS;
mahphalke 3:83b3133f544a 1346 if (open_detect_flag) {
mahphalke 3:83b3133f544a 1347 break;
mahphalke 3:83b3133f544a 1348 }
mahphalke 3:83b3133f544a 1349 } else {
mahphalke 3:83b3133f544a 1350 rw_status = FAILURE;
mahphalke 1:819ac9aa5667 1351 break;
mahphalke 1:819ac9aa5667 1352 }
mahphalke 1:819ac9aa5667 1353 } else {
mahphalke 1:819ac9aa5667 1354 rw_status = FAILURE;
mahphalke 1:819ac9aa5667 1355 break;
mahphalke 1:819ac9aa5667 1356 }
mahphalke 1:819ac9aa5667 1357 }
mahphalke 1:819ac9aa5667 1358 }
mahphalke 1:819ac9aa5667 1359 }
mahphalke 3:83b3133f544a 1360
mahphalke 3:83b3133f544a 1361 /* Disable open detect mode and clear open detect flag */
mahphalke 3:83b3133f544a 1362 (void)ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE, 0);
mahphalke 3:83b3133f544a 1363 (void)ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECTED, 0xFF);
mahphalke 3:83b3133f544a 1364
Kjansen 6:32de160dce43 1365 open_detect_queue_cnts[channel->ch_num] = 0;
mahphalke 3:83b3133f544a 1366
mahphalke 3:83b3133f544a 1367 if (rw_status == SUCCESS) {
mahphalke 3:83b3133f544a 1368 if (open_detect_flag) {
mahphalke 3:83b3133f544a 1369 open_circuit_detection_done = true;
mahphalke 3:83b3133f544a 1370 } else {
mahphalke 3:83b3133f544a 1371 open_circuit_detection_done = false;
mahphalke 3:83b3133f544a 1372 }
mahphalke 3:83b3133f544a 1373
mahphalke 3:83b3133f544a 1374 open_circuit_detect_read_done = true;
mahphalke 3:83b3133f544a 1375 return len;
mahphalke 3:83b3133f544a 1376 }
mahphalke 1:819ac9aa5667 1377 }
mahphalke 1:819ac9aa5667 1378
mahphalke 3:83b3133f544a 1379 open_circuit_detection_error = true;
mahphalke 1:819ac9aa5667 1380 return -EINVAL;
mahphalke 1:819ac9aa5667 1381 }
mahphalke 1:819ac9aa5667 1382
mahphalke 1:819ac9aa5667 1383
mahphalke 1:819ac9aa5667 1384 /*!
mahphalke 1:819ac9aa5667 1385 * @brief Getter/Setter for the adc offset calibration
mahphalke 1:819ac9aa5667 1386 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 1387 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 1388 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 1389 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 1390 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 1391 */
Kjansen 6:32de160dce43 1392 static ssize_t get_chn_calibrate_adc_offset(void *device,
Kjansen 6:32de160dce43 1393 char *buf,
Kjansen 6:32de160dce43 1394 size_t len,
Kjansen 6:32de160dce43 1395 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1396 intptr_t id)
mahphalke 1:819ac9aa5667 1397 {
mahphalke 1:819ac9aa5667 1398 float lsb_voltage;
mahphalke 1:819ac9aa5667 1399 float adc_voltage;
Kjansen 6:32de160dce43 1400 polarity_e polarity = attr_polarity_val[channel->ch_num];
Kjansen 7:054dbd5e1f45 1401 int32_t adc_raw_data;
mahphalke 1:819ac9aa5667 1402 int8_t chn_offset;
mahphalke 1:819ac9aa5667 1403
mahphalke 1:819ac9aa5667 1404 /* Perform the system offset calibration */
mahphalke 1:819ac9aa5667 1405
mahphalke 1:819ac9aa5667 1406 if (polarity == UNIPOLAR) {
Kjansen 6:32de160dce43 1407 lsb_voltage = attr_chn_range[channel->ch_num] / ADC_MAX_COUNT_UNIPOLAR;
mahphalke 1:819ac9aa5667 1408 } else {
Kjansen 6:32de160dce43 1409 lsb_voltage = attr_chn_range[channel->ch_num] / ADC_MAX_COUNT_BIPOLAR;
mahphalke 1:819ac9aa5667 1410 }
mahphalke 1:819ac9aa5667 1411
mahphalke 1:819ac9aa5667 1412 /* Sample and read the ADC channel */
Kjansen 7:054dbd5e1f45 1413 read_single_sample(channel->ch_num, (uint32_t *)&adc_raw_data);
mahphalke 1:819ac9aa5667 1414
mahphalke 1:819ac9aa5667 1415 /* Get an equivalent ADC voltage */
Kjansen 7:054dbd5e1f45 1416 adc_voltage = convert_adc_raw_to_voltage(adc_raw_data,
Kjansen 6:32de160dce43 1417 attr_scale_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 1418
mahphalke 1:819ac9aa5667 1419 /* Calculate the channel offset and write it to offset register */
mahphalke 3:83b3133f544a 1420 chn_offset = -(adc_voltage / lsb_voltage / OFFSET_REG_RESOLUTION);
mahphalke 1:819ac9aa5667 1421
Kjansen 6:32de160dce43 1422 if (ad7606_set_ch_offset(device, channel->ch_num,
mahphalke 1:819ac9aa5667 1423 chn_offset) == SUCCESS) {
mahphalke 1:819ac9aa5667 1424 return sprintf(buf, "%s", "ADC Calibration Done");
mahphalke 1:819ac9aa5667 1425 }
mahphalke 1:819ac9aa5667 1426
mahphalke 1:819ac9aa5667 1427 return -EINVAL;
mahphalke 1:819ac9aa5667 1428 }
mahphalke 1:819ac9aa5667 1429
Kjansen 6:32de160dce43 1430 static ssize_t set_chn_calibrate_adc_offset(void *device,
Kjansen 6:32de160dce43 1431 char *buf,
Kjansen 6:32de160dce43 1432 size_t len,
Kjansen 6:32de160dce43 1433 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1434 intptr_t id)
mahphalke 1:819ac9aa5667 1435 {
mahphalke 3:83b3133f544a 1436 // NA- Can't set open circuit detect
mahphalke 1:819ac9aa5667 1437 return - EINVAL;
mahphalke 1:819ac9aa5667 1438 }
mahphalke 1:819ac9aa5667 1439
mahphalke 1:819ac9aa5667 1440
mahphalke 1:819ac9aa5667 1441 /*!
mahphalke 1:819ac9aa5667 1442 * @brief Getter/Setter for the adc gain calibration
mahphalke 1:819ac9aa5667 1443 * @param device- pointer to IIO device structure
mahphalke 1:819ac9aa5667 1444 * @param buf- pointer to buffer holding attribute value
mahphalke 1:819ac9aa5667 1445 * @param len- length of buffer string data
mahphalke 1:819ac9aa5667 1446 * @param channel- pointer to IIO channel structure
mahphalke 1:819ac9aa5667 1447 * @return Number of characters read/written
mahphalke 1:819ac9aa5667 1448 */
Kjansen 6:32de160dce43 1449 static ssize_t get_chn_calibrate_adc_gain(void *device,
Kjansen 6:32de160dce43 1450 char *buf,
Kjansen 6:32de160dce43 1451 size_t len,
Kjansen 6:32de160dce43 1452 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1453 intptr_t id)
mahphalke 1:819ac9aa5667 1454 {
mahphalke 3:83b3133f544a 1455 uint8_t read_val;
mahphalke 1:819ac9aa5667 1456
mahphalke 3:83b3133f544a 1457 if (gain_calibration_done) {
mahphalke 3:83b3133f544a 1458 /* Get calibration status for previous gain value write event */
mahphalke 3:83b3133f544a 1459 gain_calibration_done = false;
mahphalke 3:83b3133f544a 1460 return sprintf(buf, "Calibration Done (Rfilter=%d K)",
Kjansen 6:32de160dce43 1461 gain_calibration_reg_val[channel->ch_num]);
mahphalke 3:83b3133f544a 1462 }
mahphalke 3:83b3133f544a 1463
mahphalke 3:83b3133f544a 1464 /* Return gain value when normal read event is triggered */
mahphalke 3:83b3133f544a 1465 if (ad7606_spi_reg_read(device,
Kjansen 6:32de160dce43 1466 AD7606_REG_GAIN_CH(channel->ch_num),
mahphalke 3:83b3133f544a 1467 &read_val) == SUCCESS) {
Kjansen 6:32de160dce43 1468 gain_calibration_reg_val[channel->ch_num] = (read_val & AD7606_GAIN_MSK);
mahphalke 3:83b3133f544a 1469 return sprintf(buf, "Rfilter= %d K",
Kjansen 6:32de160dce43 1470 gain_calibration_reg_val[channel->ch_num]);
mahphalke 1:819ac9aa5667 1471 }
mahphalke 1:819ac9aa5667 1472
mahphalke 1:819ac9aa5667 1473 return -EINVAL;
mahphalke 1:819ac9aa5667 1474 }
mahphalke 1:819ac9aa5667 1475
Kjansen 6:32de160dce43 1476 static ssize_t set_chn_calibrate_adc_gain(void *device,
Kjansen 6:32de160dce43 1477 char *buf,
Kjansen 6:32de160dce43 1478 size_t len,
Kjansen 6:32de160dce43 1479 const struct iio_ch_info *channel,
Kjansen 6:32de160dce43 1480 intptr_t id)
mahphalke 1:819ac9aa5667 1481 {
mahphalke 1:819ac9aa5667 1482 float data;
mahphalke 1:819ac9aa5667 1483
mahphalke 3:83b3133f544a 1484 if (buf[0] >= '0' && buf[0] <= '9') {
mahphalke 3:83b3133f544a 1485 (void)sscanf(buf, "%f", &data);
mahphalke 3:83b3133f544a 1486
mahphalke 3:83b3133f544a 1487 if (data >= 0 && data < ADC_CALIBRATION_GAIN_MAX) {
mahphalke 3:83b3133f544a 1488 /* Get the nearest value of unsigned integer */
Kjansen 6:32de160dce43 1489 gain_calibration_reg_val[channel->ch_num] = (uint8_t)(round(data));
mahphalke 1:819ac9aa5667 1490
mahphalke 3:83b3133f544a 1491 /* Perform the gain calibration by writing gain value into gain register */
mahphalke 3:83b3133f544a 1492 if (ad7606_set_ch_gain(device,
Kjansen 6:32de160dce43 1493 channel->ch_num,
Kjansen 6:32de160dce43 1494 gain_calibration_reg_val[channel->ch_num]) == SUCCESS) {
mahphalke 3:83b3133f544a 1495 gain_calibration_done = true;
mahphalke 3:83b3133f544a 1496 return len;
mahphalke 3:83b3133f544a 1497 }
mahphalke 3:83b3133f544a 1498 }
mahphalke 1:819ac9aa5667 1499 }
mahphalke 1:819ac9aa5667 1500
mahphalke 1:819ac9aa5667 1501 return -EINVAL;
mahphalke 1:819ac9aa5667 1502 }
mahphalke 1:819ac9aa5667 1503
mahphalke 1:819ac9aa5667 1504
mahphalke 1:819ac9aa5667 1505 /*!
Kjansen 6:32de160dce43 1506 * @brief Read the debug register value
Kjansen 6:32de160dce43 1507 * @param dev- Pointer to IIO device instance
Kjansen 6:32de160dce43 1508 * @param reg- Register address to read from
Kjansen 6:32de160dce43 1509 * @param readval- Pointer to variable to read data into
Kjansen 6:32de160dce43 1510 * @return SUCCESS in case of success, negative value otherwise
mahphalke 1:819ac9aa5667 1511 */
Kjansen 6:32de160dce43 1512 int32_t debug_reg_read(void *dev, uint32_t reg, uint32_t *readval)
mahphalke 1:819ac9aa5667 1513 {
Kjansen 6:32de160dce43 1514 /* Read the data from device */
Kjansen 6:32de160dce43 1515 if (reg <= NUM_OF_REGISTERS) {
Kjansen 6:32de160dce43 1516 if ((ad7606_spi_reg_read(dev, reg, (uint8_t *)readval) == SUCCESS)) {
Kjansen 6:32de160dce43 1517 return SUCCESS;
mahphalke 1:819ac9aa5667 1518 }
mahphalke 1:819ac9aa5667 1519 }
mahphalke 1:819ac9aa5667 1520
Kjansen 6:32de160dce43 1521 return FAILURE;
mahphalke 1:819ac9aa5667 1522 }
mahphalke 1:819ac9aa5667 1523
mahphalke 1:819ac9aa5667 1524
Kjansen 6:32de160dce43 1525 /*!
Kjansen 6:32de160dce43 1526 * @brief Write into the debug register
Kjansen 6:32de160dce43 1527 * @param dev- Pointer to IIO device instance
Kjansen 6:32de160dce43 1528 * @param reg- Register address to write into
Kjansen 6:32de160dce43 1529 * @param writeval- Register value to write
Kjansen 6:32de160dce43 1530 * @return SUCCESS in case of success, negative value otherwise
mahphalke 1:819ac9aa5667 1531 */
Kjansen 6:32de160dce43 1532 int32_t debug_reg_write(void *dev, uint32_t reg, uint32_t writeval)
mahphalke 1:819ac9aa5667 1533 {
Kjansen 6:32de160dce43 1534 if (reg <= NUM_OF_REGISTERS) {
Kjansen 6:32de160dce43 1535 if ((ad7606_spi_reg_write(dev, reg, (uint8_t)writeval) == SUCCESS)) {
Kjansen 6:32de160dce43 1536 save_local_attributes();
Kjansen 6:32de160dce43 1537 return SUCCESS;
Kjansen 6:32de160dce43 1538 }
mahphalke 1:819ac9aa5667 1539 }
mahphalke 1:819ac9aa5667 1540
Kjansen 6:32de160dce43 1541 return FAILURE;
mahphalke 1:819ac9aa5667 1542 }
mahphalke 1:819ac9aa5667 1543
mahphalke 1:819ac9aa5667 1544
mahphalke 1:819ac9aa5667 1545 /**
mahphalke 1:819ac9aa5667 1546 * @brief Read buffer data corresponding to AD7606 IIO device
mahphalke 1:819ac9aa5667 1547 * @param dev_instance[in] - IIO device instance
mahphalke 1:819ac9aa5667 1548 * @param pbuf[out] - Pointer to output data buffer
mahphalke 1:819ac9aa5667 1549 * @return SUCCESS in case of success or negative value otherwise
mahphalke 1:819ac9aa5667 1550 */
mahphalke 1:819ac9aa5667 1551 static ssize_t iio_ad7606_read_data(void *dev_instance,
mahphalke 1:819ac9aa5667 1552 char *pbuf,
mahphalke 1:819ac9aa5667 1553 size_t offset,
mahphalke 1:819ac9aa5667 1554 size_t bytes_count,
mahphalke 1:819ac9aa5667 1555 uint32_t ch_mask)
mahphalke 1:819ac9aa5667 1556 {
Kjansen 6:32de160dce43 1557 if (adc_data_capture_started == false) {
Kjansen 7:054dbd5e1f45 1558 start_data_capture(ch_mask, AD7606X_ADC_CHANNELS);
Kjansen 6:32de160dce43 1559 adc_data_capture_started = true;
mahphalke 1:819ac9aa5667 1560 }
mahphalke 1:819ac9aa5667 1561
mahphalke 1:819ac9aa5667 1562 /* Read the buffered data */
Kjansen 7:054dbd5e1f45 1563 return (ssize_t)read_buffered_data(pbuf,
Kjansen 7:054dbd5e1f45 1564 bytes_count,
Kjansen 7:054dbd5e1f45 1565 offset,
Kjansen 7:054dbd5e1f45 1566 ch_mask,
Kjansen 7:054dbd5e1f45 1567 BYTES_PER_SAMPLE);
mahphalke 1:819ac9aa5667 1568 }
mahphalke 1:819ac9aa5667 1569
mahphalke 1:819ac9aa5667 1570
mahphalke 1:819ac9aa5667 1571 /**
Kjansen 6:32de160dce43 1572 * @brief Transfer the device data into memory (optional)
mahphalke 1:819ac9aa5667 1573 * @param dev_instance[in] - IIO device instance
Kjansen 6:32de160dce43 1574 * @param bytes_count[in] - Number of bytes to read
Kjansen 6:32de160dce43 1575 * @param ch_mask[in] - Channels select mask
mahphalke 1:819ac9aa5667 1576 * @return SUCCESS in case of success or negative value otherwise
mahphalke 1:819ac9aa5667 1577 */
Kjansen 6:32de160dce43 1578 static ssize_t iio_ad7606_transfer_dev_data(void *dev_instance,
mahphalke 1:819ac9aa5667 1579 size_t bytes_count,
mahphalke 1:819ac9aa5667 1580 uint32_t ch_mask)
mahphalke 1:819ac9aa5667 1581 {
Kjansen 6:32de160dce43 1582 /* The function insures that data is first read into memory from the device.
Kjansen 6:32de160dce43 1583 * This function doesn't do any sort of data transfer but it make sure data
Kjansen 6:32de160dce43 1584 * read and it's transfer to memory from device is happening in application through
Kjansen 6:32de160dce43 1585 * iio_ad7606_read_data() function */
Kjansen 6:32de160dce43 1586
Kjansen 6:32de160dce43 1587 /* Store the requested samples count value for data capture */
Kjansen 7:054dbd5e1f45 1588 store_requested_samples_count(bytes_count, BYTES_PER_SAMPLE);
Kjansen 6:32de160dce43 1589
Kjansen 6:32de160dce43 1590 return SUCCESS;
Kjansen 6:32de160dce43 1591 }
Kjansen 6:32de160dce43 1592
Kjansen 6:32de160dce43 1593
Kjansen 6:32de160dce43 1594 /**
Kjansen 6:32de160dce43 1595 * @brief Perform tasks before new data transfer
Kjansen 6:32de160dce43 1596 * @param dev_instance[in] - IIO device instance
Kjansen 6:32de160dce43 1597 * @param ch_mask[in] - Channels select mask
Kjansen 6:32de160dce43 1598 * @return SUCCESS in case of success or negative value otherwise
Kjansen 6:32de160dce43 1599 */
Kjansen 6:32de160dce43 1600 static int32_t iio_ad7606_start_transfer(void *dev_instance, uint32_t ch_mask)
Kjansen 6:32de160dce43 1601 {
mahphalke 1:819ac9aa5667 1602 return SUCCESS;
mahphalke 1:819ac9aa5667 1603 }
mahphalke 1:819ac9aa5667 1604
mahphalke 1:819ac9aa5667 1605
mahphalke 1:819ac9aa5667 1606 /**
Kjansen 6:32de160dce43 1607 * @brief Perform tasks before end of current data transfer
Kjansen 6:32de160dce43 1608 * @param dev_instance[in] - IIO device instance
mahphalke 1:819ac9aa5667 1609 * @return SUCCESS in case of success or negative value otherwise
mahphalke 1:819ac9aa5667 1610 */
Kjansen 6:32de160dce43 1611 static int32_t iio_ad7606_stop_transfer(void *dev)
mahphalke 1:819ac9aa5667 1612 {
Kjansen 6:32de160dce43 1613 adc_data_capture_started = false;
Kjansen 7:054dbd5e1f45 1614 stop_data_capture();
mahphalke 1:819ac9aa5667 1615
mahphalke 1:819ac9aa5667 1616 return SUCCESS;
mahphalke 1:819ac9aa5667 1617 }
mahphalke 1:819ac9aa5667 1618
Kjansen 6:32de160dce43 1619 /*********************************************************
Kjansen 6:32de160dce43 1620 * IIO Attributes and Structures
Kjansen 6:32de160dce43 1621 ********************************************************/
Kjansen 6:32de160dce43 1622
Kjansen 6:32de160dce43 1623 /* IIOD channels attributes list */
Kjansen 6:32de160dce43 1624 struct iio_attribute channel_input_attributes[] = {
Kjansen 6:32de160dce43 1625 {
Kjansen 6:32de160dce43 1626 .name = "raw",
Kjansen 6:32de160dce43 1627 .show = get_chn_raw,
Kjansen 6:32de160dce43 1628 .store = set_chn_raw,
Kjansen 6:32de160dce43 1629 },
Kjansen 6:32de160dce43 1630 {
Kjansen 6:32de160dce43 1631 .name = "scale",
Kjansen 6:32de160dce43 1632 .show = get_chn_scale,
Kjansen 6:32de160dce43 1633 .store = set_chn_scale,
Kjansen 6:32de160dce43 1634 },
Kjansen 6:32de160dce43 1635 #if defined(DEV_AD7606B) || defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
Kjansen 6:32de160dce43 1636 {
Kjansen 6:32de160dce43 1637 .name = "chn_range",
Kjansen 6:32de160dce43 1638 .show = get_chn_range,
Kjansen 6:32de160dce43 1639 .store = set_chn_range,
Kjansen 6:32de160dce43 1640 },
Kjansen 6:32de160dce43 1641 {
Kjansen 6:32de160dce43 1642 .name = "offset",
Kjansen 6:32de160dce43 1643 .show = get_chn_offset,
Kjansen 6:32de160dce43 1644 .store = set_chn_offset,
Kjansen 6:32de160dce43 1645 },
Kjansen 6:32de160dce43 1646 {
Kjansen 6:32de160dce43 1647 .name = "chn_phase offset",
Kjansen 6:32de160dce43 1648 .show = get_chn_phase_offset,
Kjansen 6:32de160dce43 1649 .store = set_chn_phase_offset,
Kjansen 6:32de160dce43 1650 },
Kjansen 6:32de160dce43 1651 {
Kjansen 6:32de160dce43 1652 .name = "temperature",
Kjansen 6:32de160dce43 1653 .show = get_chn_temperature,
Kjansen 6:32de160dce43 1654 .store = set_chn_temperature,
Kjansen 6:32de160dce43 1655 },
Kjansen 6:32de160dce43 1656 {
Kjansen 6:32de160dce43 1657 .name = "vref",
Kjansen 6:32de160dce43 1658 .show = get_chn_vref,
Kjansen 6:32de160dce43 1659 .store = set_chn_vref,
Kjansen 6:32de160dce43 1660 },
Kjansen 6:32de160dce43 1661 {
Kjansen 6:32de160dce43 1662 .name = "vdrive",
Kjansen 6:32de160dce43 1663 .show = get_chn_vdrive,
Kjansen 6:32de160dce43 1664 .store = set_chn_vdrive,
Kjansen 6:32de160dce43 1665 },
Kjansen 6:32de160dce43 1666 {
Kjansen 6:32de160dce43 1667 .name = "ALDO",
Kjansen 6:32de160dce43 1668 .show = get_chn_aldo,
Kjansen 6:32de160dce43 1669 .store = set_chn_aldo,
Kjansen 6:32de160dce43 1670 },
Kjansen 6:32de160dce43 1671 {
Kjansen 6:32de160dce43 1672 .name = "DLDO",
Kjansen 6:32de160dce43 1673 .show = get_chn_dldo,
Kjansen 6:32de160dce43 1674 .store = set_chn_dldo,
Kjansen 6:32de160dce43 1675 },
Kjansen 6:32de160dce43 1676 {
Kjansen 6:32de160dce43 1677 .name = "open_circuit_detect_manual",
Kjansen 6:32de160dce43 1678 .show = get_chn_open_circuit_detect_manual,
Kjansen 6:32de160dce43 1679 .store = set_chn_open_circuit_detect_manual,
Kjansen 6:32de160dce43 1680 },
Kjansen 6:32de160dce43 1681 {
Kjansen 6:32de160dce43 1682 .name = "open_circuit_detect_auto",
Kjansen 6:32de160dce43 1683 .show = get_chn_open_circuit_detect_auto,
Kjansen 6:32de160dce43 1684 .store = set_chn_open_circuit_detect_auto,
Kjansen 6:32de160dce43 1685 },
Kjansen 6:32de160dce43 1686 {
Kjansen 6:32de160dce43 1687 .name = "calibrate_adc_offset",
Kjansen 6:32de160dce43 1688 .show = get_chn_calibrate_adc_offset,
Kjansen 6:32de160dce43 1689 .store = set_chn_calibrate_adc_offset,
Kjansen 6:32de160dce43 1690 },
Kjansen 6:32de160dce43 1691 {
Kjansen 6:32de160dce43 1692 .name = "calibrate_adc_gain",
Kjansen 6:32de160dce43 1693 .show = get_chn_calibrate_adc_gain,
Kjansen 6:32de160dce43 1694 .store = set_chn_calibrate_adc_gain,
Kjansen 6:32de160dce43 1695 },
Kjansen 6:32de160dce43 1696 #if defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
Kjansen 6:32de160dce43 1697 {
Kjansen 6:32de160dce43 1698 .name = "bandwidth",
Kjansen 6:32de160dce43 1699 .show = get_bandwidth,
Kjansen 6:32de160dce43 1700 .store = set_bandwidth,
Kjansen 6:32de160dce43 1701 },
Kjansen 6:32de160dce43 1702 #endif
Kjansen 6:32de160dce43 1703 #endif
Kjansen 6:32de160dce43 1704
Kjansen 6:32de160dce43 1705 END_ATTRIBUTES_ARRAY
Kjansen 6:32de160dce43 1706 };
Kjansen 6:32de160dce43 1707
Kjansen 6:32de160dce43 1708 /* IIOD device (global) attributes list */
Kjansen 6:32de160dce43 1709 static struct iio_attribute global_attributes[] = {
Kjansen 6:32de160dce43 1710 #if defined(DEV_AD7606B) || defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
Kjansen 6:32de160dce43 1711 {
Kjansen 6:32de160dce43 1712 .name = "operating_mode",
Kjansen 6:32de160dce43 1713 .show = get_operating_mode,
Kjansen 6:32de160dce43 1714 .store = set_operating_mode,
Kjansen 6:32de160dce43 1715 },
Kjansen 6:32de160dce43 1716 {
Kjansen 6:32de160dce43 1717 .name = "oversampling_ratio",
Kjansen 6:32de160dce43 1718 .show = get_oversampling,
Kjansen 6:32de160dce43 1719 .store = set_oversampling,
Kjansen 6:32de160dce43 1720 },
Kjansen 6:32de160dce43 1721 #else
Kjansen 6:32de160dce43 1722 {
Kjansen 6:32de160dce43 1723 .name = "power_down_mode",
Kjansen 6:32de160dce43 1724 .show = get_power_down_mode,
Kjansen 6:32de160dce43 1725 .store = set_power_down_mode,
Kjansen 6:32de160dce43 1726 },
Kjansen 6:32de160dce43 1727 {
Kjansen 6:32de160dce43 1728 .name = "dev_range",
Kjansen 6:32de160dce43 1729 .show = get_range,
Kjansen 6:32de160dce43 1730 .store = set_range,
Kjansen 6:32de160dce43 1731 },
Kjansen 6:32de160dce43 1732 #endif
Kjansen 6:32de160dce43 1733 {
Kjansen 6:32de160dce43 1734 .name = "sampling_frequency",
Kjansen 6:32de160dce43 1735 .show = get_sampling_frequency,
Kjansen 6:32de160dce43 1736 .store = set_sampling_frequency,
Kjansen 6:32de160dce43 1737 },
Kjansen 6:32de160dce43 1738
Kjansen 6:32de160dce43 1739 END_ATTRIBUTES_ARRAY
Kjansen 6:32de160dce43 1740 };
Kjansen 6:32de160dce43 1741
Kjansen 6:32de160dce43 1742 /* IIOD debug attributes list */
Kjansen 6:32de160dce43 1743 static struct iio_attribute debug_attributes[] = {
Kjansen 6:32de160dce43 1744 #if defined(DEV_AD7606B) || defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
Kjansen 6:32de160dce43 1745 {
Kjansen 6:32de160dce43 1746 .name = "direct_reg_access",
Kjansen 6:32de160dce43 1747 .show = NULL,
Kjansen 6:32de160dce43 1748 .store = NULL,
Kjansen 6:32de160dce43 1749 },
Kjansen 6:32de160dce43 1750 #endif
Kjansen 6:32de160dce43 1751
Kjansen 6:32de160dce43 1752 END_ATTRIBUTES_ARRAY
Kjansen 6:32de160dce43 1753 };
Kjansen 6:32de160dce43 1754
Kjansen 6:32de160dce43 1755 /* IIOD channels configurations */
Kjansen 6:32de160dce43 1756 struct scan_type chn_scan = {
Kjansen 6:32de160dce43 1757 .sign = 's',
Kjansen 7:054dbd5e1f45 1758 .realbits = CHN_STORAGE_BITS,
Kjansen 7:054dbd5e1f45 1759 .storagebits = CHN_STORAGE_BITS,
Kjansen 6:32de160dce43 1760 .shift = 0,
Kjansen 6:32de160dce43 1761 .is_big_endian = false
Kjansen 6:32de160dce43 1762 };
Kjansen 6:32de160dce43 1763
Kjansen 6:32de160dce43 1764 static struct iio_channel iio_ad7606_channels[] = {
Kjansen 6:32de160dce43 1765 {
Kjansen 6:32de160dce43 1766 .name = "voltage0",
Kjansen 6:32de160dce43 1767 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1768 .channel = 0,
Kjansen 6:32de160dce43 1769 .scan_index = 0,
Kjansen 6:32de160dce43 1770 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1771 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1772 .ch_out = false,
Kjansen 6:32de160dce43 1773 .indexed = true,
Kjansen 6:32de160dce43 1774 },
Kjansen 6:32de160dce43 1775 {
Kjansen 6:32de160dce43 1776 .name = "voltage1",
Kjansen 6:32de160dce43 1777 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1778 .channel = 1,
Kjansen 6:32de160dce43 1779 .scan_index = 1,
Kjansen 6:32de160dce43 1780 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1781 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1782 .ch_out = false,
Kjansen 6:32de160dce43 1783 .indexed = true
Kjansen 6:32de160dce43 1784 },
Kjansen 6:32de160dce43 1785 {
Kjansen 6:32de160dce43 1786 .name = "voltage2",
Kjansen 6:32de160dce43 1787 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1788 .channel = 2,
Kjansen 6:32de160dce43 1789 .scan_index = 2,
Kjansen 6:32de160dce43 1790 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1791 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1792 .ch_out = false,
Kjansen 6:32de160dce43 1793 .indexed = true
Kjansen 6:32de160dce43 1794 },
Kjansen 6:32de160dce43 1795 {
Kjansen 6:32de160dce43 1796 .name = "voltage3",
Kjansen 6:32de160dce43 1797 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1798 .channel = 3,
Kjansen 6:32de160dce43 1799 .scan_index = 3,
Kjansen 6:32de160dce43 1800 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1801 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1802 .ch_out = false,
Kjansen 6:32de160dce43 1803 .indexed = true
Kjansen 6:32de160dce43 1804 },
Kjansen 6:32de160dce43 1805 {
Kjansen 6:32de160dce43 1806 .name = "voltage4",
Kjansen 6:32de160dce43 1807 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1808 .channel = 4,
Kjansen 6:32de160dce43 1809 .scan_index = 4,
Kjansen 6:32de160dce43 1810 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1811 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1812 .ch_out = false,
Kjansen 6:32de160dce43 1813 .indexed = true
Kjansen 6:32de160dce43 1814 },
Kjansen 6:32de160dce43 1815 {
Kjansen 6:32de160dce43 1816 .name = "voltage5",
Kjansen 6:32de160dce43 1817 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1818 .channel = 5,
Kjansen 6:32de160dce43 1819 .scan_index = 5,
Kjansen 6:32de160dce43 1820 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1821 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1822 .ch_out = false,
Kjansen 6:32de160dce43 1823 .indexed = true
Kjansen 6:32de160dce43 1824 },
Kjansen 6:32de160dce43 1825 {
Kjansen 6:32de160dce43 1826 .name = "voltage6",
Kjansen 6:32de160dce43 1827 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1828 .channel = 6,
Kjansen 6:32de160dce43 1829 .scan_index = 6,
Kjansen 6:32de160dce43 1830 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1831 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1832 .ch_out = false,
Kjansen 6:32de160dce43 1833 .indexed = true
Kjansen 6:32de160dce43 1834 },
Kjansen 6:32de160dce43 1835 {
Kjansen 6:32de160dce43 1836 .name = "voltage7",
Kjansen 6:32de160dce43 1837 .ch_type = IIO_VOLTAGE,
Kjansen 6:32de160dce43 1838 .channel = 7,
Kjansen 6:32de160dce43 1839 .scan_index = 7,
Kjansen 6:32de160dce43 1840 .scan_type = &chn_scan,
Kjansen 6:32de160dce43 1841 .attributes = channel_input_attributes,
Kjansen 6:32de160dce43 1842 .ch_out = false,
Kjansen 6:32de160dce43 1843 .indexed = true
Kjansen 6:32de160dce43 1844 }
Kjansen 6:32de160dce43 1845 };
Kjansen 6:32de160dce43 1846
mahphalke 1:819ac9aa5667 1847
mahphalke 1:819ac9aa5667 1848 /**
mahphalke 1:819ac9aa5667 1849 * @brief Init for reading/writing and parameterization of a
mahphalke 1:819ac9aa5667 1850 * ad7606 IIO device
mahphalke 1:819ac9aa5667 1851 * @param desc[in,out] - IIO device descriptor
mahphalke 1:819ac9aa5667 1852 * @param init[in] - Configuration structure
mahphalke 1:819ac9aa5667 1853 * @return SUCCESS in case of success, FAILURE otherwise
mahphalke 1:819ac9aa5667 1854 */
Kjansen 6:32de160dce43 1855 static int32_t iio_ad7606_init(struct iio_device **desc)
mahphalke 1:819ac9aa5667 1856 {
Kjansen 6:32de160dce43 1857 struct iio_device *iio_ad7606_inst;
mahphalke 1:819ac9aa5667 1858
Kjansen 6:32de160dce43 1859 iio_ad7606_inst = calloc(1, sizeof(struct iio_device));
Kjansen 6:32de160dce43 1860 if (!iio_ad7606_inst) {
mahphalke 1:819ac9aa5667 1861 return FAILURE;
mahphalke 1:819ac9aa5667 1862 }
mahphalke 1:819ac9aa5667 1863
Kjansen 6:32de160dce43 1864 iio_ad7606_inst->num_ch = sizeof(iio_ad7606_channels) / sizeof(
Kjansen 6:32de160dce43 1865 iio_ad7606_channels[0]);
Kjansen 6:32de160dce43 1866 iio_ad7606_inst->channels = iio_ad7606_channels;
Kjansen 6:32de160dce43 1867 iio_ad7606_inst->attributes = global_attributes;
Kjansen 6:32de160dce43 1868 iio_ad7606_inst->debug_attributes = debug_attributes;
mahphalke 1:819ac9aa5667 1869
Kjansen 6:32de160dce43 1870 iio_ad7606_inst->transfer_dev_to_mem = iio_ad7606_transfer_dev_data;
Kjansen 6:32de160dce43 1871 iio_ad7606_inst->transfer_mem_to_dev = NULL;
Kjansen 6:32de160dce43 1872 iio_ad7606_inst->read_data = iio_ad7606_read_data;
Kjansen 6:32de160dce43 1873 iio_ad7606_inst->write_data = NULL;
Kjansen 6:32de160dce43 1874 iio_ad7606_inst->prepare_transfer = iio_ad7606_start_transfer;
Kjansen 6:32de160dce43 1875 iio_ad7606_inst->end_transfer = iio_ad7606_stop_transfer;
Kjansen 6:32de160dce43 1876 iio_ad7606_inst->debug_reg_read = debug_reg_read;
Kjansen 6:32de160dce43 1877 iio_ad7606_inst->debug_reg_write = debug_reg_write;
Kjansen 6:32de160dce43 1878
Kjansen 6:32de160dce43 1879 *desc = iio_ad7606_inst;
mahphalke 1:819ac9aa5667 1880
mahphalke 1:819ac9aa5667 1881 return SUCCESS;
mahphalke 1:819ac9aa5667 1882 }
mahphalke 1:819ac9aa5667 1883
mahphalke 1:819ac9aa5667 1884
mahphalke 1:819ac9aa5667 1885 /**
mahphalke 1:819ac9aa5667 1886 * @brief Release resources allocated for IIO device
mahphalke 1:819ac9aa5667 1887 * @param desc[in] - IIO device descriptor
mahphalke 1:819ac9aa5667 1888 * @return SUCCESS in case of success, FAILURE otherwise
mahphalke 1:819ac9aa5667 1889 */
Kjansen 6:32de160dce43 1890 static int32_t iio_ad7606_remove(struct iio_desc *desc)
mahphalke 1:819ac9aa5667 1891 {
mahphalke 1:819ac9aa5667 1892 int32_t status;
mahphalke 1:819ac9aa5667 1893
mahphalke 1:819ac9aa5667 1894 if (!desc) {
mahphalke 1:819ac9aa5667 1895 return FAILURE;
mahphalke 1:819ac9aa5667 1896 }
mahphalke 1:819ac9aa5667 1897
Kjansen 6:32de160dce43 1898 status = iio_unregister(desc, (char *)dev_name);
Kjansen 6:32de160dce43 1899 if (status != SUCCESS) {
mahphalke 1:819ac9aa5667 1900 return FAILURE;
mahphalke 1:819ac9aa5667 1901 }
mahphalke 1:819ac9aa5667 1902
mahphalke 1:819ac9aa5667 1903 return SUCCESS;
mahphalke 1:819ac9aa5667 1904 }
mahphalke 1:819ac9aa5667 1905
mahphalke 1:819ac9aa5667 1906
mahphalke 1:819ac9aa5667 1907 /*!
mahphalke 1:819ac9aa5667 1908 * @brief Get scale factor for adc data to voltage conversion for IIO client
mahphalke 1:819ac9aa5667 1909 * @param chn_range[in] - Current channel voltage range
mahphalke 1:819ac9aa5667 1910 * @param chn_range_bits[in] - Channel range register bits
mahphalke 1:819ac9aa5667 1911 * @return scale
mahphalke 1:819ac9aa5667 1912 * @details This function samples and capture the new data when previous data
mahphalke 1:819ac9aa5667 1913 * is transmitted to IIO client
mahphalke 1:819ac9aa5667 1914 */
mahphalke 1:819ac9aa5667 1915 static float get_vltg_conv_scale_factor(float chn_range, polarity_e polarity)
mahphalke 1:819ac9aa5667 1916 {
mahphalke 1:819ac9aa5667 1917 float scale;
mahphalke 1:819ac9aa5667 1918
mahphalke 1:819ac9aa5667 1919 /* Get the scale factor for voltage conversion from range */
mahphalke 1:819ac9aa5667 1920 if (polarity == UNIPOLAR) {
mahphalke 1:819ac9aa5667 1921 scale = (chn_range / ADC_MAX_COUNT_UNIPOLAR) * 1000;
mahphalke 1:819ac9aa5667 1922 } else {
mahphalke 1:819ac9aa5667 1923 scale = (chn_range / ADC_MAX_COUNT_BIPOLAR) * 1000;
mahphalke 1:819ac9aa5667 1924 }
mahphalke 1:819ac9aa5667 1925
mahphalke 1:819ac9aa5667 1926 return scale;
mahphalke 1:819ac9aa5667 1927 }
mahphalke 1:819ac9aa5667 1928
mahphalke 1:819ac9aa5667 1929
mahphalke 1:819ac9aa5667 1930 /**
mahphalke 1:819ac9aa5667 1931 * @brief Save local variables
mahphalke 1:819ac9aa5667 1932 * @return none
mahphalke 1:819ac9aa5667 1933 * @details This function saves the local parameters with updated device values
mahphalke 1:819ac9aa5667 1934 */
mahphalke 1:819ac9aa5667 1935 static void save_local_attributes(void)
mahphalke 1:819ac9aa5667 1936 {
mahphalke 1:819ac9aa5667 1937 char buf[50];
mahphalke 1:819ac9aa5667 1938 struct iio_ch_info channel;
mahphalke 1:819ac9aa5667 1939
Kjansen 6:32de160dce43 1940 for (uint8_t chn = 0; chn < AD7606X_ADC_CHANNELS; chn++) {
mahphalke 1:819ac9aa5667 1941 channel.ch_num = chn;
mahphalke 1:819ac9aa5667 1942
mahphalke 1:819ac9aa5667 1943 /* Get channel range */
Kjansen 6:32de160dce43 1944 (void)get_chn_range(p_ad7606_dev_inst, buf, 0, &channel, 0);
mahphalke 1:819ac9aa5667 1945
mahphalke 1:819ac9aa5667 1946 /* Get scale */
Kjansen 6:32de160dce43 1947 attr_scale_val[channel.ch_num] = get_vltg_conv_scale_factor(
Kjansen 6:32de160dce43 1948 attr_chn_range[channel.ch_num],
Kjansen 6:32de160dce43 1949 attr_polarity_val[channel.ch_num]);
mahphalke 1:819ac9aa5667 1950 }
mahphalke 1:819ac9aa5667 1951 }
mahphalke 1:819ac9aa5667 1952
mahphalke 1:819ac9aa5667 1953
mahphalke 1:819ac9aa5667 1954 /**
mahphalke 1:819ac9aa5667 1955 * @brief Initialize the IIO interface for AD7606 IIO device
mahphalke 1:819ac9aa5667 1956 * @return none
mahphalke 1:819ac9aa5667 1957 * @return SUCCESS in case of success, FAILURE otherwise
mahphalke 1:819ac9aa5667 1958 */
mahphalke 1:819ac9aa5667 1959 int32_t ad7606_iio_initialize(void)
mahphalke 1:819ac9aa5667 1960 {
mahphalke 1:819ac9aa5667 1961 int32_t init_status;
mahphalke 1:819ac9aa5667 1962
Kjansen 6:32de160dce43 1963 /* IIO device descriptor */
Kjansen 6:32de160dce43 1964 struct iio_device *p_iio_ad7606_dev;
mahphalke 1:819ac9aa5667 1965
mahphalke 1:819ac9aa5667 1966 /**
Kjansen 6:32de160dce43 1967 * IIO interface init parameters
Kjansen 6:32de160dce43 1968 */
Kjansen 6:32de160dce43 1969 struct iio_init_param iio_init_params = {
Kjansen 6:32de160dce43 1970 .phy_type = USE_UART,
Kjansen 6:32de160dce43 1971 {
Kjansen 6:32de160dce43 1972 &uart_init_params
Kjansen 6:32de160dce43 1973 }
mahphalke 1:819ac9aa5667 1974 };
mahphalke 1:819ac9aa5667 1975
mahphalke 1:819ac9aa5667 1976 /* Initialize AD7606 device and peripheral interface */
Kjansen 6:32de160dce43 1977 init_status = ad7606_init(&p_ad7606_dev_inst, &ad7606_init_str);
mahphalke 1:819ac9aa5667 1978 if (init_status != SUCCESS) {
mahphalke 1:819ac9aa5667 1979 return init_status;
mahphalke 1:819ac9aa5667 1980 }
mahphalke 1:819ac9aa5667 1981
Kjansen 6:32de160dce43 1982 /* Initialize the IIO interface */
Kjansen 6:32de160dce43 1983 init_status = iio_init(&p_ad7606_iio_desc, &iio_init_params);
mahphalke 1:819ac9aa5667 1984 if (init_status != SUCCESS) {
mahphalke 1:819ac9aa5667 1985 return init_status;
mahphalke 1:819ac9aa5667 1986 }
mahphalke 1:819ac9aa5667 1987
Kjansen 6:32de160dce43 1988 /* Initialize the AD7606 IIO application interface */
Kjansen 6:32de160dce43 1989 init_status = iio_ad7606_init(&p_iio_ad7606_dev);
mahphalke 1:819ac9aa5667 1990 if (init_status != SUCCESS) {
mahphalke 1:819ac9aa5667 1991 return init_status;
mahphalke 1:819ac9aa5667 1992 }
mahphalke 1:819ac9aa5667 1993
Kjansen 6:32de160dce43 1994 /* Register AD7606 IIO interface */
Kjansen 6:32de160dce43 1995 init_status = iio_register(p_ad7606_iio_desc,
Kjansen 6:32de160dce43 1996 p_iio_ad7606_dev,
Kjansen 6:32de160dce43 1997 (char *)dev_name,
Kjansen 6:32de160dce43 1998 p_ad7606_dev_inst,
Kjansen 6:32de160dce43 1999 NULL,
Kjansen 6:32de160dce43 2000 NULL);
mahphalke 1:819ac9aa5667 2001 if (init_status != SUCCESS) {
mahphalke 1:819ac9aa5667 2002 return init_status;
mahphalke 1:819ac9aa5667 2003 }
mahphalke 1:819ac9aa5667 2004
Kjansen 6:32de160dce43 2005 /* Init the system peripherals */
Kjansen 6:32de160dce43 2006 init_status = init_system();
mahphalke 1:819ac9aa5667 2007 if (init_status != SUCCESS) {
mahphalke 1:819ac9aa5667 2008 return init_status;
mahphalke 1:819ac9aa5667 2009 }
mahphalke 1:819ac9aa5667 2010
mahphalke 1:819ac9aa5667 2011 return init_status;
mahphalke 1:819ac9aa5667 2012 }
mahphalke 1:819ac9aa5667 2013
mahphalke 1:819ac9aa5667 2014
mahphalke 1:819ac9aa5667 2015 /**
mahphalke 1:819ac9aa5667 2016 * @brief Run the AD7606 IIO event handler
mahphalke 1:819ac9aa5667 2017 * @return none
mahphalke 1:819ac9aa5667 2018 * @details This function monitors the new IIO client event
mahphalke 1:819ac9aa5667 2019 */
mahphalke 1:819ac9aa5667 2020 void ad7606_iio_event_handler(void)
mahphalke 1:819ac9aa5667 2021 {
Kjansen 6:32de160dce43 2022 while (1) {
Kjansen 6:32de160dce43 2023 (void)iio_step(p_ad7606_iio_desc);
mahphalke 1:819ac9aa5667 2024 }
mahphalke 1:819ac9aa5667 2025 }