|
| 1 | +# Copyright (C) 2020-2025 Analog Devices, Inc. |
| 2 | +# |
| 3 | +# SPDX short identifier: ADIBSD |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from adi.context_manager import context_manager |
| 8 | +from adi.rx_tx import rx |
| 9 | + |
| 10 | + |
| 11 | +class ad7944(rx, context_manager): |
| 12 | + """AD7944, 14-bit, successive approximation analog-to-digital |
| 13 | + converter (SAR ADC) with sample rates up to 2.5 MSPS""" |
| 14 | + |
| 15 | + _compatible_parts = ["ad7944"] |
| 16 | + _device_name = "" |
| 17 | + |
| 18 | + def __init__(self, uri="ip:analog.local", device_name="ad7944"): |
| 19 | + """Initialize.""" |
| 20 | + context_manager.__init__(self, uri, self._device_name) |
| 21 | + |
| 22 | + self._ctrl = None |
| 23 | + |
| 24 | + if not device_name: |
| 25 | + device_name = self._compatible_parts[0] |
| 26 | + else: |
| 27 | + if device_name not in self._compatible_parts: |
| 28 | + raise Exception(f"Not a compatible device: {device_name}") |
| 29 | + |
| 30 | + self._rxadc = self._ctrl = self._ctx.find_device(device_name) |
| 31 | + |
| 32 | + if not self._rxadc: |
| 33 | + raise Exception(f"Error in selecting matching device {device_name}") |
| 34 | + |
| 35 | + if not self._ctrl: |
| 36 | + raise Exception(f"Error in selecting matching device {device_name}") |
| 37 | + |
| 38 | + self._rx_channel_names = [] |
| 39 | + for ch in self._rxadc.channels: |
| 40 | + name = ch._id |
| 41 | + self._rx_channel_names.append(name) |
| 42 | + |
| 43 | + # The data type depends on whether or not SPI offload support is |
| 44 | + # available. We can check this by testing for the |
| 45 | + # sampling_frequency_available attribute, as this only exists in that |
| 46 | + # case. |
| 47 | + try: |
| 48 | + self._get_iio_attr( |
| 49 | + self.rx_channel_names[0], "sampling_frequency_available", False |
| 50 | + ) |
| 51 | + self._rx_data_type = np.uint32 |
| 52 | + except KeyError: |
| 53 | + self._rx_data_type = np.uint16 |
| 54 | + |
| 55 | + rx.__init__(self) |
| 56 | + |
| 57 | + @property |
| 58 | + def sampling_frequency(self): |
| 59 | + """Get sampling frequency.""" |
| 60 | + return self._get_iio_attr(self.rx_channel_names[0], "sampling_frequency", False) |
| 61 | + |
| 62 | + @property |
| 63 | + def sampling_frequency_available(self): |
| 64 | + """Get available sampling frequency values. This property only exists if |
| 65 | + SPI offload is enabled for the driver.""" |
| 66 | + if isinstance(self._rx_data_type, np.uint32): |
| 67 | + return self._get_iio_attr( |
| 68 | + self.rx_channel_names[0], "sampling_frequency_available", False |
| 69 | + ) |
| 70 | + else: |
| 71 | + pass |
| 72 | + |
| 73 | + @sampling_frequency.setter |
| 74 | + def sampling_frequency(self, rate): |
| 75 | + """Set sampling frequency.""" |
| 76 | + self._set_iio_attr( |
| 77 | + self.rx_channel_names[0], "sampling_frequency", value=rate, output=False |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +class ad7985(ad7944): |
| 82 | + """AD7985, 16-bit, successive approximation analog-to-digital |
| 83 | + converter (SAR ADC) with sample rates up to 2.5 MSPS""" |
| 84 | + |
| 85 | + _compatible_parts = ["ad7985"] |
| 86 | + |
| 87 | + def __init__(self, uri="ip:analog.local", device_name="ad7985"): |
| 88 | + super().__init__(uri, device_name) |
| 89 | + |
| 90 | + |
| 91 | +class ad7986(ad7944): |
| 92 | + """AD7986, 18-bit, successive approximation analog-to-digital |
| 93 | + converter (SAR ADC) with sample rates up to 2 MSPS""" |
| 94 | + |
| 95 | + _compatible_parts = ["ad7986"] |
| 96 | + |
| 97 | + def __init__(self, uri="ip:analog.local", device_name="ad7986"): |
| 98 | + super().__init__(uri, device_name) |
| 99 | + self._rx_data_type = np.int32 |
0 commit comments