no-OS
Loading...
Searching...
No Matches
ad405x.c File Reference

Implementation of AD405X Driver. More...

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "ad405x.h"
#include "no_os_delay.h"
#include "no_os_alloc.h"
Include dependency graph for ad405x.c:

Functions

int ad405x_init (struct ad405x_dev **device, struct ad405x_init_param init_param)
 Initialize the communication device.
 
int ad405x_remove (struct ad405x_dev *dev)
 Remove device and free the resources allocated by ad405x_init().
 
int ad405x_soft_reset (struct ad405x_dev *dev)
 Soft reset of the device. For I3C, the I3C peripheral is not reset, keeping its dynamic address and any other I3C property.
 
int ad405x_clear_reset_flags (struct ad405x_dev *dev)
 Clear reset flags.
 
int ad405x_read (struct ad405x_dev *dev, uint8_t addr, uint8_t *data, uint8_t size)
 Read device registers.
 
int ad405x_write (struct ad405x_dev *dev, uint8_t addr, uint8_t *data, uint8_t size)
 Write registers.
 
int ad405x_get_adc (struct ad405x_dev *dev, int32_t *value)
 Get formatted ADC reading. If gp0 is set as data ready, will be used to delay the acquisition until the signal is deserted (SPI only).
 
int ad405x_get_raw (struct ad405x_dev *dev, uint32_t *raw)
 Get raw ADC reading.
 
int ad405x_get_sts_reg (struct ad405x_dev *dev, uint8_t *status_flags, uint8_t *intf_status_flags)
 Read the interface status and status registers.
 
int ad405x_clear_sts_reg (struct ad405x_dev *dev, uint8_t *status_flags, uint8_t *intf_status_flags)
 Write to clear the interface status and status registers.
 
int ad405x_test_scratch_pad (struct ad405x_dev *dev)
 Test the scratch pad register. A pseudo-random value to test against is defined at compile time.
 
int ad405x_set_adc_mode (struct ad405x_dev *dev)
 Enter ADC Mode.
 
int ad405x_set_burst_averaging_mode (struct ad405x_dev *dev)
 Enter Burst Averaging Mode.
 
int ad405x_set_persistent_auto_mode (struct ad405x_dev *dev)
 Enter Persistent Auto Mode.
 
int ad405x_set_nonpersistent_auto_mode (struct ad405x_dev *dev)
 Enter Non-Persistent Auto Mode.
 
int ad405x_set_operation_mode (struct ad405x_dev *dev, enum ad405x_operation_mode mode)
 Set operation mode.
 
int ad405x_set_sample_rate (struct ad405x_dev *dev, enum ad405x_sample_rate rate)
 Select sample rate for Burst and Autonomous Modes.
 
enum ad405x_sample_rate ad405x_get_sample_rate (struct ad405x_dev *dev)
 Get sample rate for Burst and Autonomous Modes.
 
int ad405x_set_avg_filter_length (struct ad405x_dev *dev, enum ad405x_avg_filter_l length)
 Set averaging filter window length.
 
enum ad405x_avg_filter_l ad405x_get_avg_filter_length (struct ad405x_dev *dev)
 Get averaging filter window length.
 
int ad405x_set_dev_en_polarity (struct ad405x_dev *dev, enum ad405x_dev_en_polarity polarity)
 Set DEV_EN signal polarity.
 
enum ad405x_dev_en_polarity ad405x_get_dev_en_polarity (struct ad405x_dev *dev)
 Get DEV_EN signal polarity.
 
int ad405x_set_dev_en_timer_pwr_on (struct ad405x_dev *dev, enum ad405x_dev_en_timer_pwr_on time)
 Set DEV_EN TIMER_PWR_ON.
 
enum ad405x_dev_en_timer_pwr_on ad405x_get_dev_en_timer_pwr_on (struct ad405x_dev *dev)
 Get DEV_EN TIMER_PWR_ON.
 
int ad405x_enable_invert_on_chop (struct ad405x_dev *dev)
 Enable INVERT_ON_CHOP.
 
int ad405x_disable_invert_on_chop (struct ad405x_dev *dev)
 Disable INVERT_ON_CHOP.
 
enum ad405x_invert_on_chop ad405x_get_invert_on_chop_state (struct ad405x_dev *dev)
 Get INVERT_ON_CHOP setting.
 
