Example Program for EVAL-AD7606

Dependencies:   platform_drivers

Committer:
Kjansen
Date:
Wed Jul 21 11:16:56 2021 +0100
Revision:
6:32de160dce43
Parent:
3:83b3133f544a
Child:
7:054dbd5e1f45
Updated the project directory structure to remove the duplicate drivers repositories and replaced with common no-os driver repository

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