|
| 1 | +# Copyright (C) 2025 Analog Devices, Inc. |
| 2 | +# |
| 3 | +# SPDX short identifier: ADIBSD |
| 4 | + |
| 5 | +from decimal import Decimal |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from adi.attribute import attribute |
| 10 | +from adi.context_manager import context_manager |
| 11 | +from adi.rx_tx import rx |
| 12 | + |
| 13 | + |
| 14 | +class ada4355(rx, context_manager): |
| 15 | + |
| 16 | + """ ADA4355 ADC """ |
| 17 | + |
| 18 | + _complex_data = False |
| 19 | + channel = [] # type: ignore |
| 20 | + _device_name = "ada4355" |
| 21 | + |
| 22 | + def __init__(self, uri="", device_name="ada4355"): |
| 23 | + """Constructor for ada4355 class.""" |
| 24 | + context_manager.__init__(self, uri, self._device_name) |
| 25 | + |
| 26 | + compatible_parts = ["ada4355"] |
| 27 | + |
| 28 | + self._ctrl = None |
| 29 | + |
| 30 | + if not device_name: |
| 31 | + device_name = compatible_parts[0] |
| 32 | + else: |
| 33 | + if device_name not in compatible_parts: |
| 34 | + raise Exception(f"Not a compatible device: {device_name}") |
| 35 | + |
| 36 | + # Select the device matching device_name as working device |
| 37 | + for device in self._ctx.devices: |
| 38 | + if device.name == device_name: |
| 39 | + self._ctrl = device |
| 40 | + self._rxadc = device |
| 41 | + break |
| 42 | + |
| 43 | + if not self._ctrl: |
| 44 | + raise Exception("Error in selecting matching device") |
| 45 | + |
| 46 | + if not self._rxadc: |
| 47 | + raise Exception("Error in selecting matching device") |
| 48 | + |
| 49 | + self._rx_channel_names = [] |
| 50 | + self.channel = [] |
| 51 | + for ch in self._ctrl.channels: |
| 52 | + name = ch._id |
| 53 | + self._rx_channel_names.append(name) |
| 54 | + self.channel.append(self._channel(self._ctrl, name)) |
| 55 | + |
| 56 | + rx.__init__(self) |
| 57 | + |
| 58 | + def ada4355_register_read(self, reg): |
| 59 | + """Direct Register Access via debugfs""" |
| 60 | + self._set_iio_debug_attr_str("direct_reg_access", reg, self._ctrl) |
| 61 | + return self._get_iio_debug_attr_str("direct_reg_access", self._ctrl) |
| 62 | + |
| 63 | + def ada4355_register_write(self, reg, value): |
| 64 | + """Direct Register Access via debugfs""" |
| 65 | + self._set_iio_debug_attr_str("direct_reg_access", f"{reg} {value}", self._ctrl) |
| 66 | + |
| 67 | + @property |
| 68 | + def sampling_frequency(self): |
| 69 | + """Get sampling frequency.""" |
| 70 | + return self._get_iio_dev_attr("sampling_frequency") |
| 71 | + |
| 72 | + class _channel(attribute): |
| 73 | + |
| 74 | + """ ada4355 channel """ |
| 75 | + |
| 76 | + def __init__(self, ctrl, channel_name): |
| 77 | + self.name = channel_name |
| 78 | + self._ctrl = ctrl |
| 79 | + |
| 80 | + @property |
| 81 | + def scale(self): |
| 82 | + """Get channel scale value""" |
| 83 | + return float(self._get_iio_attr_str(self.name, "scale", False)) |
| 84 | + |
| 85 | + @scale.setter |
| 86 | + def scale(self, value): |
| 87 | + """Set channel scale value""" |
| 88 | + self._set_iio_attr(self.name, "scale", False, str(Decimal(value).real)) |
| 89 | + |
| 90 | + @property |
| 91 | + def raw(self): |
| 92 | + """Get channel raw value (single sample)""" |
| 93 | + return self._get_iio_attr(self.name, "raw", False) |
0 commit comments