precision-converters-firmware
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Classes | Macros | Enumerations | Functions | Variables
ad4170_iio.c File Reference

Implementation of AD4170 IIO application interfaces. More...

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "ad4170_iio.h"
#include "app_config.h"
#include "ad4170_regs.h"
#include "ad4170_support.h"
#include "ad4170_temperature_sensor.h"
#include "no_os_error.h"
#include "no_os_irq.h"
#include "no_os_delay.h"
#include "no_os_util.h"
#include "common.h"
#include "iio_trigger.h"
#include "no_os_gpio.h"
#include "stm32_tdm_support.h"
#include "pl_gui_views.h"
#include "pl_gui_events.h"
#include "adi_fft.h"
Include dependency graph for ad4170_iio.c:

Classes

struct  adc_calibration_configs
 
struct  ad4170_cached_reg
 Cached register. More...
 

Macros

#define LOADCELL_SAMPLES_COUNT   10
 
#define CJC_CHANNEL   2
 
#define NUM_OF_IIO_DEVICES   1
 
#define IIO_TRIGGER_NAME   "ad4170_iio_trigger"
 
#define CHN_REAL_BITS   (ADC_RESOLUTION)
 
#define CHN_STORAGE_BITS   (BYTES_PER_SAMPLE * 8)
 
#define LED_TOGGLE_TIME   (500)
 
#define LED_TOGGLE_TICK_CNTR   (LED_TOGGLE_TIME / (TICKER_INTERRUPT_PERIOD_uSEC / 1000))
 
#define BYTE_SIZE   (uint32_t)8
 
#define BYTE_MASK   (uint32_t)0xff
 
#define BUF_READ_TIMEOUT   0xffffffff
 
#define DATA_BUFFER_SIZE   (16384)
 
#define APP_LOCAL_BACKEND_BUF_SIZE   0x1000
 
#define N_REGISTERS_CACHED   ADC_REGISTER_COUNT
 
#define MAX_LOCAL_BUF_SIZE   8000
 
#define MAX_DMA_NDTR   (no_os_min(65535, MAX_LOCAL_BUF_SIZE))
 
#define AD4170_IIO_VOLT_CH(nm, chn)
 
#define AD4170_IIO_TEMP_CH(nm, chn)
 

Enumerations

enum  ad4170_attr_id {
  IIO_RAW_ATTR_ID ,
  IIO_SCALE_ATTR_ID ,
  IIO_OFFSET_ATTR_ID ,
  INTERNAL_CALIB_ID ,
  SYSTEM_CALIB_ID ,
  LOADCELL_OFFSET_CALIB_ID ,
  LOADCELL_GAIN_CALIB_ID ,
  FILTER_ATTR_ID ,
  REF_SELECT_ATTR_ID
}
 
enum  calibration_state {
  FULL_SCALE_CALIB_STATE ,
  ZERO_SCALE_CALIB_STATE ,
  CALIB_COMPLETE_STATE
}
 
enum  calib_status {
  CALIB_NOT_DONE ,
  CALIB_IN_PROGRESS ,
  CALIB_DONE ,
  CALIB_ERROR ,
  CALIB_SKIPPED
}
 

Functions

int32_t perform_adc_calibration (uint8_t chn, enum ad4170_mode calib_mode)
 Perform the ADC internal/system calibration.
 
int32_t debug_reg_read (void *dev, uint32_t reg, uint32_t *readval)
 Read the debug register value.
 
int32_t debug_reg_write (void *dev, uint32_t reg, uint32_t writeval)
 Write into the debug register.
 
int ad4170_cache_register_values (void)
 Cache register values modified by attributes.
 
int ad4170_restore_cache (void)
 Restore cached register values.
 
int32_t iio_ad4170_trigger_handler (struct iio_device_data *iio_dev_data)
 Push data into IIO buffer when trigger handler IRQ is invoked.
 
