Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions adi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from adi.ad4130 import ad4130
from adi.ad4170 import ad4170
from adi.ad4630 import ad4630, adaq42xx
from adi.ad4851 import ad4851
from adi.ad4858 import ad4858
from adi.ad5592r import ad5592r
from adi.ad5686 import ad5686
Expand Down
139 changes: 139 additions & 0 deletions adi/ad4851.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Copyright (C) 2025 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD

from decimal import Decimal

from adi.attribute import attribute
from adi.context_manager import context_manager
from adi.rx_tx import rx


class ad4851(rx, context_manager):

""" AD4851 ADC """

_complex_data = False
_device_name = ""
channel = [] # type: ignore

def __init__(self, uri="", device_name="ad4851"):
context_manager.__init__(self, uri, self._device_name)

compatible_parts = [
"ad4851",
"ad4852",
"ad4853",
"ad4854",
"ad4855",
"ad4856",
"ad4857",
"ad4858",
"ad4858i",
]
self._ctrl = None

if not device_name:
device_name = compatible_parts[0]
else:
if device_name not in compatible_parts:
raise Exception(f"Not a compatible device: {device_name}")

# Select the device matching device_name as working device
for device in self._ctx.devices:
if device.name == device_name:
self._ctrl = device
self._rxadc = device
break

# Raise an exception if the device isn't found
if not self._ctrl:
raise Exception(f"{device_name} device not found")

if not self._rxadc:
raise Exception("Error in selecting matching device")

self._rx_channel_names = []
self.channel = []
for ch in self._ctrl.channels:
name = ch.id
self._rx_channel_names.append(name)
self.channel.append(self._channel(self._ctrl, name))

rx.__init__(self)

class _channel(attribute):
"""AD4851 channel"""

def __init__(self, ctrl, channel_name):
self.name = channel_name
self._ctrl = ctrl

@property
def scale_available(self):
"""Get Scale available values"""
return self._get_iio_attr(self.name, "scale_available", False)

@property
def scale(self):
"""Get Scale value"""
return self._get_iio_attr(self.name, "scale", False)

@scale.setter
def scale(self, value):
"""Set scale value"""
self._set_iio_attr(self.name, "scale", False, str(Decimal(value).real))

@property
def calibbias(self):
"""Get calibration bias/offset value."""
return self._get_iio_attr(self.name, "calibbias", False)

@calibbias.setter
def calibbias(self, value):
"""Set channel calibration bias/offset."""
self._set_iio_attr(self.name, "calibbias", False, value)

@property
def calibscale(self):
"""Get calibration scale value."""
return self._get_iio_attr(self.name, "calibscale", False)

@calibscale.setter
def calibscale(self, value):
"""Set channel calibration phascalese."""
self._set_iio_attr(self.name, "calibscale", False, value)

@property
def sampling_frequency(self):
"""Get Sampling frequency value"""
return self._get_iio_dev_attr("sampling_frequency", False)

@sampling_frequency.setter
def sampling_frequency(self, value):
"""Set sampling frequency."""
self._set_iio_dev_attr("sampling_frequency", value)

@property
def oversampling_ratio_available(self):
"""Get the oversampling ratio available values"""
return self._get_iio_dev_attr("oversampling_ratio_available", False)

@property
def oversampling_ratio(self):
"""Get the oversampling ratio value"""
return self._get_iio_dev_attr("oversampling_ratio", False)

@oversampling_ratio.setter
def oversampling_ratio(self, value):
"""Set the oversampling ratio value"""
self._set_iio_dev_attr("oversampling_ratio", value)

def reg_read(self, reg):
"""Direct Register Access via debugfs"""
self._set_iio_debug_attr_str("direct_reg_access", reg, self._ctrl)
return self._get_iio_debug_attr_str("direct_reg_access", self._ctrl)

def reg_write(self, reg, value):
"""Direct Register Access via debugfs"""
self._set_iio_debug_attr_str("direct_reg_access", f"{reg} {value}", self._ctrl)
7 changes: 7 additions & 0 deletions doc/source/devices/adi.ad4851.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ad4851
=================

.. automodule:: adi.ad4851
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions doc/source/devices/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Supported Devices
adi.ad4170
adi.ad4630
adi.ad469x
adi.ad4851
adi.ad5592r
adi.ad5627
adi.ad5686
Expand Down
72 changes: 72 additions & 0 deletions examples/ad4851_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (C) 2025 Analog Devices, Inc.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# - Neither the name of Analog Devices, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# - The use of this software may or may not infringe the patent rights
# of one or more patent holders. This license does not release you
# from the requirement that you obtain separate licenses from these
# patent holders to use this software.
# - Use of the software either in source or binary form, must be run
# on or directly connected to an Analog Devices Inc. component.
#
# THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.
#
# IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY
# RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import sys
from time import sleep

import matplotlib.pyplot as plt

from adi import ad4851

# Optionally pass URI as command line argument,
# else use default ip:analog.local
my_uri = sys.argv[1] if len(sys.argv) >= 2 else "ip:analog.local"
print("uri: " + str(my_uri))

my_adc = ad4851(uri=my_uri, device_name="ad4858")
# my_adc.rx_buffer_size = 1024

