Analog Devices / Mbed OS EVAL-AD4110

Dependencies:   tempsensors sdp_k1_sdram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ad4110_support.c Source File

ad4110_support.c

00001 /***************************************************************************//*
00002  * @file    ad4110_support.c
00003  * @brief   AD4110 No-OS driver support file
00004 ******************************************************************************
00005  * Copyright (c) 2022 Analog Devices, Inc. All Rights Reserved.
00006  *
00007  * This software is proprietary to Analog Devices, Inc. and its licensors.
00008  * By using this software you agree to the terms of the associated
00009  * Analog Devices Software License Agreement.
00010 ******************************************************************************/
00011 
00012 /******************************************************************************/
00013 /***************************** Include Files **********************************/
00014 /******************************************************************************/
00015 
00016 #include "app_config.h"
00017 #include "ad4110_iio.h"
00018 
00019 /******************************************************************************/
00020 /********************* Macros and Constants Definitions ***********************/
00021 /******************************************************************************/
00022 
00023 #define AD4110_LV_CHANNEL_PGA       1
00024 
00025 /******************************************************************************/
00026 /******************** Variables and User Defined Data Types *******************/
00027 /******************************************************************************/
00028 
00029 /******************************************************************************/
00030 /************************** Functions Definitions *****************************/
00031 /******************************************************************************/
00032 
00033 /*!
00034  * @brief Perform sign conversion for handling negative voltages in bipolar mode
00035  * @param adc_raw_data[in] - ADC raw value
00036  * @return ADC data after signed conversion
00037  */
00038 int32_t perform_sign_conversion(uint32_t adc_raw_data)
00039 {
00040     int32_t adc_data;
00041     bool bipolar = ad4110_dev_inst->bipolar;
00042 
00043     if (bipolar) {
00044         adc_data =  adc_raw_data - ADC_MAX_COUNT_BIPOLAR;
00045     } else {
00046         adc_data = adc_raw_data;
00047     }
00048 
00049     return adc_data;
00050 }
00051 
00052 
00053 /*!
00054  * @brief Convert the ADC raw value into equivalent RTD resistance
00055  * @param adc_raw[in] - ADC raw sample
00056  * @param rtd_ref[in] - RTD reference resistance in ohms
00057  * @return RTD resistance value
00058  * @note RTD is biased with constant excitation current. Below formula
00059  *          is based on ratiometric measurement, where fixed value of RTD RREF
00060  *          (reference resistor) and gain is taken into account
00061  */
00062 float convert_adc_raw_into_rtd_resistance(uint32_t adc_raw, float rtd_ref)
00063 {
00064     int32_t adc_data;
00065     bool bipolar = ad4110_dev_inst->bipolar;
00066 
00067     adc_data = perform_sign_conversion(adc_raw);
00068 
00069     if (bipolar) {
00070         return (((float)adc_data * rtd_ref) / (ADC_MAX_COUNT_BIPOLAR *
00071                                AD4110_DEFAULT_PGA));
00072     } else {
00073         return (((float)adc_data * rtd_ref) / (ADC_MAX_COUNT_UNIPOLAR *
00074                                AD4110_DEFAULT_PGA));
00075     }
00076 }
00077 
00078 
00079 /*!
00080  * @brief Convert the ADC raw value into equivalent voltage
00081  * @param adc_raw[in]- ADC raw data
00082  * @param channel_id - Channel ID (number)
00083  * @return ADC voltage value
00084  */
00085 float convert_adc_sample_into_voltage(uint32_t adc_raw, uint8_t channel_id)
00086 {
00087     int32_t adc_data;
00088     bool bipolar = ad4110_dev_inst->bipolar;
00089     uint8_t gain = 0;
00090 
00091     adc_data = perform_sign_conversion(adc_raw);
00092 
00093     /* NOTE: PGA can be configured only for channel-0. The gain for the
00094      * other channels (LV Channels) is always 1 */
00095     if (channel_id == HV_CHANNEL) {
00096         gain = AD4110_DEFAULT_PGA;
00097     } else {
00098         gain = AD4110_LV_CHANNEL_PGA;
00099     }
00100 
00101     if (bipolar) {
00102         return (adc_data * (AD4110_REF_VOLTAGE / (ADC_MAX_COUNT_BIPOLAR * gain)));
00103     } else {
00104         return (adc_data * (AD4110_REF_VOLTAGE / (ADC_MAX_COUNT_UNIPOLAR * gain)));
00105     }
00106 }