Skip to content

Commit

Permalink
Merge pull request #583 from spectrum70/wip/bl/ad738x.py
Browse files Browse the repository at this point in the history
Add ad738x driver, test script, example code, rst file and emu xml.
  • Loading branch information
tfcollins authored Aug 22, 2024
2 parents 3c3afaf + f7e59d1 commit 37563ff
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 1 deletion.
1 change: 1 addition & 0 deletions adi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from adi.ad579x import ad579x
from adi.ad717x import ad717x
from adi.ad719x import ad719x
from adi.ad738x import ad738x
from adi.ad777x import ad777x
from adi.ad936x import Pluto, ad9361, ad9363, ad9364
from adi.ad937x import ad9371, ad9375
Expand Down
118 changes: 118 additions & 0 deletions adi/ad738x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright (C) 2024 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD

from decimal import Decimal

import numpy as np
from adi.attribute import attribute
from adi.context_manager import context_manager
from adi.rx_tx import rx


class ad738x(rx, context_manager):

""" AD738x ADC """

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

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

compatible_parts = [
"ad7380",
"ad7380-4",
"ad7389-4",
"ad7381",
"ad7381-4",
"ad7383",
"ad7384",
"ad7386",
"ad7387",
"ad7388",
"ad4680",
"ad4681",
"ad4682",
"ad4683",
]

self._ctrl = None

