|
| 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 tx |
| 10 | + |
| 11 | + |
| 12 | +class ad9740(tx, context_manager): |
| 13 | + """AD9740/AD9742/AD9744/AD9748 10/12/14/8-bit, 210 MSPS DAC with DDS support""" |
| 14 | + |
| 15 | + _complex_data = False |
| 16 | + _device_name = "AD9740" |
| 17 | + |
| 18 | + def disable_dds(self): |
| 19 | + """Override DDS disable to use data_source attribute instead. |
| 20 | +
|
| 21 | + AD9740 uses data_source control, not the standard DDS raw attribute. |
| 22 | + When switching to DMA mode, set data_source to 'normal'. |
| 23 | + """ |
| 24 | + # Set data source to normal (DMA mode) |
| 25 | + if hasattr(self, 'channel') and len(self.channel) > 0: |
| 26 | + try: |
| 27 | + self.channel[0].data_source = "normal" |
| 28 | + except (AttributeError, OSError): |
| 29 | + # If data_source doesn't work, just pass |
| 30 | + # (driver might already be in correct mode) |
| 31 | + pass |
| 32 | + |
| 33 | + def __init__(self, uri="", device_name=""): |
| 34 | + """Constructor for AD9740 driver class""" |
| 35 | + |
| 36 | + context_manager.__init__(self, uri, self._device_name) |
| 37 | + |
| 38 | + compatible_parts = [ |
| 39 | + "ad9740", # 10-bit DAC |
| 40 | + "ad9742", # 12-bit DAC |
| 41 | + "ad9744", # 14-bit DAC |
| 42 | + "ad9748", # 8-bit DAC |
| 43 | + ] |
| 44 | + |
| 45 | + self._ctrl = None |
| 46 | + self._txdac = None |
| 47 | + |
| 48 | + if not device_name: |
| 49 | + device_name = compatible_parts[0] |
| 50 | + else: |
| 51 | + if device_name not in compatible_parts: |
| 52 | + raise Exception( |
| 53 | + f"Not a compatible device: {device_name}. Supported device names " |
| 54 | + f"are: {','.join(compatible_parts)}" |
| 55 | + ) |
| 56 | + |
| 57 | + # Select the device matching device_name as working device |
| 58 | + for device in self._ctx.devices: |
| 59 | + if device.name == device_name: |
| 60 | + self._ctrl = device |
| 61 | + self._txdac = device |
| 62 | + break |
| 63 | + |
| 64 | + if not self._ctrl: |
| 65 | + raise Exception("Error in selecting matching device") |
| 66 | + |
| 67 | + if not self._txdac: |
| 68 | + raise Exception("Error in selecting matching device") |
| 69 | + |
| 70 | + self.output_bits = [] |
| 71 | + self.channel = [] |
| 72 | + self._tx_channel_names = [] |
| 73 | + for ch in self._ctrl.channels: |
| 74 | + name = ch._id |
| 75 | + output = ch._output |
| 76 | + self.output_bits.append(ch.data_format.bits) |
| 77 | + self._tx_channel_names.append(name) |
| 78 | + self.channel.append(self._channel(self._ctrl, name, output)) |
| 79 | + if output is True: |
| 80 | + setattr(self, name, self._channel(self._ctrl, name, output)) |
| 81 | + |
| 82 | + tx.__init__(self) |
| 83 | + |
| 84 | + class _channel(attribute): |
| 85 | + """AD9740 channel""" |
| 86 | + |
| 87 | + def __init__(self, ctrl, channel_name, output): |
| 88 | + self.name = channel_name |
| 89 | + self._ctrl = ctrl |
| 90 | + self._output = output |
| 91 | + |
| 92 | + @property |
| 93 | + def sample_rate(self): |
| 94 | + """Sample rate of the DAC (from backend) |
| 95 | + Note: AD9740 driver doesn't expose sampling_frequency attribute, |
| 96 | + so we return a default value. Override if needed.""" |
| 97 | + try: |
| 98 | + return self._get_iio_attr(self.name, "sampling_frequency", True) |
| 99 | + except (KeyError, OSError): |
| 100 | + # Attribute doesn't exist, return default (210 MHz from ADF4351) |
| 101 | + return 210000000 |
| 102 | + |
| 103 | + @sample_rate.setter |
| 104 | + def sample_rate(self, value): |
| 105 | + """Set sample rate - may not be supported by all backends""" |
| 106 | + try: |
| 107 | + self._set_iio_attr(self.name, "sampling_frequency", True, value) |
| 108 | + except (KeyError, OSError): |
| 109 | + # Attribute doesn't exist, silently ignore |
| 110 | + pass |
| 111 | + |
| 112 | + @property |
| 113 | + def raw(self): |
| 114 | + """Get channel raw value |
| 115 | + DAC code in the range 0-16383 (14-bit)""" |
| 116 | + return self._get_iio_attr(self.name, "raw", True) |
| 117 | + |
| 118 | + @raw.setter |
| 119 | + def raw(self, value): |
| 120 | + """Set channel raw value""" |
| 121 | + self._set_iio_attr(self.name, "raw", True, str(int(value))) |
| 122 | + |
| 123 | + @property |
| 124 | + def data_source(self): |
| 125 | + """Get/Set data source: normal, dds, or ramp""" |
| 126 | + return self._get_iio_attr_str(self.name, "data_source", True) |
| 127 | + |
| 128 | + @data_source.setter |
| 129 | + def data_source(self, value): |
| 130 | + """Set data source (normal, dds, ramp)""" |
| 131 | + self._set_iio_attr(self.name, "data_source", True, value) |
| 132 | + |
| 133 | + @property |
| 134 | + def data_source_available(self): |
| 135 | + """Available data sources""" |
| 136 | + return self._get_iio_attr_str(self.name, "data_source_available", True) |
| 137 | + |
| 138 | + # DDS Tone 0 controls |
| 139 | + @property |
| 140 | + def frequency0(self): |
| 141 | + """DDS Tone 0 frequency in Hz""" |
| 142 | + return int(self._get_iio_attr(self.name, "frequency0", True)) |
| 143 | + |
| 144 | + @frequency0.setter |
| 145 | + def frequency0(self, value): |
| 146 | + self._set_iio_attr(self.name, "frequency0", True, str(int(value))) |
| 147 | + |
| 148 | + @property |
| 149 | + def scale0(self): |
| 150 | + """DDS Tone 0 scale (0.0 to 1.0)""" |
| 151 | + return float(self._get_iio_attr_str(self.name, "scale0", True)) |
| 152 | + |
| 153 | + @scale0.setter |
| 154 | + def scale0(self, value): |
| 155 | + self._set_iio_attr(self.name, "scale0", True, str(Decimal(value).real)) |
| 156 | + |
| 157 | + @property |
| 158 | + def phase0(self): |
| 159 | + """DDS Tone 0 phase in degrees""" |
| 160 | + # Kernel returns phase in radians as a decimal string |
| 161 | + radians = float(self._get_iio_attr_str(self.name, "phase0", True)) |
| 162 | + return radians * 180.0 / 3.14159265359 # Convert to degrees |
| 163 | + |
| 164 | + @phase0.setter |
| 165 | + def phase0(self, value): |
| 166 | + """Set phase in degrees""" |
| 167 | + # Convert degrees to radians for kernel |
| 168 | + radians = float(value) * 3.14159265359 / 180.0 |
| 169 | + self._set_iio_attr(self.name, "phase0", True, f"{radians:.5f}") |
| 170 | + |
| 171 | + # DDS Tone 1 controls |
| 172 | + @property |
| 173 | + def frequency1(self): |
| 174 | + """DDS Tone 1 frequency in Hz""" |
| 175 | + return int(self._get_iio_attr(self.name, "frequency1", True)) |
| 176 | + |
| 177 | + @frequency1.setter |
| 178 | + def frequency1(self, value): |
| 179 | + self._set_iio_attr(self.name, "frequency1", True, str(int(value))) |
| 180 | + |
| 181 | + @property |
| 182 | + def scale1(self): |
| 183 | + """DDS Tone 1 scale (0.0 to 1.0)""" |
| 184 | + return float(self._get_iio_attr_str(self.name, "scale1", True)) |
| 185 | + |
| 186 | + @scale1.setter |
| 187 | + def scale1(self, value): |
| 188 | + self._set_iio_attr(self.name, "scale1", True, str(Decimal(value).real)) |
| 189 | + |
| 190 | + @property |
| 191 | + def phase1(self): |
| 192 | + """DDS Tone 1 phase in degrees""" |
| 193 | + # Kernel returns phase in radians as a decimal string |
| 194 | + radians = float(self._get_iio_attr_str(self.name, "phase1", True)) |
| 195 | + return radians * 180.0 / 3.14159265359 # Convert to degrees |
| 196 | + |
| 197 | + @phase1.setter |
| 198 | + def phase1(self, value): |
| 199 | + """Set phase in degrees""" |
| 200 | + # Convert degrees to radians for kernel |
| 201 | + radians = float(value) * 3.14159265359 / 180.0 |
| 202 | + self._set_iio_attr(self.name, "phase1", True, f"{radians:.5f}") |
0 commit comments