Skip to content

Commit 127b8b3

Browse files
authored
Merge pull request #595 from SaikiranGudla/ad7091r_support
Add support for AD7091R-2, AD7091R-4 and AD7091R-8
2 parents a0c09a0 + 22f3784 commit 127b8b3

File tree

9 files changed

+209
-0
lines changed

9 files changed

+209
-0
lines changed

adi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from adi.ad5754r import ad5754r
2727
from adi.ad5940 import ad5940
2828
from adi.ad6676 import ad6676
29+
from adi.ad7091rx import ad7091rx
2930
from adi.ad7124 import ad7124
3031
from adi.ad7134 import ad7134
3132
from adi.ad7291 import ad7291

adi/ad7091rx.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright (C) 2025 Analog Devices, Inc.
2+
#
3+
# SPDX short identifier: ADIBSD
4+
5+
import numpy as np
6+
7+
from adi.attribute import attribute
8+
from adi.context_manager import context_manager
9+
from adi.rx_tx import rx
10+
11+
12+
class ad7091rx(rx, context_manager):
13+
"""AD7091R-2/AD7091R-4/AD7091R-8 SPI interface,
14+
2-/4-/8-channel, 12-bit SAR ADC"""
15+
16+
_complex_data = False
17+
channel = [] # type: ignore
18+
_device_name = ""
19+
20+
def __init__(self, uri="", device_name=""):
21+
22+
context_manager.__init__(self, uri, self._device_name)
23+
24+
compatible_parts = [
25+
"ad7091r-2",
26+
"ad7091r-4",
27+
"ad7091r-8",
28+
]
29+
30+
self._ctrl = None
31+
32+
if not device_name:
33+
device_name = compatible_parts[2]
34+
else:
35+
if device_name not in compatible_parts:
36+
raise Exception("Not a compatible device: " + device_name)
37+
38+
# Selecting the device matching device_name AD7091RX family as working device.
39+
for device in self._ctx.devices:
40+
if device.name == device_name:
41+
self._ctrl = device
42+
self._rxadc = device
43+
break
44+
45+
for ch in self._ctrl.channels:
46+
name = ch._id
47+
self._rx_channel_names.append(name)
48+
self.channel.append(self._channel(self._ctrl, name))
49+
setattr(self, name, self._channel(self._ctrl, name))
50+
51+
rx.__init__(self)
52+
53+
class _channel(attribute):
54+
"""AD7091R-8/-4/-2 Input Voltage Channels"""
55+
56+
def __init__(self, ctrl, channel_name):
57+
self.name = channel_name
58+
self._ctrl = ctrl
59+
60+
@property
61+
def raw(self):
62+
"""AD7091r channel raw value"""
63+
return self._get_iio_attr(self.name, "raw", False)
64+
65+
@property
66+
def scale(self):
67+
"""AD7091r channel scale"""
68+
return float(self._get_iio_attr_str(self.name, "scale", False))
69+
70+
def to_mvolts(self, index, val):
71+
"""Converts raw value to mV"""
72+
_scale = self.channel[index].scale
73+
74+
ret = None
75+
76+
if isinstance(val, np.uint16):
77+
ret = val * _scale
78+
79+
if isinstance(val, np.ndarray):
80+
ret = [x * _scale for x in val]
81+
82+
return ret
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ad7091rx
2+
=================
3+
4+
.. automodule:: adi.ad7091rx
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

doc/source/devices/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Supported Devices
2626
adi.ad5754r
2727
adi.ad5940
2828
adi.ad6676
29+
adi.ad7091rx
2930
adi.ad7124
3031
adi.ad7134
3132
adi.ad717x