void ticker_callback (void *ctx)
 This is an ISR (Interrupt Service Routine) for Ticker object.
 
void ad4170_configure_filter_params (void)
 Configure filter parameters according to active device chosen.
 
int32_t ad4170_iio_initialize (void)
 Initialize the IIO interface for AD4170 IIO device.
 
void ad4170_iio_event_handler (void)
 Run the AD4170 IIO event handler.
 

Variables

struct ad4170_dev * p_ad4170_dev_inst = NULL
 
struct iio_device * p_iio_ad4170_dev
 
struct iio_device_init iio_device_init_params [NUM_OF_IIO_DEVICES]
 
uint8_t num_of_active_channels
 
struct iio_device_data * ad4170_iio_dev_data
 
volatile bool tdm_read_started = false
 
bool data_capture_operation = false
 
struct pl_gui_views pocket_lab_gui_views []
 
struct adi_fft_init_params fft_init_params
 
struct pl_gui_device_param pl_gui_device_params
 
struct pl_gui_descpocket_lab_gui_desc
 
struct stm32_spi_init_param * spi_init_param
 
volatile struct iio_device_data * iio_dev_data_g
 
uint32_t nb_of_samples_g
 
int32_t data_read
 
volatile bool dma_config_updated = false
 
volatile bool ad4170_dma_buff_full = false
 
volatile uint32_t * buff_start_addr
 
uint8_t local_buf [MAX_LOCAL_BUF_SIZE]
 
struct ad4170_cached_reg reg_values [N_REGISTERS_CACHED]
 
uint8_t read_reg_id = 0
 
struct iio_attribute channel_input_attributes []
 

Detailed Description

Implementation of AD4170 IIO application interfaces.

This module acts as an interface for AD4170 IIO application

Copyright (c) 2021-2025 Analog Devices, Inc. All rights reserved.

This software is proprietary to Analog Devices, Inc. and its licensors. By using this software you agree to the terms of the associated Analog Devices Software License Agreement.

Macro Definition Documentation

◆ AD4170_IIO_TEMP_CH

#define AD4170_IIO_TEMP_CH (   nm,
  chn 
)
Value:
{\
.name = nm,\
.ch_type = IIO_TEMP,\
.ch_out = false,\
.indexed = true,\
.channel = chn,\
.scan_index = chn,\
.scan_type = &chn_scan[chn],\
.attributes = channel_input_attributes\
}
struct scan_type chn_scan[RESOLVER_CHANNELS]
Definition ad2s1210_iio.c:110
struct iio_attribute channel_input_attributes[]
Definition ad4170_iio.c:2497

◆ AD4170_IIO_VOLT_CH

#define AD4170_IIO_VOLT_CH (   nm,
  chn 
)
Value:
{\
.name = nm,\
.ch_type = IIO_VOLTAGE,\
.ch_out = false,\
.indexed = true,\
.channel = chn,\
.scan_index = chn,\
.scan_type = &chn_scan[chn],\
.attributes = channel_input_attributes\
}

◆ APP_LOCAL_BACKEND_BUF_SIZE

#define APP_LOCAL_BACKEND_BUF_SIZE   0x1000

◆ BUF_READ_TIMEOUT

#define BUF_READ_TIMEOUT   0xffffffff

◆ BYTE_MASK

#define BYTE_MASK   (uint32_t)0xff

◆ BYTE_SIZE

#define BYTE_SIZE   (uint32_t)8

◆ CHN_REAL_BITS

#define CHN_REAL_BITS   (ADC_RESOLUTION)

◆ CHN_STORAGE_BITS

#define CHN_STORAGE_BITS   (BYTES_PER_SAMPLE * 8)

◆ CJC_CHANNEL

#define CJC_CHANNEL   2

◆ DATA_BUFFER_SIZE

#define DATA_BUFFER_SIZE   (16384)

◆ IIO_TRIGGER_NAME

#define IIO_TRIGGER_NAME   "ad4170_iio_trigger"