if not device_name:
device_name = compatible_parts[0]
else:
if device_name not in compatible_parts:
raise Exception("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

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

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

""" AD738x channel """

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

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

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

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

@property
def offset(self):
"""AD738x channel offset."""
return float(self._get_iio_attr_str(self.name, "offset", False))

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

def to_volts(self, index, val):
"""Converts raw value to SI."""
_scale = self.channel[index].scale

ret = None

if isinstance(val, np.int32):
ret = val * _scale

if isinstance(val, np.ndarray):
ret = [x * _scale for x in val]

if ret is None:
raise Exception("Error in converting to actual voltage")

return ret
7 changes: 7 additions & 0 deletions doc/source/devices/adi.ad738x.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ad738x
=================

.. automodule:: adi.ad738x
: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 @@ -28,6 +28,7 @@ Supported Devices
adi.ad717x
adi.ad719x
adi.ad7291
adi.ad738x
adi.ad7606
adi.ad7689
adi.ad7746
Expand Down
19 changes: 19 additions & 0 deletions examples/ad738x_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2022 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD
import sys

import adi
import numpy as np

my_uri = sys.argv[1] if len(sys.argv) >= 2 else "ip:analog.local"
print("uri: " + str(my_uri))

ad738x_dev = adi.ad738x(uri=my_uri, device_name="ad7381")
ad738x_dev.rx_annotated = True
ad738x_dev.rx_output_type = "SI"
ad738x_dev.rx_enabled_channels = [1, 2]

data = ad738x_dev.rx()

print(data)
1 change: 1 addition & 0 deletions supported_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,4 @@
- MAX31865
- MAX9611
- AD7690
- AD738x (AD7380, AD7380-4, AD7381, AD7381-4, AD7389-4, AD7383, AD7384, AD7385, AD7386, AD7387, AD7388, AD4680, AD4681, AD4682, AD4683)
1 change: 1 addition & 0 deletions test/emu/devices/ad7381.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.2.5.219 Linux zed-6 6.10.0-rc6-ad7380x-mainline-g16279534cc5c-dirty #274 SMP PREEMPT Mon Jul 15 16:41:27 CEST 2024 armv7l" ><context-attribute name="hw_model" value="EVAL-AD7383FMCZ on Xilinx Zynq ZED" /><context-attribute name="hw_carrier" value="Xilinx Zynq ZED" /><context-attribute name="hw_mezzanine" value="EVAL-AD7383FMCZ" /><context-attribute name="hw_name" value="AD7383" /><context-attribute name="hw_vendor" value="Analog Devices" /><context-attribute name="hw_serial" value="6065780000000087" /><context-attribute name="local,kernel" value="6.10.0-rc6-ad7380x-mainline-g16279534cc5c-dirty" /><context-attribute name="uri" value="ip:10.2.5.219" /><context-attribute name="ip,ip-addr" value="10.2.5.219" /><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="44000" /><attribute name="max_alarm" filename="temp1_max_alarm" value="0" /></channel></device><device id="iio:device0" name="ad7381" ><channel id="voltage0-voltage1" type="input" ><scan-element index="0" format="le:s14/16&gt;&gt;0" scale="0.402832" /><attribute name="oversampling_ratio" filename="in_voltage-voltage_oversampling_ratio" value="1" /><attribute name="oversampling_ratio_available" filename="in_voltage-voltage_oversampling_ratio_available" value="1 2 4 8 16 32" /><attribute name="raw" filename="in_voltage0-voltage1_raw" value="-2961" /><attribute name="scale" filename="in_voltage-voltage_scale" value="0.402832031" /></channel><channel id="voltage2-voltage3" type="input" ><scan-element index="1" format="le:s14/16&gt;&gt;0" scale="0.402832" /><attribute name="oversampling_ratio" filename="in_voltage-voltage_oversampling_ratio" value="1" /><attribute name="oversampling_ratio_available" filename="in_voltage-voltage_oversampling_ratio_available" value="1 2 4 8 16 32" /><attribute name="raw" filename="in_voltage2-voltage3_raw" value="0" /><attribute name="scale" filename="in_voltage-voltage_scale" value="0.402832031" /></channel><channel id="timestamp" type="input" ><scan-element index="2" format="le:S64/64&gt;&gt;0" /></channel><attribute name="current_timestamp_clock" value="realtime" /><attribute name="waiting_for_supplier" value="0" /><buffer-attribute name="data_available" value="1603" /><buffer-attribute name="direction" value="in" /><debug-attribute name="direct_reg_access" value="0x1D0" /></device><device id="iio:device1" name="xadc" ><channel id="voltage5" name="vccoddr" type="input" ><attribute name="label" filename="in_voltage5_vccoddr_label" value="vccoddr" /><attribute name="raw" filename="in_voltage5_vccoddr_raw" value="2026" /><attribute name="scale" filename="in_voltage5_vccoddr_scale" value="0.732421875" /></channel><channel id="voltage0" name="vccint" type="input" ><attribute name="label" filename="in_voltage0_vccint_label" value="vccint" /><attribute name="raw" filename="in_voltage0_vccint_raw" value="1370" /><attribute name="scale" filename="in_voltage0_vccint_scale" value="0.732421875" /></channel><channel id="voltage4" name="vccpaux" type="input" ><attribute name="label" filename="in_voltage4_vccpaux_label" value="vccpaux" /><attribute name="raw" filename="in_voltage4_vccpaux_raw" value="2435" /><attribute name="scale" filename="in_voltage4_vccpaux_scale" value="0.732421875" /></channel><channel id="temp0" type="input" ><attribute name="offset" filename="in_temp0_offset" value="-2219" /><attribute name="raw" filename="in_temp0_raw" value="2635" /><attribute name="scale" filename="in_temp0_scale" value="123.040771484" /></channel><channel id="voltage7" name="vrefn" type="input" ><attribute name="label" filename="in_voltage7_vrefn_label" value="vrefn" /><attribute name="raw" filename="in_voltage7_vrefn_raw" value="-10" /><attribute name="scale" filename="in_voltage7_vrefn_scale" value="0.732421875" /></channel><channel id="voltage1" name="vccaux" type="input" ><attribute name="label" filename="in_voltage1_vccaux_label" value="vccaux" /><attribute name="raw" filename="in_voltage1_vccaux_raw" value="2440" /><attribute name="scale" filename="in_voltage1_vccaux_scale" value="0.732421875" /></channel><channel id="voltage2" name="vccbram" type="input" ><attribute name="label" filename="in_voltage2_vccbram_label" value="vccbram" /><attribute name="raw" filename="in_voltage2_vccbram_raw" value="1366" /><attribute name="scale" filename="in_voltage2_vccbram_scale" value="0.732421875" /></channel><channel id="voltage3" name="vccpint" type="input" ><attribute name="label" filename="in_voltage3_vccpint_label" value="vccpint" /><attribute name="raw" filename="in_voltage3_vccpint_raw" value="1370" /><attribute name="scale" filename="in_voltage3_vccpint_scale" value="0.732421875" /></channel><channel id="voltage6" name="vrefp" type="input" ><attribute name="label" filename="in_voltage6_vrefp_label" value="vrefp" /><attribute name="raw" filename="in_voltage6_vrefp_raw" value="1691" /><attribute name="scale" filename="in_voltage6_vrefp_scale" value="0.732421875" /></channel><attribute name="sampling_frequency" value="961538" /><attribute name="waiting_for_supplier" value="0" /></device><device id="iio_sysfs_trigger" ><attribute name="add_trigger" value="ERROR" /><attribute name="remove_trigger" value="ERROR" /></device></context>
12 changes: 11 additions & 1 deletion test/emu/hardware_map.yml
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ lm75:
- filename: lm75.xml
- data_devices:
- hwmon0

ad405x:
- ad4052
- pyadi_iio_class_support:
Expand All @@ -610,3 +610,13 @@ ad4170:
- filename: ad4170.xml
- data_devices:
- iio:device0

ad738x:
- ad738x
- pyadi_iio_class_support:
- ad738x
- emulate:
- filename: ad7381.xml
- data_devices:
- iio:device0

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

hardware = "ad738x"
classname = "adi.ad738x"

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

0 comments on commit 37563ff

Please sign in to comment.