|
| 1 | +# Copyright (C) 2025 Analog Devices, Inc. |
| 2 | +# |
| 3 | +# SPDX short identifier: ADIBSD |
| 4 | + |
| 5 | +from decimal import Decimal |
| 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 ad4851(rx, context_manager): |
| 13 | + |
| 14 | + """ AD4851 ADC """ |
| 15 | + |
| 16 | + _complex_data = False |
| 17 | + _device_name = "" |
| 18 | + channel = [] # type: ignore |
| 19 | + |
| 20 | + def __init__(self, uri="", device_name="ad4851"): |
| 21 | + context_manager.__init__(self, uri, self._device_name) |
| 22 | + |
| 23 | + compatible_parts = [ |
| 24 | + "ad4851", |
| 25 | + "ad4852", |
| 26 | + "ad4853", |
| 27 | + "ad4854", |
| 28 | + "ad4855", |
| 29 | + "ad4856", |
| 30 | + "ad4857", |
| 31 | + "ad4858", |
| 32 | + "ad4858i", |
| 33 | + ] |
| 34 | + self._ctrl = None |
| 35 | + |
| 36 | + if not device_name: |
| 37 | + device_name = compatible_parts[0] |
| 38 | + else: |
| 39 | + if device_name not in compatible_parts: |
| 40 | + raise Exception(f"Not a compatible device: {device_name}") |
| 41 | + |
| 42 | + # Select the device matching device_name as working device |
| 43 | + for device in self._ctx.devices: |
| 44 | + if device.name == device_name: |
| 45 | + self._ctrl = device |
| 46 | + self._rxadc = device |
| 47 | + break |
| 48 | + |
| 49 | + # Raise an exception if the device isn't found |
| 50 | + if not self._ctrl: |
| 51 | + raise Exception(f"{device_name} device not found") |
| 52 | + |
| 53 | + if not self._rxadc: |
| 54 | + raise Exception("Error in selecting matching device") |
| 55 | + |
| 56 | + self._rx_channel_names = [] |
| 57 | + self.channel = [] |
| 58 | + for ch in self._ctrl.channels: |
| 59 | + name = ch.id |
| 60 | + self._rx_channel_names.append(name) |
| 61 | + self.channel.append(self._channel(self._ctrl, name)) |
| 62 | + |
| 63 | + rx.__init__(self) |
| 64 | + |
| 65 | + class _channel(attribute): |
| 66 | + """AD4851 channel""" |
| 67 | + |
| 68 | + def __init__(self, ctrl, channel_name): |
| 69 | + self.name = channel_name |
| 70 | + self._ctrl = ctrl |
| 71 | + |
| 72 | + @property |
| 73 | + def scale_available(self): |
| 74 | + """Get Scale available values""" |
| 75 | + return self._get_iio_attr(self.name, "scale_available", False) |
| 76 | + |
| 77 | + @property |
| 78 | + def scale(self): |
| 79 | + """Get Scale value""" |
| 80 | + return self._get_iio_attr(self.name, "scale", False) |
| 81 | + |
| 82 | + @scale.setter |
| 83 | + def scale(self, value): |
| 84 | + """Set scale value""" |
| 85 | + self._set_iio_attr(self.name, "scale", False, str(Decimal(value).real)) |
| 86 | + |
| 87 | + @property |
| 88 | + def calibbias(self): |
| 89 | + """Get calibration bias/offset value.""" |
| 90 | + return self._get_iio_attr(self.name, "calibbias", False) |
| 91 | + |
| 92 | + @calibbias.setter |
| 93 | + def calibbias(self, value): |
| 94 | + """Set channel calibration bias/offset.""" |
| 95 | + self._set_iio_attr(self.name, "calibbias", False, value) |
| 96 | + |
| 97 | + @property |
| 98 | + def calibscale(self): |
| 99 | + """Get calibration scale value.""" |
| 100 | + return self._get_iio_attr(self.name, "calibscale", False) |
| 101 | + |
| 102 | + @calibscale.setter |
| 103 | + def calibscale(self, value): |
| 104 | + """Set channel calibration phascalese.""" |
| 105 | + self._set_iio_attr(self.name, "calibscale", False, value) |
| 106 | + |
| 107 | + @property |
| 108 | + def sampling_frequency(self): |
| 109 | + """Get Sampling frequency value""" |
| 110 | + return self._get_iio_dev_attr("sampling_frequency", False) |
| 111 | + |
| 112 | + @sampling_frequency.setter |
| 113 | + def sampling_frequency(self, value): |
| 114 | + """Set sampling frequency.""" |
| 115 | + self._set_iio_dev_attr("sampling_frequency", value) |
| 116 | + |
| 117 | + @property |
| 118 | + def oversampling_ratio_available(self): |
| 119 | + """Get the oversampling ratio available values""" |
| 120 | + return self._get_iio_dev_attr("oversampling_ratio_available", False) |
| 121 | + |
| 122 | + @property |
| 123 | + def oversampling_ratio(self): |
| 124 | + """Get the oversampling ratio value""" |
| 125 | + return self._get_iio_dev_attr("oversampling_ratio", False) |
| 126 | + |
| 127 | + @oversampling_ratio.setter |
| 128 | + def oversampling_ratio(self, value): |
| 129 | + """Set the oversampling ratio value""" |
| 130 | + self._set_iio_dev_attr("oversampling_ratio", value) |
| 131 | + |
| 132 | + def reg_read(self, reg): |
| 133 | + """Direct Register Access via debugfs""" |
| 134 | + self._set_iio_debug_attr_str("direct_reg_access", reg, self._ctrl) |
| 135 | + return self._get_iio_debug_attr_str("direct_reg_access", self._ctrl) |
| 136 | + |
| 137 | + def reg_write(self, reg, value): |
| 138 | + """Direct Register Access via debugfs""" |
| 139 | + self._set_iio_debug_attr_str("direct_reg_access", f"{reg} {value}", self._ctrl) |
0 commit comments