Skip to content

Commit

Permalink
projects:ad4130_iio: Added analysis/FFT support
Browse files Browse the repository at this point in the history
Added FFT analysis support into ad4130 IIO pocket lab firmware

Signed-off-by: MPhalke <[email protected]>
  • Loading branch information
mphalke committed Oct 25, 2023
1 parent 9878f9f commit 4ab04b0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
62 changes: 62 additions & 0 deletions projects/ad4130_iio/app/ad4130_iio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#if (ACTIVE_IIO_CLIENT == IIO_CLIENT_LOCAL)
#include "pl_gui_views.h"
#include "pl_gui_events.h"
#include "adi_fft.h"
#endif

/******** Forward declaration of functions ********/
Expand All @@ -55,6 +56,10 @@ static int iio_ad4130_local_backend_event_read(void *conn, uint8_t *buf,
static int iio_ad4130_local_backend_event_write(void *conn, uint8_t *buf,
uint32_t len);

static float ad4130_data_to_voltage_without_vref(int32_t data, uint8_t chn);
static float ad4130_data_to_voltage_wrt_vref(int32_t data, uint8_t chn);
static int32_t ad4130_code_to_straight_binary(uint32_t code, uint8_t chn);

/******************************************************************************/
/************************ Macros/Constants ************************************/
/******************************************************************************/
Expand Down Expand Up @@ -140,7 +145,12 @@ static int iio_ad4130_local_backend_event_write(void *conn, uint8_t *buf,
#define adc_data_buffer SDRAM_START_ADDRESS
#define DATA_BUFFER_SIZE SDRAM_SIZE_BYTES
#else
#if (ACTIVE_IIO_CLIENT == IIO_CLIENT_LOCAL)
/* Note: Setting lower size due to memory constraints on MCU */
#define DATA_BUFFER_SIZE (16384) // 16kbytes
#else
#define DATA_BUFFER_SIZE (32768) // 32kbytes
#endif
static int8_t adc_data_buffer[DATA_BUFFER_SIZE] = { 0 };
#endif

Expand Down Expand Up @@ -368,13 +378,32 @@ struct pl_gui_views pocket_lab_gui_views[] = {
PL_GUI_ADD_REG_DEBUG_DEF_VIEW,
PL_GUI_ADD_DMM_DEF_VIEW,
PL_GUI_ADD_CAPTURE_DEF_VIEW,
PL_GUI_ADD_ANALYSIS_DEF_VIEW,
PL_GUI_ADD_ABOUT_DEF_VIEW,
{ NULL }
};

/* FFT init parameters */
struct adi_fft_init_params fft_init_params = {
.vref = AD4170_2_5V_INT_REF_VOLTAGE,
.sample_rate = AD4130_MIN_SAMPLING_FREQ,
.samples_count = ADI_FFT_MAX_SAMPLES,
.input_data_zero_scale = ADC_MAX_COUNT_BIPOLAR,
.input_data_full_scale = ADC_MAX_COUNT_UNIPOLAR,
.convert_data_to_volt_without_vref = &ad4130_data_to_voltage_without_vref,
.convert_data_to_volt_wrt_vref = &ad4130_data_to_voltage_wrt_vref,
.convert_code_to_straight_binary = &ad4130_code_to_straight_binary
};

/* Pocket lab GUI device init parameters */
struct pl_gui_device_param pl_gui_device_params = {
.fft_params = &fft_init_params
};

/* Pocket lab GUI init parameters */
static struct pl_gui_init_param pocket_lab_gui_init_params = {
.views = pocket_lab_gui_views,
.device_params = &pl_gui_device_params
};

struct pl_gui_desc *pocket_lab_gui_desc;
Expand Down Expand Up @@ -1421,6 +1450,39 @@ static void update_vltg_conv_scale_factor(uint8_t chn)
#endif
}

/**
* @brief Convert ADC data to voltage without Vref
* @param data[in] - ADC data in straight binary format (signed)
* @param chn[in] - ADC channel
* @return voltage
*/
static float ad4130_data_to_voltage_without_vref(int32_t data, uint8_t chn)
{
return convert_adc_data_to_voltage_without_vref(ad4130_dev_inst, data, chn);
}

/**
* @brief Convert ADC data to voltage with respect to Vref
* @param data[in] - ADC data in straight binary format (signed)
* @param chn[in] - ADC channel
* @return voltage
*/
static float ad4130_data_to_voltage_wrt_vref(int32_t data, uint8_t chn)
{
return convert_adc_data_to_voltage_wrt_vref(ad4130_dev_inst, data, chn);
}

/**
* @brief Convert ADC code to straight binary data
* @param code[in] - ADC code (unsigned)
* @param chn[in] - ADC channel
* @return ADC straight binary data (signed)
*/
static int32_t ad4130_code_to_straight_binary(uint32_t code, uint8_t chn)
{
return perform_sign_conversion(ad4130_dev_inst, code, chn);
}

/**
* @brief Read the IIO local backend event data
* @param conn[in] - connection descriptor
Expand Down
47 changes: 47 additions & 0 deletions projects/ad4130_iio/app/ad4130_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,53 @@ float convert_adc_sample_into_voltage(void *dev, uint32_t adc_raw,
}
}

/**
* @brief Convert ADC data to voltage without Vref
* @param dev[in] - Device instance
* @param data[in] - ADC data in straight binary format (signed)
* @param chn[in] - ADC channel
* @return voltage
*/
float convert_adc_data_to_voltage_without_vref(void *dev, int32_t data,
uint8_t chn)
{
enum ad413x_gain pga;
uint8_t preset = ((struct ad413x_dev *)dev)->ch[chn].preset;
bool bipolar = ((struct ad413x_dev *)dev)->bipolar;

pga = ((struct ad413x_dev *)dev)->preset[preset].gain;

if (bipolar) {
return ((float)data / (ADC_MAX_COUNT_BIPOLAR * (1 << pga)));
} else {
return ((float)data / (ADC_MAX_COUNT_UNIPOLAR * (1 << pga)));
}
}

/**
* @brief Convert ADC data to voltage w.r.t Vref
* @param dev[in] - Device instance
* @param data[in] - ADC data in straight binary format (signed)
* @param chn[in] - ADC channel
* @return voltage
*/
float convert_adc_data_to_voltage_wrt_vref(void *dev, int32_t data, uint8_t chn)
{
enum ad413x_gain pga;
float vref;
uint8_t preset = ((struct ad413x_dev *)dev)->ch[chn].preset;
bool bipolar = ((struct ad413x_dev *)dev)->bipolar;

pga = ((struct ad413x_dev *)dev)->preset[preset].gain;
vref = ad4130_get_reference_voltage(((struct ad413x_dev *)dev), chn);

if (bipolar) {
return ((float)data * (vref / (ADC_MAX_COUNT_BIPOLAR * (1 << pga))));
} else {
return ((float)data * (vref / (ADC_MAX_COUNT_UNIPOLAR * (1 << pga))));
}
}

/*!
* @brief Convert the ADC raw value into equivalent RTD resistance
* @param dev[in] - Device instance
Expand Down
4 changes: 4 additions & 0 deletions projects/ad4130_iio/app/ad4130_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ int32_t perform_sign_conversion(struct ad413x_dev *dev, uint32_t adc_raw_data,
uint8_t chn);
float convert_adc_sample_into_voltage(void *dev, uint32_t adc_raw,
uint8_t chn);
float convert_adc_data_to_voltage_wrt_vref(void *dev, int32_t code,
uint8_t chn);
float convert_adc_data_to_voltage_without_vref(void *dev, int32_t code,
uint8_t chn);
float convert_adc_raw_into_rtd_resistance(void *dev, uint32_t adc_raw,
float rtd_ref, uint8_t chn);
int32_t ad4130_read_fifo(struct ad413x_dev *dev, uint32_t *data,
Expand Down

0 comments on commit 4ab04b0

Please sign in to comment.