IIO firmware for the AD4110
Dependencies: tempsensors sdp_k1_sdram
app/ad4110_iio.c@0:6ca37a8f8ba9, 2022-07-27 (annotated)
- Committer:
- Janani Sunil
- Date:
- Wed Jul 27 17:04:15 2022 +0530
- Revision:
- 0:6ca37a8f8ba9
Initial implementation for AD4110 IIO Firmware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Janani Sunil | 0:6ca37a8f8ba9 | 1 | /***************************************************************************//** |
Janani Sunil | 0:6ca37a8f8ba9 | 2 | * @file ad4110_iio.c |
Janani Sunil | 0:6ca37a8f8ba9 | 3 | * @brief Implementation of AD4110-1 IIO application interfaces |
Janani Sunil | 0:6ca37a8f8ba9 | 4 | ******************************************************************************** |
Janani Sunil | 0:6ca37a8f8ba9 | 5 | * Copyright (c) 2022 Analog Devices, Inc. |
Janani Sunil | 0:6ca37a8f8ba9 | 6 | * All rights reserved. |
Janani Sunil | 0:6ca37a8f8ba9 | 7 | * |
Janani Sunil | 0:6ca37a8f8ba9 | 8 | * This software is proprietary to Analog Devices, Inc. and its licensors. |
Janani Sunil | 0:6ca37a8f8ba9 | 9 | * By using this software you agree to the terms of the associated |
Janani Sunil | 0:6ca37a8f8ba9 | 10 | * Analog Devices Software License Agreement. |
Janani Sunil | 0:6ca37a8f8ba9 | 11 | *******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 12 | |
Janani Sunil | 0:6ca37a8f8ba9 | 13 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 14 | /***************************** Include Files **********************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 15 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 16 | |
Janani Sunil | 0:6ca37a8f8ba9 | 17 | #include <stdlib.h> |
Janani Sunil | 0:6ca37a8f8ba9 | 18 | #include "ad4110_iio.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 19 | #include "app_config.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 20 | #include "ad4110.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 21 | #include "no_os_error.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 22 | #include "no_os_util.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 23 | #include "ad4110_data_capture.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 24 | #include "ad4110_temperature_sensor.h" |
Janani Sunil | 0:6ca37a8f8ba9 | 25 | |
Janani Sunil | 0:6ca37a8f8ba9 | 26 | /* Forward declaration of getter/setter functions */ |
Janani Sunil | 0:6ca37a8f8ba9 | 27 | static int ad4110_get_attribute(void *device, char *buf, uint32_t len, |
Janani Sunil | 0:6ca37a8f8ba9 | 28 | const struct iio_ch_info *channel, intptr_t priv); |
Janani Sunil | 0:6ca37a8f8ba9 | 29 | |
Janani Sunil | 0:6ca37a8f8ba9 | 30 | static int ad4110_set_attribute(void *device, char *buf, uint32_t len, |
Janani Sunil | 0:6ca37a8f8ba9 | 31 | const struct iio_ch_info *channel,intptr_t priv); |
Janani Sunil | 0:6ca37a8f8ba9 | 32 | |
Janani Sunil | 0:6ca37a8f8ba9 | 33 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 34 | /************************ Macros/Constants ************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 35 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 36 | |
Janani Sunil | 0:6ca37a8f8ba9 | 37 | /* Bytes per sample (*Note: 4 bytes needed per sample for data range |
Janani Sunil | 0:6ca37a8f8ba9 | 38 | * of 0 to 32-bit) */ |
Janani Sunil | 0:6ca37a8f8ba9 | 39 | #define BYTES_PER_SAMPLE sizeof(uint32_t) |
Janani Sunil | 0:6ca37a8f8ba9 | 40 | |
Janani Sunil | 0:6ca37a8f8ba9 | 41 | /* Number of data storage bits (needed for IIO client) */ |
Janani Sunil | 0:6ca37a8f8ba9 | 42 | #define CHN_STORAGE_BITS (BYTES_PER_SAMPLE * 8) |
Janani Sunil | 0:6ca37a8f8ba9 | 43 | |
Janani Sunil | 0:6ca37a8f8ba9 | 44 | /* Channel Attribute definition */ |
Janani Sunil | 0:6ca37a8f8ba9 | 45 | #define AD4110_CH_ATTR(_name, _idx) {\ |
Janani Sunil | 0:6ca37a8f8ba9 | 46 | .name = _name,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 47 | .priv = _idx,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 48 | .show = ad4110_get_attribute,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 49 | .store = ad4110_set_attribute\ |
Janani Sunil | 0:6ca37a8f8ba9 | 50 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 51 | |
Janani Sunil | 0:6ca37a8f8ba9 | 52 | /* Channel definition */ |
Janani Sunil | 0:6ca37a8f8ba9 | 53 | #define AD4110_CHANNEL(_name, _idx) {\ |
Janani Sunil | 0:6ca37a8f8ba9 | 54 | .name = _name # _idx, \ |
Janani Sunil | 0:6ca37a8f8ba9 | 55 | .ch_type = IIO_CHANNEL_TYPE,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 56 | .ch_out = false,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 57 | .indexed = true,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 58 | .channel = _idx,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 59 | .scan_index = _idx,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 60 | .scan_type = &ad4110_scan_type,\ |
Janani Sunil | 0:6ca37a8f8ba9 | 61 | .attributes = ad4110_channel_attributes\ |
Janani Sunil | 0:6ca37a8f8ba9 | 62 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 63 | |
Janani Sunil | 0:6ca37a8f8ba9 | 64 | #if (REGISTER_READ_SEL == AD4110_ADC) |
Janani Sunil | 0:6ca37a8f8ba9 | 65 | #define REGISTER_MAX_VAL AD4110_ADC_GAIN3 |
Janani Sunil | 0:6ca37a8f8ba9 | 66 | #else // AD4110_AFE |
Janani Sunil | 0:6ca37a8f8ba9 | 67 | #define REGISTER_MAX_VAL AD4110_REG_AFE_NO_PWR_DEFAULT_STATUS |
Janani Sunil | 0:6ca37a8f8ba9 | 68 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 69 | |
Janani Sunil | 0:6ca37a8f8ba9 | 70 | /* SCale value for voltage and current mode configuration */ |
Janani Sunil | 0:6ca37a8f8ba9 | 71 | #define AD4110_BIPOLAR_SCALE ((AD4110_REF_VOLTAGE / (ADC_MAX_COUNT_BIPOLAR)) * 1000) /AD4110_DEFAULT_PGA |
Janani Sunil | 0:6ca37a8f8ba9 | 72 | #define AD4110_UNIPOLAR_SCALE ((AD4110_REF_VOLTAGE / (ADC_MAX_COUNT_UNIPOLAR)) * 1000) /AD4110_DEFAULT_PGA |
Janani Sunil | 0:6ca37a8f8ba9 | 73 | |
Janani Sunil | 0:6ca37a8f8ba9 | 74 | /* IIO Channel type for each demo mode config */ |
Janani Sunil | 0:6ca37a8f8ba9 | 75 | #if (ACTIVE_DEMO_MODE_CONFIG == VOLTAGE_MODE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 76 | #define IIO_CHANNEL_TYPE IIO_VOLTAGE |
Janani Sunil | 0:6ca37a8f8ba9 | 77 | #elif (ACTIVE_DEMO_MODE_CONFIG == CURRENT_MODE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 78 | #define IIO_CHANNEL_TYPE IIO_CURRENT |
Janani Sunil | 0:6ca37a8f8ba9 | 79 | #elif (ACTIVE_DEMO_MODE_CONFIG == FIELD_POWER_SUPPLY_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 80 | #define IIO_CHANNEL_TYPE IIO_CURRENT |
Janani Sunil | 0:6ca37a8f8ba9 | 81 | #elif (ACTIVE_DEMO_MODE_CONFIG == THERMOCOUPLE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 82 | #define IIO_CHANNEL_TYPE IIO_TEMP |
Janani Sunil | 0:6ca37a8f8ba9 | 83 | #elif (ACTIVE_DEMO_MODE_CONFIG == RTD_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 84 | #define IIO_CHANNEL_TYPE IIO_TEMP |
Janani Sunil | 0:6ca37a8f8ba9 | 85 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 86 | |
Janani Sunil | 0:6ca37a8f8ba9 | 87 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 88 | /*************************** Types Declarations *******************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 89 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 90 | |
Janani Sunil | 0:6ca37a8f8ba9 | 91 | /* IIO interface descriptor */ |
Janani Sunil | 0:6ca37a8f8ba9 | 92 | static struct iio_desc *ad4110_iio_desc; |
Janani Sunil | 0:6ca37a8f8ba9 | 93 | |
Janani Sunil | 0:6ca37a8f8ba9 | 94 | /* AD4110-1 Device Descriptor */ |
Janani Sunil | 0:6ca37a8f8ba9 | 95 | struct ad4110_dev *ad4110_dev_inst; |
Janani Sunil | 0:6ca37a8f8ba9 | 96 | |
Janani Sunil | 0:6ca37a8f8ba9 | 97 | /* IIO Device name */ |
Janani Sunil | 0:6ca37a8f8ba9 | 98 | static const char *dev_name = ACTIVE_DEVICE; |
Janani Sunil | 0:6ca37a8f8ba9 | 99 | |
Janani Sunil | 0:6ca37a8f8ba9 | 100 | /* Channel Attribute ID */ |
Janani Sunil | 0:6ca37a8f8ba9 | 101 | enum ad4110_attribute_id { |
Janani Sunil | 0:6ca37a8f8ba9 | 102 | RAW_ATTR_ID, |
Janani Sunil | 0:6ca37a8f8ba9 | 103 | SCALE_ATTR_ID, |
Janani Sunil | 0:6ca37a8f8ba9 | 104 | OFFSET_ATTR_ID, |
Janani Sunil | 0:6ca37a8f8ba9 | 105 | SAMPLING_FREQ_ATTR_ID, |
Janani Sunil | 0:6ca37a8f8ba9 | 106 | DEMO_CONFIG_ATTR_ID |
Janani Sunil | 0:6ca37a8f8ba9 | 107 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 108 | |
Janani Sunil | 0:6ca37a8f8ba9 | 109 | struct scan_type ad4110_scan_type = { |
Janani Sunil | 0:6ca37a8f8ba9 | 110 | .storagebits = CHN_STORAGE_BITS, |
Janani Sunil | 0:6ca37a8f8ba9 | 111 | .shift = 0, |
Janani Sunil | 0:6ca37a8f8ba9 | 112 | .is_big_endian = false |
Janani Sunil | 0:6ca37a8f8ba9 | 113 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 114 | |
Janani Sunil | 0:6ca37a8f8ba9 | 115 | /* Global attributes */ |
Janani Sunil | 0:6ca37a8f8ba9 | 116 | static struct iio_attribute ad4110_global_attributes[] = { |
Janani Sunil | 0:6ca37a8f8ba9 | 117 | AD4110_CH_ATTR("sampling_frequency", SAMPLING_FREQ_ATTR_ID), |
Janani Sunil | 0:6ca37a8f8ba9 | 118 | AD4110_CH_ATTR("demo_config", DEMO_CONFIG_ATTR_ID), |
Janani Sunil | 0:6ca37a8f8ba9 | 119 | |
Janani Sunil | 0:6ca37a8f8ba9 | 120 | END_ATTRIBUTES_ARRAY |
Janani Sunil | 0:6ca37a8f8ba9 | 121 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 122 | |
Janani Sunil | 0:6ca37a8f8ba9 | 123 | /* Channel attributes */ |
Janani Sunil | 0:6ca37a8f8ba9 | 124 | static struct iio_attribute ad4110_channel_attributes[] = { |
Janani Sunil | 0:6ca37a8f8ba9 | 125 | AD4110_CH_ATTR("raw", RAW_ATTR_ID), |
Janani Sunil | 0:6ca37a8f8ba9 | 126 | AD4110_CH_ATTR("scale", SCALE_ATTR_ID), |
Janani Sunil | 0:6ca37a8f8ba9 | 127 | AD4110_CH_ATTR("offset", OFFSET_ATTR_ID), |
Janani Sunil | 0:6ca37a8f8ba9 | 128 | |
Janani Sunil | 0:6ca37a8f8ba9 | 129 | END_ATTRIBUTES_ARRAY |
Janani Sunil | 0:6ca37a8f8ba9 | 130 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 131 | |
Janani Sunil | 0:6ca37a8f8ba9 | 132 | /* IIOD channels configurations for AD4110-1 */ |
Janani Sunil | 0:6ca37a8f8ba9 | 133 | static struct iio_channel ad4110_iio_channels[] = { |
Janani Sunil | 0:6ca37a8f8ba9 | 134 | AD4110_CHANNEL("Chn", 0), |
Janani Sunil | 0:6ca37a8f8ba9 | 135 | AD4110_CHANNEL("Chn", 1), |
Janani Sunil | 0:6ca37a8f8ba9 | 136 | AD4110_CHANNEL("Chn", 2), |
Janani Sunil | 0:6ca37a8f8ba9 | 137 | AD4110_CHANNEL("Chn", 3) |
Janani Sunil | 0:6ca37a8f8ba9 | 138 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 139 | |
Janani Sunil | 0:6ca37a8f8ba9 | 140 | /* IIO context attributes list */ |
Janani Sunil | 0:6ca37a8f8ba9 | 141 | static struct iio_context_attribute ad4110_iio_context_attributes[] = { |
Janani Sunil | 0:6ca37a8f8ba9 | 142 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 143 | .name = "hw_mezzanine", |
Janani Sunil | 0:6ca37a8f8ba9 | 144 | .value = HW_MEZZANINE_NAME |
Janani Sunil | 0:6ca37a8f8ba9 | 145 | }, |
Janani Sunil | 0:6ca37a8f8ba9 | 146 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 147 | .name = "hw_carrier", |
Janani Sunil | 0:6ca37a8f8ba9 | 148 | .value = HW_CARRIER_NAME |
Janani Sunil | 0:6ca37a8f8ba9 | 149 | }, |
Janani Sunil | 0:6ca37a8f8ba9 | 150 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 151 | .name = "hw_name", |
Janani Sunil | 0:6ca37a8f8ba9 | 152 | .value = HW_NAME |
Janani Sunil | 0:6ca37a8f8ba9 | 153 | }, |
Janani Sunil | 0:6ca37a8f8ba9 | 154 | END_ATTRIBUTES_ARRAY |
Janani Sunil | 0:6ca37a8f8ba9 | 155 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 156 | |
Janani Sunil | 0:6ca37a8f8ba9 | 157 | /* Scale attribute value per channel */ |
Janani Sunil | 0:6ca37a8f8ba9 | 158 | float attr_scale_val[AD4110_NUM_CHANNELS]; |
Janani Sunil | 0:6ca37a8f8ba9 | 159 | |
Janani Sunil | 0:6ca37a8f8ba9 | 160 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 161 | /************************ Functions Definitions *******************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 162 | /******************************************************************************/ |
Janani Sunil | 0:6ca37a8f8ba9 | 163 | |
Janani Sunil | 0:6ca37a8f8ba9 | 164 | /*! |
Janani Sunil | 0:6ca37a8f8ba9 | 165 | * @brief Calculate the scale value |
Janani Sunil | 0:6ca37a8f8ba9 | 166 | * @param chan_id[in] - ADC input channel |
Janani Sunil | 0:6ca37a8f8ba9 | 167 | * @param adc_raw[in] - Raw ADC Data |
Janani Sunil | 0:6ca37a8f8ba9 | 168 | * @return 0 in case of success, negative error code otherwise. |
Janani Sunil | 0:6ca37a8f8ba9 | 169 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 170 | static int32_t ad4110_get_scale(uint8_t chan_id, int32_t adc_raw) |
Janani Sunil | 0:6ca37a8f8ba9 | 171 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 172 | float temperature = 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 173 | uint32_t cjc_raw_data; |
Janani Sunil | 0:6ca37a8f8ba9 | 174 | float cjc_temp; |
Janani Sunil | 0:6ca37a8f8ba9 | 175 | int32_t ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 176 | |
Janani Sunil | 0:6ca37a8f8ba9 | 177 | #if (ACTIVE_DEMO_MODE_CONFIG == RTD_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 178 | temperature = get_rtd_temperature(adc_raw); |
Janani Sunil | 0:6ca37a8f8ba9 | 179 | attr_scale_val[chan_id] = (temperature / adc_raw) * 1000.0; |
Janani Sunil | 0:6ca37a8f8ba9 | 180 | #elif (ACTIVE_DEMO_MODE_CONFIG == THERMOCOUPLE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 181 | /* Sample the CJC Channel. TC is already sampled through the get_adc_attribute() function */ |
Janani Sunil | 0:6ca37a8f8ba9 | 182 | if (chan_id != CJC_CHANNEL) { |
Janani Sunil | 0:6ca37a8f8ba9 | 183 | /* Disable the requested channel chan_id */ |
Janani Sunil | 0:6ca37a8f8ba9 | 184 | ret = ad4110_set_channel_status(ad4110_dev_inst, chan_id, false); |
Janani Sunil | 0:6ca37a8f8ba9 | 185 | if (ret) { |
Janani Sunil | 0:6ca37a8f8ba9 | 186 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 187 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 188 | |
Janani Sunil | 0:6ca37a8f8ba9 | 189 | /* Enable the channel on which CJC has been incorporated */ |
Janani Sunil | 0:6ca37a8f8ba9 | 190 | ret = ad4110_set_channel_status(ad4110_dev_inst, CJC_CHANNEL, true); |
Janani Sunil | 0:6ca37a8f8ba9 | 191 | if (ret) { |
Janani Sunil | 0:6ca37a8f8ba9 | 192 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 193 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 194 | |
Janani Sunil | 0:6ca37a8f8ba9 | 195 | /* Read the CJC raw data */ |
Janani Sunil | 0:6ca37a8f8ba9 | 196 | if (ad4110_do_single_read(ad4110_dev_inst, &cjc_raw_data)) { |
Janani Sunil | 0:6ca37a8f8ba9 | 197 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 198 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 199 | |
Janani Sunil | 0:6ca37a8f8ba9 | 200 | /* Disable the CJC channel */ |
Janani Sunil | 0:6ca37a8f8ba9 | 201 | ret = ad4110_set_channel_status(ad4110_dev_inst, CJC_CHANNEL, false); |
Janani Sunil | 0:6ca37a8f8ba9 | 202 | if (ret) { |
Janani Sunil | 0:6ca37a8f8ba9 | 203 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 204 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 205 | |
Janani Sunil | 0:6ca37a8f8ba9 | 206 | } else { |
Janani Sunil | 0:6ca37a8f8ba9 | 207 | /* For calculating CJC value, TC raw value does not matter */ |
Janani Sunil | 0:6ca37a8f8ba9 | 208 | chan_id = 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 209 | cjc_raw_data = adc_raw; |
Janani Sunil | 0:6ca37a8f8ba9 | 210 | adc_raw = 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 211 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 212 | |
Janani Sunil | 0:6ca37a8f8ba9 | 213 | /* Calculate the TC and CJC temperature and update scale factor */ |
Janani Sunil | 0:6ca37a8f8ba9 | 214 | temperature = get_tc_temperature(adc_raw, HV_CHANNEL, cjc_raw_data, CJC_CHANNEL, |
Janani Sunil | 0:6ca37a8f8ba9 | 215 | &cjc_temp); |
Janani Sunil | 0:6ca37a8f8ba9 | 216 | attr_scale_val[chan_id] = (temperature / adc_raw) * 1000; |
Janani Sunil | 0:6ca37a8f8ba9 | 217 | attr_scale_val[CJC_CHANNEL] = (cjc_temp / cjc_raw_data) * 1000; |
Janani Sunil | 0:6ca37a8f8ba9 | 218 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 219 | |
Janani Sunil | 0:6ca37a8f8ba9 | 220 | return 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 221 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 222 | |
Janani Sunil | 0:6ca37a8f8ba9 | 223 | |
Janani Sunil | 0:6ca37a8f8ba9 | 224 | /*! |
Janani Sunil | 0:6ca37a8f8ba9 | 225 | * @brief Getter/Setter function for ADC attributes |
Janani Sunil | 0:6ca37a8f8ba9 | 226 | * @param device[in]- Pointer to IIO device instance |
Janani Sunil | 0:6ca37a8f8ba9 | 227 | * @param buf[in]- IIO input data buffer |
Janani Sunil | 0:6ca37a8f8ba9 | 228 | * @param len[in]- Number of input bytes |
Janani Sunil | 0:6ca37a8f8ba9 | 229 | * @param channel[in] - ADC input channel |
Janani Sunil | 0:6ca37a8f8ba9 | 230 | * @param priv[in] - Attribute private ID |
Janani Sunil | 0:6ca37a8f8ba9 | 231 | * @return len in case of success, negative error code otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 232 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 233 | static int ad4110_get_attribute(void *device, char *buf, uint32_t len, |
Janani Sunil | 0:6ca37a8f8ba9 | 234 | const struct iio_ch_info *channel, intptr_t priv) |
Janani Sunil | 0:6ca37a8f8ba9 | 235 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 236 | int ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 237 | uint32_t raw_data_read = 0 ; |
Janani Sunil | 0:6ca37a8f8ba9 | 238 | int32_t offset = 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 239 | uint8_t ch_id; |
Janani Sunil | 0:6ca37a8f8ba9 | 240 | float scale; |
Janani Sunil | 0:6ca37a8f8ba9 | 241 | |
Janani Sunil | 0:6ca37a8f8ba9 | 242 | switch (priv) { |
Janani Sunil | 0:6ca37a8f8ba9 | 243 | case RAW_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 244 | /* Enable the requested channel, disable the rest */ |
Janani Sunil | 0:6ca37a8f8ba9 | 245 | for (ch_id = 0; ch_id < AD4110_NUM_CHANNELS; ch_id++) { |
Janani Sunil | 0:6ca37a8f8ba9 | 246 | if (ch_id == channel->ch_num) { |
Janani Sunil | 0:6ca37a8f8ba9 | 247 | ret = ad4110_set_channel_status(device, ch_id, true); |
Janani Sunil | 0:6ca37a8f8ba9 | 248 | } else { |
Janani Sunil | 0:6ca37a8f8ba9 | 249 | ret = ad4110_set_channel_status(device, ch_id, false); |
Janani Sunil | 0:6ca37a8f8ba9 | 250 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 251 | if (ret) { |
Janani Sunil | 0:6ca37a8f8ba9 | 252 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 253 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 254 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 255 | |
Janani Sunil | 0:6ca37a8f8ba9 | 256 | /* Perform single conversion */ |
Janani Sunil | 0:6ca37a8f8ba9 | 257 | ret = ad4110_do_single_read(device, &raw_data_read); |
Janani Sunil | 0:6ca37a8f8ba9 | 258 | if (ret) { |
Janani Sunil | 0:6ca37a8f8ba9 | 259 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 260 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 261 | /* Update scale factor depending on the channel requested */ |
Janani Sunil | 0:6ca37a8f8ba9 | 262 | ret = ad4110_get_scale(channel->ch_num, raw_data_read); |
Janani Sunil | 0:6ca37a8f8ba9 | 263 | if (ret) { |
Janani Sunil | 0:6ca37a8f8ba9 | 264 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 265 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 266 | |
Janani Sunil | 0:6ca37a8f8ba9 | 267 | return sprintf(buf, "%d", raw_data_read); |
Janani Sunil | 0:6ca37a8f8ba9 | 268 | |
Janani Sunil | 0:6ca37a8f8ba9 | 269 | case SCALE_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 270 | return sprintf(buf, "%g", attr_scale_val[channel->ch_num]); |
Janani Sunil | 0:6ca37a8f8ba9 | 271 | |
Janani Sunil | 0:6ca37a8f8ba9 | 272 | case OFFSET_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 273 | #if (ACTIVE_DEMO_MODE_CONFIG == VOLTAGE_MODE_CONFIG ||\ |
Janani Sunil | 0:6ca37a8f8ba9 | 274 | ACTIVE_DEMO_MODE_CONFIG == CURRENT_MODE_CONFIG ||\ |
Janani Sunil | 0:6ca37a8f8ba9 | 275 | ACTIVE_DEMO_MODE_CONFIG == FIELD_POWER_SUPPLY_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 276 | /* Offset values for other demo modes are incorporated as a part of the ad4110_get_scale() function */ |
Janani Sunil | 0:6ca37a8f8ba9 | 277 | if (ad4110_dev_inst->bipolar) { |
Janani Sunil | 0:6ca37a8f8ba9 | 278 | offset = -ADC_MAX_COUNT_BIPOLAR; |
Janani Sunil | 0:6ca37a8f8ba9 | 279 | } else { |
Janani Sunil | 0:6ca37a8f8ba9 | 280 | offset = 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 281 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 282 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 283 | return sprintf(buf, "%ld", offset); |
Janani Sunil | 0:6ca37a8f8ba9 | 284 | |
Janani Sunil | 0:6ca37a8f8ba9 | 285 | case SAMPLING_FREQ_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 286 | return sprintf(buf, "%d", AD4110_SAMPLING_RATE); |
Janani Sunil | 0:6ca37a8f8ba9 | 287 | |
Janani Sunil | 0:6ca37a8f8ba9 | 288 | case DEMO_CONFIG_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 289 | #if (ACTIVE_DEMO_MODE_CONFIG == VOLTAGE_MODE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 290 | return sprintf(buf, "%s", "Voltage"); |
Janani Sunil | 0:6ca37a8f8ba9 | 291 | #elif (ACTIVE_DEMO_MODE_CONFIG == CURRENT_MODE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 292 | return sprintf(buf, "%s", "Current"); |
Janani Sunil | 0:6ca37a8f8ba9 | 293 | #elif (ACTIVE_DEMO_MODE_CONFIG == RTD_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 294 | return sprintf(buf, "%s", "RTD"); |
Janani Sunil | 0:6ca37a8f8ba9 | 295 | #elif (ACTIVE_DEMO_MODE_CONFIG == FIELD_POWER_SUPPLY_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 296 | return sprintf(buf, "%s", "Field Power Supply"); |
Janani Sunil | 0:6ca37a8f8ba9 | 297 | #elif (ACTIVE_DEMO_MODE_CONFIG == THERMOCOUPLE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 298 | return sprintf(buf, "%s", "Thermocouple"); |
Janani Sunil | 0:6ca37a8f8ba9 | 299 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 300 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 301 | |
Janani Sunil | 0:6ca37a8f8ba9 | 302 | return -EINVAL; |
Janani Sunil | 0:6ca37a8f8ba9 | 303 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 304 | |
Janani Sunil | 0:6ca37a8f8ba9 | 305 | static int ad4110_set_attribute(void *device, char *buf, uint32_t len, |
Janani Sunil | 0:6ca37a8f8ba9 | 306 | const struct iio_ch_info *channel, intptr_t priv) |
Janani Sunil | 0:6ca37a8f8ba9 | 307 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 308 | switch (priv) { |
Janani Sunil | 0:6ca37a8f8ba9 | 309 | case RAW_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 310 | case SCALE_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 311 | case SAMPLING_FREQ_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 312 | case DEMO_CONFIG_ATTR_ID: |
Janani Sunil | 0:6ca37a8f8ba9 | 313 | default: |
Janani Sunil | 0:6ca37a8f8ba9 | 314 | break; |
Janani Sunil | 0:6ca37a8f8ba9 | 315 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 316 | |
Janani Sunil | 0:6ca37a8f8ba9 | 317 | return len; |
Janani Sunil | 0:6ca37a8f8ba9 | 318 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 319 | |
Janani Sunil | 0:6ca37a8f8ba9 | 320 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 321 | * @brief Transfer the device data into memory (optional) |
Janani Sunil | 0:6ca37a8f8ba9 | 322 | * @param dev_instance[in] - IIO device instance |
Janani Sunil | 0:6ca37a8f8ba9 | 323 | * @param ch_mask[in] - Channels select mask |
Janani Sunil | 0:6ca37a8f8ba9 | 324 | * @return 0 in case of success or negative value otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 325 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 326 | static int32_t iio_ad4110_pre_enable(void *dev_instance, uint32_t chn_mask) |
Janani Sunil | 0:6ca37a8f8ba9 | 327 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 328 | return prepare_data_transfer(chn_mask, AD4110_NUM_CHANNELS, BYTES_PER_SAMPLE); |
Janani Sunil | 0:6ca37a8f8ba9 | 329 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 330 | |
Janani Sunil | 0:6ca37a8f8ba9 | 331 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 332 | * @brief Read buffer data corresponding to AD4110 IIO device |
Janani Sunil | 0:6ca37a8f8ba9 | 333 | * @param iio_dev_data[in] - Pointer to IIO device data structure |
Janani Sunil | 0:6ca37a8f8ba9 | 334 | * @return 0 in case of success or negative value otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 335 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 336 | static int32_t iio_ad4110_submit_buffer(struct iio_device_data *iio_dev_data) |
Janani Sunil | 0:6ca37a8f8ba9 | 337 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 338 | int32_t ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 339 | |
Janani Sunil | 0:6ca37a8f8ba9 | 340 | /* Read the samples counts equal to buffer size/block */ |
Janani Sunil | 0:6ca37a8f8ba9 | 341 | ret = read_buffered_data(&iio_dev_data->buffer->buf->buff, |
Janani Sunil | 0:6ca37a8f8ba9 | 342 | iio_dev_data->buffer->size); |
Janani Sunil | 0:6ca37a8f8ba9 | 343 | |
Janani Sunil | 0:6ca37a8f8ba9 | 344 | /* Increment the write spin count as buffer reads all 'n' samples |
Janani Sunil | 0:6ca37a8f8ba9 | 345 | * in one go which is also the size of buffer block for a given instance. |
Janani Sunil | 0:6ca37a8f8ba9 | 346 | * The read spin count is incremented from IIO library during buffer |
Janani Sunil | 0:6ca37a8f8ba9 | 347 | * write/offloading into transmit buffer */ |
Janani Sunil | 0:6ca37a8f8ba9 | 348 | if (iio_dev_data->buffer->buf->write.spin_count >= UINT32_MAX) { |
Janani Sunil | 0:6ca37a8f8ba9 | 349 | iio_dev_data->buffer->buf->write.spin_count = 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 350 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 351 | |
Janani Sunil | 0:6ca37a8f8ba9 | 352 | iio_dev_data->buffer->buf->write.spin_count += 1; |
Janani Sunil | 0:6ca37a8f8ba9 | 353 | |
Janani Sunil | 0:6ca37a8f8ba9 | 354 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 355 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 356 | |
Janani Sunil | 0:6ca37a8f8ba9 | 357 | |
Janani Sunil | 0:6ca37a8f8ba9 | 358 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 359 | * @brief Perform tasks before end of current data transfer |
Janani Sunil | 0:6ca37a8f8ba9 | 360 | * @param dev[in] - IIO device instance |
Janani Sunil | 0:6ca37a8f8ba9 | 361 | * @return 0 in case of success or negative value otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 362 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 363 | static int32_t iio_ad4110_post_disable(void *dev) |
Janani Sunil | 0:6ca37a8f8ba9 | 364 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 365 | return end_data_transfer(); |
Janani Sunil | 0:6ca37a8f8ba9 | 366 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 367 | |
Janani Sunil | 0:6ca37a8f8ba9 | 368 | /*! |
Janani Sunil | 0:6ca37a8f8ba9 | 369 | * @brief Read the device register value |
Janani Sunil | 0:6ca37a8f8ba9 | 370 | * @param dev[in]- Pointer to IIO device instance |
Janani Sunil | 0:6ca37a8f8ba9 | 371 | * @param reg[in]- Register address to read from |
Janani Sunil | 0:6ca37a8f8ba9 | 372 | * @param readval[out]- Pointer to variable to read data into |
Janani Sunil | 0:6ca37a8f8ba9 | 373 | * @return 0 in case of success, negative error code otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 374 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 375 | static int32_t ad4110_read_register(void *dev, uint32_t reg, uint32_t *readval) |
Janani Sunil | 0:6ca37a8f8ba9 | 376 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 377 | int ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 378 | |
Janani Sunil | 0:6ca37a8f8ba9 | 379 | if (reg > REGISTER_MAX_VAL) { |
Janani Sunil | 0:6ca37a8f8ba9 | 380 | return -EINVAL; |
Janani Sunil | 0:6ca37a8f8ba9 | 381 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 382 | |
Janani Sunil | 0:6ca37a8f8ba9 | 383 | ret = ad4110_spi_int_reg_read(dev, REGISTER_READ_SEL, reg, readval); |
Janani Sunil | 0:6ca37a8f8ba9 | 384 | if (ret < 0) { |
Janani Sunil | 0:6ca37a8f8ba9 | 385 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 386 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 387 | |
Janani Sunil | 0:6ca37a8f8ba9 | 388 | return 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 389 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 390 | |
Janani Sunil | 0:6ca37a8f8ba9 | 391 | /*! |
Janani Sunil | 0:6ca37a8f8ba9 | 392 | * @brief Write into the device register |
Janani Sunil | 0:6ca37a8f8ba9 | 393 | * @param dev[in] - Pointer to IIO device instance |
Janani Sunil | 0:6ca37a8f8ba9 | 394 | * @param reg[in] - Register address to write into |
Janani Sunil | 0:6ca37a8f8ba9 | 395 | * @param writeval[in] - Register value to write |
Janani Sunil | 0:6ca37a8f8ba9 | 396 | * @return 0 in case of success, negative error code otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 397 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 398 | static int32_t ad4110_write_register(void *dev, uint32_t reg, uint32_t writeval) |
Janani Sunil | 0:6ca37a8f8ba9 | 399 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 400 | int ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 401 | |
Janani Sunil | 0:6ca37a8f8ba9 | 402 | if (reg > REGISTER_MAX_VAL) { |
Janani Sunil | 0:6ca37a8f8ba9 | 403 | return -EINVAL; |
Janani Sunil | 0:6ca37a8f8ba9 | 404 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 405 | |
Janani Sunil | 0:6ca37a8f8ba9 | 406 | ret = ad4110_spi_int_reg_write(dev, REGISTER_READ_SEL, reg, writeval); |
Janani Sunil | 0:6ca37a8f8ba9 | 407 | if (ret < 0) { |
Janani Sunil | 0:6ca37a8f8ba9 | 408 | return ret; |
Janani Sunil | 0:6ca37a8f8ba9 | 409 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 410 | |
Janani Sunil | 0:6ca37a8f8ba9 | 411 | return 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 412 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 413 | |
Janani Sunil | 0:6ca37a8f8ba9 | 414 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 415 | * @brief Init for reading/writing and parameterization of a AD4110-1 IIO device |
Janani Sunil | 0:6ca37a8f8ba9 | 416 | * @param desc[in,out] - IIO device descriptor |
Janani Sunil | 0:6ca37a8f8ba9 | 417 | * @return 0 in case of success, negative error code otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 418 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 419 | int32_t ad4110_iio_init(struct iio_device **desc) |
Janani Sunil | 0:6ca37a8f8ba9 | 420 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 421 | struct iio_device *ad4110_iio_device; |
Janani Sunil | 0:6ca37a8f8ba9 | 422 | |
Janani Sunil | 0:6ca37a8f8ba9 | 423 | ad4110_iio_device = (struct iio_device *)calloc(1, |
Janani Sunil | 0:6ca37a8f8ba9 | 424 | sizeof(*ad4110_iio_device)); |
Janani Sunil | 0:6ca37a8f8ba9 | 425 | if (!ad4110_iio_device) { |
Janani Sunil | 0:6ca37a8f8ba9 | 426 | return -ENOMEM; |
Janani Sunil | 0:6ca37a8f8ba9 | 427 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 428 | |
Janani Sunil | 0:6ca37a8f8ba9 | 429 | ad4110_iio_device->num_ch = NO_OS_ARRAY_SIZE(ad4110_iio_channels); |
Janani Sunil | 0:6ca37a8f8ba9 | 430 | ad4110_iio_device->channels = ad4110_iio_channels; |
Janani Sunil | 0:6ca37a8f8ba9 | 431 | ad4110_iio_device->attributes = ad4110_global_attributes; |
Janani Sunil | 0:6ca37a8f8ba9 | 432 | ad4110_iio_device->context_attributes = ad4110_iio_context_attributes; |
Janani Sunil | 0:6ca37a8f8ba9 | 433 | ad4110_iio_device->pre_enable = iio_ad4110_pre_enable; |
Janani Sunil | 0:6ca37a8f8ba9 | 434 | ad4110_iio_device->post_disable = iio_ad4110_post_disable; |
Janani Sunil | 0:6ca37a8f8ba9 | 435 | ad4110_iio_device->submit = iio_ad4110_submit_buffer; |
Janani Sunil | 0:6ca37a8f8ba9 | 436 | ad4110_iio_device->debug_reg_read = ad4110_read_register; |
Janani Sunil | 0:6ca37a8f8ba9 | 437 | ad4110_iio_device->debug_reg_write = ad4110_write_register; |
Janani Sunil | 0:6ca37a8f8ba9 | 438 | |
Janani Sunil | 0:6ca37a8f8ba9 | 439 | *desc = ad4110_iio_device; |
Janani Sunil | 0:6ca37a8f8ba9 | 440 | |
Janani Sunil | 0:6ca37a8f8ba9 | 441 | return 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 442 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 443 | |
Janani Sunil | 0:6ca37a8f8ba9 | 444 | |
Janani Sunil | 0:6ca37a8f8ba9 | 445 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 446 | * @brief Update channel attribute structure |
Janani Sunil | 0:6ca37a8f8ba9 | 447 | * @param dev[in] - AD4110 device descriptor |
Janani Sunil | 0:6ca37a8f8ba9 | 448 | * @return None |
Janani Sunil | 0:6ca37a8f8ba9 | 449 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 450 | static void update_channel_scan_parameters(struct ad4110_dev *dev) |
Janani Sunil | 0:6ca37a8f8ba9 | 451 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 452 | uint8_t channel_id; |
Janani Sunil | 0:6ca37a8f8ba9 | 453 | |
Janani Sunil | 0:6ca37a8f8ba9 | 454 | if (dev->bipolar) { |
Janani Sunil | 0:6ca37a8f8ba9 | 455 | ad4110_scan_type.sign = 's'; |
Janani Sunil | 0:6ca37a8f8ba9 | 456 | ad4110_scan_type.realbits = CHN_STORAGE_BITS; |
Janani Sunil | 0:6ca37a8f8ba9 | 457 | |
Janani Sunil | 0:6ca37a8f8ba9 | 458 | /* Update scale value in case of voltage or current mode */ |
Janani Sunil | 0:6ca37a8f8ba9 | 459 | for (channel_id = 0; channel_id < AD4110_NUM_CHANNELS; channel_id++) { |
Janani Sunil | 0:6ca37a8f8ba9 | 460 | #if (ACTIVE_DEMO_MODE_CONFIG == VOLTAGE_MODE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 461 | attr_scale_val[channel_id] = AD4110_BIPOLAR_SCALE; |
Janani Sunil | 0:6ca37a8f8ba9 | 462 | #elif (ACTIVE_DEMO_MODE_CONFIG == CURRENT_MODE_CONFIG) ||\ |
Janani Sunil | 0:6ca37a8f8ba9 | 463 | (ACTIVE_DEMO_MODE_CONFIG == FIELD_POWER_SUPPLY_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 464 | attr_scale_val[channel_id] = AD4110_BIPOLAR_SCALE/AD4110_R_SENSE; |
Janani Sunil | 0:6ca37a8f8ba9 | 465 | |
Janani Sunil | 0:6ca37a8f8ba9 | 466 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 467 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 468 | } else { |
Janani Sunil | 0:6ca37a8f8ba9 | 469 | ad4110_scan_type.sign = 'u'; |
Janani Sunil | 0:6ca37a8f8ba9 | 470 | ad4110_scan_type.realbits = AD4110_RESOLUTION; |
Janani Sunil | 0:6ca37a8f8ba9 | 471 | |
Janani Sunil | 0:6ca37a8f8ba9 | 472 | for (channel_id = 0; channel_id < AD4110_NUM_CHANNELS; channel_id++) { |
Janani Sunil | 0:6ca37a8f8ba9 | 473 | #if (ACTIVE_DEMO_MODE_CONFIG == VOLTAGE_MODE_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 474 | attr_scale_val[channel_id] = AD4110_UNIPOLAR_SCALE; |
Janani Sunil | 0:6ca37a8f8ba9 | 475 | #elif (ACTIVE_DEMO_MODE_CONFIG == CURRENT_MODE_CONFIG) ||\ |
Janani Sunil | 0:6ca37a8f8ba9 | 476 | (ACTIVE_DEMO_MODE_CONFIG == FIELD_POWER_SUPPLY_CONFIG) |
Janani Sunil | 0:6ca37a8f8ba9 | 477 | attr_scale_val[channel_id] = AD4110_UNIPOLAR_SCALE / AD4110_R_SENSE; |
Janani Sunil | 0:6ca37a8f8ba9 | 478 | |
Janani Sunil | 0:6ca37a8f8ba9 | 479 | #endif |
Janani Sunil | 0:6ca37a8f8ba9 | 480 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 481 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 482 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 483 | |
Janani Sunil | 0:6ca37a8f8ba9 | 484 | |
Janani Sunil | 0:6ca37a8f8ba9 | 485 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 486 | * @brief Initialize the AD4110-1 IIO application |
Janani Sunil | 0:6ca37a8f8ba9 | 487 | * @return 0 in case of success, negative error code otherwise |
Janani Sunil | 0:6ca37a8f8ba9 | 488 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 489 | int32_t ad4110_iio_initialize(void) |
Janani Sunil | 0:6ca37a8f8ba9 | 490 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 491 | int32_t ret_status; |
Janani Sunil | 0:6ca37a8f8ba9 | 492 | |
Janani Sunil | 0:6ca37a8f8ba9 | 493 | /* IIO Device Descriptor */ |
Janani Sunil | 0:6ca37a8f8ba9 | 494 | struct iio_device *ad4110_iio_dev; |
Janani Sunil | 0:6ca37a8f8ba9 | 495 | |
Janani Sunil | 0:6ca37a8f8ba9 | 496 | /* IIO interface init parameters */ |
Janani Sunil | 0:6ca37a8f8ba9 | 497 | struct iio_init_param iio_init_params = { |
Janani Sunil | 0:6ca37a8f8ba9 | 498 | .phy_type = USE_UART, |
Janani Sunil | 0:6ca37a8f8ba9 | 499 | .nb_devs = 1, |
Janani Sunil | 0:6ca37a8f8ba9 | 500 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 501 | |
Janani Sunil | 0:6ca37a8f8ba9 | 502 | /* IIOD init parameters */ |
Janani Sunil | 0:6ca37a8f8ba9 | 503 | struct iio_device_init iio_device_init_params = { |
Janani Sunil | 0:6ca37a8f8ba9 | 504 | .name = (char *)dev_name, |
Janani Sunil | 0:6ca37a8f8ba9 | 505 | .raw_buf = adc_data_buffer, |
Janani Sunil | 0:6ca37a8f8ba9 | 506 | .raw_buf_len = DATA_BUFFER_SIZE |
Janani Sunil | 0:6ca37a8f8ba9 | 507 | }; |
Janani Sunil | 0:6ca37a8f8ba9 | 508 | |
Janani Sunil | 0:6ca37a8f8ba9 | 509 | /* Initialize the system peripherals */ |
Janani Sunil | 0:6ca37a8f8ba9 | 510 | ret_status = init_system(); |
Janani Sunil | 0:6ca37a8f8ba9 | 511 | if (ret_status) { |
Janani Sunil | 0:6ca37a8f8ba9 | 512 | return ret_status; |
Janani Sunil | 0:6ca37a8f8ba9 | 513 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 514 | |
Janani Sunil | 0:6ca37a8f8ba9 | 515 | /* Initialize the AD4110 Device */ |
Janani Sunil | 0:6ca37a8f8ba9 | 516 | ret_status = ad4110_setup(&ad4110_dev_inst, ad4110_init_params); |
Janani Sunil | 0:6ca37a8f8ba9 | 517 | if (ret_status) { |
Janani Sunil | 0:6ca37a8f8ba9 | 518 | return ret_status; |
Janani Sunil | 0:6ca37a8f8ba9 | 519 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 520 | |
Janani Sunil | 0:6ca37a8f8ba9 | 521 | /* Update channel scan attribute parameters */ |
Janani Sunil | 0:6ca37a8f8ba9 | 522 | update_channel_scan_parameters(ad4110_dev_inst); |
Janani Sunil | 0:6ca37a8f8ba9 | 523 | |
Janani Sunil | 0:6ca37a8f8ba9 | 524 | /* Initialize the AD4110-1 IIO application interface */ |
Janani Sunil | 0:6ca37a8f8ba9 | 525 | ret_status = ad4110_iio_init(&ad4110_iio_dev); |
Janani Sunil | 0:6ca37a8f8ba9 | 526 | if (ret_status) { |
Janani Sunil | 0:6ca37a8f8ba9 | 527 | return ret_status; |
Janani Sunil | 0:6ca37a8f8ba9 | 528 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 529 | |
Janani Sunil | 0:6ca37a8f8ba9 | 530 | /* Update iio interface and IIOD init params */ |
Janani Sunil | 0:6ca37a8f8ba9 | 531 | iio_init_params.uart_desc = uart_desc; |
Janani Sunil | 0:6ca37a8f8ba9 | 532 | iio_device_init_params.dev = ad4110_dev_inst; |
Janani Sunil | 0:6ca37a8f8ba9 | 533 | iio_device_init_params.dev_descriptor = ad4110_iio_dev; |
Janani Sunil | 0:6ca37a8f8ba9 | 534 | iio_init_params.devs = &iio_device_init_params; |
Janani Sunil | 0:6ca37a8f8ba9 | 535 | |
Janani Sunil | 0:6ca37a8f8ba9 | 536 | /* Initialize the IIO interface */ |
Janani Sunil | 0:6ca37a8f8ba9 | 537 | ret_status = iio_init(&ad4110_iio_desc, &iio_init_params); |
Janani Sunil | 0:6ca37a8f8ba9 | 538 | if (ret_status) { |
Janani Sunil | 0:6ca37a8f8ba9 | 539 | return ret_status; |
Janani Sunil | 0:6ca37a8f8ba9 | 540 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 541 | |
Janani Sunil | 0:6ca37a8f8ba9 | 542 | return 0; |
Janani Sunil | 0:6ca37a8f8ba9 | 543 | } |
Janani Sunil | 0:6ca37a8f8ba9 | 544 | |
Janani Sunil | 0:6ca37a8f8ba9 | 545 | |
Janani Sunil | 0:6ca37a8f8ba9 | 546 | /** |
Janani Sunil | 0:6ca37a8f8ba9 | 547 | * @brief Run the AD4110-1 IIO event handler |
Janani Sunil | 0:6ca37a8f8ba9 | 548 | * @return None |
Janani Sunil | 0:6ca37a8f8ba9 | 549 | */ |
Janani Sunil | 0:6ca37a8f8ba9 | 550 | void ad4110_iio_event_handler(void) |
Janani Sunil | 0:6ca37a8f8ba9 | 551 | { |
Janani Sunil | 0:6ca37a8f8ba9 | 552 | (void)iio_step(ad4110_iio_desc); |
Janani Sunil | 0:6ca37a8f8ba9 | 553 | } |