Skip to content

Commit

Permalink
adi:ad5754r: Add initial support
Browse files Browse the repository at this point in the history
Add initial attribute r/w support for ad5754r

Signed-off-by: RibhuDP <[email protected]>
  • Loading branch information
ribdp committed Sep 21, 2023
1 parent ff613be commit 3fc6c38
Show file tree
Hide file tree
Showing 9 changed files with 321 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 @@ -13,6 +13,7 @@
from adi.ad4630 import ad4630
from adi.ad5592r import ad5592r
from adi.ad5686 import ad5686
from adi.ad5754r import ad5754r
from adi.ad5940 import ad5940
from adi.ad6676 import ad6676
from adi.ad7124 import ad7124
Expand Down
209 changes: 209 additions & 0 deletions adi/ad5754r.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Copyright (C) 2020-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 ad5754r(tx, context_manager):
""" AD5754R DAC """

_complex_data = False
channel = []
_device_name = ""

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

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

compatible_parts = ["ad5754r"]
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 int_ref_powerup(self):
"""Get internal reference powerup"""
return self._get_iio_dev_attr_str("int_ref_powerup")

@property
def int_ref_powerup_available(self):
"""Get list of all internal reference powerup settings"""
return self._get_iio_dev_attr_str("int_ref_powerup_available")

@int_ref_powerup.setter
def int_ref_powerup(self, value):
"""Set internal reference powerup"""
if value in self.int_ref_powerup_available:
self._set_iio_dev_attr_str("int_ref_powerup", value)
else:
raise ValueError(
"Error: internal reference powerup not supported \nUse one of: "
+ str(self.int_ref_powerup_available)
)

@property
def clear_setting(self):
"""Get clear code setting"""
return self._get_iio_dev_attr_str("clear_setting")

@property
def clear_setting_available(self):
"""Get list of all clear code settings"""
return self._get_iio_dev_attr_str("clear_setting_available")

@clear_setting.setter
def clear_setting(self, value):
"""Set clear setting"""
if value in self.clear_setting_available:
self._set_iio_dev_attr_str("clear_setting", value)
else:
raise ValueError(
"Error: clear setting not supported \nUse one of: "
+ str(self.clear_setting_available)
)

@property
def sdo_disable(self):
"""Get sdo disable"""
return self._get_iio_dev_attr_str("sdo_disable")

@property
def sdo_disable_available(self):
"""Get list of all sdo enable/disable settings"""
return self._get_iio_dev_attr_str("sdo_disable_available")

@sdo_disable.setter
def sdo_disable(self, value):
"""Set sdo enable/disable setting"""
if value in self.sdo_disable_available:
self._set_iio_dev_attr_str("sdo_disable", value)
else:
raise ValueError(
"Error: sdo setting not supported \nUse one of: "
+ str(self.sdo_disable_available)
)

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

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

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

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

@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):
"""Set channel scale"""
self._set_iio_attr(self.name, "scale", True, str(Decimal(value).real))

@property
def powerup(self):
"""Get DAC chn powerup"""
return self._get_iio_attr_str(self.name, "powerup", True)

@property
def powerup_available(self):
"""Get list of DAC chn powerup settings"""
return self._get_iio_attr_str(self.name, "powerup_available", True)

@powerup.setter
def powerup(self, value):
"""Set DAC chn powerup"""
if value in self.powerup_available:
self._set_iio_attr(self.name, "powerup", True, value)
else:
raise ValueError(
"Error: powerup setting not supported \nUse one of: "
+ str(self.powerup_available)
)

@property
def range(self):
"""Get output range"""
return self._get_iio_attr(self.name, "range", True)

@property
def range_available(self):
"""Get list of all output ranges"""
return self._get_iio_attr_str(self.name, "range_available", True)

@range.setter
def range(self, value):
"""Set DAC chn range"""
if value in self.range_available:
self._set_iio_attr(self.name, "range", True, value)
else:
raise ValueError(
"Error: range setting not supported \nUse one of: "
+ str(self.range_available)
)
7 changes: 7 additions & 0 deletions doc/source/devices/adi.ad5754r.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ad5754r
=================

.. automodule:: adi.ad5754r
: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 @@ -16,6 +16,7 @@ Supported Devices
adi.ad5592r
adi.ad5627
adi.ad5686
adi.ad5754r
adi.ad5940
adi.ad6676
adi.ad7124
Expand Down
48 changes: 48 additions & 0 deletions examples/ad5754r_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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

# Set up AD5754R
ad578x_dev = adi.ad5754r(uri="serial:COM46,230400,8n1n")
ad578x.int_ref_powerup = "powerup"

# Configure channel 0
chn_num = 0
ad5754r_chan = ad5754r_dev.channel[chn_num]
ad5754r_chan.powerup = "powerup"

