Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for ad3552r_hs #629

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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 @@ -13,6 +13,7 @@
from adi.ad936x import Pluto, ad9361, ad9363, ad9364
from adi.ad937x import ad9371, ad9375
from adi.ad3552r import ad3552r
from adi.ad3552r_hs import ad3552r_hs
from adi.ad4020 import ad4000, ad4001, ad4002, ad4003, ad4020
from adi.ad4110 import ad4110
from adi.ad4130 import ad4130
Expand Down
141 changes: 141 additions & 0 deletions adi/ad3552r_hs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Copyright (C) 2024-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 tx


class ad3552r_hs(tx, context_manager):
"""AD3552R_HS DAC"""

_complex_data = False
_device_name = "AD3552R_HS"

def __init__(self, uri="", device_name=""):
""" Constructor for AD3552R_HS driver class """

context_manager.__init__(self, uri, self._device_name)

compatible_parts = [
"ad3552r",
"ad3551r",
"ad3542r",
"ad3541r",
]

self._ctrl = None
self._txdac = 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}. Supported device names "
f"are: {','.join(compatible_parts)}"
)

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

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

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

self.output_bits = []
self.channel = []
self._tx_channel_names = []
for ch in self._ctrl.channels:
name = ch._id
output = ch._output
self.output_bits.append(ch.data_format.bits)
self._tx_channel_names.append(name)
self.channel.append(self._channel(self._ctrl, name, output))
if output is True:
setattr(self, name, self._channel(self._ctrl, name, output))

tx.__init__(self)

@property
def input_source(self):
"""Input source of the DAC"""
return self._get_iio_dev_attr_str("input_source", self._txdac)

@input_source.setter
def input_source(self, value):
self._set_iio_dev_attr_str("input_source", value, self._txdac)

@property
def stream_status(self):
"""Stream status of the DAC"""
return self._get_iio_dev_attr_str("stream_status", self._txdac)

@stream_status.setter
def stream_status(self, value):
self._set_iio_dev_attr_str("stream_status", value, self._txdac)

@property
def output_range(self):
"""Stream status of the DAC"""
return self._get_iio_dev_attr_str("output_range", self._txdac)

@output_range.setter
def output_range(self, value):
self._set_iio_dev_attr_str("output_range", value, self._txdac)

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

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

@property
def sample_rate(self):
"""Sample rate of the DAC"""
return self._get_iio_attr(self.name, "sampling_frequency", True)

@sample_rate.setter
def sample_rate(self, value):
self._set_iio_attr(self.name, "sampling_frequency", True, value)

@property
def raw(self):
"""Get channel raw value
DAC code in the range 0-65535"""
return self._get_iio_attr(self.name, "raw", True)

@raw.setter
def raw(self, value):
"""Set channel raw value"""
self._set_iio_attr(self.name, "raw", True, str(int(value)))

@property
def offset(self):
"""Get channel offset"""
return self._get_iio_attr_str(self.name, "offset", True)

@offset.setter
def offset(self, value):
"""Set channel offset"""
self._set_iio_attr(self.name, "offset", True, str(Decimal(value).real))

@property
def scale(self):
"""Get channel scale"""
return float(self._get_iio_attr_str(self.name, "scale", True))

@scale.setter
def scale(self, value):
self._set_iio_attr(self.name, "scale", True, str(Decimal(value).real))
7 changes: 7 additions & 0 deletions doc/source/devices/adi.ad3552r_hs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
adi.ad3552r_hs module
=====================

.. automodule:: adi.ad3552r_hs
: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 @@ -10,6 +10,7 @@ Supported Devices
adi.QuadMxFE_multi
adi.ad2s1210
adi.ad3552r
adi.ad3552r_hs
adi.ad4020
adi.ad405x
adi.ad4110
Expand Down
57 changes: 57 additions & 0 deletions examples/ad3552r_hs_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import sys
import time

import matplotlib.pyplot as plt
import numpy as np

import adi

# 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))

# device connection
dev = adi.ad3552r_hs(uri=my_uri, device_name="ad3552r")

dev.tx_enabled_channels = [0, 1]
dev.tx_cyclic_buffer = False

dev._tx_data_type = np.uint16