int ad405x_set_gp_mode (struct ad405x_dev *dev, enum ad405x_gp_select gp, enum ad405x_gp_mode mode)
 Set GP mode.
 
enum ad405x_gp_mode ad405x_get_gp_mode (struct ad405x_dev *dev, enum ad405x_gp_select gp)
 Get GP mode setting.
 
int ad405x_set_data_format (struct ad405x_dev *dev, enum ad405x_out_data_format data_format)
 Set output data format.
 
int ad405x_set_data_format_straight_binary (struct ad405x_dev *dev)
 Set ADC output data format as straight binary.
 
int ad405x_set_data_format_twos_complement (struct ad405x_dev *dev)
 Set ADC output data format as two's complement.
 
enum ad405x_out_data_format ad405x_get_data_format (struct ad405x_dev *dev)
 Get output data format setting.
 
int ad405x_set_threshold (struct ad405x_dev *dev, struct ad405x_thresh *thresh)
 Set threshold configuration. A simple memory swap puts the ad405x_thresh strict into format to write the 6 registers of interest.
 
int ad405x_get_threshold (struct ad405x_dev *dev, struct ad405x_thresh *thresh)
 Get threshold configuration.
 
int ad405x_exit_command (struct ad405x_dev *dev)
 Send EXIT command (SPI only).
 
int ad405x_trigger_adc_conv (struct ad405x_dev *dev)
 Trigger an ADC conversion (SPI only). Toggle the CNV pin to start the conversion.
 
int ad405x_set_averaging_mode (struct ad405x_dev *dev)
 Enter Averaging Mode (SPI only).
 
int ad405x_set_config_mode (struct ad405x_dev *dev, enum ad405x_operation_mode *mode)
 Enter Configuration Mode (SPI only).
 
int ad405x_test_common_ccc (struct ad405x_dev *dev)
 Test the GETBCR, GETDCR, GETPID, AND GETSTATUS CCCs (I3C only). Compare received BCR and DCR against expected values.
 
int ad405x_pool_ibi_config (struct ad405x_dev *dev)
 Pool IBI status and config registers (I3C only). Values are updated on the device descriptor.
 
int ad405x_set_ibi_enable (struct ad405x_dev *dev, bool enable)
 Enable/disable IBI (I3C only).
 
int ad405x_set_ibi_mask (struct ad405x_dev *dev, uint16_t en_mask)
 Set IBI mask (I3C only).
 

Detailed Description

Implementation of AD405X Driver.

Author
George Mois (georg.nosp@m.e.mo.nosp@m.is@an.nosp@m.alog.nosp@m..com)
Ribhu Das Purkayastha (ribhu.nosp@m..das.nosp@m.purka.nosp@m.yast.nosp@m.ha@an.nosp@m.alog.nosp@m..com)
Jorge Marques (jorge.nosp@m..mar.nosp@m.ques@.nosp@m.anal.nosp@m.og.co.nosp@m.m)

Copyright 2024~2025(c) Analog Devices, Inc.

SPDX-License-Identifier: BSD-3-Clause

Function Documentation

◆ ad405x_clear_reset_flags()

int ad405x_clear_reset_flags ( struct ad405x_dev * dev)

Clear reset flags.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_clear_sts_reg()

int ad405x_clear_sts_reg ( struct ad405x_dev * dev,
uint8_t * status_flags,
uint8_t * intf_status_flags )

Write to clear the interface status and status registers.

Parameters
dev- The device descriptor.
status_flags- Status register value (optional).
intf_status_flags- Interface register value (optional).
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_disable_invert_on_chop()

int ad405x_disable_invert_on_chop ( struct ad405x_dev * dev)

Disable INVERT_ON_CHOP.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_enable_invert_on_chop()

int ad405x_enable_invert_on_chop ( struct ad405x_dev * dev)

Enable INVERT_ON_CHOP.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_exit_command()

int ad405x_exit_command ( struct ad405x_dev * dev)

Send EXIT command (SPI only).

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_get_adc()

int ad405x_get_adc ( struct ad405x_dev * dev,
int32_t * value )

Get formatted ADC reading. If gp0 is set as data ready, will be used to delay the acquisition until the signal is deserted (SPI only).

Parameters
dev- The device descriptor.
value- ADC value.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_get_avg_filter_length()

enum ad405x_avg_filter_l ad405x_get_avg_filter_length ( struct ad405x_dev * dev)

Get averaging filter window length.