print("Sampling Frequency: ", my_adc.sampling_frequency)
print("Oversampling Ratio: ", my_adc.oversampling_ratio)
print("Enabled Channels: ", my_adc.rx_enabled_channels)
for ch in my_adc.rx_enabled_channels:
print(f"Channel {ch} calibbias value: {my_adc.channel[ch].calibbias}")
print(f"Channel {ch} calibscale value: {my_adc.channel[ch].calibscale}")
print(f"Channel {ch} scale value: {my_adc.channel[ch].scale}")
print(f"Channel {ch} scale available values: {my_adc.channel[ch].scale_available}")

plt.clf()
sleep(0.5)
data = my_adc.rx()
for ch in my_adc.rx_enabled_channels:
plt.plot(range(0, len(data[0])), data[ch], label="voltage" + str(ch))
plt.xlabel("Data Point")
plt.ylabel("ADC counts")
plt.legend(
bbox_to_anchor=(0.0, 1.02, 1.0, 0.102),
loc="lower left",
ncol=4,
mode="expand",
borderaxespad=0.0,
)
plt.show()
9 changes: 9 additions & 0 deletions supported_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
- AD4696
- AD4697
- AD4698
- AD4851
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to include all parts now supported by the AD4851 class

- AD4852
- AD4853
- AD4854
- AD4855
- AD4856
- AD4857
- AD4858
- AD4858i
- AD5310R
- AD5311R
- AD5592R
Expand Down
1 change: 1 addition & 0 deletions test/emu/devices/ad4851.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE context [<!ELEMENT context (device | context-attribute)*><!ELEMENT context-attribute EMPTY><!ELEMENT device (channel | attribute | debug-attribute | buffer-attribute)*><!ELEMENT channel (scan-element?, attribute*)><!ELEMENT attribute EMPTY><!ELEMENT scan-element EMPTY><!ELEMENT debug-attribute EMPTY><!ELEMENT buffer-attribute EMPTY><!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED><!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED><!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED><!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED><!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED><!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED value CDATA #IMPLIED><!ATTLIST debug-attribute name CDATA #REQUIRED value CDATA #IMPLIED><!ATTLIST buffer-attribute name CDATA #REQUIRED value CDATA #IMPLIED>]><context name="network" description="10.48.65.134 Linux analog 6.15.0-rc1-00272-g753278a2953a-dirty #261 SMP PREEMPT Thu May 22 12:22:14 EEST 2025 armv7l" ><context-attribute name="hw_model" value="EVAL-AD4858FMCZ on Xilinx Zynq ZED" /><context-attribute name="hw_carrier" value="Avnet ZedBoard board" /><context-attribute name="local,kernel" value="6.15.0-rc1-00272-g753278a2953a-dirty" /><context-attribute name="uri" value="ip:10.48.65.134" /><context-attribute name="ip,ip-addr" value="10.48.65.134" /><device id="hwmon0" name="e000b000ethernetffffffff00" ><channel id="temp1" type="input" ><attribute name="crit" filename="temp1_crit" value="100000" /><attribute name="input" filename="temp1_input" value="47000" /><attribute name="max_alarm" filename="temp1_max_alarm" value="0" /></channel></device><device id="iio:device0" name="ad4858" ><channel id="voltage0-voltage8" type="input" ><scan-element index="0" format="le:s20/32&gt;&gt;0" scale="0.076293" /><attribute name="calibbias" filename="in_voltage0-voltage8_calibbias" value="0" /><attribute name="calibscale" filename="in_voltage0-voltage8_calibscale" value="1.000000000" /><attribute name="scale" filename="in_voltage0-voltage8_scale" value="0.076293" /><attribute name="scale_available" filename="in_voltage0-voltage8_scale_available" value="0.004768 0.009536 0.011920 0.019073 0.023841 0.038146 0.047683 0.076293" /></channel><channel id="voltage1" type="input" ><scan-element index="1" format="le:u20/32&gt;&gt;0" scale="0.038146" /><attribute name="calibbias" filename="in_voltage1_calibbias" value="0" /><attribute name="calibscale" filename="in_voltage1_calibscale" value="1.000000000" /><attribute name="scale" filename="in_voltage1_scale" value="0.038146" /><attribute name="scale_available" filename="in_voltage1_scale_available" value="0.002384 0.004768 0.005960 0.009536 0.011920 0.019073 0.023841 0.038146" /></channel><attribute name="oversampling_ratio" value="1" /><attribute name="oversampling_ratio_available" value="1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536" /><attribute name="sampling_frequency" value="1000000.000000000" /><attribute name="waiting_for_supplier" value="0" /><buffer-attribute name="data_available" value="0" /><buffer-attribute name="direction" value="in" /><buffer-attribute name="length_align_bytes" value="8" /><debug-attribute name="direct_reg_access" value="0x10" /></device></context>
10 changes: 9 additions & 1 deletion test/emu/hardware_map.yml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need a rebase

Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,12 @@ ad7490:
- emulate:
- filename: ad7490.xml
- data_devices:
- iio:device0
- iio:device0
ad4851:
- ad4851
- pyadi_iio_class_support:
- ad4851
- emulate:
- filename: ad4851.xml
- data_devices:
- iio:device0
11 changes: 11 additions & 0 deletions test/test_ad4851.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

hardware = "ad4851"
classname = "adi.ad4851"

#########################################
@pytest.mark.iio_hardware(hardware, True)
@pytest.mark.parametrize("classname", [(classname)])
@pytest.mark.parametrize("channel", [0])
def test_ad4851_rx_data(test_dma_rx, iio_uri, classname, channel):
test_dma_rx(iio_uri, classname, channel)
Loading