# signal generation
fs = int(dev.channel[0].sample_rate)
# Signal frequency
fc = 80000
# Number of samples
N = int(fs / fc)
# Period
ts = 1 / float(fs)
# Time array
t = np.arange(0, N * ts, ts)
# Sine generation
samples = np.sin(2 * np.pi * t * fc)
# Amplitude (full_scale / 2)
samples *= (2 ** 15) - 1
# Offset (full_scale / 2)
samples += 2 ** 15
# conversion to unsigned int and offset binary
samples = np.uint16(samples)

print("sample rate:", dev.channel[0].sample_rate)
print("Sample data min:", samples.min())
print("Sample data max:", samples.max())

if dev.tx_cyclic_buffer == True:
dev.tx([samples, samples])
else:
for i in range(2):
dev.tx([samples, samples])

plt.suptitle("AD3552R samples data")
plt.plot(t, samples)
plt.xlabel("Samples")
plt.show()

dev.tx_destroy_buffer()
1 change: 1 addition & 0 deletions supported_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
### Currently supported hardware
- AD2S1210
- AD3552r (AD3542r)
- AD3552r-hs (AD3551r, AD3541r, AD3542r)
- AD4000 (AD4004, AD4008)
- AD4001 (AD4005)
- AD4002 (AD4006, AD4010)
Expand Down
1 change: 1 addition & 0 deletions test/emu/devices/ad3552r_hs.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="192.168.0.166 Linux analog 6.13.0-rc7-xilinx-gfebd1b8aa9f3 #35 SMP PREEMPT Thu Jan 23 16:19:36 CET 2025 armv7l" ><context-attribute name="hw_carrier" value="Avnet ZedBoard board" /><context-attribute name="local,kernel" value="6.13.0-rc7-xilinx-gfebd1b8aa9f3" /><context-attribute name="uri" value="ip:192.168.0.166" /><context-attribute name="ip,ip-addr" value="192.168.0.166" /><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="26000" /><attribute name="max_alarm" filename="temp1_max_alarm" value="0" /></channel></device><device id="iio:device0" name="ad3552r" ><channel id="voltage0" type="output" ><scan-element index="0" format="be:U16/16&gt;&gt;0" scale="0.152588" /><attribute name="offset" filename="out_voltage0_offset" value="-32768.000000" /><attribute name="raw" filename="out_voltage0_raw" value="31450" /><attribute name="sampling_frequency" filename="out_voltage0_sampling_frequency" value="16666667" /><attribute name="scale" filename="out_voltage0_scale" value="0.152588" /></channel><channel id="voltage1" type="output" ><scan-element index="1" format="be:U16/16&gt;&gt;0" scale="0.152588" /><attribute name="offset" filename="out_voltage1_offset" value="-32768.000000" /><attribute name="raw" filename="out_voltage1_raw" value="31450" /><attribute name="sampling_frequency" filename="out_voltage1_sampling_frequency" value="16666667" /><attribute name="scale" filename="out_voltage1_scale" value="0.152588" /></channel><attribute name="waiting_for_supplier" value="0" /><buffer-attribute name="data_available" value="3328" /><buffer-attribute name="direction" value="out" /><buffer-attribute name="length_align_bytes" value="4" /></device></context>
10 changes: 10 additions & 0 deletions test/emu/hardware_map.yml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ ad3552r:
- filename: ad3552r.xml
- data_devices:
- iio:device0

ad3552r_hs:
- ad3552r_hs
- pyadi_iio_class_support:
- ad3552r_hs
- emulate:
- filename: ad3552r_hs.xml
- data_devices:
- iio:device0

ad9434:
- axi-ad9434-core-lpc
- ad9517-4
Expand Down
30 changes: 30 additions & 0 deletions test/test_ad3552r_hs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

hardware = ["ad3552r", "ad3551r", "ad3542r", "ad3541r"]
classname = "adi.ad3552r_hs"

#########################################
@pytest.mark.iio_hardware(hardware)
@pytest.mark.parametrize("classname", [(classname)])
@pytest.mark.parametrize(
"attr, start, stop, step, tol, repeats, sub_channel",
[
("raw", 0, 4000, 1000, 1, 3, "voltage0"),
("raw", 0, 4000, 1000, 1, 3, "voltage1"),
],
)
def test_ad3552r_hs_raw_attr(
test_attribute_single_value,
iio_uri,
classname,
attr,
start,
stop,
step,
tol,
repeats,
sub_channel,
):
test_attribute_single_value(
iio_uri, classname, attr, start, stop, step, tol, repeats, sub_channel
)
Loading