IIO firmware for the AD4110

Dependencies:   tempsensors sdp_k1_sdram

Committer:
Janani Sunil
Date:
Mon Aug 01 11:09:48 2022 +0530
Revision:
1:a78dbaa4b05d
Parent:
0:6ca37a8f8ba9
Initial implementation of AD4110 IIO Firmware

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Janani Sunil 0:6ca37a8f8ba9 1 /***************************************************************************//*
Janani Sunil 0:6ca37a8f8ba9 2 * @file ad4110_support.c
Janani Sunil 0:6ca37a8f8ba9 3 * @brief AD4110 No-OS driver support file
Janani Sunil 0:6ca37a8f8ba9 4 ******************************************************************************
Janani Sunil 0:6ca37a8f8ba9 5 * Copyright (c) 2022 Analog Devices, Inc. All Rights Reserved.
Janani Sunil 0:6ca37a8f8ba9 6 *
Janani Sunil 0:6ca37a8f8ba9 7 * This software is proprietary to Analog Devices, Inc. and its licensors.
Janani Sunil 0:6ca37a8f8ba9 8 * By using this software you agree to the terms of the associated
Janani Sunil 0:6ca37a8f8ba9 9 * Analog Devices Software License Agreement.
Janani Sunil 0:6ca37a8f8ba9 10 ******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 11
Janani Sunil 0:6ca37a8f8ba9 12 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 13 /***************************** Include Files **********************************/
Janani Sunil 0:6ca37a8f8ba9 14 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 15
Janani Sunil 0:6ca37a8f8ba9 16 #include "app_config.h"
Janani Sunil 0:6ca37a8f8ba9 17 #include "ad4110_iio.h"
Janani Sunil 0:6ca37a8f8ba9 18
Janani Sunil 0:6ca37a8f8ba9 19 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 20 /********************* Macros and Constants Definitions ***********************/
Janani Sunil 0:6ca37a8f8ba9 21 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 22
Janani Sunil 0:6ca37a8f8ba9 23 #define AD4110_LV_CHANNEL_PGA 1
Janani Sunil 0:6ca37a8f8ba9 24
Janani Sunil 0:6ca37a8f8ba9 25 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 26 /******************** Variables and User Defined Data Types *******************/
Janani Sunil 0:6ca37a8f8ba9 27 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 28
Janani Sunil 0:6ca37a8f8ba9 29 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 30 /************************** Functions Definitions *****************************/
Janani Sunil 0:6ca37a8f8ba9 31 /******************************************************************************/
Janani Sunil 0:6ca37a8f8ba9 32
Janani Sunil 0:6ca37a8f8ba9 33 /*!
Janani Sunil 0:6ca37a8f8ba9 34 * @brief Perform sign conversion for handling negative voltages in bipolar mode
Janani Sunil 0:6ca37a8f8ba9 35 * @param adc_raw_data[in] - ADC raw value
Janani Sunil 0:6ca37a8f8ba9 36 * @return ADC data after signed conversion
Janani Sunil 0:6ca37a8f8ba9 37 */
Janani Sunil 0:6ca37a8f8ba9 38 int32_t perform_sign_conversion(uint32_t adc_raw_data)
Janani Sunil 0:6ca37a8f8ba9 39 {
Janani Sunil 0:6ca37a8f8ba9 40 int32_t adc_data;
Janani Sunil 0:6ca37a8f8ba9 41 bool bipolar = ad4110_dev_inst->bipolar;
Janani Sunil 0:6ca37a8f8ba9 42
Janani Sunil 0:6ca37a8f8ba9 43 if (bipolar) {
Janani Sunil 0:6ca37a8f8ba9 44 adc_data = adc_raw_data - ADC_MAX_COUNT_BIPOLAR;
Janani Sunil 0:6ca37a8f8ba9 45 } else {
Janani Sunil 0:6ca37a8f8ba9 46 adc_data = adc_raw_data;
Janani Sunil 0:6ca37a8f8ba9 47 }
Janani Sunil 0:6ca37a8f8ba9 48
Janani Sunil 0:6ca37a8f8ba9 49 return adc_data;
Janani Sunil 0:6ca37a8f8ba9 50 }
Janani Sunil 0:6ca37a8f8ba9 51
Janani Sunil 0:6ca37a8f8ba9 52
Janani Sunil 0:6ca37a8f8ba9 53 /*!
Janani Sunil 0:6ca37a8f8ba9 54 * @brief Convert the ADC raw value into equivalent RTD resistance
Janani Sunil 0:6ca37a8f8ba9 55 * @param adc_raw[in] - ADC raw sample
Janani Sunil 0:6ca37a8f8ba9 56 * @param rtd_ref[in] - RTD reference resistance in ohms
Janani Sunil 0:6ca37a8f8ba9 57 * @return RTD resistance value
Janani Sunil 0:6ca37a8f8ba9 58 * @note RTD is biased with constant excitation current. Below formula
Janani Sunil 0:6ca37a8f8ba9 59 * is based on ratiometric measurement, where fixed value of RTD RREF
Janani Sunil 0:6ca37a8f8ba9 60 * (reference resistor) and gain is taken into account
Janani Sunil 0:6ca37a8f8ba9 61 */
Janani Sunil 0:6ca37a8f8ba9 62 float convert_adc_raw_into_rtd_resistance(uint32_t adc_raw, float rtd_ref)
Janani Sunil 0:6ca37a8f8ba9 63 {
Janani Sunil 0:6ca37a8f8ba9 64 int32_t adc_data;
Janani Sunil 0:6ca37a8f8ba9 65 bool bipolar = ad4110_dev_inst->bipolar;
Janani Sunil 0:6ca37a8f8ba9 66
Janani Sunil 0:6ca37a8f8ba9 67 adc_data = perform_sign_conversion(adc_raw);
Janani Sunil 0:6ca37a8f8ba9 68
Janani Sunil 0:6ca37a8f8ba9 69 if (bipolar) {
Janani Sunil 0:6ca37a8f8ba9 70 return (((float)adc_data * rtd_ref) / (ADC_MAX_COUNT_BIPOLAR *
Janani Sunil 0:6ca37a8f8ba9 71 AD4110_DEFAULT_PGA));
Janani Sunil 0:6ca37a8f8ba9 72 } else {
Janani Sunil 0:6ca37a8f8ba9 73 return (((float)adc_data * rtd_ref) / (ADC_MAX_COUNT_UNIPOLAR *
Janani Sunil 0:6ca37a8f8ba9 74 AD4110_DEFAULT_PGA));
Janani Sunil 0:6ca37a8f8ba9 75 }
Janani Sunil 0:6ca37a8f8ba9 76 }
Janani Sunil 0:6ca37a8f8ba9 77
Janani Sunil 0:6ca37a8f8ba9 78
Janani Sunil 0:6ca37a8f8ba9 79 /*!
Janani Sunil 0:6ca37a8f8ba9 80 * @brief Convert the ADC raw value into equivalent voltage
Janani Sunil 0:6ca37a8f8ba9 81 * @param adc_raw[in]- ADC raw data
Janani Sunil 0:6ca37a8f8ba9 82 * @param channel_id - Channel ID (number)
Janani Sunil 0:6ca37a8f8ba9 83 * @return ADC voltage value
Janani Sunil 0:6ca37a8f8ba9 84 */
Janani Sunil 0:6ca37a8f8ba9 85 float convert_adc_sample_into_voltage(uint32_t adc_raw, uint8_t channel_id)
Janani Sunil 0:6ca37a8f8ba9 86 {
Janani Sunil 0:6ca37a8f8ba9 87 int32_t adc_data;
Janani Sunil 0:6ca37a8f8ba9 88 bool bipolar = ad4110_dev_inst->bipolar;
Janani Sunil 0:6ca37a8f8ba9 89 uint8_t gain = 0;
Janani Sunil 0:6ca37a8f8ba9 90
Janani Sunil 0:6ca37a8f8ba9 91 adc_data = perform_sign_conversion(adc_raw);
Janani Sunil 0:6ca37a8f8ba9 92
Janani Sunil 0:6ca37a8f8ba9 93 /* NOTE: PGA can be configured only for channel-0. The gain for the
Janani Sunil 0:6ca37a8f8ba9 94 * other channels (LV Channels) is always 1 */
Janani Sunil 0:6ca37a8f8ba9 95 if (channel_id == HV_CHANNEL) {
Janani Sunil 0:6ca37a8f8ba9 96 gain = AD4110_DEFAULT_PGA;
Janani Sunil 0:6ca37a8f8ba9 97 } else {
Janani Sunil 0:6ca37a8f8ba9 98 gain = AD4110_LV_CHANNEL_PGA;
Janani Sunil 0:6ca37a8f8ba9 99 }
Janani Sunil 0:6ca37a8f8ba9 100
Janani Sunil 0:6ca37a8f8ba9 101 if (bipolar) {
Janani Sunil 0:6ca37a8f8ba9 102 return (adc_data * (AD4110_REF_VOLTAGE / (ADC_MAX_COUNT_BIPOLAR * gain)));
Janani Sunil 0:6ca37a8f8ba9 103 } else {
Janani Sunil 0:6ca37a8f8ba9 104 return (adc_data * (AD4110_REF_VOLTAGE / (ADC_MAX_COUNT_UNIPOLAR * gain)));
Janani Sunil 0:6ca37a8f8ba9 105 }
Janani Sunil 0:6ca37a8f8ba9 106 }