Parameters
dev- The device descriptor.
Returns
Average filter value or -EINVAL if dev is invalid.

◆ ad405x_get_data_format()

enum ad405x_out_data_format ad405x_get_data_format ( struct ad405x_dev * dev)

Get output data format setting.

Parameters
dev- The device descriptor.
Returns
DATA_FORMAT setting or -EINVAL if dev is invalid.

◆ ad405x_get_dev_en_polarity()

enum ad405x_dev_en_polarity ad405x_get_dev_en_polarity ( struct ad405x_dev * dev)

Get DEV_EN signal polarity.

Parameters
dev- The device descriptor.
Returns
DEV_EN power-on timer setting or -EINVAL if dev is invalid.

◆ ad405x_get_dev_en_timer_pwr_on()

enum ad405x_dev_en_timer_pwr_on ad405x_get_dev_en_timer_pwr_on ( struct ad405x_dev * dev)

Get DEV_EN TIMER_PWR_ON.

Parameters
dev- The device descriptor.
Returns
DEV_EN TIMER_PWR_ON setting or -EINVAL if dev is invalid.

◆ ad405x_get_gp_mode()

enum ad405x_gp_mode ad405x_get_gp_mode ( struct ad405x_dev * dev,
enum ad405x_gp_select gp )

Get GP mode setting.

Parameters
dev- The device descriptor.
gp- GP1/GP0 select.
Returns
GP mode or -EINVAL if dev or gp is invalid.

◆ ad405x_get_invert_on_chop_state()

enum ad405x_invert_on_chop ad405x_get_invert_on_chop_state ( struct ad405x_dev * dev)

Get INVERT_ON_CHOP setting.

Parameters
dev- The device descriptor.
Returns
INVERT_ON_CHOP setting or -EINVAL if dev is invalid.

◆ ad405x_get_raw()

int ad405x_get_raw ( struct ad405x_dev * dev,
uint32_t * raw )

Get raw ADC reading.

Parameters
dev- The device descriptor.
raw- Raw ADC data.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_get_sample_rate()

enum ad405x_sample_rate ad405x_get_sample_rate ( struct ad405x_dev * dev)

Get sample rate for Burst and Autonomous Modes.

Parameters
dev- The device descriptor.
Returns
Sample rate value or -EINVAL if dev is invalid.

◆ ad405x_get_sts_reg()

int ad405x_get_sts_reg ( struct ad405x_dev * dev,
uint8_t * status_flags,
uint8_t * intf_status_flags )

Read the interface status and status registers.

Parameters
dev- The device descriptor.
status_flags- Status register value.
intf_status_flags- Interface register value (optional).
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_get_threshold()

int ad405x_get_threshold ( struct ad405x_dev * dev,
struct ad405x_thresh * thresh )

Get threshold configuration.

Parameters
dev- The device descriptor.
thresh- The threshold configuration structure.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_init()

int ad405x_init ( struct ad405x_dev ** device,
struct ad405x_init_param init_param )

Initialize the communication device.

Parameters
device- The device descriptor.
init_param- The device initial descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_pool_ibi_config()

int ad405x_pool_ibi_config ( struct ad405x_dev * dev)

Pool IBI status and config registers (I3C only). Values are updated on the device descriptor.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_read()

int ad405x_read ( struct ad405x_dev * dev,
uint8_t addr,
uint8_t * data,
uint8_t size )

Read device registers.

Parameters
dev- The device descriptor.
addr- Address of the base register.
data- The read data buffer
size- The number of bytes to be read.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_remove()

int ad405x_remove ( struct ad405x_dev * dev)

Remove device and free the resources allocated by ad405x_init().

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_adc_mode()

int ad405x_set_adc_mode ( struct ad405x_dev * dev)

Enter ADC Mode.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_averaging_mode()

int ad405x_set_averaging_mode ( struct ad405x_dev * dev)

Enter Averaging Mode (SPI only).

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_avg_filter_length()

int ad405x_set_avg_filter_length ( struct ad405x_dev * dev,
enum ad405x_avg_filter_l length )

Set averaging filter window length.

Parameters
dev- The device descriptor.
length- Averaging filter length value.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_burst_averaging_mode()

int ad405x_set_burst_averaging_mode ( struct ad405x_dev * dev)

Enter Burst Averaging Mode.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_config_mode()

int ad405x_set_config_mode ( struct ad405x_dev * dev,
enum ad405x_operation_mode * mode )

