Example program for EVAL-AD4130

Dependencies:   tempsensors sdp_k1_sdram

Committer:
Mahesh Phalke
Date:
Wed Jul 20 18:12:00 2022 +0530
Revision:
2:7b2b268ea49c
Initial firmware commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mahesh Phalke 2:7b2b268ea49c 1 /***************************************************************************//**
Mahesh Phalke 2:7b2b268ea49c 2 * @file ad4130_iio.c
Mahesh Phalke 2:7b2b268ea49c 3 * @brief Implementation of AD4130 IIO application interfaces
Mahesh Phalke 2:7b2b268ea49c 4 * @details This module acts as an interface for AD4130 IIO application
Mahesh Phalke 2:7b2b268ea49c 5 ********************************************************************************
Mahesh Phalke 2:7b2b268ea49c 6 * Copyright (c) 2020-2022 Analog Devices, Inc.
Mahesh Phalke 2:7b2b268ea49c 7 * All rights reserved.
Mahesh Phalke 2:7b2b268ea49c 8 *
Mahesh Phalke 2:7b2b268ea49c 9 * This software is proprietary to Analog Devices, Inc. and its licensors.
Mahesh Phalke 2:7b2b268ea49c 10 * By using this software you agree to the terms of the associated
Mahesh Phalke 2:7b2b268ea49c 11 * Analog Devices Software License Agreement.
Mahesh Phalke 2:7b2b268ea49c 12 *******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 13
Mahesh Phalke 2:7b2b268ea49c 14 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 15 /***************************** Include Files **********************************/
Mahesh Phalke 2:7b2b268ea49c 16 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 17
Mahesh Phalke 2:7b2b268ea49c 18 #include <stdint.h>
Mahesh Phalke 2:7b2b268ea49c 19 #include <string.h>
Mahesh Phalke 2:7b2b268ea49c 20 #include <stdio.h>
Mahesh Phalke 2:7b2b268ea49c 21 #include <errno.h>
Mahesh Phalke 2:7b2b268ea49c 22
Mahesh Phalke 2:7b2b268ea49c 23 #include "app_config.h"
Mahesh Phalke 2:7b2b268ea49c 24 #include "ad4130_iio.h"
Mahesh Phalke 2:7b2b268ea49c 25 #include "ad4130_data_capture.h"
Mahesh Phalke 2:7b2b268ea49c 26 #include "ad4130_support.h"
Mahesh Phalke 2:7b2b268ea49c 27 #include "ad4130_user_config.h"
Mahesh Phalke 2:7b2b268ea49c 28 #include "ad4130_temperature_sensor.h"
Mahesh Phalke 2:7b2b268ea49c 29 #include "ad4130_regs.h"
Mahesh Phalke 2:7b2b268ea49c 30 #include "no_os_error.h"
Mahesh Phalke 2:7b2b268ea49c 31
Mahesh Phalke 2:7b2b268ea49c 32 /******** Forward declaration of getter/setter functions ********/
Mahesh Phalke 2:7b2b268ea49c 33 static int iio_ad4130_attr_get(void *device, char *buf, uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 34 const struct iio_ch_info *channel, intptr_t priv);
Mahesh Phalke 2:7b2b268ea49c 35
Mahesh Phalke 2:7b2b268ea49c 36 static int iio_ad4130_attr_set(void *device, char *buf, uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 37 const struct iio_ch_info *channel, intptr_t priv);
Mahesh Phalke 2:7b2b268ea49c 38
Mahesh Phalke 2:7b2b268ea49c 39 static int iio_ad4130_attr_available_get(void *device, char *buf,
Mahesh Phalke 2:7b2b268ea49c 40 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 41 const struct iio_ch_info *channel, intptr_t priv);
Mahesh Phalke 2:7b2b268ea49c 42
Mahesh Phalke 2:7b2b268ea49c 43 static int iio_ad4130_attr_available_set(void *device, char *buf,
Mahesh Phalke 2:7b2b268ea49c 44 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 45 const struct iio_ch_info *channel, intptr_t priv);
Mahesh Phalke 2:7b2b268ea49c 46
Mahesh Phalke 2:7b2b268ea49c 47 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 48 /************************ Macros/Constants ************************************/
Mahesh Phalke 2:7b2b268ea49c 49 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 50
Mahesh Phalke 2:7b2b268ea49c 51 #define AD4130_CHN_ATTR(_name, _priv) {\
Mahesh Phalke 2:7b2b268ea49c 52 .name = _name,\
Mahesh Phalke 2:7b2b268ea49c 53 .priv = _priv,\
Mahesh Phalke 2:7b2b268ea49c 54 .show = iio_ad4130_attr_get,\
Mahesh Phalke 2:7b2b268ea49c 55 .store = iio_ad4130_attr_set\
Mahesh Phalke 2:7b2b268ea49c 56 }
Mahesh Phalke 2:7b2b268ea49c 57
Mahesh Phalke 2:7b2b268ea49c 58 #define AD4130_CHN_AVAIL_ATTR(_name, _priv) {\
Mahesh Phalke 2:7b2b268ea49c 59 .name = _name,\
Mahesh Phalke 2:7b2b268ea49c 60 .priv = _priv,\
Mahesh Phalke 2:7b2b268ea49c 61 .show = iio_ad4130_attr_available_get,\
Mahesh Phalke 2:7b2b268ea49c 62 .store = iio_ad4130_attr_available_set\
Mahesh Phalke 2:7b2b268ea49c 63 }
Mahesh Phalke 2:7b2b268ea49c 64
Mahesh Phalke 2:7b2b268ea49c 65 #define AD4130_CH(_name, _idx, _type) {\
Mahesh Phalke 2:7b2b268ea49c 66 .name = _name, \
Mahesh Phalke 2:7b2b268ea49c 67 .ch_type = _type,\
Mahesh Phalke 2:7b2b268ea49c 68 .ch_out = 0,\
Mahesh Phalke 2:7b2b268ea49c 69 .indexed = true,\
Mahesh Phalke 2:7b2b268ea49c 70 .channel = _idx,\
Mahesh Phalke 2:7b2b268ea49c 71 .scan_index = _idx,\
Mahesh Phalke 2:7b2b268ea49c 72 .scan_type = &chn_scan,\
Mahesh Phalke 2:7b2b268ea49c 73 .attributes = ad4130_iio_ch_attributes\
Mahesh Phalke 2:7b2b268ea49c 74 }
Mahesh Phalke 2:7b2b268ea49c 75
Mahesh Phalke 2:7b2b268ea49c 76 /* Minimum sampling frequency supported/configured in the firmware.
Mahesh Phalke 2:7b2b268ea49c 77 * Note: This is not an actual device sampling frequency.
Mahesh Phalke 2:7b2b268ea49c 78 * It is just used for IIO oscilloscope timeout calculations. */
Mahesh Phalke 2:7b2b268ea49c 79 #define AD4130_MIN_SAMPLING_FREQ (50 / ADC_USER_CHANNELS)
Mahesh Phalke 2:7b2b268ea49c 80
Mahesh Phalke 2:7b2b268ea49c 81 /* Default offset value for AD4130 */
Mahesh Phalke 2:7b2b268ea49c 82 #define AD4130_DEFAULT_OFFSET 0x800000
Mahesh Phalke 2:7b2b268ea49c 83
Mahesh Phalke 2:7b2b268ea49c 84 /* Bytes per sample. This count should divide the total 256 bytes into 'n' equivalent
Mahesh Phalke 2:7b2b268ea49c 85 * ADC samples as IIO library requests only 256bytes of data at a time in a given
Mahesh Phalke 2:7b2b268ea49c 86 * data read query.
Mahesh Phalke 2:7b2b268ea49c 87 * For 1 to 8-bit ADC, bytes per sample = 1 (2^0)
Mahesh Phalke 2:7b2b268ea49c 88 * For 9 to 16-bit ADC, bytes per sample = 2 (2^1)
Mahesh Phalke 2:7b2b268ea49c 89 * For 17 to 32-bit ADC, bytes per sample = 4 (2^2)
Mahesh Phalke 2:7b2b268ea49c 90 **/
Mahesh Phalke 2:7b2b268ea49c 91 #define BYTES_PER_SAMPLE sizeof(uint32_t) // For ADC resolution of 24-bits
Mahesh Phalke 2:7b2b268ea49c 92
Mahesh Phalke 2:7b2b268ea49c 93 /* Number of data storage bits (needed for IIO client to plot ADC data) */
Mahesh Phalke 2:7b2b268ea49c 94 #define CHN_STORAGE_BITS (BYTES_PER_SAMPLE * 8)
Mahesh Phalke 2:7b2b268ea49c 95
Mahesh Phalke 2:7b2b268ea49c 96 /* Number of adc samples for loadcell calibration */
Mahesh Phalke 2:7b2b268ea49c 97 #define LOADCELL_SAMPLES_COUNT 10
Mahesh Phalke 2:7b2b268ea49c 98
Mahesh Phalke 2:7b2b268ea49c 99 /* CJC channel is 1 (common sensor for all Thermocouples).
Mahesh Phalke 2:7b2b268ea49c 100 * Chn0 is used for TC connections */
Mahesh Phalke 2:7b2b268ea49c 101 #define CJC_CHANNEL 1
Mahesh Phalke 2:7b2b268ea49c 102
Mahesh Phalke 2:7b2b268ea49c 103 /* Shunt resistance (in ohms) for AVDD/IOVDD current calculation */
Mahesh Phalke 2:7b2b268ea49c 104 #define I_RSENSE 10
Mahesh Phalke 2:7b2b268ea49c 105
Mahesh Phalke 2:7b2b268ea49c 106 /* Multiplier for AVDD/IOVDD voltage calculation */
Mahesh Phalke 2:7b2b268ea49c 107 #define V_SCALE 6
Mahesh Phalke 2:7b2b268ea49c 108
Mahesh Phalke 2:7b2b268ea49c 109 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 110 /*************************** Types Declarations *******************************/
Mahesh Phalke 2:7b2b268ea49c 111 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 112
Mahesh Phalke 2:7b2b268ea49c 113 /* IIO interface descriptor */
Mahesh Phalke 2:7b2b268ea49c 114 static struct iio_desc *p_ad4130_iio_desc;
Mahesh Phalke 2:7b2b268ea49c 115
Mahesh Phalke 2:7b2b268ea49c 116 /**
Mahesh Phalke 2:7b2b268ea49c 117 * Device name.
Mahesh Phalke 2:7b2b268ea49c 118 */
Mahesh Phalke 2:7b2b268ea49c 119 static const char dev_name[] = ACTIVE_DEVICE_NAME;
Mahesh Phalke 2:7b2b268ea49c 120
Mahesh Phalke 2:7b2b268ea49c 121 /**
Mahesh Phalke 2:7b2b268ea49c 122 * Pointer to the struct representing the AD4130 IIO device
Mahesh Phalke 2:7b2b268ea49c 123 */
Mahesh Phalke 2:7b2b268ea49c 124 struct ad413x_dev *ad4130_dev_inst = NULL;
Mahesh Phalke 2:7b2b268ea49c 125
Mahesh Phalke 2:7b2b268ea49c 126 /* Device attributes with default values */
Mahesh Phalke 2:7b2b268ea49c 127
Mahesh Phalke 2:7b2b268ea49c 128 /* Scale attribute value per channel */
Mahesh Phalke 2:7b2b268ea49c 129 static float attr_scale_val[ADC_USER_CHANNELS];
Mahesh Phalke 2:7b2b268ea49c 130
Mahesh Phalke 2:7b2b268ea49c 131 /* IIOD channels scan structure */
Mahesh Phalke 2:7b2b268ea49c 132 static struct scan_type chn_scan;
Mahesh Phalke 2:7b2b268ea49c 133
Mahesh Phalke 2:7b2b268ea49c 134 /* AD4130 Attribute IDs */
Mahesh Phalke 2:7b2b268ea49c 135 enum ad4130_attribute_id {
Mahesh Phalke 2:7b2b268ea49c 136 RAW_ATTR_ID,
Mahesh Phalke 2:7b2b268ea49c 137 SCALE_ATTR_ID,
Mahesh Phalke 2:7b2b268ea49c 138 OFFSET_ATTR_ID,
Mahesh Phalke 2:7b2b268ea49c 139 SAMPLING_FREQ_ATTR_ID,
Mahesh Phalke 2:7b2b268ea49c 140 DEMO_CONFIG_ATTR_ID,
Mahesh Phalke 2:7b2b268ea49c 141 INTERNAL_CALIB_ID,
Mahesh Phalke 2:7b2b268ea49c 142 SYSTEM_CALIB_ID,
Mahesh Phalke 2:7b2b268ea49c 143 LOADCELL_GAIN_CALIB_ID,
Mahesh Phalke 2:7b2b268ea49c 144 LOADCELL_OFFSET_CALIB_ID,
Mahesh Phalke 2:7b2b268ea49c 145 };
Mahesh Phalke 2:7b2b268ea49c 146
Mahesh Phalke 2:7b2b268ea49c 147 /* Calibration state */
Mahesh Phalke 2:7b2b268ea49c 148 enum calibration_state {
Mahesh Phalke 2:7b2b268ea49c 149 FULL_SCALE_CALIB_STATE,
Mahesh Phalke 2:7b2b268ea49c 150 ZERO_SCALE_CALIB_STATE,
Mahesh Phalke 2:7b2b268ea49c 151 CALIB_COMPLETE_STATE
Mahesh Phalke 2:7b2b268ea49c 152 };
Mahesh Phalke 2:7b2b268ea49c 153
Mahesh Phalke 2:7b2b268ea49c 154 /* Calibration status */
Mahesh Phalke 2:7b2b268ea49c 155 enum calib_status {
Mahesh Phalke 2:7b2b268ea49c 156 CALIB_NOT_DONE,
Mahesh Phalke 2:7b2b268ea49c 157 CALIB_IN_PROGRESS,
Mahesh Phalke 2:7b2b268ea49c 158 CALIB_DONE,
Mahesh Phalke 2:7b2b268ea49c 159 CALIB_ERROR,
Mahesh Phalke 2:7b2b268ea49c 160 CALIB_SKIPPED
Mahesh Phalke 2:7b2b268ea49c 161 };
Mahesh Phalke 2:7b2b268ea49c 162
Mahesh Phalke 2:7b2b268ea49c 163 /* ADC calibration configs */
Mahesh Phalke 2:7b2b268ea49c 164 typedef struct {
Mahesh Phalke 2:7b2b268ea49c 165 uint32_t gain_before_calib;
Mahesh Phalke 2:7b2b268ea49c 166 uint32_t gain_after_calib;
Mahesh Phalke 2:7b2b268ea49c 167 uint32_t offset_before_calib;
Mahesh Phalke 2:7b2b268ea49c 168 uint32_t offset_after_calib;
Mahesh Phalke 2:7b2b268ea49c 169 } adc_calibration_configs;
Mahesh Phalke 2:7b2b268ea49c 170
Mahesh Phalke 2:7b2b268ea49c 171 /* ADC calibration variables */
Mahesh Phalke 2:7b2b268ea49c 172 static enum calibration_state system_calibration_state = ZERO_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 173 static enum calibration_state internal_calibration_state =
Mahesh Phalke 2:7b2b268ea49c 174 FULL_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 175 static enum calib_status adc_calibration_status[ADC_USER_CHANNELS];
Mahesh Phalke 2:7b2b268ea49c 176 static adc_calibration_configs adc_calibration_config[ADC_USER_CHANNELS];
Mahesh Phalke 2:7b2b268ea49c 177
Mahesh Phalke 2:7b2b268ea49c 178 /* IIOD channels attributes list */
Mahesh Phalke 2:7b2b268ea49c 179 static struct iio_attribute ad4130_iio_ch_attributes[] = {
Mahesh Phalke 2:7b2b268ea49c 180 AD4130_CHN_ATTR("raw", RAW_ATTR_ID),
Mahesh Phalke 2:7b2b268ea49c 181 AD4130_CHN_ATTR("scale", SCALE_ATTR_ID),
Mahesh Phalke 2:7b2b268ea49c 182 AD4130_CHN_ATTR("offset", OFFSET_ATTR_ID),
Mahesh Phalke 2:7b2b268ea49c 183 AD4130_CHN_ATTR("internal_calibration", INTERNAL_CALIB_ID),
Mahesh Phalke 2:7b2b268ea49c 184 AD4130_CHN_ATTR("system_calibration", SYSTEM_CALIB_ID),
Mahesh Phalke 2:7b2b268ea49c 185 #if (ACTIVE_DEMO_MODE_CONFIG == LOADCELL_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 186 AD4130_CHN_ATTR("loadcell_gain_calibration", LOADCELL_GAIN_CALIB_ID),
Mahesh Phalke 2:7b2b268ea49c 187 AD4130_CHN_ATTR("loadcell_offset_calibration", LOADCELL_OFFSET_CALIB_ID),
Mahesh Phalke 2:7b2b268ea49c 188 #endif
Mahesh Phalke 2:7b2b268ea49c 189 END_ATTRIBUTES_ARRAY
Mahesh Phalke 2:7b2b268ea49c 190 };
Mahesh Phalke 2:7b2b268ea49c 191
Mahesh Phalke 2:7b2b268ea49c 192 /* IIOD device (global) attributes list */
Mahesh Phalke 2:7b2b268ea49c 193 static struct iio_attribute ad4130_iio_global_attributes[] = {
Mahesh Phalke 2:7b2b268ea49c 194 AD4130_CHN_ATTR("sampling_frequency", SAMPLING_FREQ_ATTR_ID),
Mahesh Phalke 2:7b2b268ea49c 195 AD4130_CHN_ATTR("demo_config", DEMO_CONFIG_ATTR_ID),
Mahesh Phalke 2:7b2b268ea49c 196 END_ATTRIBUTES_ARRAY
Mahesh Phalke 2:7b2b268ea49c 197 };
Mahesh Phalke 2:7b2b268ea49c 198
Mahesh Phalke 2:7b2b268ea49c 199 /* IIOD context attributes list */
Mahesh Phalke 2:7b2b268ea49c 200 static struct iio_context_attribute ad4130_iio_context_attributes[] = {
Mahesh Phalke 2:7b2b268ea49c 201 {
Mahesh Phalke 2:7b2b268ea49c 202 .name = "hw_mezzanine",
Mahesh Phalke 2:7b2b268ea49c 203 .value = HW_MEZZANINE_NAME
Mahesh Phalke 2:7b2b268ea49c 204 },
Mahesh Phalke 2:7b2b268ea49c 205 {
Mahesh Phalke 2:7b2b268ea49c 206 .name = "hw_carrier",
Mahesh Phalke 2:7b2b268ea49c 207 .value = HW_CARRIER_NAME
Mahesh Phalke 2:7b2b268ea49c 208 },
Mahesh Phalke 2:7b2b268ea49c 209 {
Mahesh Phalke 2:7b2b268ea49c 210 .name = "hw_name",
Mahesh Phalke 2:7b2b268ea49c 211 .value = HW_NAME
Mahesh Phalke 2:7b2b268ea49c 212 },
Mahesh Phalke 2:7b2b268ea49c 213 END_ATTRIBUTES_ARRAY
Mahesh Phalke 2:7b2b268ea49c 214 };
Mahesh Phalke 2:7b2b268ea49c 215
Mahesh Phalke 2:7b2b268ea49c 216 static struct iio_channel ad4130_iio_channels[] = {
Mahesh Phalke 2:7b2b268ea49c 217 #if (ACTIVE_DEMO_MODE_CONFIG == THERMISTOR_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 218 AD4130_CH("Sensor1", SENSOR_CHANNEL0, IIO_TEMP)
Mahesh Phalke 2:7b2b268ea49c 219 #elif (ACTIVE_DEMO_MODE_CONFIG == RTD_3WIRE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 220 AD4130_CH("Sensor1", SENSOR_CHANNEL0, IIO_TEMP)
Mahesh Phalke 2:7b2b268ea49c 221 #elif (ACTIVE_DEMO_MODE_CONFIG == RTD_2WIRE_CONFIG || \
Mahesh Phalke 2:7b2b268ea49c 222 ACTIVE_DEMO_MODE_CONFIG == RTD_4WIRE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 223 AD4130_CH("Sensor1", SENSOR_CHANNEL0, IIO_TEMP)
Mahesh Phalke 2:7b2b268ea49c 224 #elif (ACTIVE_DEMO_MODE_CONFIG == THERMOCOUPLE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 225 AD4130_CH("Sensor1", SENSOR_CHANNEL0, IIO_TEMP),
Mahesh Phalke 2:7b2b268ea49c 226 AD4130_CH("CJC", CJC_CHANNEL, IIO_TEMP),
Mahesh Phalke 2:7b2b268ea49c 227 #elif (ACTIVE_DEMO_MODE_CONFIG == LOADCELL_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 228 /* Note: Channel type is considered as voltage as IIO
Mahesh Phalke 2:7b2b268ea49c 229 * oscilloscope doesn't support loadcell unit fomat of gram */
Mahesh Phalke 2:7b2b268ea49c 230 AD4130_CH("Sensor1", SENSOR_CHANNEL0, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 231 #elif (ACTIVE_DEMO_MODE_CONFIG == ECG_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 232 AD4130_CH("Sensor1", SENSOR_CHANNEL0, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 233 #elif (ACTIVE_DEMO_MODE_CONFIG == NOISE_TEST_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 234 AD4130_CH("Chn0", 0, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 235 #elif (ACTIVE_DEMO_MODE_CONFIG == POWER_TEST_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 236 AD4130_CH("V_AVDD", POWER_TEST_V_AVDD_CHN, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 237 AD4130_CH("V_IOVDD", POWER_TEST_V_IOVDD_CHN, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 238 AD4130_CH("I_AVDD", POWER_TEST_I_AVDD_CHN, IIO_CURRENT),
Mahesh Phalke 2:7b2b268ea49c 239 AD4130_CH("I_IOVDD", POWER_TEST_I_IOVDD_CHN, IIO_CURRENT),
Mahesh Phalke 2:7b2b268ea49c 240 AD4130_CH("V_AVSS-DGND", POWER_TEST_V_AVSS_DGND_CHN, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 241 AD4130_CH("V_REF", POWER_TEST_V_REF_CHN, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 242 #else
Mahesh Phalke 2:7b2b268ea49c 243 /* User default config */
Mahesh Phalke 2:7b2b268ea49c 244 AD4130_CH("Chn0", 0, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 245 AD4130_CH("Chn1", 1, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 246 AD4130_CH("Chn2", 2, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 247 AD4130_CH("Chn3", 3, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 248 AD4130_CH("Chn4", 4, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 249 AD4130_CH("Chn5", 5, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 250 AD4130_CH("Chn6", 6, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 251 AD4130_CH("Chn7", 7, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 252 #if (ADC_USER_CHANNELS > 8)
Mahesh Phalke 2:7b2b268ea49c 253 AD4130_CH("Chn8", 8, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 254 AD4130_CH("Chn9", 9, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 255 AD4130_CH("Chn10", 10, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 256 AD4130_CH("Chn11", 11, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 257 AD4130_CH("Chn12", 12, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 258 AD4130_CH("Chn13", 13, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 259 AD4130_CH("Chn14", 14, IIO_VOLTAGE),
Mahesh Phalke 2:7b2b268ea49c 260 AD4130_CH("Chn15", 15, IIO_VOLTAGE)
Mahesh Phalke 2:7b2b268ea49c 261 #endif
Mahesh Phalke 2:7b2b268ea49c 262 #endif
Mahesh Phalke 2:7b2b268ea49c 263 };
Mahesh Phalke 2:7b2b268ea49c 264
Mahesh Phalke 2:7b2b268ea49c 265 /* ADC raw averaged values from loadcell calibration */
Mahesh Phalke 2:7b2b268ea49c 266 static uint32_t adc_raw_offset;
Mahesh Phalke 2:7b2b268ea49c 267 static uint32_t adc_raw_gain;
Mahesh Phalke 2:7b2b268ea49c 268
Mahesh Phalke 2:7b2b268ea49c 269 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 270 /************************ Functions Prototypes ********************************/
Mahesh Phalke 2:7b2b268ea49c 271 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 272
Mahesh Phalke 2:7b2b268ea49c 273 static void update_vltg_conv_scale_factor(uint8_t chn);
Mahesh Phalke 2:7b2b268ea49c 274 static void perform_sensor_measurement_and_update_scale(uint32_t adc_raw,
Mahesh Phalke 2:7b2b268ea49c 275 uint16_t chn);
Mahesh Phalke 2:7b2b268ea49c 276 static int get_calibration_status(char *buf,
Mahesh Phalke 2:7b2b268ea49c 277 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 278 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 279 intptr_t id);
Mahesh Phalke 2:7b2b268ea49c 280 static int set_calibration_routine(char *buf,
Mahesh Phalke 2:7b2b268ea49c 281 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 282 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 283 intptr_t id);
Mahesh Phalke 2:7b2b268ea49c 284 static int get_loadcell_calibration_status(char *buf,
Mahesh Phalke 2:7b2b268ea49c 285 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 286 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 287 intptr_t id);
Mahesh Phalke 2:7b2b268ea49c 288 static int set_loadcell_calibration_status(char *buf,
Mahesh Phalke 2:7b2b268ea49c 289 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 290 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 291 intptr_t id);
Mahesh Phalke 2:7b2b268ea49c 292
Mahesh Phalke 2:7b2b268ea49c 293 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 294 /************************ Functions Definitions *******************************/
Mahesh Phalke 2:7b2b268ea49c 295 /******************************************************************************/
Mahesh Phalke 2:7b2b268ea49c 296
Mahesh Phalke 2:7b2b268ea49c 297 static char *get_demo_mode_config(void)
Mahesh Phalke 2:7b2b268ea49c 298 {
Mahesh Phalke 2:7b2b268ea49c 299 #if (ACTIVE_DEMO_MODE_CONFIG == RTD_2WIRE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 300 return "2-Wire RTD";
Mahesh Phalke 2:7b2b268ea49c 301 #elif (ACTIVE_DEMO_MODE_CONFIG == RTD_3WIRE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 302 return "3-Wire RTD";
Mahesh Phalke 2:7b2b268ea49c 303 #elif (ACTIVE_DEMO_MODE_CONFIG == RTD_4WIRE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 304 return "4-Wire RTD";
Mahesh Phalke 2:7b2b268ea49c 305 #elif (ACTIVE_DEMO_MODE_CONFIG == THERMISTOR_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 306 return "Thermistor";
Mahesh Phalke 2:7b2b268ea49c 307 #elif (ACTIVE_DEMO_MODE_CONFIG == THERMOCOUPLE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 308 return "Thermocouple";
Mahesh Phalke 2:7b2b268ea49c 309 #elif (ACTIVE_DEMO_MODE_CONFIG == LOADCELL_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 310 return "Loadcell";
Mahesh Phalke 2:7b2b268ea49c 311 #elif (ACTIVE_DEMO_MODE_CONFIG == ECG_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 312 return "ECG";
Mahesh Phalke 2:7b2b268ea49c 313 #elif (ACTIVE_DEMO_MODE_CONFIG == NOISE_TEST_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 314 return "Noise Test";
Mahesh Phalke 2:7b2b268ea49c 315 #elif (ACTIVE_DEMO_MODE_CONFIG == POWER_TEST_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 316 return "Power Test";
Mahesh Phalke 2:7b2b268ea49c 317 #else
Mahesh Phalke 2:7b2b268ea49c 318 return "User Default";
Mahesh Phalke 2:7b2b268ea49c 319 #endif
Mahesh Phalke 2:7b2b268ea49c 320 }
Mahesh Phalke 2:7b2b268ea49c 321
Mahesh Phalke 2:7b2b268ea49c 322 /*!
Mahesh Phalke 2:7b2b268ea49c 323 * @brief Getter functions for AD4130 attributes
Mahesh Phalke 2:7b2b268ea49c 324 * @param device[in]- Pointer to IIO device instance
Mahesh Phalke 2:7b2b268ea49c 325 * @param buf[in]- IIO input data buffer
Mahesh Phalke 2:7b2b268ea49c 326 * @param len[in]- Number of input bytes
Mahesh Phalke 2:7b2b268ea49c 327 * @param channel[in] - Input channel
Mahesh Phalke 2:7b2b268ea49c 328 * @param priv[in] - Attribute private ID
Mahesh Phalke 2:7b2b268ea49c 329 * @return 0 in case of success, negative error code otherwise
Mahesh Phalke 2:7b2b268ea49c 330 * @Note This sampling_frequency attribute is used to define the
Mahesh Phalke 2:7b2b268ea49c 331 * timeout period in IIO client during data capture.
Mahesh Phalke 2:7b2b268ea49c 332 * Timeout (1 chn) = (requested samples * sampling frequency) + 1sec
Mahesh Phalke 2:7b2b268ea49c 333 * Timeout (n chns) = ((requested samples * n) / sampling frequency) + 1sec
Mahesh Phalke 2:7b2b268ea49c 334 * e.g. If sampling frequency = 31.5KSPS, requested samples = 4000, n=1min or 8max
Mahesh Phalke 2:7b2b268ea49c 335 * Timeout (1 chn) = (4000 / 315000) + 1 = ~1.13sec
Mahesh Phalke 2:7b2b268ea49c 336 * Timeout (8 chns) = ((4000 * 8) / 315000) + 1 = ~2.01sec
Mahesh Phalke 2:7b2b268ea49c 337 */
Mahesh Phalke 2:7b2b268ea49c 338 static int iio_ad4130_attr_get(void *device,
Mahesh Phalke 2:7b2b268ea49c 339 char *buf,
Mahesh Phalke 2:7b2b268ea49c 340 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 341 const struct iio_ch_info *channel,
Mahesh Phalke 2:7b2b268ea49c 342 intptr_t priv)
Mahesh Phalke 2:7b2b268ea49c 343 {
Mahesh Phalke 2:7b2b268ea49c 344 uint32_t adc_data_raw;
Mahesh Phalke 2:7b2b268ea49c 345 int32_t offset = 0;
Mahesh Phalke 2:7b2b268ea49c 346 uint8_t bipolar;
Mahesh Phalke 2:7b2b268ea49c 347 uint32_t val;
Mahesh Phalke 2:7b2b268ea49c 348 int32_t ret;
Mahesh Phalke 2:7b2b268ea49c 349 uint8_t preset = ad4130_dev_inst->ch[channel->ch_num].preset;
Mahesh Phalke 2:7b2b268ea49c 350
Mahesh Phalke 2:7b2b268ea49c 351 val = no_os_str_to_uint32(buf);
Mahesh Phalke 2:7b2b268ea49c 352
Mahesh Phalke 2:7b2b268ea49c 353 switch (priv) {
Mahesh Phalke 2:7b2b268ea49c 354 case RAW_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 355 /* Apply calibrated coefficients before new sampling */
Mahesh Phalke 2:7b2b268ea49c 356 if (adc_calibration_status[channel->ch_num] == CALIB_DONE) {
Mahesh Phalke 2:7b2b268ea49c 357 ret = ad413x_reg_write(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 358 AD413X_REG_OFFSET(preset),
Mahesh Phalke 2:7b2b268ea49c 359 adc_calibration_config[channel->ch_num].offset_after_calib);
Mahesh Phalke 2:7b2b268ea49c 360 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 361 break;
Mahesh Phalke 2:7b2b268ea49c 362 }
Mahesh Phalke 2:7b2b268ea49c 363
Mahesh Phalke 2:7b2b268ea49c 364 ret = ad413x_reg_write(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 365 AD413X_REG_GAIN(preset),
Mahesh Phalke 2:7b2b268ea49c 366 adc_calibration_config[channel->ch_num].gain_after_calib);
Mahesh Phalke 2:7b2b268ea49c 367 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 368 break;
Mahesh Phalke 2:7b2b268ea49c 369 }
Mahesh Phalke 2:7b2b268ea49c 370 }
Mahesh Phalke 2:7b2b268ea49c 371
Mahesh Phalke 2:7b2b268ea49c 372 /* Capture the raw adc data */
Mahesh Phalke 2:7b2b268ea49c 373 ret = read_single_sample((uint8_t)channel->ch_num, &adc_data_raw);
Mahesh Phalke 2:7b2b268ea49c 374 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 375 break;
Mahesh Phalke 2:7b2b268ea49c 376 }
Mahesh Phalke 2:7b2b268ea49c 377
Mahesh Phalke 2:7b2b268ea49c 378 perform_sensor_measurement_and_update_scale(adc_data_raw, channel->ch_num);
Mahesh Phalke 2:7b2b268ea49c 379 return sprintf(buf, "%d", adc_data_raw);
Mahesh Phalke 2:7b2b268ea49c 380
Mahesh Phalke 2:7b2b268ea49c 381 case SCALE_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 382 return snprintf(buf, len, "%.10f", attr_scale_val[channel->ch_num]);
Mahesh Phalke 2:7b2b268ea49c 383
Mahesh Phalke 2:7b2b268ea49c 384 case OFFSET_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 385 #if (ACTIVE_DEMO_MODE_CONFIG == USER_DEFAULT_CONFIG || \
Mahesh Phalke 2:7b2b268ea49c 386 ACTIVE_DEMO_MODE_CONFIG == LOADCELL_CONFIG || \
Mahesh Phalke 2:7b2b268ea49c 387 ACTIVE_DEMO_MODE_CONFIG == ECG_CONFIG || \
Mahesh Phalke 2:7b2b268ea49c 388 ACTIVE_DEMO_MODE_CONFIG == NOISE_TEST_CONFIG || \
Mahesh Phalke 2:7b2b268ea49c 389 ACTIVE_DEMO_MODE_CONFIG == POWER_TEST_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 390 /* Note: For temperature type channels, the offset
Mahesh Phalke 2:7b2b268ea49c 391 * is ignored, as signed conversion needed for IIO client
Mahesh Phalke 2:7b2b268ea49c 392 * is done through perform_sensor_measurement_and_update_scale() */
Mahesh Phalke 2:7b2b268ea49c 393 bipolar = ad4130_dev_inst->bipolar;
Mahesh Phalke 2:7b2b268ea49c 394 if (bipolar) {
Mahesh Phalke 2:7b2b268ea49c 395 /* For IIO clients, the raw to voltage conversion happens
Mahesh Phalke 2:7b2b268ea49c 396 * using formula: voltage = (adc_raw + offset) * scale
Mahesh Phalke 2:7b2b268ea49c 397 * Offset is determined based on the coding scheme of device.
Mahesh Phalke 2:7b2b268ea49c 398 * 'Offset binary coding' is used in bipolar mode while
Mahesh Phalke 2:7b2b268ea49c 399 * 'Streight binary coding' is used in unipolar mode.
Mahesh Phalke 2:7b2b268ea49c 400 * */
Mahesh Phalke 2:7b2b268ea49c 401 offset = -ADC_MAX_COUNT_BIPOLAR;
Mahesh Phalke 2:7b2b268ea49c 402 }
Mahesh Phalke 2:7b2b268ea49c 403 #endif
Mahesh Phalke 2:7b2b268ea49c 404 return sprintf(buf, "%d", offset);
Mahesh Phalke 2:7b2b268ea49c 405
Mahesh Phalke 2:7b2b268ea49c 406 case SAMPLING_FREQ_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 407 /* Sampling frequency for IIO oscilloscope timeout purpose.
Mahesh Phalke 2:7b2b268ea49c 408 * Does not indicate an actual sampling rate of device.
Mahesh Phalke 2:7b2b268ea49c 409 * Refer the 'note' in function description above for timeout calculations */
Mahesh Phalke 2:7b2b268ea49c 410 return sprintf(buf, "%d", AD4130_MIN_SAMPLING_FREQ);
Mahesh Phalke 2:7b2b268ea49c 411
Mahesh Phalke 2:7b2b268ea49c 412 case DEMO_CONFIG_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 413 return sprintf(buf, "%s", get_demo_mode_config());
Mahesh Phalke 2:7b2b268ea49c 414
Mahesh Phalke 2:7b2b268ea49c 415 case INTERNAL_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 416 case SYSTEM_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 417 return get_calibration_status(buf, len, channel->ch_num, priv);
Mahesh Phalke 2:7b2b268ea49c 418
Mahesh Phalke 2:7b2b268ea49c 419 case LOADCELL_GAIN_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 420 case LOADCELL_OFFSET_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 421 return get_loadcell_calibration_status(buf, len, channel->ch_num, priv);
Mahesh Phalke 2:7b2b268ea49c 422
Mahesh Phalke 2:7b2b268ea49c 423 default:
Mahesh Phalke 2:7b2b268ea49c 424 break;
Mahesh Phalke 2:7b2b268ea49c 425 }
Mahesh Phalke 2:7b2b268ea49c 426
Mahesh Phalke 2:7b2b268ea49c 427 return len;
Mahesh Phalke 2:7b2b268ea49c 428 }
Mahesh Phalke 2:7b2b268ea49c 429
Mahesh Phalke 2:7b2b268ea49c 430 /*!
Mahesh Phalke 2:7b2b268ea49c 431 * @brief Setter functions for AD4130 attributes
Mahesh Phalke 2:7b2b268ea49c 432 * @param device[in]- Pointer to IIO device instance
Mahesh Phalke 2:7b2b268ea49c 433 * @param buf[in]- IIO input data buffer
Mahesh Phalke 2:7b2b268ea49c 434 * @param len[in]- Number of input bytes
Mahesh Phalke 2:7b2b268ea49c 435 * @param channel[in] - Input channel
Mahesh Phalke 2:7b2b268ea49c 436 * @param priv[in] - Attribute private ID
Mahesh Phalke 2:7b2b268ea49c 437 * @return 0 in case of success, negative error code otherwise
Mahesh Phalke 2:7b2b268ea49c 438 */
Mahesh Phalke 2:7b2b268ea49c 439 static int iio_ad4130_attr_set(void *device,
Mahesh Phalke 2:7b2b268ea49c 440 char *buf,
Mahesh Phalke 2:7b2b268ea49c 441 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 442 const struct iio_ch_info *channel,
Mahesh Phalke 2:7b2b268ea49c 443 intptr_t priv)
Mahesh Phalke 2:7b2b268ea49c 444 {
Mahesh Phalke 2:7b2b268ea49c 445 switch (priv) {
Mahesh Phalke 2:7b2b268ea49c 446 case RAW_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 447 case SCALE_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 448 case SAMPLING_FREQ_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 449 case DEMO_CONFIG_ATTR_ID:
Mahesh Phalke 2:7b2b268ea49c 450 /* All are read-only attributes */
Mahesh Phalke 2:7b2b268ea49c 451 break;
Mahesh Phalke 2:7b2b268ea49c 452
Mahesh Phalke 2:7b2b268ea49c 453 case INTERNAL_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 454 case SYSTEM_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 455 return set_calibration_routine(buf, len, channel->ch_num, priv);
Mahesh Phalke 2:7b2b268ea49c 456
Mahesh Phalke 2:7b2b268ea49c 457 case LOADCELL_GAIN_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 458 case LOADCELL_OFFSET_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 459 return set_loadcell_calibration_status(buf, len, channel->ch_num, priv);
Mahesh Phalke 2:7b2b268ea49c 460
Mahesh Phalke 2:7b2b268ea49c 461 default:
Mahesh Phalke 2:7b2b268ea49c 462 break;
Mahesh Phalke 2:7b2b268ea49c 463 }
Mahesh Phalke 2:7b2b268ea49c 464
Mahesh Phalke 2:7b2b268ea49c 465 return len;
Mahesh Phalke 2:7b2b268ea49c 466 }
Mahesh Phalke 2:7b2b268ea49c 467
Mahesh Phalke 2:7b2b268ea49c 468 /*!
Mahesh Phalke 2:7b2b268ea49c 469 * @brief Perform the ADC internal/system calibration
Mahesh Phalke 2:7b2b268ea49c 470 * @param chn[in] - ADC channel
Mahesh Phalke 2:7b2b268ea49c 471 * @param calib_mode[in] - ADC calibration mode
Mahesh Phalke 2:7b2b268ea49c 472 * @return 0 in case of success, negative error code otherwise
Mahesh Phalke 2:7b2b268ea49c 473 */
Mahesh Phalke 2:7b2b268ea49c 474 static int32_t perform_adc_calibration(uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 475 enum ad413x_adc_mode calib_mode)
Mahesh Phalke 2:7b2b268ea49c 476 {
Mahesh Phalke 2:7b2b268ea49c 477 int32_t ret;
Mahesh Phalke 2:7b2b268ea49c 478 uint32_t data;
Mahesh Phalke 2:7b2b268ea49c 479 uint8_t preset = ad4130_dev_inst->ch[chn].preset;
Mahesh Phalke 2:7b2b268ea49c 480 uint8_t pga = ad4130_dev_inst->preset[preset].gain;
Mahesh Phalke 2:7b2b268ea49c 481
Mahesh Phalke 2:7b2b268ea49c 482 /* Put ADC into standby mode */
Mahesh Phalke 2:7b2b268ea49c 483 ret = ad413x_set_adc_mode(ad4130_dev_inst, AD413X_STANDBY_MODE);
Mahesh Phalke 2:7b2b268ea49c 484 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 485 return ret;
Mahesh Phalke 2:7b2b268ea49c 486 }
Mahesh Phalke 2:7b2b268ea49c 487
Mahesh Phalke 2:7b2b268ea49c 488 /* Read the gain/offset coefficient values before calibration */
Mahesh Phalke 2:7b2b268ea49c 489 if ((calib_mode == AD413X_INT_GAIN_CAL)
Mahesh Phalke 2:7b2b268ea49c 490 || (calib_mode == AD413X_SYS_GAIN_CAL)) {
Mahesh Phalke 2:7b2b268ea49c 491 if (calib_mode == AD413X_INT_GAIN_CAL) {
Mahesh Phalke 2:7b2b268ea49c 492 /* Write offset default value before internal gain calibration
Mahesh Phalke 2:7b2b268ea49c 493 * as internal offset calibration is performed after internal
Mahesh Phalke 2:7b2b268ea49c 494 * gain calibration */
Mahesh Phalke 2:7b2b268ea49c 495 ret = ad413x_reg_write(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 496 AD413X_REG_OFFSET(preset),
Mahesh Phalke 2:7b2b268ea49c 497 AD4130_DEFAULT_OFFSET);
Mahesh Phalke 2:7b2b268ea49c 498 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 499 return ret;
Mahesh Phalke 2:7b2b268ea49c 500 }
Mahesh Phalke 2:7b2b268ea49c 501 }
Mahesh Phalke 2:7b2b268ea49c 502
Mahesh Phalke 2:7b2b268ea49c 503 ret = ad413x_reg_read(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 504 AD413X_REG_GAIN(preset),
Mahesh Phalke 2:7b2b268ea49c 505 &data);
Mahesh Phalke 2:7b2b268ea49c 506 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 507 return ret;
Mahesh Phalke 2:7b2b268ea49c 508 }
Mahesh Phalke 2:7b2b268ea49c 509 adc_calibration_config[chn].gain_before_calib = data;
Mahesh Phalke 2:7b2b268ea49c 510 } else {
Mahesh Phalke 2:7b2b268ea49c 511 ret = ad413x_reg_read(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 512 AD413X_REG_OFFSET(preset),
Mahesh Phalke 2:7b2b268ea49c 513 &data);
Mahesh Phalke 2:7b2b268ea49c 514
Mahesh Phalke 2:7b2b268ea49c 515 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 516 return ret;
Mahesh Phalke 2:7b2b268ea49c 517 }
Mahesh Phalke 2:7b2b268ea49c 518 adc_calibration_config[chn].offset_before_calib = data;
Mahesh Phalke 2:7b2b268ea49c 519 }
Mahesh Phalke 2:7b2b268ea49c 520
Mahesh Phalke 2:7b2b268ea49c 521 /* Enable channel for calibration */
Mahesh Phalke 2:7b2b268ea49c 522 ret = ad413x_ch_en(ad4130_dev_inst, chn, 1);
Mahesh Phalke 2:7b2b268ea49c 523 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 524 return ret;
Mahesh Phalke 2:7b2b268ea49c 525 }
Mahesh Phalke 2:7b2b268ea49c 526
Mahesh Phalke 2:7b2b268ea49c 527 if ((calib_mode == AD413X_INT_GAIN_CAL)
Mahesh Phalke 2:7b2b268ea49c 528 || (calib_mode == AD413X_SYS_GAIN_CAL)) {
Mahesh Phalke 2:7b2b268ea49c 529 if ((calib_mode == AD413X_INT_GAIN_CAL) && (pga == AD413X_GAIN_1)) {
Mahesh Phalke 2:7b2b268ea49c 530 /* Internal gain calibration is not supported at gain of 1 */
Mahesh Phalke 2:7b2b268ea49c 531 adc_calibration_config[chn].gain_after_calib =
Mahesh Phalke 2:7b2b268ea49c 532 adc_calibration_config[chn].gain_before_calib;
Mahesh Phalke 2:7b2b268ea49c 533 adc_calibration_status[chn] = CALIB_SKIPPED;
Mahesh Phalke 2:7b2b268ea49c 534 return 0;
Mahesh Phalke 2:7b2b268ea49c 535 }
Mahesh Phalke 2:7b2b268ea49c 536
Mahesh Phalke 2:7b2b268ea49c 537 /* Perform internal/system gain (full-scale) calibration */
Mahesh Phalke 2:7b2b268ea49c 538 ret = ad413x_set_adc_mode(ad4130_dev_inst, calib_mode);
Mahesh Phalke 2:7b2b268ea49c 539 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 540 return ret;
Mahesh Phalke 2:7b2b268ea49c 541 }
Mahesh Phalke 2:7b2b268ea49c 542
Mahesh Phalke 2:7b2b268ea49c 543 /* Wait for conversion to finish */
Mahesh Phalke 2:7b2b268ea49c 544 no_os_mdelay(200);
Mahesh Phalke 2:7b2b268ea49c 545
Mahesh Phalke 2:7b2b268ea49c 546 /* Read the gain coefficient value (post calibrated) */
Mahesh Phalke 2:7b2b268ea49c 547 ret = ad413x_reg_read(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 548 AD413X_REG_GAIN(preset),
Mahesh Phalke 2:7b2b268ea49c 549 &data);
Mahesh Phalke 2:7b2b268ea49c 550 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 551 return ret;
Mahesh Phalke 2:7b2b268ea49c 552 }
Mahesh Phalke 2:7b2b268ea49c 553 adc_calibration_config[chn].gain_after_calib = data;
Mahesh Phalke 2:7b2b268ea49c 554
Mahesh Phalke 2:7b2b268ea49c 555 /* Compare the pre and post adc calibration gain coefficients to check calibration status */
Mahesh Phalke 2:7b2b268ea49c 556 if (adc_calibration_config[chn].gain_after_calib ==
Mahesh Phalke 2:7b2b268ea49c 557 adc_calibration_config[chn].gain_before_calib) {
Mahesh Phalke 2:7b2b268ea49c 558 /* Error in gain calibration */
Mahesh Phalke 2:7b2b268ea49c 559 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 560 }
Mahesh Phalke 2:7b2b268ea49c 561 } else {
Mahesh Phalke 2:7b2b268ea49c 562 /* Perform internal/system offset (zero-scale) calibration */
Mahesh Phalke 2:7b2b268ea49c 563 ret = ad413x_set_adc_mode(ad4130_dev_inst, calib_mode);
Mahesh Phalke 2:7b2b268ea49c 564 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 565 return ret;
Mahesh Phalke 2:7b2b268ea49c 566 }
Mahesh Phalke 2:7b2b268ea49c 567
Mahesh Phalke 2:7b2b268ea49c 568 /* Wait for conversion to finish */
Mahesh Phalke 2:7b2b268ea49c 569 no_os_mdelay(200);
Mahesh Phalke 2:7b2b268ea49c 570
Mahesh Phalke 2:7b2b268ea49c 571 /* Read the coefficient value (post calibrated) */
Mahesh Phalke 2:7b2b268ea49c 572 ret = ad413x_reg_read(ad4130_dev_inst,
Mahesh Phalke 2:7b2b268ea49c 573 AD413X_REG_OFFSET(preset),
Mahesh Phalke 2:7b2b268ea49c 574 &data);
Mahesh Phalke 2:7b2b268ea49c 575 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 576 return ret;
Mahesh Phalke 2:7b2b268ea49c 577 }
Mahesh Phalke 2:7b2b268ea49c 578 adc_calibration_config[chn].offset_after_calib = data;
Mahesh Phalke 2:7b2b268ea49c 579
Mahesh Phalke 2:7b2b268ea49c 580 /* Compare the pre and post adc calibration offset coefficients to check calibration status */
Mahesh Phalke 2:7b2b268ea49c 581 if (adc_calibration_config[chn].offset_after_calib ==
Mahesh Phalke 2:7b2b268ea49c 582 adc_calibration_config[chn].offset_before_calib) {
Mahesh Phalke 2:7b2b268ea49c 583 /* Error in offset calibration */
Mahesh Phalke 2:7b2b268ea49c 584 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 585 }
Mahesh Phalke 2:7b2b268ea49c 586 }
Mahesh Phalke 2:7b2b268ea49c 587
Mahesh Phalke 2:7b2b268ea49c 588 /* Disable previously enabled channel */
Mahesh Phalke 2:7b2b268ea49c 589 ret = ad413x_ch_en(ad4130_dev_inst, chn, 0);
Mahesh Phalke 2:7b2b268ea49c 590 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 591 return ret;
Mahesh Phalke 2:7b2b268ea49c 592 }
Mahesh Phalke 2:7b2b268ea49c 593
Mahesh Phalke 2:7b2b268ea49c 594 return 0;
Mahesh Phalke 2:7b2b268ea49c 595 }
Mahesh Phalke 2:7b2b268ea49c 596
Mahesh Phalke 2:7b2b268ea49c 597 /*!
Mahesh Phalke 2:7b2b268ea49c 598 * @brief Getter for the ADC internal/system calibration
Mahesh Phalke 2:7b2b268ea49c 599 * @param buf[in]- pointer to buffer holding attribute value
Mahesh Phalke 2:7b2b268ea49c 600 * @param len[in]- length of buffer string data
Mahesh Phalke 2:7b2b268ea49c 601 * @param chn[in]- ADC channel
Mahesh Phalke 2:7b2b268ea49c 602 * @param id[in]- Attribute ID
Mahesh Phalke 2:7b2b268ea49c 603 * @return Number of characters read/written
Mahesh Phalke 2:7b2b268ea49c 604 */
Mahesh Phalke 2:7b2b268ea49c 605 static int get_calibration_status(char *buf,
Mahesh Phalke 2:7b2b268ea49c 606 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 607 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 608 intptr_t id)
Mahesh Phalke 2:7b2b268ea49c 609 {
Mahesh Phalke 2:7b2b268ea49c 610 uint8_t buf_offset = 0;
Mahesh Phalke 2:7b2b268ea49c 611
Mahesh Phalke 2:7b2b268ea49c 612 switch (id) {
Mahesh Phalke 2:7b2b268ea49c 613 case SYSTEM_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 614 case INTERNAL_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 615 if (id == SYSTEM_CALIB_ID && system_calibration_state == CALIB_COMPLETE_STATE) {
Mahesh Phalke 2:7b2b268ea49c 616 system_calibration_state = ZERO_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 617 } else if (id == INTERNAL_CALIB_ID
Mahesh Phalke 2:7b2b268ea49c 618 && internal_calibration_state == CALIB_COMPLETE_STATE) {
Mahesh Phalke 2:7b2b268ea49c 619 internal_calibration_state = FULL_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 620 } else {
Mahesh Phalke 2:7b2b268ea49c 621 if (adc_calibration_status[chn] != CALIB_ERROR
Mahesh Phalke 2:7b2b268ea49c 622 && adc_calibration_status[chn] != CALIB_SKIPPED
Mahesh Phalke 2:7b2b268ea49c 623 && adc_calibration_status[chn] != CALIB_IN_PROGRESS) {
Mahesh Phalke 2:7b2b268ea49c 624 /* Return NA to indicate that system calibration is not supported
Mahesh Phalke 2:7b2b268ea49c 625 * using IIO oscilloscope. Pyadi-iio script needs to be executed
Mahesh Phalke 2:7b2b268ea49c 626 * to perform a system calibration due to manual intervention
Mahesh Phalke 2:7b2b268ea49c 627 **/
Mahesh Phalke 2:7b2b268ea49c 628 return snprintf(buf, len, "%s", "NA");
Mahesh Phalke 2:7b2b268ea49c 629 }
Mahesh Phalke 2:7b2b268ea49c 630 }
Mahesh Phalke 2:7b2b268ea49c 631
Mahesh Phalke 2:7b2b268ea49c 632 sprintf(buf + buf_offset, "%08x",
Mahesh Phalke 2:7b2b268ea49c 633 adc_calibration_config[chn].gain_before_calib);
Mahesh Phalke 2:7b2b268ea49c 634 buf_offset += 8;
Mahesh Phalke 2:7b2b268ea49c 635 sprintf(buf + buf_offset, "%08x",
Mahesh Phalke 2:7b2b268ea49c 636 adc_calibration_config[chn].gain_after_calib);
Mahesh Phalke 2:7b2b268ea49c 637 buf_offset += 8;
Mahesh Phalke 2:7b2b268ea49c 638 sprintf(buf + buf_offset, "%08x",
Mahesh Phalke 2:7b2b268ea49c 639 adc_calibration_config[chn].offset_before_calib);
Mahesh Phalke 2:7b2b268ea49c 640 buf_offset += 8;
Mahesh Phalke 2:7b2b268ea49c 641 sprintf(buf + buf_offset, "%08x",
Mahesh Phalke 2:7b2b268ea49c 642 adc_calibration_config[chn].offset_after_calib);
Mahesh Phalke 2:7b2b268ea49c 643 buf_offset += 8;
Mahesh Phalke 2:7b2b268ea49c 644
Mahesh Phalke 2:7b2b268ea49c 645 if (adc_calibration_status[chn] == CALIB_ERROR) {
Mahesh Phalke 2:7b2b268ea49c 646 sprintf(buf + buf_offset, "%s", "calibration_failed");
Mahesh Phalke 2:7b2b268ea49c 647 buf_offset += (strlen("calibration_failed") + 1);
Mahesh Phalke 2:7b2b268ea49c 648 adc_calibration_status[chn] = CALIB_NOT_DONE;
Mahesh Phalke 2:7b2b268ea49c 649 } else if (adc_calibration_status[chn] == CALIB_SKIPPED) {
Mahesh Phalke 2:7b2b268ea49c 650 sprintf(buf + buf_offset, "%s", "calibration_skipped");
Mahesh Phalke 2:7b2b268ea49c 651 buf_offset += (strlen("calibration_skipped") + 1);
Mahesh Phalke 2:7b2b268ea49c 652 adc_calibration_status[chn] = CALIB_NOT_DONE;
Mahesh Phalke 2:7b2b268ea49c 653 } else {
Mahesh Phalke 2:7b2b268ea49c 654 sprintf(buf + buf_offset, "%s", "calibration_done");
Mahesh Phalke 2:7b2b268ea49c 655 buf_offset += (strlen("calibration_done") + 1);
Mahesh Phalke 2:7b2b268ea49c 656 }
Mahesh Phalke 2:7b2b268ea49c 657
Mahesh Phalke 2:7b2b268ea49c 658 return buf_offset;
Mahesh Phalke 2:7b2b268ea49c 659
Mahesh Phalke 2:7b2b268ea49c 660 default:
Mahesh Phalke 2:7b2b268ea49c 661 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 662 }
Mahesh Phalke 2:7b2b268ea49c 663
Mahesh Phalke 2:7b2b268ea49c 664 return len;
Mahesh Phalke 2:7b2b268ea49c 665 }
Mahesh Phalke 2:7b2b268ea49c 666
Mahesh Phalke 2:7b2b268ea49c 667 /*!
Mahesh Phalke 2:7b2b268ea49c 668 * @brief Setter for the ADC internal/system calibration
Mahesh Phalke 2:7b2b268ea49c 669 * @param buf[in]- pointer to buffer holding attribute value
Mahesh Phalke 2:7b2b268ea49c 670 * @param len[in]- length of buffer string data
Mahesh Phalke 2:7b2b268ea49c 671 * @param chn[in]- ADC channel
Mahesh Phalke 2:7b2b268ea49c 672 * @param id[in]- Attribute ID
Mahesh Phalke 2:7b2b268ea49c 673 * @return Number of characters read/written
Mahesh Phalke 2:7b2b268ea49c 674 */
Mahesh Phalke 2:7b2b268ea49c 675 static int set_calibration_routine(char *buf,
Mahesh Phalke 2:7b2b268ea49c 676 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 677 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 678 intptr_t id)
Mahesh Phalke 2:7b2b268ea49c 679 {
Mahesh Phalke 2:7b2b268ea49c 680 switch (id) {
Mahesh Phalke 2:7b2b268ea49c 681 case INTERNAL_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 682 if (!strncmp(buf, "start_calibration", strlen(buf))) {
Mahesh Phalke 2:7b2b268ea49c 683 switch (internal_calibration_state) {
Mahesh Phalke 2:7b2b268ea49c 684 case FULL_SCALE_CALIB_STATE:
Mahesh Phalke 2:7b2b268ea49c 685 adc_calibration_status[chn] = CALIB_IN_PROGRESS;
Mahesh Phalke 2:7b2b268ea49c 686 if (perform_adc_calibration(chn,
Mahesh Phalke 2:7b2b268ea49c 687 AD413X_INT_GAIN_CAL)) {
Mahesh Phalke 2:7b2b268ea49c 688 adc_calibration_status[chn] = CALIB_ERROR;
Mahesh Phalke 2:7b2b268ea49c 689 }
Mahesh Phalke 2:7b2b268ea49c 690 internal_calibration_state = ZERO_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 691 break;
Mahesh Phalke 2:7b2b268ea49c 692
Mahesh Phalke 2:7b2b268ea49c 693 case ZERO_SCALE_CALIB_STATE:
Mahesh Phalke 2:7b2b268ea49c 694 if (perform_adc_calibration(chn,
Mahesh Phalke 2:7b2b268ea49c 695 AD413X_INT_OFFSET_CAL)) {
Mahesh Phalke 2:7b2b268ea49c 696 adc_calibration_status[chn] = CALIB_ERROR;
Mahesh Phalke 2:7b2b268ea49c 697 internal_calibration_state = FULL_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 698 break;
Mahesh Phalke 2:7b2b268ea49c 699 }
Mahesh Phalke 2:7b2b268ea49c 700 adc_calibration_status[chn] = CALIB_DONE;
Mahesh Phalke 2:7b2b268ea49c 701 internal_calibration_state = CALIB_COMPLETE_STATE;
Mahesh Phalke 2:7b2b268ea49c 702 break;
Mahesh Phalke 2:7b2b268ea49c 703
Mahesh Phalke 2:7b2b268ea49c 704 case CALIB_COMPLETE_STATE:
Mahesh Phalke 2:7b2b268ea49c 705 default:
Mahesh Phalke 2:7b2b268ea49c 706 internal_calibration_state = FULL_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 707 break;
Mahesh Phalke 2:7b2b268ea49c 708 }
Mahesh Phalke 2:7b2b268ea49c 709 }
Mahesh Phalke 2:7b2b268ea49c 710 break;
Mahesh Phalke 2:7b2b268ea49c 711
Mahesh Phalke 2:7b2b268ea49c 712 case SYSTEM_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 713 if (!strncmp(buf, "start_calibration", strlen(buf))) {
Mahesh Phalke 2:7b2b268ea49c 714 switch (system_calibration_state) {
Mahesh Phalke 2:7b2b268ea49c 715 case ZERO_SCALE_CALIB_STATE:
Mahesh Phalke 2:7b2b268ea49c 716 adc_calibration_status[chn] = CALIB_IN_PROGRESS;
Mahesh Phalke 2:7b2b268ea49c 717 if (perform_adc_calibration(chn,
Mahesh Phalke 2:7b2b268ea49c 718 AD413X_SYS_OFFSET_CAL)) {
Mahesh Phalke 2:7b2b268ea49c 719 adc_calibration_status[chn] = CALIB_ERROR;
Mahesh Phalke 2:7b2b268ea49c 720 }
Mahesh Phalke 2:7b2b268ea49c 721 system_calibration_state = FULL_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 722 break;
Mahesh Phalke 2:7b2b268ea49c 723
Mahesh Phalke 2:7b2b268ea49c 724 case FULL_SCALE_CALIB_STATE:
Mahesh Phalke 2:7b2b268ea49c 725 if (perform_adc_calibration(chn,
Mahesh Phalke 2:7b2b268ea49c 726 AD413X_SYS_GAIN_CAL)) {
Mahesh Phalke 2:7b2b268ea49c 727 adc_calibration_status[chn] = CALIB_ERROR;
Mahesh Phalke 2:7b2b268ea49c 728 system_calibration_state = ZERO_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 729 break;
Mahesh Phalke 2:7b2b268ea49c 730 }
Mahesh Phalke 2:7b2b268ea49c 731 adc_calibration_status[chn] = CALIB_DONE;
Mahesh Phalke 2:7b2b268ea49c 732 system_calibration_state = CALIB_COMPLETE_STATE;
Mahesh Phalke 2:7b2b268ea49c 733 break;
Mahesh Phalke 2:7b2b268ea49c 734
Mahesh Phalke 2:7b2b268ea49c 735 case CALIB_COMPLETE_STATE:
Mahesh Phalke 2:7b2b268ea49c 736 default:
Mahesh Phalke 2:7b2b268ea49c 737 system_calibration_state = ZERO_SCALE_CALIB_STATE;
Mahesh Phalke 2:7b2b268ea49c 738 break;
Mahesh Phalke 2:7b2b268ea49c 739 }
Mahesh Phalke 2:7b2b268ea49c 740 }
Mahesh Phalke 2:7b2b268ea49c 741 break;
Mahesh Phalke 2:7b2b268ea49c 742
Mahesh Phalke 2:7b2b268ea49c 743 default:
Mahesh Phalke 2:7b2b268ea49c 744 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 745 }
Mahesh Phalke 2:7b2b268ea49c 746
Mahesh Phalke 2:7b2b268ea49c 747 return len;
Mahesh Phalke 2:7b2b268ea49c 748 }
Mahesh Phalke 2:7b2b268ea49c 749
Mahesh Phalke 2:7b2b268ea49c 750 /*!
Mahesh Phalke 2:7b2b268ea49c 751 * @brief Getter for the Loadcell offset/gain calibration
Mahesh Phalke 2:7b2b268ea49c 752 * @param buf[in]- pointer to buffer holding attribute value
Mahesh Phalke 2:7b2b268ea49c 753 * @param len[in]- length of buffer string data
Mahesh Phalke 2:7b2b268ea49c 754 * @param chn[in]- ADC channel
Mahesh Phalke 2:7b2b268ea49c 755 * @param id[in]- Attribute ID
Mahesh Phalke 2:7b2b268ea49c 756 * @return Number of characters read/written
Mahesh Phalke 2:7b2b268ea49c 757 */
Mahesh Phalke 2:7b2b268ea49c 758 static int get_loadcell_calibration_status(char *buf,
Mahesh Phalke 2:7b2b268ea49c 759 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 760 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 761 intptr_t id)
Mahesh Phalke 2:7b2b268ea49c 762 {
Mahesh Phalke 2:7b2b268ea49c 763 switch (id) {
Mahesh Phalke 2:7b2b268ea49c 764 case LOADCELL_OFFSET_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 765 return sprintf(buf, "%d", adc_raw_offset);
Mahesh Phalke 2:7b2b268ea49c 766
Mahesh Phalke 2:7b2b268ea49c 767 case LOADCELL_GAIN_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 768 return sprintf(buf, "%d", adc_raw_gain);
Mahesh Phalke 2:7b2b268ea49c 769
Mahesh Phalke 2:7b2b268ea49c 770 default:
Mahesh Phalke 2:7b2b268ea49c 771 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 772 }
Mahesh Phalke 2:7b2b268ea49c 773
Mahesh Phalke 2:7b2b268ea49c 774 return len;
Mahesh Phalke 2:7b2b268ea49c 775 }
Mahesh Phalke 2:7b2b268ea49c 776
Mahesh Phalke 2:7b2b268ea49c 777 /*!
Mahesh Phalke 2:7b2b268ea49c 778 * @brief Setter for the Loadcell offset/gain calibration
Mahesh Phalke 2:7b2b268ea49c 779 * @param buf[in]- pointer to buffer holding attribute value
Mahesh Phalke 2:7b2b268ea49c 780 * @param len[in]- length of buffer string data
Mahesh Phalke 2:7b2b268ea49c 781 * @param chn[in]- ADC channel
Mahesh Phalke 2:7b2b268ea49c 782 * @param id[in]- Attribute ID
Mahesh Phalke 2:7b2b268ea49c 783 * @return Number of characters read/written
Mahesh Phalke 2:7b2b268ea49c 784 */
Mahesh Phalke 2:7b2b268ea49c 785 static int set_loadcell_calibration_status(char *buf,
Mahesh Phalke 2:7b2b268ea49c 786 uint32_t len,
Mahesh Phalke 2:7b2b268ea49c 787 uint8_t chn,
Mahesh Phalke 2:7b2b268ea49c 788 intptr_t id)
Mahesh Phalke 2:7b2b268ea49c 789 {
Mahesh Phalke 2:7b2b268ea49c 790 uint32_t adc_raw;
Mahesh Phalke 2:7b2b268ea49c 791 uint8_t sample_cnt;
Mahesh Phalke 2:7b2b268ea49c 792 uint64_t adc_raw_avg = 0;
Mahesh Phalke 2:7b2b268ea49c 793
Mahesh Phalke 2:7b2b268ea49c 794 if (!strncmp(buf, "start_calibration", strlen(buf))) {
Mahesh Phalke 2:7b2b268ea49c 795 switch (id) {
Mahesh Phalke 2:7b2b268ea49c 796 case LOADCELL_OFFSET_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 797 for (sample_cnt = 0; sample_cnt < LOADCELL_SAMPLES_COUNT; sample_cnt++) {
Mahesh Phalke 2:7b2b268ea49c 798 read_single_sample(chn, &adc_raw);
Mahesh Phalke 2:7b2b268ea49c 799 adc_raw_avg += adc_raw;
Mahesh Phalke 2:7b2b268ea49c 800 }
Mahesh Phalke 2:7b2b268ea49c 801
Mahesh Phalke 2:7b2b268ea49c 802 adc_raw_avg /= LOADCELL_SAMPLES_COUNT;
Mahesh Phalke 2:7b2b268ea49c 803 adc_raw_offset = (uint32_t)adc_raw_avg;
Mahesh Phalke 2:7b2b268ea49c 804 break;
Mahesh Phalke 2:7b2b268ea49c 805
Mahesh Phalke 2:7b2b268ea49c 806 case LOADCELL_GAIN_CALIB_ID:
Mahesh Phalke 2:7b2b268ea49c 807 for (sample_cnt = 0; sample_cnt < LOADCELL_SAMPLES_COUNT; sample_cnt++) {
Mahesh Phalke 2:7b2b268ea49c 808 read_single_sample(chn, &adc_raw);
Mahesh Phalke 2:7b2b268ea49c 809 adc_raw_avg += adc_raw;
Mahesh Phalke 2:7b2b268ea49c 810 }
Mahesh Phalke 2:7b2b268ea49c 811
Mahesh Phalke 2:7b2b268ea49c 812 adc_raw_avg /= LOADCELL_SAMPLES_COUNT;
Mahesh Phalke 2:7b2b268ea49c 813 adc_raw_gain = (uint32_t)adc_raw_avg;
Mahesh Phalke 2:7b2b268ea49c 814 break;
Mahesh Phalke 2:7b2b268ea49c 815
Mahesh Phalke 2:7b2b268ea49c 816 default:
Mahesh Phalke 2:7b2b268ea49c 817 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 818 }
Mahesh Phalke 2:7b2b268ea49c 819 }
Mahesh Phalke 2:7b2b268ea49c 820
Mahesh Phalke 2:7b2b268ea49c 821 return len;
Mahesh Phalke 2:7b2b268ea49c 822 }
Mahesh Phalke 2:7b2b268ea49c 823
Mahesh Phalke 2:7b2b268ea49c 824 /*!
Mahesh Phalke 2:7b2b268ea49c 825 * @brief Read the debug register value
Mahesh Phalke 2:7b2b268ea49c 826 * @param dev[in]- Pointer to IIO device instance
Mahesh Phalke 2:7b2b268ea49c 827 * @param reg[in]- Register address to read from
Mahesh Phalke 2:7b2b268ea49c 828 * @param readval[in,out]- Pointer to variable to read data into
Mahesh Phalke 2:7b2b268ea49c 829 * @return 0 in case of success or negative value otherwise
Mahesh Phalke 2:7b2b268ea49c 830 */
Mahesh Phalke 2:7b2b268ea49c 831 int32_t debug_reg_read(void *dev, uint32_t reg, uint32_t *readval)
Mahesh Phalke 2:7b2b268ea49c 832 {
Mahesh Phalke 2:7b2b268ea49c 833 int32_t ret;
Mahesh Phalke 2:7b2b268ea49c 834
Mahesh Phalke 2:7b2b268ea49c 835 if (!dev || !readval || (reg > MAX_REGISTER_ADDRESS)) {
Mahesh Phalke 2:7b2b268ea49c 836 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 837 }
Mahesh Phalke 2:7b2b268ea49c 838
Mahesh Phalke 2:7b2b268ea49c 839 ret = ad413x_reg_read(dev, ad413x_regs[reg], readval);
Mahesh Phalke 2:7b2b268ea49c 840 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 841 return ret;
Mahesh Phalke 2:7b2b268ea49c 842 }
Mahesh Phalke 2:7b2b268ea49c 843
Mahesh Phalke 2:7b2b268ea49c 844 return 0;
Mahesh Phalke 2:7b2b268ea49c 845 }
Mahesh Phalke 2:7b2b268ea49c 846
Mahesh Phalke 2:7b2b268ea49c 847 /*!
Mahesh Phalke 2:7b2b268ea49c 848 * @brief Write into the debug register
Mahesh Phalke 2:7b2b268ea49c 849 * @param dev[in]- Pointer to IIO device instance
Mahesh Phalke 2:7b2b268ea49c 850 * @param reg[in]- Register address to write into
Mahesh Phalke 2:7b2b268ea49c 851 * @param writeval[in]- Register value to write
Mahesh Phalke 2:7b2b268ea49c 852 * @return 0 in case of success or negative value otherwise
Mahesh Phalke 2:7b2b268ea49c 853 */
Mahesh Phalke 2:7b2b268ea49c 854 int32_t debug_reg_write(void *dev, uint32_t reg, uint32_t writeval)
Mahesh Phalke 2:7b2b268ea49c 855 {
Mahesh Phalke 2:7b2b268ea49c 856 int32_t ret;
Mahesh Phalke 2:7b2b268ea49c 857
Mahesh Phalke 2:7b2b268ea49c 858 if (!dev || (reg > MAX_REGISTER_ADDRESS)) {
Mahesh Phalke 2:7b2b268ea49c 859 return -EINVAL;
Mahesh Phalke 2:7b2b268ea49c 860 }
Mahesh Phalke 2:7b2b268ea49c 861
Mahesh Phalke 2:7b2b268ea49c 862 ret = ad413x_reg_write(dev, ad413x_regs[reg], writeval);
Mahesh Phalke 2:7b2b268ea49c 863 if (ret) {
Mahesh Phalke 2:7b2b268ea49c 864 return ret;
Mahesh Phalke 2:7b2b268ea49c 865 }
Mahesh Phalke 2:7b2b268ea49c 866
Mahesh Phalke 2:7b2b268ea49c 867 return 0;
Mahesh Phalke 2:7b2b268ea49c 868 }
Mahesh Phalke 2:7b2b268ea49c 869
Mahesh Phalke 2:7b2b268ea49c 870 /**
Mahesh Phalke 2:7b2b268ea49c 871 * @brief Read buffer data corresponding to AD4130 ADC IIO device
Mahesh Phalke 2:7b2b268ea49c 872 * @param iio_dev_data[in] - IIO device data instance
Mahesh Phalke 2:7b2b268ea49c 873 * @return 0 in case of success or negative value otherwise
Mahesh Phalke 2:7b2b268ea49c 874 */
Mahesh Phalke 2:7b2b268ea49c 875 static int32_t iio_ad4130_submit_buffer(struct iio_device_data *iio_dev_data)
Mahesh Phalke 2:7b2b268ea49c 876 {
Mahesh Phalke 2:7b2b268ea49c 877 uint32_t ret;
Mahesh Phalke 2:7b2b268ea49c 878
Mahesh Phalke 2:7b2b268ea49c 879 /* Read the samples counts equal to buffer size/block */
Mahesh Phalke 2:7b2b268ea49c 880 ret = read_buffered_data(&iio_dev_data->buffer->buf->buff,
Mahesh Phalke 2:7b2b268ea49c 881 iio_dev_data->buffer->size);
Mahesh Phalke 2:7b2b268ea49c 882
Mahesh Phalke 2:7b2b268ea49c 883 /* Increment the write spin count as buffer reads all 'n' samples
Mahesh Phalke 2:7b2b268ea49c 884 * in one go which is also the size of buffer block for a given instance.
Mahesh Phalke 2:7b2b268ea49c 885 * The read spin count is incremented from IIO library during buffer
Mahesh Phalke 2:7b2b268ea49c 886 * write/offloading into transmit buffer */
Mahesh Phalke 2:7b2b268ea49c 887 if (iio_dev_data->buffer->buf->write.spin_count >= UINT32_MAX) {
Mahesh Phalke 2:7b2b268ea49c 888 iio_dev_data->buffer->buf->write.spin_count = 0;
Mahesh Phalke 2:7b2b268ea49c 889 }
Mahesh Phalke 2:7b2b268ea49c 890
Mahesh Phalke 2:7b2b268ea49c 891 iio_dev_data->buffer->buf->write.spin_count += 1;
Mahesh Phalke 2:7b2b268ea49c 892
Mahesh Phalke 2:7b2b268ea49c 893 return ret;
Mahesh Phalke 2:7b2b268ea49c 894 }
Mahesh Phalke 2:7b2b268ea49c 895
Mahesh Phalke 2:7b2b268ea49c 896 /**
Mahesh Phalke 2:7b2b268ea49c 897 * @brief Prepare for data transfer
Mahesh Phalke 2:7b2b268ea49c 898 * @param dev[in] - IIO device instance
Mahesh Phalke 2:7b2b268ea49c 899 * @param ch_mask[in] - Channels select mask
Mahesh Phalke 2:7b2b268ea49c 900 * @return 0 in case of success or negative value otherwise
Mahesh Phalke 2:7b2b268ea49c 901 */
Mahesh Phalke 2:7b2b268ea49c 902 static int32_t iio_ad4130_prepare_transfer(void *dev,
Mahesh Phalke 2:7b2b268ea49c 903 uint32_t ch_mask)
Mahesh Phalke 2:7b2b268ea49c 904 {
Mahesh Phalke 2:7b2b268ea49c 905 return prepare_data_transfer(ch_mask, BYTES_PER_SAMPLE);
Mahesh Phalke 2:7b2b268ea49c 906 }
Mahesh Phalke 2:7b2b268ea49c 907
Mahesh Phalke 2:7b2b268ea49c 908 /**
Mahesh Phalke 2:7b2b268ea49c 909 * @brief Terminate current data transfer
Mahesh Phalke 2:7b2b268ea49c 910 * @param dev[in] - IIO device instance
Mahesh Phalke 2:7b2b268ea49c 911 * @return 0 in case of success or negative value otherwise
Mahesh Phalke 2:7b2b268ea49c 912 */
Mahesh Phalke 2:7b2b268ea49c 913 static int32_t iio_ad4130_end_transfer(void *dev)
Mahesh Phalke 2:7b2b268ea49c 914 {
Mahesh Phalke 2:7b2b268ea49c 915 return end_data_transfer();
Mahesh Phalke 2:7b2b268ea49c 916 }
Mahesh Phalke 2:7b2b268ea49c 917
Mahesh Phalke 2:7b2b268ea49c 918 /**
Mahesh Phalke 2:7b2b268ea49c 919 * @brief Perform the sensor measurement as per current demo config and update
Mahesh Phalke 2:7b2b268ea49c 920 * the adc_raw value to sensor conversion scale factor for IIO client
Mahesh Phalke 2:7b2b268ea49c 921 * @param adc_raw[in] - ADC raw value
Mahesh Phalke 2:7b2b268ea49c 922 * @param chn[in] - ADC channel
Mahesh Phalke 2:7b2b268ea49c 923 * @return none
Mahesh Phalke 2:7b2b268ea49c 924 */
Mahesh Phalke 2:7b2b268ea49c 925 static void perform_sensor_measurement_and_update_scale(uint32_t adc_raw,
Mahesh Phalke 2:7b2b268ea49c 926 uint16_t chn)
Mahesh Phalke 2:7b2b268ea49c 927 {
Mahesh Phalke 2:7b2b268ea49c 928 float temperature = 0;
Mahesh Phalke 2:7b2b268ea49c 929 int32_t cjc_raw_data;
Mahesh Phalke 2:7b2b268ea49c 930 float cjc_temp;
Mahesh Phalke 2:7b2b268ea49c 931
Mahesh Phalke 2:7b2b268ea49c 932 #if (ACTIVE_DEMO_MODE_CONFIG == THERMISTOR_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 933 temperature = get_ntc_thermistor_temperature(ad4130_dev_inst, adc_raw, chn);
Mahesh Phalke 2:7b2b268ea49c 934 attr_scale_val[chn] = (temperature / adc_raw) * 1000.0;
Mahesh Phalke 2:7b2b268ea49c 935 #elif ((ACTIVE_DEMO_MODE_CONFIG == RTD_2WIRE_CONFIG) || \
Mahesh Phalke 2:7b2b268ea49c 936 (ACTIVE_DEMO_MODE_CONFIG == RTD_3WIRE_CONFIG) || \
Mahesh Phalke 2:7b2b268ea49c 937 (ACTIVE_DEMO_MODE_CONFIG == RTD_4WIRE_CONFIG))
Mahesh Phalke 2:7b2b268ea49c 938 temperature = get_rtd_temperature(ad4130_dev_inst, adc_raw, chn);
Mahesh Phalke 2:7b2b268ea49c 939 attr_scale_val[chn] = (temperature / adc_raw) * 1000.0;
Mahesh Phalke 2:7b2b268ea49c 940 #elif (ACTIVE_DEMO_MODE_CONFIG == THERMOCOUPLE_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 941 if (chn != CJC_CHANNEL) {
Mahesh Phalke 2:7b2b268ea49c 942 /* Sample the CJC channel (TC channel is already sampled through
Mahesh Phalke 2:7b2b268ea49c 943 * get_raw() function) */
Mahesh Phalke 2:7b2b268ea49c 944 if (read_single_sample(CJC_CHANNEL, (uint32_t *)&cjc_raw_data)) {
Mahesh Phalke 2:7b2b268ea49c 945 return;
Mahesh Phalke 2:7b2b268ea49c 946 }
Mahesh Phalke 2:7b2b268ea49c 947 } else {
Mahesh Phalke 2:7b2b268ea49c 948 /* For calculating CJC temperature, TC raw data is not used */
Mahesh Phalke 2:7b2b268ea49c 949 chn = SENSOR_CHANNEL0;
Mahesh Phalke 2:7b2b268ea49c 950 cjc_raw_data = adc_raw;
Mahesh Phalke 2:7b2b268ea49c 951 adc_raw = 0;
Mahesh Phalke 2:7b2b268ea49c 952 }
Mahesh Phalke 2:7b2b268ea49c 953
Mahesh Phalke 2:7b2b268ea49c 954 /* Calculate the TC and CJC temperature and update scale factor */
Mahesh Phalke 2:7b2b268ea49c 955 temperature = get_tc_temperature(ad4130_dev_inst, adc_raw,
Mahesh Phalke 2:7b2b268ea49c 956 cjc_raw_data, chn, CJC_CHANNEL, &cjc_temp);
Mahesh Phalke 2:7b2b268ea49c 957 attr_scale_val[chn] = (temperature / adc_raw) * 1000.0;
Mahesh Phalke 2:7b2b268ea49c 958 attr_scale_val[CJC_CHANNEL] = (cjc_temp / cjc_raw_data) * 1000.0;
Mahesh Phalke 2:7b2b268ea49c 959 #endif
Mahesh Phalke 2:7b2b268ea49c 960 }
Mahesh Phalke 2:7b2b268ea49c 961
Mahesh Phalke 2:7b2b268ea49c 962 /*!
Mahesh Phalke 2:7b2b268ea49c 963 * @brief Update scale factor for adc data to voltage conversion
Mahesh Phalke 2:7b2b268ea49c 964 * for IIO client
Mahesh Phalke 2:7b2b268ea49c 965 * @param chn[in] - Input channel
Mahesh Phalke 2:7b2b268ea49c 966 * @return none
Mahesh Phalke 2:7b2b268ea49c 967 */
Mahesh Phalke 2:7b2b268ea49c 968 static void update_vltg_conv_scale_factor(uint8_t chn)
Mahesh Phalke 2:7b2b268ea49c 969 {
Mahesh Phalke 2:7b2b268ea49c 970 enum ad413x_gain pga;
Mahesh Phalke 2:7b2b268ea49c 971 uint8_t preset;
Mahesh Phalke 2:7b2b268ea49c 972 uint8_t bipolar;
Mahesh Phalke 2:7b2b268ea49c 973 float vref;
Mahesh Phalke 2:7b2b268ea49c 974
Mahesh Phalke 2:7b2b268ea49c 975 preset = ad4130_dev_inst->ch[chn].preset;
Mahesh Phalke 2:7b2b268ea49c 976 pga = ad4130_dev_inst->preset[preset].gain;
Mahesh Phalke 2:7b2b268ea49c 977 bipolar = ad4130_dev_inst->bipolar;
Mahesh Phalke 2:7b2b268ea49c 978
Mahesh Phalke 2:7b2b268ea49c 979 vref = ad4130_get_reference_voltage(ad4130_dev_inst, chn);
Mahesh Phalke 2:7b2b268ea49c 980
Mahesh Phalke 2:7b2b268ea49c 981 /* Get the scale factor for voltage conversion */
Mahesh Phalke 2:7b2b268ea49c 982 if (bipolar) {
Mahesh Phalke 2:7b2b268ea49c 983 attr_scale_val[chn] = (vref / (ADC_MAX_COUNT_BIPOLAR *
Mahesh Phalke 2:7b2b268ea49c 984 (1 << pga))) * 1000;
Mahesh Phalke 2:7b2b268ea49c 985 } else {
Mahesh Phalke 2:7b2b268ea49c 986 attr_scale_val[chn] = (vref / (ADC_MAX_COUNT_UNIPOLAR *
Mahesh Phalke 2:7b2b268ea49c 987 (1 << pga))) * 1000;
Mahesh Phalke 2:7b2b268ea49c 988 }
Mahesh Phalke 2:7b2b268ea49c 989
Mahesh Phalke 2:7b2b268ea49c 990 #if (ACTIVE_DEMO_MODE_CONFIG == POWER_TEST_CONFIG)
Mahesh Phalke 2:7b2b268ea49c 991 switch (chn) {
Mahesh Phalke 2:7b2b268ea49c 992 case POWER_TEST_I_AVDD_CHN:
Mahesh Phalke 2:7b2b268ea49c 993 case POWER_TEST_I_IOVDD_CHN:
Mahesh Phalke 2:7b2b268ea49c 994 attr_scale_val[chn] /= I_RSENSE;
Mahesh Phalke 2:7b2b268ea49c 995 break;
Mahesh Phalke 2:7b2b268ea49c 996
Mahesh Phalke 2:7b2b268ea49c 997 case POWER_TEST_V_AVDD_CHN:
Mahesh Phalke 2:7b2b268ea49c 998 case POWER_TEST_V_IOVDD_CHN:
Mahesh Phalke 2:7b2b268ea49c 999 attr_scale_val[chn] *= V_SCALE;
Mahesh Phalke 2:7b2b268ea49c 1000 break;
Mahesh Phalke 2:7b2b268ea49c 1001
Mahesh Phalke 2:7b2b268ea49c 1002 default:
Mahesh Phalke 2:7b2b268ea49c 1003 break;
Mahesh Phalke 2:7b2b268ea49c 1004 }
Mahesh Phalke 2:7b2b268ea49c 1005 #endif
Mahesh Phalke 2:7b2b268ea49c 1006 }
Mahesh Phalke 2:7b2b268ea49c 1007
Mahesh Phalke 2:7b2b268ea49c 1008 /**
Mahesh Phalke 2:7b2b268ea49c 1009 * @brief Init for reading/writing and parameterization of a
Mahesh Phalke 2:7b2b268ea49c 1010 * ad4130 IIO device
Mahesh Phalke 2:7b2b268ea49c 1011 * @param desc[in,out] - IIO device descriptor
Mahesh Phalke 2:7b2b268ea49c 1012 * @return 0 in case of success, negative error code otherwise
Mahesh Phalke 2:7b2b268ea49c 1013 */
Mahesh Phalke 2:7b2b268ea49c 1014 int32_t ad4130_iio_init(struct iio_device **desc)
Mahesh Phalke 2:7b2b268ea49c 1015 {
Mahesh Phalke 2:7b2b268ea49c 1016 struct iio_device *iio_ad4130_inst;
Mahesh Phalke 2:7b2b268ea49c 1017 uint8_t chn;
Mahesh Phalke 2:7b2b268ea49c 1018 uint8_t bipolar;
Mahesh Phalke 2:7b2b268ea49c 1019
Mahesh Phalke 2:7b2b268ea49c 1020 iio_ad4130_inst = calloc(1, sizeof(struct iio_device));
Mahesh Phalke 2:7b2b268ea49c 1021 if (!iio_ad4130_inst) {
Mahesh Phalke 2:7b2b268ea49c 1022 return -ENOMEM;
Mahesh Phalke 2:7b2b268ea49c 1023 }
Mahesh Phalke 2:7b2b268ea49c 1024
Mahesh Phalke 2:7b2b268ea49c 1025 /* Update IIO device init parameters */
Mahesh Phalke 2:7b2b268ea49c 1026 for (chn = 0; chn < ADC_USER_CHANNELS; chn++) {
Mahesh Phalke 2:7b2b268ea49c 1027 update_vltg_conv_scale_factor(chn);
Mahesh Phalke 2:7b2b268ea49c 1028 }
Mahesh Phalke 2:7b2b268ea49c 1029
Mahesh Phalke 2:7b2b268ea49c 1030 /* Get the polarity of device */
Mahesh Phalke 2:7b2b268ea49c 1031 bipolar = ad4130_dev_inst->bipolar;
Mahesh Phalke 2:7b2b268ea49c 1032
Mahesh Phalke 2:7b2b268ea49c 1033 if (bipolar) {
Mahesh Phalke 2:7b2b268ea49c 1034 /* Using offset-binary coding for bipolar mode */
Mahesh Phalke 2:7b2b268ea49c 1035 chn_scan.sign = 's';
Mahesh Phalke 2:7b2b268ea49c 1036 chn_scan.realbits = CHN_STORAGE_BITS;
Mahesh Phalke 2:7b2b268ea49c 1037 } else {
Mahesh Phalke 2:7b2b268ea49c 1038 /* Using streight-binary coding for bipolar mode */
Mahesh Phalke 2:7b2b268ea49c 1039 chn_scan.sign = 'u';
Mahesh Phalke 2:7b2b268ea49c 1040 chn_scan.realbits = ADC_RESOLUTION;
Mahesh Phalke 2:7b2b268ea49c 1041 }
Mahesh Phalke 2:7b2b268ea49c 1042
Mahesh Phalke 2:7b2b268ea49c 1043 chn_scan.storagebits = CHN_STORAGE_BITS;
Mahesh Phalke 2:7b2b268ea49c 1044 chn_scan.shift = 0;
Mahesh Phalke 2:7b2b268ea49c 1045 chn_scan.is_big_endian = false;
Mahesh Phalke 2:7b2b268ea49c 1046
Mahesh Phalke 2:7b2b268ea49c 1047 iio_ad4130_inst->num_ch = NO_OS_ARRAY_SIZE(ad4130_iio_channels);
Mahesh Phalke 2:7b2b268ea49c 1048 iio_ad4130_inst->channels = ad4130_iio_channels;
Mahesh Phalke 2:7b2b268ea49c 1049 iio_ad4130_inst->attributes = ad4130_iio_global_attributes;
Mahesh Phalke 2:7b2b268ea49c 1050 iio_ad4130_inst->context_attributes = ad4130_iio_context_attributes;
Mahesh Phalke 2:7b2b268ea49c 1051
Mahesh Phalke 2:7b2b268ea49c 1052 iio_ad4130_inst->submit = iio_ad4130_submit_buffer;
Mahesh Phalke 2:7b2b268ea49c 1053 iio_ad4130_inst->pre_enable = iio_ad4130_prepare_transfer;
Mahesh Phalke 2:7b2b268ea49c 1054 iio_ad4130_inst->post_disable = iio_ad4130_end_transfer;
Mahesh Phalke 2:7b2b268ea49c 1055
Mahesh Phalke 2:7b2b268ea49c 1056 iio_ad4130_inst->debug_reg_read = debug_reg_read;
Mahesh Phalke 2:7b2b268ea49c 1057 iio_ad4130_inst->debug_reg_write = debug_reg_write;
Mahesh Phalke 2:7b2b268ea49c 1058
Mahesh Phalke 2:7b2b268ea49c 1059 *desc = iio_ad4130_inst;
Mahesh Phalke 2:7b2b268ea49c 1060
Mahesh Phalke 2:7b2b268ea49c 1061 return 0;
Mahesh Phalke 2:7b2b268ea49c 1062 }
Mahesh Phalke 2:7b2b268ea49c 1063
Mahesh Phalke 2:7b2b268ea49c 1064 /**
Mahesh Phalke 2:7b2b268ea49c 1065 * @brief Release resources allocated for IIO device
Mahesh Phalke 2:7b2b268ea49c 1066 * @param desc[in] - IIO device descriptor
Mahesh Phalke 2:7b2b268ea49c 1067 * @return 0 in case of success, negative error code otherwise
Mahesh Phalke 2:7b2b268ea49c 1068 */
Mahesh Phalke 2:7b2b268ea49c 1069 int32_t ad4130_iio_remove(struct iio_desc *desc)
Mahesh Phalke 2:7b2b268ea49c 1070 {
Mahesh Phalke 2:7b2b268ea49c 1071 int32_t status;
Mahesh Phalke 2:7b2b268ea49c 1072
Mahesh Phalke 2:7b2b268ea49c 1073 if (!desc) {
Mahesh Phalke 2:7b2b268ea49c 1074 return -ENOMEM;
Mahesh Phalke 2:7b2b268ea49c 1075 }
Mahesh Phalke 2:7b2b268ea49c 1076
Mahesh Phalke 2:7b2b268ea49c 1077 status = iio_remove(desc);
Mahesh Phalke 2:7b2b268ea49c 1078 if (status) {
Mahesh Phalke 2:7b2b268ea49c 1079 return status;
Mahesh Phalke 2:7b2b268ea49c 1080 }
Mahesh Phalke 2:7b2b268ea49c 1081
Mahesh Phalke 2:7b2b268ea49c 1082 return 0;
Mahesh Phalke 2:7b2b268ea49c 1083 }
Mahesh Phalke 2:7b2b268ea49c 1084
Mahesh Phalke 2:7b2b268ea49c 1085 /**
Mahesh Phalke 2:7b2b268ea49c 1086 * @brief Initialize the IIO interface for AD4130 IIO device
Mahesh Phalke 2:7b2b268ea49c 1087 * @return 0 in case of success, negative error code otherwise
Mahesh Phalke 2:7b2b268ea49c 1088 */
Mahesh Phalke 2:7b2b268ea49c 1089 int32_t ad4130_iio_initialize(void)
Mahesh Phalke 2:7b2b268ea49c 1090 {
Mahesh Phalke 2:7b2b268ea49c 1091 int32_t init_status;
Mahesh Phalke 2:7b2b268ea49c 1092
Mahesh Phalke 2:7b2b268ea49c 1093 /* IIO device descriptor */
Mahesh Phalke 2:7b2b268ea49c 1094 struct iio_device *p_iio_ad4130_dev;
Mahesh Phalke 2:7b2b268ea49c 1095
Mahesh Phalke 2:7b2b268ea49c 1096 /* IIO interface init parameters */
Mahesh Phalke 2:7b2b268ea49c 1097 struct iio_init_param iio_init_params = {
Mahesh Phalke 2:7b2b268ea49c 1098 .phy_type = USE_UART,
Mahesh Phalke 2:7b2b268ea49c 1099 .nb_devs = 1,
Mahesh Phalke 2:7b2b268ea49c 1100 };
Mahesh Phalke 2:7b2b268ea49c 1101
Mahesh Phalke 2:7b2b268ea49c 1102 /* IIOD init parameters */
Mahesh Phalke 2:7b2b268ea49c 1103 struct iio_device_init iio_device_init_params = {
Mahesh Phalke 2:7b2b268ea49c 1104 .name = (char *)dev_name,
Mahesh Phalke 2:7b2b268ea49c 1105 .raw_buf = adc_data_buffer,
Mahesh Phalke 2:7b2b268ea49c 1106 .raw_buf_len = DATA_BUFFER_SIZE
Mahesh Phalke 2:7b2b268ea49c 1107 };
Mahesh Phalke 2:7b2b268ea49c 1108
Mahesh Phalke 2:7b2b268ea49c 1109 /* Init the system peripherals */
Mahesh Phalke 2:7b2b268ea49c 1110 init_status = init_system();
Mahesh Phalke 2:7b2b268ea49c 1111 if (init_status) {
Mahesh Phalke 2:7b2b268ea49c 1112 return init_status;
Mahesh Phalke 2:7b2b268ea49c 1113 }
Mahesh Phalke 2:7b2b268ea49c 1114
Mahesh Phalke 2:7b2b268ea49c 1115 /* Initialize AD4130 device and peripheral interface */
Mahesh Phalke 2:7b2b268ea49c 1116 init_status = ad413x_init(&ad4130_dev_inst, ad4130_init_params);
Mahesh Phalke 2:7b2b268ea49c 1117 if (init_status) {
Mahesh Phalke 2:7b2b268ea49c 1118 return init_status;
Mahesh Phalke 2:7b2b268ea49c 1119 }
Mahesh Phalke 2:7b2b268ea49c 1120
Mahesh Phalke 2:7b2b268ea49c 1121 /* Initialize the AD4130 IIO application interface */
Mahesh Phalke 2:7b2b268ea49c 1122 init_status = ad4130_iio_init(&p_iio_ad4130_dev);
Mahesh Phalke 2:7b2b268ea49c 1123 if (init_status) {
Mahesh Phalke 2:7b2b268ea49c 1124 return init_status;
Mahesh Phalke 2:7b2b268ea49c 1125 }
Mahesh Phalke 2:7b2b268ea49c 1126
Mahesh Phalke 2:7b2b268ea49c 1127 /* Initialize the IIO interface */
Mahesh Phalke 2:7b2b268ea49c 1128 iio_init_params.uart_desc = uart_desc;
Mahesh Phalke 2:7b2b268ea49c 1129 iio_device_init_params.dev = ad4130_dev_inst;
Mahesh Phalke 2:7b2b268ea49c 1130 iio_device_init_params.dev_descriptor = p_iio_ad4130_dev;
Mahesh Phalke 2:7b2b268ea49c 1131 iio_init_params.devs = &iio_device_init_params;
Mahesh Phalke 2:7b2b268ea49c 1132 init_status = iio_init(&p_ad4130_iio_desc, &iio_init_params);
Mahesh Phalke 2:7b2b268ea49c 1133 if (init_status) {
Mahesh Phalke 2:7b2b268ea49c 1134 return init_status;
Mahesh Phalke 2:7b2b268ea49c 1135 }
Mahesh Phalke 2:7b2b268ea49c 1136
Mahesh Phalke 2:7b2b268ea49c 1137 /* Perform data capture initialization */
Mahesh Phalke 2:7b2b268ea49c 1138 init_status = ad4130_data_capture_init();
Mahesh Phalke 2:7b2b268ea49c 1139 if (init_status) {
Mahesh Phalke 2:7b2b268ea49c 1140 return init_status;
Mahesh Phalke 2:7b2b268ea49c 1141 }
Mahesh Phalke 2:7b2b268ea49c 1142
Mahesh Phalke 2:7b2b268ea49c 1143 return 0;
Mahesh Phalke 2:7b2b268ea49c 1144 }
Mahesh Phalke 2:7b2b268ea49c 1145
Mahesh Phalke 2:7b2b268ea49c 1146 /**
Mahesh Phalke 2:7b2b268ea49c 1147 * @brief Run the AD4130 IIO event handler
Mahesh Phalke 2:7b2b268ea49c 1148 * @return none
Mahesh Phalke 2:7b2b268ea49c 1149 * @details This function monitors the new IIO client event
Mahesh Phalke 2:7b2b268ea49c 1150 */
Mahesh Phalke 2:7b2b268ea49c 1151 void ad4130_iio_event_handler(void)
Mahesh Phalke 2:7b2b268ea49c 1152 {
Mahesh Phalke 2:7b2b268ea49c 1153 (void)iio_step(p_ad4130_iio_desc);
Mahesh Phalke 2:7b2b268ea49c 1154 }