raw = int(ad5754r_chan.raw)
scale = float(ad5754r_chan.scale)
offset = int(ad5754r_chan.offset)
print(f"Channel{chn_num} voltage: {(raw + offset) * scale}")
1 change: 1 addition & 0 deletions supported_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- AD5695R
- AD5696
- AD5696R
- AD5754R
- AD5940
- AD6676
- AD7124
Expand Down
1 change: 1 addition & 0 deletions test/emu/devices/ad5754r.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/projects/NO_OS_PROJECT 0.1" ><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="ad5754r" ><channel id="voltage0" name="Chn0" type="output" ><attribute name="raw" filename="out_voltage0_raw" value="0" /><attribute name="scale" filename="out_voltage0_scale" value="0.0000762939" /><attribute name="offset" filename="out_voltage0_offset" value="0" /><attribute name="powerup" filename="out_voltage0_powerup" value="powerdown" /><attribute name="powerup_available" filename="out_voltage0_powerup_available" value="powerdown powerup" /><attribute name="range" filename="out_voltage0_range" value="0v_to_5v" /><attribute name="range_available" filename="out_voltage0_range_available" value="0v_to_5v 0v_to_10v 0v_to_10v8 neg5v_to_5v neg10v_to_10v neg10v8_to_10v8" /></channel><channel id="voltage1" name="Chn1" type="output" ><attribute name="raw" filename="out_voltage1_raw" value="0" /><attribute name="scale" filename="out_voltage1_scale" value="0.0000762939" /><attribute name="offset" filename="out_voltage1_offset" value="0" /><attribute name="powerup" filename="out_voltage1_powerup" value="powerdown" /><attribute name="powerup_available" filename="out_voltage1_powerup_available" value="powerdown powerup" /><attribute name="range" filename="out_voltage1_range" value="0v_to_5v" /><attribute name="range_available" filename="out_voltage1_range_available" value="0v_to_5v 0v_to_10v 0v_to_10v8 neg5v_to_5v neg10v_to_10v neg10v8_to_10v8" /></channel><channel id="voltage2" name="Chn2" type="output" ><attribute name="raw" filename="out_voltage2_raw" value="0" /><attribute name="scale" filename="out_voltage2_scale" value="0.0000762939" /><attribute name="offset" filename="out_voltage2_offset" value="0" /><attribute name="powerup" filename="out_voltage2_powerup" value="powerdown" /><attribute name="powerup_available" filename="out_voltage2_powerup_available" value="powerdown powerup" /><attribute name="range" filename="out_voltage2_range" value="0v_to_5v" /><attribute name="range_available" filename="out_voltage2_range_available" value="0v_to_5v 0v_to_10v 0v_to_10v8 neg5v_to_5v neg10v_to_10v neg10v8_to_10v8" /></channel><channel id="voltage3" name="Chn3" type="output" ><attribute name="raw" filename="out_voltage3_raw" value="0" /><attribute name="scale" filename="out_voltage3_scale" value="0.0000762939" /><attribute name="offset" filename="out_voltage3_offset" value="0" /><attribute name="powerup" filename="out_voltage3_powerup" value="powerdown" /><attribute name="powerup_available" filename="out_voltage3_powerup_available" value="powerdown powerup" /><attribute name="range" filename="out_voltage3_range" value="0v_to_5v" /><attribute name="range_available" filename="out_voltage3_range_available" value="0v_to_5v 0v_to_10v 0v_to_10v8 neg5v_to_5v neg10v_to_10v neg10v8_to_10v8" /></channel><attribute name="int_ref_powerup" value="powerdown" /><attribute name="int_ref_powerup_available" value="powerdown powerup" /><attribute name="clear_setting" value="midscale_code" /><attribute name="clear_setting_available" value="0v midscale_code" /><attribute name="sdo_disable" value="enable" /><attribute name="sdo_disable_available" value="enable disable" /><attribute name="sampling_frequency" value="2000" /><debug-attribute name="direct_reg_access" value="0" /></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 @@ -350,6 +350,15 @@ ad5592r:
- filename: ad5592r.xml
- data_devices:
- iio:device0

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

ad7291:
- ad7291
Expand Down
44 changes: 44 additions & 0 deletions test/test_ad5754r.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import adi
import pytest

hardware = "ad5754r"
classname = "adi.ad5754r"

#########################################
@pytest.mark.iio_hardware(hardware)
@pytest.mark.parametrize("classname", [(classname)])
@pytest.mark.parametrize(
"attr, val",
[
("int_ref_powerup", ["powerdown", "powerup"],),
("clear_setting", ["0v", "midscale_code"],),
("sdo_disable", ["enable", "disable"],),
],
)
def test_ad5754r_global_attr(
test_attribute_multipe_values, iio_uri, classname, attr, val
):
test_attribute_multipe_values(iio_uri, classname, attr, val, 0)


#########################################
@pytest.mark.iio_hardware(hardware)
@pytest.mark.parametrize("classname", [(classname)])
@pytest.mark.parametrize("channel", [0, 1, 2, 3])
@pytest.mark.parametrize(
"attr, val",
[
("int_ref_powerup", ["powerdown", "powerup"],),
("clear_setting", ["0v", "midscale_code"],),
("sdo_disable", ["enable", "disable"],),
],
)
def test_ad5754r_channel_attr(iio_uri, classname, channel, attr, val):
dev = adi.ad5754r(iio_uri)
for ch in channel:
dev_chan = dev.channel[ch]
for attrib in attr:
for value in val:
eval(f"dev_chan.{attrib} = {value}")
val_read = eval(f"dev_chan.{attrib}")
assert val_read == value

0 comments on commit 3fc6c38

Please sign in to comment.