Skip to content

Commit

Permalink
Merge pull request #494 from analogdevicesinc/master
Browse files Browse the repository at this point in the history
merge from old master
  • Loading branch information
tfcollins authored Oct 27, 2023
2 parents 7c3d411 + 505bcbf commit 8bcfac1
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions adi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from adi.ad2s1210 import ad2s1210
from adi.ad469x import ad469x
from adi.ad578x import ad578x
from adi.ad717x import ad717x
from adi.ad719x import ad719x
from adi.ad777x import ad777x
Expand Down
143 changes: 143 additions & 0 deletions adi/ad578x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Copyright (C) 2023 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 ad578x(tx, context_manager):
""" AD578x DAC """

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

def __init__(self, uri="", device_name=""):
"""Constructor for AD578x class."""
context_manager.__init__(self, uri, self._device_name)

compatible_parts = [
"ad5780",
"ad5781",
"ad5790",
"ad5791",
"ad5760",
]

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}. 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 = []
for ch in self._ctrl.channels:
name = ch.id
self.output_bits.append(ch.data_format.bits)
self._tx_channel_names.append(name)
self.channel.append(self._channel(self._ctrl, name))

tx.__init__(self)

@property
def powerdown_mode(self):
"""Ad578x powerdown_mode config"""
return self._get_iio_dev_attr_str("powerdown_mode")

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

@property
def powerdown_mode_available(self):
"""AD578x powedown mode available"""
return self._get_iio_dev_attr_str("powerdown_mode_available")

@property
def sampling_frequency(self):
"""AD578x sampling frequency config"""
return self._get_iio_dev_attr_str("sampling_frequency")

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

@property
def code_select(self):
"""AD578x code format config"""
return self._get_iio_dev_attr_str("code_select")

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

@property
def code_select_available(self):
"""AD578x code format available"""
return self._get_iio_dev_attr_str("code_select_available")

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

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

@property
def raw(self):
"""AD578x channel raw value"""
return self._get_iio_attr(self.name, "raw", True)

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

@property
def offset(self):
"""AD578x channel offset"""
return self._get_iio_attr(self.name, "offset", True)

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

@property
def scale(self):
"""AD578x channel scale"""
return self._get_iio_attr(self.name, "scale", True)

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

@property
def powerdown(self):
"""AD578x powerdown config"""
return self._get_iio_attr(self.name, "powerdown", True)

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

.. automodule:: adi.ad578x
: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 @@ -17,6 +17,7 @@ Supported Devices
adi.ad5592r
adi.ad5627
adi.ad5686
adi.ad578x
adi.ad5754r
adi.ad5940
adi.ad6676
Expand Down
50 changes: 50 additions & 0 deletions examples/ad578x_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (C) 2023 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 adi
import numpy as np

# Set up AD578X
ad578x_dev = adi.ad578x(uri="ip:analog")

chn = 0
ad578x_dev._tx_data_type = np.int32
ad578x_dev.tx_enabled_channels = [chn]

raw = ad578x_dev.channel[0].raw
print(raw)

arr = [0, 1000, 2000, 3000, 4000]
data = np.array(arr)

ad578x_dev.tx(data)
5 changes: 5 additions & 0 deletions supported_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
- AD5695R
- AD5696
- AD5696R
- AD5760
- AD5780
- AD5781
- AD5790
- AD5791
- AD5754R
- AD5940
- AD6676
Expand Down
1 change: 1 addition & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def checkparts(c):
"ad719x",
"ad469x",
"ad777x",
"ad578x",
]
for c in dir(mod):
if (
Expand Down
1 change: 1 addition & 0 deletions test/emu/devices/ad578x.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="serial" description="no-OS 0.1" ><context-attribute name="hw_carrier" value="SDP_K1" /><context-attribute name="hw_mezzanine" value="EVAL-AD5780ARDZ" /><context-attribute name="hw_name" value="DAC" /><context-attribute name="uri" value="serial:/dev/ttyS0,230400,8n1n" /><context-attribute name="serial,port" value="/dev/ttyS0" /><context-attribute name="serial,description" value="ttyS0" /><device id="iio:device0" name="ad5780" ><channel id="voltage0" name="Chn0" type="output" ><scan-element index="0" format="le:s18/32&gt;&gt;0" /><attribute name="raw" filename="out_voltage0_raw" value="131072" /><attribute name="scale" filename="out_voltage0_scale" value=" 0.076294" /><attribute name="offset" filename="out_voltage0_offset" value="-262143" /><attribute name="powerdown" filename="out_voltage0_powerdown" value="1" /></channel><attribute name="clear_code" value="0" /><attribute name="output_amplifier" value="unity_gain_mode" /><attribute name="output_amplifier_available" value="gain_of_two unity_gain_mode" /><attribute name="powerdown_mode" value="three_state" /><attribute name="powerdown_mode_available" value="6kohm_to_gnd three_state" /><attribute name="code_select" value="2s_complement" /><attribute name="code_select_available" value="2s_complement offset_binary" /><attribute name="sampling_frequency" value="62500" /><debug-attribute name="direct_reg_access" value="0" /></device><device id="trigger0" name="ad578x_iio_trigger" ></device></context>
9 changes: 9 additions & 0 deletions test/emu/hardware_map.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ ad5592r:
- data_devices:
- iio:device0

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

ad5754r:
- ad5754r
- pyadi_iio_class_support:
Expand Down
11 changes: 11 additions & 0 deletions test/test_ad578x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

hardware = "ad5780"
classname = "adi.ad578x"

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

0 comments on commit 8bcfac1

Please sign in to comment.