◆ LED_TOGGLE_TICK_CNTR

#define LED_TOGGLE_TICK_CNTR   (LED_TOGGLE_TIME / (TICKER_INTERRUPT_PERIOD_uSEC / 1000))

◆ LED_TOGGLE_TIME

#define LED_TOGGLE_TIME   (500)

◆ LOADCELL_SAMPLES_COUNT

#define LOADCELL_SAMPLES_COUNT   10

◆ MAX_DMA_NDTR

#define MAX_DMA_NDTR   (no_os_min(65535, MAX_LOCAL_BUF_SIZE))

◆ MAX_LOCAL_BUF_SIZE

#define MAX_LOCAL_BUF_SIZE   8000

◆ N_REGISTERS_CACHED

#define N_REGISTERS_CACHED   ADC_REGISTER_COUNT

◆ NUM_OF_IIO_DEVICES

#define NUM_OF_IIO_DEVICES   1

Enumeration Type Documentation

◆ ad4170_attr_id

Enumerator
IIO_RAW_ATTR_ID 
IIO_SCALE_ATTR_ID 
IIO_OFFSET_ATTR_ID 
INTERNAL_CALIB_ID 
SYSTEM_CALIB_ID 
LOADCELL_OFFSET_CALIB_ID 
LOADCELL_GAIN_CALIB_ID 
FILTER_ATTR_ID 
REF_SELECT_ATTR_ID 

◆ calib_status

Enumerator
CALIB_NOT_DONE 
CALIB_IN_PROGRESS 
CALIB_DONE 
CALIB_ERROR 
CALIB_SKIPPED 

◆ calibration_state

Enumerator
FULL_SCALE_CALIB_STATE 
ZERO_SCALE_CALIB_STATE 
CALIB_COMPLETE_STATE 

Function Documentation

◆ ad4170_cache_register_values()

int ad4170_cache_register_values ( void  )

Cache register values modified by attributes.

Returns
0 in case of success, negative error code otherwise

◆ ad4170_configure_filter_params()

void ad4170_configure_filter_params ( void  )

Configure filter parameters according to active device chosen.

Parameters
None
Returns
none
Here is the caller graph for this function:

◆ ad4170_iio_event_handler()

void ad4170_iio_event_handler ( void  )

Run the AD4170 IIO event handler.

Returns
none

This function monitors the new IIO client event

Here is the caller graph for this function:

◆ ad4170_iio_initialize()

int32_t ad4170_iio_initialize ( void  )

Initialize the IIO interface for AD4170 IIO device.

Returns
none
0 in case of success, negative error code otherwise
Here is the caller graph for this function:

◆ ad4170_restore_cache()

int ad4170_restore_cache ( void  )

Restore cached register values.

Returns
0 in case of success, negative error code otherwise

◆ debug_reg_read()

int32_t debug_reg_read ( void *  dev,
uint32_t  reg,
uint32_t *  readval 
)

Read the debug register value.

Parameters
dev-Pointer to IIO device instance
reg-Register address to read from
readval-Pointer to variable to read data into
Returns
0 in case of success or negative value otherwise

◆ debug_reg_write()

int32_t debug_reg_write ( void *  dev,
uint32_t  reg,
uint32_t  writeval 
)

Write into the debug register.

Parameters
dev-Pointer to IIO device instance
reg-Register address to write into
writeval-Register value to write
Returns
0 in case of success or negative value otherwise

◆ iio_ad4170_trigger_handler()

int32_t iio_ad4170_trigger_handler ( struct iio_device_data *  iio_dev_data)

Push data into IIO buffer when trigger handler IRQ is invoked.

Parameters
iio_dev_data[in]- IIO device data instance
Returns
0 in case of success or negative value otherwise

◆ perform_adc_calibration()

int32_t perform_adc_calibration ( uint8_t  chn,
enum ad4170_mode  calib_mode 
)