examples/ad7091rx_example.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (C) 2025 Analog Devices, Inc.
2+
#
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without modification,
6+
# are permitted provided that the following conditions are met:
7+
# - Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# - Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in
11+
# the documentation and/or other materials provided with the
12+
# distribution.
13+
# - Neither the name of Analog Devices, Inc. nor the names of its
14+
# contributors may be used to endorse or promote products derived
15+
# from this software without specific prior written permission.
16+
# - The use of this software may or may not infringe the patent rights
17+
# of one or more patent holders. This license does not release you
18+
# from the requirement that you obtain separate licenses from these
19+
# patent holders to use this software.
20+
# - Use of the software either in source or binary form, must be run
21+
# on or directly connected to an Analog Devices Inc. component.
22+
#
23+
# THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24+
# INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
25+
# PARTICULAR PURPOSE ARE DISCLAIMED.
26+
#
27+
# IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY
29+
# RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30+
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32+
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
34+
import argparse
35+
36+
import numpy as np
37+
38+
from adi.ad7091rx import ad7091rx
39+
40+
41+
def main():
42+
# Set up argument parser
43+
parser = argparse.ArgumentParser(description="AD7091R Example Script")
44+
parser.add_argument(
45+
"--uri",
46+
type=str,
47+
help="The URI for the AD7091R device",
48+
default="serial:COM7,230400,8n1",
49+
)
50+
parser.add_argument(
51+
"--device_name",
52+
type=str,
53+
choices=["ad7091r-2", "ad7091r-4", "ad7091r-8"],
54+
help="The device name (Supported devices are ad7091r-2, ad7091r-4, ad7091r-8)",
55+
default="ad7091r-8",
56+
)
57+
58+
# Parse arguments
59+
args = parser.parse_args()
60+
61+
# Set up AD7091R device
62+
ad7091r_dev = ad7091rx(uri=args.uri, device_name=args.device_name)
63+
64+
# Get ADC channel 0 raw value and print it
65+
raw = ad7091r_dev.channel[0].raw
66+
print(f"Raw value read from channel0 is {raw}")
67+
68+
# Capture a buffer of 100 samples from channel 0 and display them
69+
chn = 0
70+
ad7091r_dev._rx_data_type = np.int32
71+
ad7091r_dev.rx_output_type = "raw"
72+
ad7091r_dev.rx_enabled_channels = [chn]
73+
ad7091r_dev.rx_buffer_size = 100
74+
75+
data = ad7091r_dev.rx()
76+
77+
print(data)
78+
79+
80+
if __name__ == "__main__":
81+
main()

supported_parts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
- AD5754R
7777
- AD5940
7878
- AD6676
79+
- AD7091RX (AD7091R-2, AD7091R-4, AD7091R-8)
7980
- AD7124
8081
- AD7134
8182
- AD7172-2

test/emu/devices/ad7091rx.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.1.3 Linux raspberrypi 6.8.0-rc4-v8+ #4 SMP PREEMPT Thu Feb 15 10:45:25 -03 2024 aarch64" ><context-attribute name="local,kernel" value="6.8.0-rc4-v8+" /><context-attribute name="uri" value="ip:192.168.1.3" /><context-attribute name="ip,ip-addr" value="192.168.1.3" /><device id="iio:device0" name="ad7091r-8" ><channel id="voltage1" type="input" ><attribute name="raw" filename="in_voltage1_raw" value="2603" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage5" type="input" ><attribute name="raw" filename="in_voltage5_raw" value="0" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage2" type="input" ><attribute name="raw" filename="in_voltage2_raw" value="533" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage6" type="input" ><attribute name="raw" filename="in_voltage6_raw" value="1076" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage3" type="input" ><attribute name="raw" filename="in_voltage3_raw" value="1922" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage7" type="input" ><attribute name="raw" filename="in_voltage7_raw" value="1747" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage0" type="input" ><attribute name="raw" filename="in_voltage0_raw" value="2229" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><channel id="voltage4" type="input" ><attribute name="raw" filename="in_voltage4_raw" value="2376" /><attribute name="scale" filename="in_voltage_scale" value="0.610351562" /></channel><attribute name="waiting_for_supplier" value="0" /></device></context>

test/emu/hardware_map.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ ad6676:
536536
- data_devices:
537537
- iio:device1
538538

539+
ad7091r8:
540+
- ad7091r8
541+
- pyadi_iio_class_support:
542+
- ad7091rx
543+
- emulate:
544+
- filename: ad7091rx.xml
545+
- data_devices:
546+
- iio:device0
547+
539548
ad7768:
540549
- ad7768
541550
- pyadi_iio_class_support:

test/test_ad7091rx.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
hardware = "ad7091r8"
4+
classname = "adi.ad7091rx"
5+
6+
#########################################
7+
@pytest.mark.iio_hardware(hardware)
8+
@pytest.mark.parametrize("classname", [(classname)])
9+
@pytest.mark.parametrize("attr", ["raw"])
10+
@pytest.mark.parametrize(
11+
"channel",
12+
[
13+
"voltage0",
14+
"voltage1",
15+
"voltage2",
16+
"voltage3",
17+
"voltage4",
18+
"voltage5",
19+
"voltage6",
20+
"voltage7",
21+
],
22+
)
23+
def test_ad7091rx_channel_attr_raw(
24+
test_attribute_single_value_channel_readonly, iio_uri, classname, channel, attr
25+
):
26+
test_attribute_single_value_channel_readonly(iio_uri, classname, channel, attr)

0 commit comments

Comments
 (0)