Enter Configuration Mode (SPI only).

Parameters
dev- The device descriptor.
mode- Store the current op mode, to resume later (optional).
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_data_format()

int ad405x_set_data_format ( struct ad405x_dev * dev,
enum ad405x_out_data_format data_format )

Set output data format.

Parameters
dev- The device descriptor.
data_format- Format select.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_data_format_straight_binary()

int ad405x_set_data_format_straight_binary ( struct ad405x_dev * dev)

Set ADC output data format as straight binary.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_data_format_twos_complement()

int ad405x_set_data_format_twos_complement ( struct ad405x_dev * dev)

Set ADC output data format as two's complement.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_dev_en_polarity()

int ad405x_set_dev_en_polarity ( struct ad405x_dev * dev,
enum ad405x_dev_en_polarity polarity )

Set DEV_EN signal polarity.

Parameters
dev- The device descriptor.
polarity- DEV_EN signal polarity.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_dev_en_timer_pwr_on()

int ad405x_set_dev_en_timer_pwr_on ( struct ad405x_dev * dev,
enum ad405x_dev_en_timer_pwr_on time )

Set DEV_EN TIMER_PWR_ON.

Parameters
dev- The device descriptor.
time- DEV_EN power-on timer setting.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_gp_mode()

int ad405x_set_gp_mode ( struct ad405x_dev * dev,
enum ad405x_gp_select gp,
enum ad405x_gp_mode mode )

Set GP mode.

Parameters
dev- The device descriptor.
gp- GP1/GP0 select.
mode- mode select.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_ibi_enable()

int ad405x_set_ibi_enable ( struct ad405x_dev * dev,
bool enable )

Enable/disable IBI (I3C only).

Parameters
dev- The device descriptor.
enable- Enable IBI.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_ibi_mask()

int ad405x_set_ibi_mask ( struct ad405x_dev * dev,
uint16_t en_mask )

Set IBI mask (I3C only).

Parameters
dev- The device descriptor.
en_mask- Enable interface and ADC flags.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_nonpersistent_auto_mode()

int ad405x_set_nonpersistent_auto_mode ( struct ad405x_dev * dev)

Enter Non-Persistent Auto Mode.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_operation_mode()

int ad405x_set_operation_mode ( struct ad405x_dev * dev,
enum ad405x_operation_mode mode )

Set operation mode.

Parameters
dev- The device descriptor.
mode- Operation mode.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_persistent_auto_mode()

int ad405x_set_persistent_auto_mode ( struct ad405x_dev * dev)

Enter Persistent Auto Mode.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_sample_rate()

int ad405x_set_sample_rate ( struct ad405x_dev * dev,
enum ad405x_sample_rate rate )

Select sample rate for Burst and Autonomous Modes.

Parameters
dev- The device descriptor.
rate- The sample rate.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_set_threshold()

int ad405x_set_threshold ( struct ad405x_dev * dev,
struct ad405x_thresh * thresh )

Set threshold configuration. A simple memory swap puts the ad405x_thresh strict into format to write the 6 registers of interest.

Parameters
dev- The device descriptor.
thresh- The threshold configuration structure.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_soft_reset()

int ad405x_soft_reset ( struct ad405x_dev * dev)

Soft reset of the device. For I3C, the I3C peripheral is not reset, keeping its dynamic address and any other I3C property.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_test_common_ccc()

int ad405x_test_common_ccc ( struct ad405x_dev * dev)

Test the GETBCR, GETDCR, GETPID, AND GETSTATUS CCCs (I3C only). Compare received BCR and DCR against expected values.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_test_scratch_pad()

int ad405x_test_scratch_pad ( struct ad405x_dev * dev)

Test the scratch pad register. A pseudo-random value to test against is defined at compile time.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_trigger_adc_conv()

int ad405x_trigger_adc_conv ( struct ad405x_dev * dev)

Trigger an ADC conversion (SPI only). Toggle the CNV pin to start the conversion.

Parameters
dev- The device descriptor.
Returns
0 in case of success, negative error code otherwise.

◆ ad405x_write()

int ad405x_write ( struct ad405x_dev * dev,
uint8_t addr,
uint8_t * data,
uint8_t size )

Write registers.

Parameters
dev- The device descriptor.
addr- Address of the base register.
data- The data which is going to be written.
size- The number of bytes to be written.
Returns
0 in case of success, negative error code otherwise.