Perform the ADC internal/system calibration.

Parameters
chn[in]- ADC channel
calib_mode[in]- Calibration mode
Returns
0 in case of success, negative error code otherwise

◆ ticker_callback()

void ticker_callback ( void *  ctx)

This is an ISR (Interrupt Service Routine) for Ticker object.

Parameters
ctx[in]- Callback context (unused)
Returns
none

This function is periodically called based on the time period configured during Ticker instance creation/initialization.

Variable Documentation

◆ ad4170_dma_buff_full

volatile bool ad4170_dma_buff_full = false

◆ ad4170_iio_dev_data

struct iio_device_data* ad4170_iio_dev_data

◆ buff_start_addr

volatile uint32_t* buff_start_addr

◆ channel_input_attributes

struct iio_attribute channel_input_attributes[]

◆ data_capture_operation

bool data_capture_operation = false

◆ data_read

int32_t data_read

◆ dma_config_updated

volatile bool dma_config_updated = false

◆ fft_init_params

struct adi_fft_init_params fft_init_params
Initial value:
= {
.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 = &ad4170_data_to_voltage_without_vref,
.convert_data_to_volt_wrt_vref = &ad4170_data_to_voltage_wrt_vref,
.convert_code_to_straight_binary = &ad4170_code_to_straight_binary
}
#define ADC_MAX_COUNT_UNIPOLAR
Definition app_config.h:184
#define ADC_MAX_COUNT_BIPOLAR
Definition app_config.h:187
#define AD4170_DEFLT_SAMPLING_FREQUENCY
Definition app_config.h:320
#define AD4170_REFIN_REFOUT_VOLTAGE
Definition app_config.h:285
#define ADI_FFT_MAX_SAMPLES
Definition adi_fft.h:33

◆ iio_dev_data_g

volatile struct iio_device_data* iio_dev_data_g

◆ iio_device_init_params

struct iio_device_init iio_device_init_params[NUM_OF_IIO_DEVICES]
Initial value:
= {
{
.trigger_id = "trigger0",
}
}

◆ local_buf

uint8_t local_buf[MAX_LOCAL_BUF_SIZE]

◆ nb_of_samples_g

uint32_t nb_of_samples_g

◆ num_of_active_channels

uint8_t num_of_active_channels

◆ p_ad4170_dev_inst

struct ad4170_dev* p_ad4170_dev_inst = NULL

Pointer to the struct representing the AD4170 IIO device

◆ p_iio_ad4170_dev

struct iio_device* p_iio_ad4170_dev

◆ pl_gui_device_params

struct pl_gui_device_param pl_gui_device_params
Initial value:
= {
.fft_params = &fft_init_params
}
struct adi_fft_init_params fft_init_params
Definition ad4170_iio.c:352

◆ pocket_lab_gui_desc

struct pl_gui_desc* pocket_lab_gui_desc

◆ pocket_lab_gui_views

struct pl_gui_views pocket_lab_gui_views[]
Initial value:
= {
{ NULL }
}
#define PL_GUI_ADD_DMM_DEF_VIEW
Definition pl_gui_views.h:43
#define PL_GUI_ADD_ATTR_EDIT_DEF_VIEW
Definition pl_gui_views.h:33
#define PL_GUI_ADD_REG_DEBUG_DEF_VIEW
Definition pl_gui_views.h:38
#define PL_GUI_ADD_CAPTURE_DEF_VIEW
Definition pl_gui_views.h:48
#define PL_GUI_ADD_ANALYSIS_DEF_VIEW
Definition pl_gui_views.h:53
#define PL_GUI_ADD_ABOUT_DEF_VIEW
Definition pl_gui_views.h:58

◆ read_reg_id

uint8_t read_reg_id = 0

◆ reg_values

◆ spi_init_param

struct stm32_spi_init_param* spi_init_param

◆ tdm_read_started

volatile bool tdm_read_started = false