precision-converters-firmware
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 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   (131072)
 
#define N_REGISTERS_CACHED   ADC_REGISTER_COUNT
 
#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 ,
  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 ,
  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. More...
 
int32_t debug_reg_read (void *dev, uint32_t reg, uint32_t *readval)
 Read the debug register value. More...
 
int32_t debug_reg_write (void *dev, uint32_t reg, uint32_t writeval)
 Write into the debug register. More...
 
int ad4170_cache_register_values (void)
 Cache register values modified by attributes. More...
 
int ad4170_restore_cache (void)
 Restore cached register values. More...
 
int32_t iio_ad4170_trigger_handler (struct iio_device_data *iio_dev_data)
 Push data into IIO buffer when trigger handler IRQ is invoked. More...
 
void ticker_callback (void *ctx)
 This is an ISR (Interrupt Service Routine) for Ticker object. More...
 
int32_t ad4170_iio_initialize (void)
 Initialize the IIO interface for AD4170 IIO device. More...
 
void ad4170_iio_event_handler (void)
 Run the AD4170 IIO event handler. More...
 

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 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-24 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:2470

◆ 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\
}

◆ 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   (131072)

◆ 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

◆ 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 
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 
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_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_iio_dev_data

struct iio_device_data* ad4170_iio_dev_data

◆ channel_input_attributes

struct iio_attribute channel_input_attributes[]

◆ data_capture_operation

bool data_capture_operation = false

◆ iio_device_init_params

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

◆ 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

◆ read_reg_id

uint8_t read_reg_id = 0

◆ reg_values

◆ tdm_read_started

volatile bool tdm_read_started = false