Forked repository for pushing changes to EVAL-AD4696

Dependencies:   platform_drivers

Committer:
pmallick
Date:
Thu Sep 30 11:01:05 2021 +0530
Revision:
1:8792acb5a039
AD4696 IIO Application- Initial Revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 1:8792acb5a039 1 /***************************************************************************//**
pmallick 1:8792acb5a039 2 * @file iio_ad4696.c
pmallick 1:8792acb5a039 3 * @brief Implementation of AD4696 IIO application interfaces
pmallick 1:8792acb5a039 4 * @details This module acts as an interface for AD4696 IIO application
pmallick 1:8792acb5a039 5 ********************************************************************************
pmallick 1:8792acb5a039 6 * Copyright (c) 2021 Analog Devices, Inc.
pmallick 1:8792acb5a039 7 *
pmallick 1:8792acb5a039 8 * This software is proprietary to Analog Devices, Inc. and its licensors.
pmallick 1:8792acb5a039 9 * By using this software you agree to the terms of the associated
pmallick 1:8792acb5a039 10 * Analog Devices Software License Agreement.
pmallick 1:8792acb5a039 11 *******************************************************************************/
pmallick 1:8792acb5a039 12
pmallick 1:8792acb5a039 13 /******************************************************************************/
pmallick 1:8792acb5a039 14 /***************************** Include Files **********************************/
pmallick 1:8792acb5a039 15 /******************************************************************************/
pmallick 1:8792acb5a039 16
pmallick 1:8792acb5a039 17 #include <inttypes.h>
pmallick 1:8792acb5a039 18 #include <string.h>
pmallick 1:8792acb5a039 19 #include <math.h>
pmallick 1:8792acb5a039 20
pmallick 1:8792acb5a039 21 #include "app_config.h"
pmallick 1:8792acb5a039 22 #include "tinyiiod.h"
pmallick 1:8792acb5a039 23 #include "iio_ad4696.h"
pmallick 1:8792acb5a039 24 #include "adc_data_capture.h"
pmallick 1:8792acb5a039 25 #include "ad4696_support.h"
pmallick 1:8792acb5a039 26 #include "ad4696_user_config.h"
pmallick 1:8792acb5a039 27 #include "error.h"
pmallick 1:8792acb5a039 28
pmallick 1:8792acb5a039 29 /******************************************************************************/
pmallick 1:8792acb5a039 30 /************************ Macros/Constants ************************************/
pmallick 1:8792acb5a039 31 /******************************************************************************/
pmallick 1:8792acb5a039 32
pmallick 1:8792acb5a039 33 /* ADC Raw to Voltage conversion default scale factor for IIO client */
pmallick 1:8792acb5a039 34 #if defined(PSEUDO_BIPOLAR_MODE)
pmallick 1:8792acb5a039 35 /* Device supports pseudo-bipolar mode only with INX- = Vref / 2 */
pmallick 1:8792acb5a039 36 #define DEFAULT_SCALE (((DEFAULT_VREF / 2) / ADC_MAX_COUNT_BIPOLAR) * 1000)
pmallick 1:8792acb5a039 37 #else
pmallick 1:8792acb5a039 38 #define DEFAULT_SCALE ((DEFAULT_VREF / ADC_MAX_COUNT_UNIPOLAR) * 1000)
pmallick 1:8792acb5a039 39 #endif
pmallick 1:8792acb5a039 40
pmallick 1:8792acb5a039 41 /* Bytes per sample. This count should divide the total 256 bytes into 'n' equivalent
pmallick 1:8792acb5a039 42 * ADC samples as IIO library requests only 256bytes of data at a time in a given
pmallick 1:8792acb5a039 43 * data read query.
pmallick 1:8792acb5a039 44 * For 1 to 8-bit ADC, bytes per sample = 1 (2^0)
pmallick 1:8792acb5a039 45 * For 9 to 16-bit ADC, bytes per sample = 2 (2^1)
pmallick 1:8792acb5a039 46 * For 17 to 32-bit ADC, bytes per sample = 4 (2^2)
pmallick 1:8792acb5a039 47 **/
pmallick 1:8792acb5a039 48 #define BYTES_PER_SAMPLE sizeof(uint16_t) // For ADC resolution of 16-bits
pmallick 1:8792acb5a039 49
pmallick 1:8792acb5a039 50 /* Number of data storage bits (needed for IIO client to plot ADC data) */
pmallick 1:8792acb5a039 51 #define CHN_STORAGE_BITS (BYTES_PER_SAMPLE * 8)
pmallick 1:8792acb5a039 52
pmallick 1:8792acb5a039 53 /* Private IDs for IIO attributes */
pmallick 1:8792acb5a039 54 #define IIO_RAW_ATTR_ID 0
pmallick 1:8792acb5a039 55 #define IIO_SCALE_ATTR_ID 1
pmallick 1:8792acb5a039 56 #define IIO_OFFSET_ATTR_ID 2
pmallick 1:8792acb5a039 57
pmallick 1:8792acb5a039 58 /******************************************************************************/
pmallick 1:8792acb5a039 59 /*************************** Types Declarations *******************************/
pmallick 1:8792acb5a039 60 /******************************************************************************/
pmallick 1:8792acb5a039 61
pmallick 1:8792acb5a039 62 /* Pointer to the struct representing the AD4696 IIO device */
pmallick 1:8792acb5a039 63 struct ad469x_dev *p_ad4696_dev = NULL;
pmallick 1:8792acb5a039 64
pmallick 1:8792acb5a039 65 /* IIO interface descriptor */
pmallick 1:8792acb5a039 66 static struct iio_desc *p_ad4696_iio_desc;
pmallick 1:8792acb5a039 67
pmallick 1:8792acb5a039 68 /* Device name */
pmallick 1:8792acb5a039 69 static const char dev_name [] = ACTIVE_DEVICE_NAME;
pmallick 1:8792acb5a039 70
pmallick 1:8792acb5a039 71 /* Scale value per channel */
pmallick 1:8792acb5a039 72 static float attr_scale_val[NO_OF_CHANNELS] = {
pmallick 1:8792acb5a039 73 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE,
pmallick 1:8792acb5a039 74 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE,
pmallick 1:8792acb5a039 75 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE,
pmallick 1:8792acb5a039 76 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE
pmallick 1:8792acb5a039 77 };
pmallick 1:8792acb5a039 78
pmallick 1:8792acb5a039 79 /* Flag to trigger new data capture */
pmallick 1:8792acb5a039 80 static bool adc_data_capture_started = false;
pmallick 1:8792acb5a039 81
pmallick 1:8792acb5a039 82 /******************************************************************************/
pmallick 1:8792acb5a039 83 /************************ Functions Definitions *******************************/
pmallick 1:8792acb5a039 84 /******************************************************************************/
pmallick 1:8792acb5a039 85
pmallick 1:8792acb5a039 86 /*!
pmallick 1:8792acb5a039 87 * @brief Getter/Setter for the raw, offset and scale attribute value
pmallick 1:8792acb5a039 88 * @param device- pointer to IIO device structure
pmallick 1:8792acb5a039 89 * @param buf- pointer to buffer holding attribute value
pmallick 1:8792acb5a039 90 * @param len- length of buffer string data
pmallick 1:8792acb5a039 91 * @param channel- pointer to IIO channel structure
pmallick 1:8792acb5a039 92 * @param id- Attribute ID
pmallick 1:8792acb5a039 93 * @return Number of characters read/written
pmallick 1:8792acb5a039 94 */
pmallick 1:8792acb5a039 95 static ssize_t get_adc_raw(void *device,
pmallick 1:8792acb5a039 96 char *buf,
pmallick 1:8792acb5a039 97 size_t len,
pmallick 1:8792acb5a039 98 const struct iio_ch_info *channel,
pmallick 1:8792acb5a039 99 intptr_t id)
pmallick 1:8792acb5a039 100 {
pmallick 1:8792acb5a039 101 static uint32_t adc_data_raw = 0;
pmallick 1:8792acb5a039 102 int32_t offset = 0;
pmallick 1:8792acb5a039 103
pmallick 1:8792acb5a039 104 switch (id) {
pmallick 1:8792acb5a039 105 case IIO_RAW_ATTR_ID:
pmallick 1:8792acb5a039 106 /* Capture the raw adc data */
pmallick 1:8792acb5a039 107 if (read_single_sample((uint32_t)channel->ch_num, &adc_data_raw) != FAILURE) {
pmallick 1:8792acb5a039 108 return (ssize_t) sprintf(buf, "%d", adc_data_raw);
pmallick 1:8792acb5a039 109 }
pmallick 1:8792acb5a039 110 break;
pmallick 1:8792acb5a039 111
pmallick 1:8792acb5a039 112 case IIO_SCALE_ATTR_ID:
pmallick 1:8792acb5a039 113 return (ssize_t) sprintf(buf, "%f", attr_scale_val[channel->ch_num]);
pmallick 1:8792acb5a039 114 break;
pmallick 1:8792acb5a039 115
pmallick 1:8792acb5a039 116 case IIO_OFFSET_ATTR_ID:
pmallick 1:8792acb5a039 117 #if defined(PSEUDO_BIPOLAR_MODE)
pmallick 1:8792acb5a039 118 if (adc_data_raw >= ADC_MAX_COUNT_BIPOLAR) {
pmallick 1:8792acb5a039 119 offset = -ADC_MAX_COUNT_UNIPOLAR ;
pmallick 1:8792acb5a039 120 }
pmallick 1:8792acb5a039 121 else {
pmallick 1:8792acb5a039 122 offset = 0;
pmallick 1:8792acb5a039 123 }
pmallick 1:8792acb5a039 124 #endif
pmallick 1:8792acb5a039 125 return (ssize_t) sprintf(buf, "%d", offset);
pmallick 1:8792acb5a039 126 break;
pmallick 1:8792acb5a039 127
pmallick 1:8792acb5a039 128 default:
pmallick 1:8792acb5a039 129 break;
pmallick 1:8792acb5a039 130 }
pmallick 1:8792acb5a039 131
pmallick 1:8792acb5a039 132 return -EINVAL;
pmallick 1:8792acb5a039 133 }
pmallick 1:8792acb5a039 134
pmallick 1:8792acb5a039 135 static ssize_t set_adc_raw(void *device,
pmallick 1:8792acb5a039 136 char *buf,
pmallick 1:8792acb5a039 137 size_t len,
pmallick 1:8792acb5a039 138 const struct iio_ch_info *channel,
pmallick 1:8792acb5a039 139 intptr_t id)
pmallick 1:8792acb5a039 140 {
pmallick 1:8792acb5a039 141 /* ADC raw value, offset factor and scale factor are fixed for given configurations set
pmallick 1:8792acb5a039 142 * in the firmware */
pmallick 1:8792acb5a039 143 return len;
pmallick 1:8792acb5a039 144 }
pmallick 1:8792acb5a039 145
pmallick 1:8792acb5a039 146 /*!
pmallick 1:8792acb5a039 147 * @brief Getter/Setter for the sampling frequency attribute value
pmallick 1:8792acb5a039 148 * @param device- pointer to IIO device structure
pmallick 1:8792acb5a039 149 * @param buf- pointer to buffer holding attribute value
pmallick 1:8792acb5a039 150 * @param len- length of buffer string data
pmallick 1:8792acb5a039 151 * @param channel- pointer to IIO channel structure
pmallick 1:8792acb5a039 152 * @return Number of characters read/written
pmallick 1:8792acb5a039 153 * @Note This attribute is used to define the timeout period in IIO
pmallick 1:8792acb5a039 154 * client during data capture.
pmallick 1:8792acb5a039 155 * Timeout = (number of requested samples * (1/sampling frequency)) + 1sec
pmallick 1:8792acb5a039 156 * e.g. if sampling frequency = 50SPS and requested samples = 400
pmallick 1:8792acb5a039 157 * Timeout = (400 * 0.02) + 1 = 9sec
pmallick 1:8792acb5a039 158 */
pmallick 1:8792acb5a039 159 ssize_t get_sampling_frequency(void *device,
pmallick 1:8792acb5a039 160 char *buf,
pmallick 1:8792acb5a039 161 size_t len,
pmallick 1:8792acb5a039 162 const struct iio_ch_info *channel,
pmallick 1:8792acb5a039 163 intptr_t id)
pmallick 1:8792acb5a039 164 {
pmallick 1:8792acb5a039 165 return (ssize_t) sprintf(buf, "%d", SAMPLING_RATE);
pmallick 1:8792acb5a039 166 }
pmallick 1:8792acb5a039 167
pmallick 1:8792acb5a039 168 ssize_t set_sampling_frequency(void *device,
pmallick 1:8792acb5a039 169 char *buf,
pmallick 1:8792acb5a039 170 size_t len,
pmallick 1:8792acb5a039 171 const struct iio_ch_info *channel,
pmallick 1:8792acb5a039 172 intptr_t id)
pmallick 1:8792acb5a039 173 {
pmallick 1:8792acb5a039 174 /* Sampling frequency determines the IIO client timeout. It is defined in the
pmallick 1:8792acb5a039 175 * software and not allowed to change externally */
pmallick 1:8792acb5a039 176 return -EINVAL;
pmallick 1:8792acb5a039 177 }
pmallick 1:8792acb5a039 178
pmallick 1:8792acb5a039 179 /*!
pmallick 1:8792acb5a039 180 * @brief Read the debug register value
pmallick 1:8792acb5a039 181 * @param dev- Pointer to IIO device instance
pmallick 1:8792acb5a039 182 * @param reg- Register address to read from
pmallick 1:8792acb5a039 183 * @param readval- Pointer to variable to read data into
pmallick 1:8792acb5a039 184 * @return SUCCESS in case of success, negative value otherwise
pmallick 1:8792acb5a039 185 */
pmallick 1:8792acb5a039 186 int32_t debug_reg_read(void *dev, uint32_t reg, uint8_t *readval)
pmallick 1:8792acb5a039 187 {
pmallick 1:8792acb5a039 188 /* Read the data from device */
pmallick 1:8792acb5a039 189 if (reg <= NUM_OF_REGISTERS) {
pmallick 1:8792acb5a039 190 if (ad469x_spi_reg_read(dev, reg, readval) == SUCCESS) {
pmallick 1:8792acb5a039 191 return SUCCESS;
pmallick 1:8792acb5a039 192 }
pmallick 1:8792acb5a039 193 }
pmallick 1:8792acb5a039 194
pmallick 1:8792acb5a039 195 return FAILURE;
pmallick 1:8792acb5a039 196 }
pmallick 1:8792acb5a039 197
pmallick 1:8792acb5a039 198 /*!
pmallick 1:8792acb5a039 199 * @brief Write into the debug register
pmallick 1:8792acb5a039 200 * @param dev- Pointer to IIO device instance
pmallick 1:8792acb5a039 201 * @param reg- Register address to write into
pmallick 1:8792acb5a039 202 * @param writeval- Register value to write
pmallick 1:8792acb5a039 203 * @return SUCCESS in case of success, negative value otherwise
pmallick 1:8792acb5a039 204 */
pmallick 1:8792acb5a039 205 int32_t debug_reg_write(void *dev, uint32_t reg, uint32_t writeval)
pmallick 1:8792acb5a039 206 {
pmallick 1:8792acb5a039 207 if (reg <= NUM_OF_REGISTERS) {
pmallick 1:8792acb5a039 208 if (ad469x_spi_reg_write(dev, reg, writeval) == SUCCESS) {
pmallick 1:8792acb5a039 209 return SUCCESS;
pmallick 1:8792acb5a039 210 }
pmallick 1:8792acb5a039 211 }
pmallick 1:8792acb5a039 212
pmallick 1:8792acb5a039 213 return FAILURE;
pmallick 1:8792acb5a039 214 }
pmallick 1:8792acb5a039 215
pmallick 1:8792acb5a039 216 /**
pmallick 1:8792acb5a039 217 * @brief Read buffer data corresponding to AD4696 IIO device
pmallick 1:8792acb5a039 218 * @param dev_instance[in] - IIO device instance
pmallick 1:8792acb5a039 219 * @param pbuf[out] - Pointer to output data buffer
pmallick 1:8792acb5a039 220 * @param offset[in] - Data buffer offset
pmallick 1:8792acb5a039 221 * @param bytes_count[in] - Number of bytes to read
pmallick 1:8792acb5a039 222 * @param ch_mask[in] - Channels select mask
pmallick 1:8792acb5a039 223 * @return SUCCESS in case of success or negative value otherwise
pmallick 1:8792acb5a039 224 */
pmallick 1:8792acb5a039 225 static ssize_t iio_ad4696_read_data(void *dev_instance,
pmallick 1:8792acb5a039 226 char *pbuf,
pmallick 1:8792acb5a039 227 size_t offset,
pmallick 1:8792acb5a039 228 size_t bytes_count,
pmallick 1:8792acb5a039 229 uint32_t ch_mask)
pmallick 1:8792acb5a039 230 {
pmallick 1:8792acb5a039 231 if (adc_data_capture_started == false) {
pmallick 1:8792acb5a039 232 start_data_capture(ch_mask, AD469x_CHANNEL_NO);
pmallick 1:8792acb5a039 233 adc_data_capture_started = true;
pmallick 1:8792acb5a039 234 }
pmallick 1:8792acb5a039 235
pmallick 1:8792acb5a039 236 /* Read the buffered data */
pmallick 1:8792acb5a039 237 return (ssize_t)read_buffered_data(pbuf, bytes_count, offset, ch_mask,
pmallick 1:8792acb5a039 238 BYTES_PER_SAMPLE);
pmallick 1:8792acb5a039 239
pmallick 1:8792acb5a039 240 }
pmallick 1:8792acb5a039 241
pmallick 1:8792acb5a039 242 /**
pmallick 1:8792acb5a039 243 * @brief Transfer the device data into memory (optional)
pmallick 1:8792acb5a039 244 * @param dev_instance[in] - IIO device instance
pmallick 1:8792acb5a039 245 * @param bytes_count[in] - Number of bytes to read
pmallick 1:8792acb5a039 246 * @param ch_mask[in] - Channels select mask
pmallick 1:8792acb5a039 247 * @return SUCCESS in case of success or negative value otherwise
pmallick 1:8792acb5a039 248 */
pmallick 1:8792acb5a039 249 static ssize_t iio_ad4696_transfer_dev_data(void *dev_instance,
pmallick 1:8792acb5a039 250 size_t bytes_count, uint32_t ch_mask)
pmallick 1:8792acb5a039 251 {
pmallick 1:8792acb5a039 252 store_requested_samples_count(bytes_count, BYTES_PER_SAMPLE);
pmallick 1:8792acb5a039 253 return SUCCESS;
pmallick 1:8792acb5a039 254 }
pmallick 1:8792acb5a039 255
pmallick 1:8792acb5a039 256 /**
pmallick 1:8792acb5a039 257 * @brief Perform tasks before new data transfer
pmallick 1:8792acb5a039 258 * @param dev_instance[in] - IIO device instance
pmallick 1:8792acb5a039 259 * @param ch_mask[in] - Channels select mask
pmallick 1:8792acb5a039 260 * @return SUCCESS in case of success or negative value otherwise
pmallick 1:8792acb5a039 261 */
pmallick 1:8792acb5a039 262 static int32_t iio_ad4696_start_transfer(void *dev_instance, uint32_t ch_mask)
pmallick 1:8792acb5a039 263 {
pmallick 1:8792acb5a039 264 return SUCCESS;
pmallick 1:8792acb5a039 265 }
pmallick 1:8792acb5a039 266
pmallick 1:8792acb5a039 267
pmallick 1:8792acb5a039 268 /**
pmallick 1:8792acb5a039 269 * @brief Perform tasks before end of current data transfer
pmallick 1:8792acb5a039 270 * @param dev_instance[in] - IIO device instance
pmallick 1:8792acb5a039 271 * @return SUCCESS in case of success or negative value otherwise
pmallick 1:8792acb5a039 272 */
pmallick 1:8792acb5a039 273 static int32_t iio_ad4696_stop_transfer(void *dev)
pmallick 1:8792acb5a039 274 {
pmallick 1:8792acb5a039 275 adc_data_capture_started = false;
pmallick 1:8792acb5a039 276 stop_data_capture();
pmallick 1:8792acb5a039 277
pmallick 1:8792acb5a039 278 return SUCCESS;
pmallick 1:8792acb5a039 279 }
pmallick 1:8792acb5a039 280
pmallick 1:8792acb5a039 281 /*********************************************************
pmallick 1:8792acb5a039 282 * IIO Attributes and Structures
pmallick 1:8792acb5a039 283 ********************************************************/
pmallick 1:8792acb5a039 284
pmallick 1:8792acb5a039 285 /* IIOD channels attributes list */
pmallick 1:8792acb5a039 286 struct iio_attribute channel_input_attributes[] = {
pmallick 1:8792acb5a039 287 {
pmallick 1:8792acb5a039 288 .name = "raw",
pmallick 1:8792acb5a039 289 .show = get_adc_raw,
pmallick 1:8792acb5a039 290 .store = set_adc_raw,
pmallick 1:8792acb5a039 291 .priv = IIO_RAW_ATTR_ID
pmallick 1:8792acb5a039 292 },
pmallick 1:8792acb5a039 293 {
pmallick 1:8792acb5a039 294 .name = "scale",
pmallick 1:8792acb5a039 295 .show = get_adc_raw,
pmallick 1:8792acb5a039 296 .store = set_adc_raw,
pmallick 1:8792acb5a039 297 .priv = IIO_SCALE_ATTR_ID
pmallick 1:8792acb5a039 298 },
pmallick 1:8792acb5a039 299 {
pmallick 1:8792acb5a039 300 .name = "offset",
pmallick 1:8792acb5a039 301 .show = get_adc_raw,
pmallick 1:8792acb5a039 302 .store = set_adc_raw,
pmallick 1:8792acb5a039 303 .priv = IIO_OFFSET_ATTR_ID
pmallick 1:8792acb5a039 304 },
pmallick 1:8792acb5a039 305
pmallick 1:8792acb5a039 306 END_ATTRIBUTES_ARRAY
pmallick 1:8792acb5a039 307 };
pmallick 1:8792acb5a039 308
pmallick 1:8792acb5a039 309 /* IIOD device (global) attributes list */
pmallick 1:8792acb5a039 310 static struct iio_attribute global_attributes[] = {
pmallick 1:8792acb5a039 311 {
pmallick 1:8792acb5a039 312 .name = "sampling_frequency",
pmallick 1:8792acb5a039 313 .show = get_sampling_frequency,
pmallick 1:8792acb5a039 314 .store = set_sampling_frequency,
pmallick 1:8792acb5a039 315 },
pmallick 1:8792acb5a039 316
pmallick 1:8792acb5a039 317 END_ATTRIBUTES_ARRAY
pmallick 1:8792acb5a039 318 };
pmallick 1:8792acb5a039 319
pmallick 1:8792acb5a039 320 /* IIOD debug attributes list */
pmallick 1:8792acb5a039 321 static struct iio_attribute debug_attributes[] = {
pmallick 1:8792acb5a039 322 {
pmallick 1:8792acb5a039 323 .name = "direct_reg_access",
pmallick 1:8792acb5a039 324 .show = NULL,
pmallick 1:8792acb5a039 325 .store = NULL,
pmallick 1:8792acb5a039 326 },
pmallick 1:8792acb5a039 327
pmallick 1:8792acb5a039 328 END_ATTRIBUTES_ARRAY
pmallick 1:8792acb5a039 329 };
pmallick 1:8792acb5a039 330
pmallick 1:8792acb5a039 331 /* IIOD channels configurations */
pmallick 1:8792acb5a039 332 struct scan_type chn_scan = {
pmallick 1:8792acb5a039 333 #if defined(PSEUDO_BIPOLAR_MODE)
pmallick 1:8792acb5a039 334 .sign = 's',
pmallick 1:8792acb5a039 335 #else
pmallick 1:8792acb5a039 336 .sign = 'u',
pmallick 1:8792acb5a039 337 #endif
pmallick 1:8792acb5a039 338 .realbits = CHN_STORAGE_BITS,
pmallick 1:8792acb5a039 339 .storagebits = CHN_STORAGE_BITS,
pmallick 1:8792acb5a039 340 .shift = 0,
pmallick 1:8792acb5a039 341 .is_big_endian = false
pmallick 1:8792acb5a039 342 };
pmallick 1:8792acb5a039 343 static struct iio_channel iio_ad4696_channels[] = {
pmallick 1:8792acb5a039 344 {
pmallick 1:8792acb5a039 345 .name = "voltage0",
pmallick 1:8792acb5a039 346 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 347 .channel = 0,
pmallick 1:8792acb5a039 348 .scan_index = 0,
pmallick 1:8792acb5a039 349 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 350 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 351 .ch_out = false,
pmallick 1:8792acb5a039 352 .indexed = true,
pmallick 1:8792acb5a039 353 },
pmallick 1:8792acb5a039 354 {
pmallick 1:8792acb5a039 355 .name = "voltage1",
pmallick 1:8792acb5a039 356 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 357 .channel = 1,
pmallick 1:8792acb5a039 358 .scan_index = 1,
pmallick 1:8792acb5a039 359 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 360 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 361 .ch_out = false,
pmallick 1:8792acb5a039 362 .indexed = true
pmallick 1:8792acb5a039 363 },
pmallick 1:8792acb5a039 364 {
pmallick 1:8792acb5a039 365 .name = "voltage2",
pmallick 1:8792acb5a039 366 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 367 .channel = 2,
pmallick 1:8792acb5a039 368 .scan_index = 2,
pmallick 1:8792acb5a039 369 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 370 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 371 .ch_out = false,
pmallick 1:8792acb5a039 372 .indexed = true
pmallick 1:8792acb5a039 373 },
pmallick 1:8792acb5a039 374 {
pmallick 1:8792acb5a039 375 .name = "voltage3",
pmallick 1:8792acb5a039 376 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 377 .channel = 3,
pmallick 1:8792acb5a039 378 .scan_index = 3,
pmallick 1:8792acb5a039 379 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 380 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 381 .ch_out = false,
pmallick 1:8792acb5a039 382 .indexed = true
pmallick 1:8792acb5a039 383 },
pmallick 1:8792acb5a039 384 {
pmallick 1:8792acb5a039 385 .name = "voltage4",
pmallick 1:8792acb5a039 386 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 387 .channel = 4,
pmallick 1:8792acb5a039 388 .scan_index = 4,
pmallick 1:8792acb5a039 389 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 390 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 391 .ch_out = false,
pmallick 1:8792acb5a039 392 .indexed = true
pmallick 1:8792acb5a039 393 },
pmallick 1:8792acb5a039 394 {
pmallick 1:8792acb5a039 395 .name = "voltage5",
pmallick 1:8792acb5a039 396 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 397 .channel = 5,
pmallick 1:8792acb5a039 398 .scan_index = 5,
pmallick 1:8792acb5a039 399 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 400 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 401 .ch_out = false,
pmallick 1:8792acb5a039 402 .indexed = true
pmallick 1:8792acb5a039 403 },
pmallick 1:8792acb5a039 404 {
pmallick 1:8792acb5a039 405 .name = "voltage6",
pmallick 1:8792acb5a039 406 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 407 .channel = 6,
pmallick 1:8792acb5a039 408 .scan_index = 6,
pmallick 1:8792acb5a039 409 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 410 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 411 .ch_out = false,
pmallick 1:8792acb5a039 412 .indexed = true
pmallick 1:8792acb5a039 413 },
pmallick 1:8792acb5a039 414 {
pmallick 1:8792acb5a039 415 .name = "voltage7",
pmallick 1:8792acb5a039 416 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 417 .channel = 7,
pmallick 1:8792acb5a039 418 .scan_index = 7,
pmallick 1:8792acb5a039 419 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 420 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 421 .ch_out = false,
pmallick 1:8792acb5a039 422 .indexed = true
pmallick 1:8792acb5a039 423 },
pmallick 1:8792acb5a039 424 {
pmallick 1:8792acb5a039 425 .name = "voltage8",
pmallick 1:8792acb5a039 426 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 427 .channel = 8,
pmallick 1:8792acb5a039 428 .scan_index = 8,
pmallick 1:8792acb5a039 429 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 430 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 431 .ch_out = false,
pmallick 1:8792acb5a039 432 .indexed = true,
pmallick 1:8792acb5a039 433 },
pmallick 1:8792acb5a039 434 {
pmallick 1:8792acb5a039 435 .name = "voltage9",
pmallick 1:8792acb5a039 436 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 437 .channel = 9,
pmallick 1:8792acb5a039 438 .scan_index = 9,
pmallick 1:8792acb5a039 439 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 440 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 441 .ch_out = false,
pmallick 1:8792acb5a039 442 .indexed = true
pmallick 1:8792acb5a039 443 },
pmallick 1:8792acb5a039 444 {
pmallick 1:8792acb5a039 445 .name = "voltage10",
pmallick 1:8792acb5a039 446 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 447 .channel = 10,
pmallick 1:8792acb5a039 448 .scan_index = 10,
pmallick 1:8792acb5a039 449 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 450 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 451 .ch_out = false,
pmallick 1:8792acb5a039 452 .indexed = true
pmallick 1:8792acb5a039 453 },
pmallick 1:8792acb5a039 454 {
pmallick 1:8792acb5a039 455 .name = "voltage11",
pmallick 1:8792acb5a039 456 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 457 .channel = 11,
pmallick 1:8792acb5a039 458 .scan_index = 11,
pmallick 1:8792acb5a039 459 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 460 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 461 .ch_out = false,
pmallick 1:8792acb5a039 462 .indexed = true
pmallick 1:8792acb5a039 463 },
pmallick 1:8792acb5a039 464 {
pmallick 1:8792acb5a039 465 .name = "voltage12",
pmallick 1:8792acb5a039 466 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 467 .channel = 12,
pmallick 1:8792acb5a039 468 .scan_index = 12,
pmallick 1:8792acb5a039 469 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 470 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 471 .ch_out = false,
pmallick 1:8792acb5a039 472 .indexed = true
pmallick 1:8792acb5a039 473 },
pmallick 1:8792acb5a039 474 {
pmallick 1:8792acb5a039 475 .name = "voltage13",
pmallick 1:8792acb5a039 476 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 477 .channel = 13,
pmallick 1:8792acb5a039 478 .scan_index = 13,
pmallick 1:8792acb5a039 479 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 480 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 481 .ch_out = false,
pmallick 1:8792acb5a039 482 .indexed = true
pmallick 1:8792acb5a039 483 },
pmallick 1:8792acb5a039 484 {
pmallick 1:8792acb5a039 485 .name = "voltage14",
pmallick 1:8792acb5a039 486 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 487 .channel = 14,
pmallick 1:8792acb5a039 488 .scan_index = 14,
pmallick 1:8792acb5a039 489 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 490 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 491 .ch_out = false,
pmallick 1:8792acb5a039 492 .indexed = true
pmallick 1:8792acb5a039 493 },
pmallick 1:8792acb5a039 494 {
pmallick 1:8792acb5a039 495 .name = "voltage15",
pmallick 1:8792acb5a039 496 .ch_type = IIO_VOLTAGE,
pmallick 1:8792acb5a039 497 .channel = 15,
pmallick 1:8792acb5a039 498 .scan_index = 15,
pmallick 1:8792acb5a039 499 .scan_type = &chn_scan,
pmallick 1:8792acb5a039 500 .attributes = channel_input_attributes,
pmallick 1:8792acb5a039 501 .ch_out = false,
pmallick 1:8792acb5a039 502 .indexed = true
pmallick 1:8792acb5a039 503 },
pmallick 1:8792acb5a039 504 };
pmallick 1:8792acb5a039 505
pmallick 1:8792acb5a039 506 /**
pmallick 1:8792acb5a039 507 * @brief Init for reading/writing and parameterization of a
pmallick 1:8792acb5a039 508 * ad4696 IIO device
pmallick 1:8792acb5a039 509 * @param desc[in,out] - IIO device descriptor
pmallick 1:8792acb5a039 510 * @return SUCCESS in case of success, FAILURE otherwise
pmallick 1:8792acb5a039 511 */
pmallick 1:8792acb5a039 512 static int32_t iio_ad4696_init(struct iio_device **desc)
pmallick 1:8792acb5a039 513 {
pmallick 1:8792acb5a039 514 struct iio_device *iio_ad4696_inst;
pmallick 1:8792acb5a039 515
pmallick 1:8792acb5a039 516 iio_ad4696_inst = calloc(1, sizeof(struct iio_device));
pmallick 1:8792acb5a039 517 if (!iio_ad4696_inst) {
pmallick 1:8792acb5a039 518 return FAILURE;
pmallick 1:8792acb5a039 519 }
pmallick 1:8792acb5a039 520
pmallick 1:8792acb5a039 521 iio_ad4696_inst->num_ch = sizeof(iio_ad4696_channels) / sizeof(
pmallick 1:8792acb5a039 522 iio_ad4696_channels[0]);
pmallick 1:8792acb5a039 523 iio_ad4696_inst->channels = iio_ad4696_channels;
pmallick 1:8792acb5a039 524 iio_ad4696_inst->attributes = global_attributes;
pmallick 1:8792acb5a039 525 iio_ad4696_inst->debug_attributes = debug_attributes;
pmallick 1:8792acb5a039 526
pmallick 1:8792acb5a039 527 iio_ad4696_inst->transfer_dev_to_mem = iio_ad4696_transfer_dev_data;
pmallick 1:8792acb5a039 528 iio_ad4696_inst->transfer_mem_to_dev = NULL;
pmallick 1:8792acb5a039 529 iio_ad4696_inst->read_data = iio_ad4696_read_data;
pmallick 1:8792acb5a039 530 iio_ad4696_inst->write_data = NULL;
pmallick 1:8792acb5a039 531 iio_ad4696_inst->prepare_transfer = iio_ad4696_start_transfer;
pmallick 1:8792acb5a039 532 iio_ad4696_inst->end_transfer = iio_ad4696_stop_transfer;
pmallick 1:8792acb5a039 533 iio_ad4696_inst->debug_reg_read = debug_reg_read;
pmallick 1:8792acb5a039 534 iio_ad4696_inst->debug_reg_write = debug_reg_write;
pmallick 1:8792acb5a039 535
pmallick 1:8792acb5a039 536 *desc = iio_ad4696_inst;
pmallick 1:8792acb5a039 537
pmallick 1:8792acb5a039 538 return SUCCESS;
pmallick 1:8792acb5a039 539 }
pmallick 1:8792acb5a039 540
pmallick 1:8792acb5a039 541 /**
pmallick 1:8792acb5a039 542 * @brief Release resources allocated for IIO device
pmallick 1:8792acb5a039 543 * @param desc[in] - IIO device descriptor
pmallick 1:8792acb5a039 544 * @return SUCCESS in case of success, FAILURE otherwise
pmallick 1:8792acb5a039 545 */
pmallick 1:8792acb5a039 546 static int32_t iio_ad4696_remove(struct iio_desc *desc)
pmallick 1:8792acb5a039 547 {
pmallick 1:8792acb5a039 548 int32_t status;
pmallick 1:8792acb5a039 549
pmallick 1:8792acb5a039 550 if (!desc) {
pmallick 1:8792acb5a039 551 return FAILURE;
pmallick 1:8792acb5a039 552 }
pmallick 1:8792acb5a039 553
pmallick 1:8792acb5a039 554 status = iio_unregister(desc, (char *)dev_name);
pmallick 1:8792acb5a039 555 if (status != SUCCESS) {
pmallick 1:8792acb5a039 556 return FAILURE;
pmallick 1:8792acb5a039 557 }
pmallick 1:8792acb5a039 558
pmallick 1:8792acb5a039 559 return SUCCESS;
pmallick 1:8792acb5a039 560 }
pmallick 1:8792acb5a039 561
pmallick 1:8792acb5a039 562
pmallick 1:8792acb5a039 563 /**
pmallick 1:8792acb5a039 564 * @brief Initialize the IIO interface for AD4696 IIO device
pmallick 1:8792acb5a039 565 * @return SUCCESS in case of success, FAILURE otherwise
pmallick 1:8792acb5a039 566 */
pmallick 1:8792acb5a039 567 int32_t ad4696_iio_initialize(void)
pmallick 1:8792acb5a039 568 {
pmallick 1:8792acb5a039 569 int32_t init_status;
pmallick 1:8792acb5a039 570
pmallick 1:8792acb5a039 571 /* IIO device descriptor */
pmallick 1:8792acb5a039 572 struct iio_device *p_iio_ad4696_dev;
pmallick 1:8792acb5a039 573
pmallick 1:8792acb5a039 574 /* IIO interface init parameter */
pmallick 1:8792acb5a039 575 struct iio_init_param iio_init_params = {
pmallick 1:8792acb5a039 576 .phy_type = USE_UART,
pmallick 1:8792acb5a039 577 {
pmallick 1:8792acb5a039 578 &uart_init_params
pmallick 1:8792acb5a039 579 }
pmallick 1:8792acb5a039 580 };
pmallick 1:8792acb5a039 581
pmallick 1:8792acb5a039 582 /* Initialize AD4696 device and peripheral interface */
pmallick 1:8792acb5a039 583 init_status = ad469x_init(&p_ad4696_dev, &ad4696_init_str);
pmallick 1:8792acb5a039 584 if (init_status != SUCCESS) {
pmallick 1:8792acb5a039 585 return init_status;
pmallick 1:8792acb5a039 586 }
pmallick 1:8792acb5a039 587
pmallick 1:8792acb5a039 588 /* Initialize the IIO interface */
pmallick 1:8792acb5a039 589 init_status = iio_init(&p_ad4696_iio_desc, &iio_init_params);
pmallick 1:8792acb5a039 590 if (init_status != SUCCESS) {
pmallick 1:8792acb5a039 591 return init_status;
pmallick 1:8792acb5a039 592 }
pmallick 1:8792acb5a039 593
pmallick 1:8792acb5a039 594 /* Register and initialize the AD4696 device into IIO interface */
pmallick 1:8792acb5a039 595 init_status = iio_ad4696_init(&p_iio_ad4696_dev);
pmallick 1:8792acb5a039 596 if (init_status != SUCCESS) {
pmallick 1:8792acb5a039 597 return init_status;
pmallick 1:8792acb5a039 598 }
pmallick 1:8792acb5a039 599
pmallick 1:8792acb5a039 600 /* Register AD4696 IIO interface */
pmallick 1:8792acb5a039 601 init_status = iio_register(p_ad4696_iio_desc, p_iio_ad4696_dev,
pmallick 1:8792acb5a039 602 (char *)dev_name, p_ad4696_dev, NULL,
pmallick 1:8792acb5a039 603 NULL);
pmallick 1:8792acb5a039 604 if (init_status != SUCCESS) {
pmallick 1:8792acb5a039 605 return init_status;
pmallick 1:8792acb5a039 606 }
pmallick 1:8792acb5a039 607
pmallick 1:8792acb5a039 608 /* Init the system peripherals */
pmallick 1:8792acb5a039 609 init_status = init_system();
pmallick 1:8792acb5a039 610 if (init_status != SUCCESS) {
pmallick 1:8792acb5a039 611 return init_status;
pmallick 1:8792acb5a039 612 }
pmallick 1:8792acb5a039 613
pmallick 1:8792acb5a039 614 /* To enable manual trigger mode for operation */
pmallick 1:8792acb5a039 615 init_status = ad4696_enable_manual_trigger_mode(p_ad4696_dev);
pmallick 1:8792acb5a039 616 if (init_status != SUCCESS) {
pmallick 1:8792acb5a039 617 return init_status;
pmallick 1:8792acb5a039 618 }
pmallick 1:8792acb5a039 619
pmallick 1:8792acb5a039 620 return SUCCESS;
pmallick 1:8792acb5a039 621 }
pmallick 1:8792acb5a039 622
pmallick 1:8792acb5a039 623 /**
pmallick 1:8792acb5a039 624 * @brief Run the AD4696 IIO event handler
pmallick 1:8792acb5a039 625 * @return none
pmallick 1:8792acb5a039 626 * @details This function monitors the new IIO client event
pmallick 1:8792acb5a039 627 */
pmallick 1:8792acb5a039 628 void ad4696_iio_event_handler(void)
pmallick 1:8792acb5a039 629 {
pmallick 1:8792acb5a039 630 while(1) {
pmallick 1:8792acb5a039 631 (void)iio_step(p_ad4696_iio_desc);
pmallick 1:8792acb5a039 632 }
pmallick 1:8792acb5a039 633 }