|
| 1 | +# Copyright (C) 2025 Analog Devices, Inc. |
| 2 | +# |
| 3 | +# SPDX short identifier: ADIBSD |
| 4 | +# Copyright (C) 2025 Analog Devices, Inc. |
| 5 | +# |
| 6 | +# All rights reserved. |
| 7 | +# |
| 8 | +# Redistribution and use in source and binary forms, with or without modification, |
| 9 | +# are permitted provided that the following conditions are met: |
| 10 | +# - Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# - Redistributions in binary form must reproduce the above copyright |
| 13 | +# notice, this list of conditions and the following disclaimer in |
| 14 | +# the documentation and/or other materials provided with the |
| 15 | +# distribution. |
| 16 | +# - Neither the name of Analog Devices, Inc. nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived |
| 18 | +# from this software without specific prior written permission. |
| 19 | +# - The use of this software may or may not infringe the patent rights |
| 20 | +# of one or more patent holders. This license does not release you |
| 21 | +# from the requirement that you obtain separate licenses from these |
| 22 | +# patent holders to use this software. |
| 23 | +# - Use of the software either in source or binary form, must be run |
| 24 | +# on or directly connected to an Analog Devices Inc. component. |
| 25 | +# |
| 26 | +# THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 27 | +# INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A |
| 28 | +# PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | +# |
| 30 | +# IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY |
| 32 | +# RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 33 | +# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 34 | +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 35 | +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | + |
| 37 | +from decimal import Decimal |
| 38 | + |
| 39 | +from adi.attribute import attribute |
| 40 | +from adi.context_manager import context_manager |
| 41 | +from adi.rx_tx import tx |
| 42 | + |
| 43 | + |
| 44 | +class ad353xr(tx, context_manager): |
| 45 | + """ AD353xr DAC """ |
| 46 | + |
| 47 | + _complex_data = False |
| 48 | + channel = [] # type: ignore |
| 49 | + _device_name = "" |
| 50 | + |
| 51 | + def __init__(self, uri="", device_name=""): |
| 52 | + """Constructor for AD353xr class.""" |
| 53 | + context_manager.__init__(self, uri, self._device_name) |
| 54 | + |
| 55 | + compatible_parts = ["ad3530r"] |
| 56 | + |
| 57 | + self._ctrl = None |
| 58 | + |
| 59 | + if not device_name: |
| 60 | + device_name = compatible_parts[0] |
| 61 | + else: |
| 62 | + if device_name not in compatible_parts: |
| 63 | + raise Exception( |
| 64 | + f"Not a compatible device: {device_name}. Supported device names " |
| 65 | + f"are: {','.join(compatible_parts)}" |
| 66 | + ) |
| 67 | + |
| 68 | + # Select the device matching device_name as working device |
| 69 | + for device in self._ctx.devices: |
| 70 | + if device.name == device_name: |
| 71 | + self._ctrl = device |
| 72 | + self._txdac = device |
| 73 | + break |
| 74 | + |
| 75 | + if not self._ctrl: |
| 76 | + raise Exception("Error in selecting matching device") |
| 77 | + |
| 78 | + if not self._txdac: |
| 79 | + raise Exception("Error in selecting matching device") |
| 80 | + |
| 81 | + self.output_bits = [] |
| 82 | + for ch in self._ctrl.channels: |
| 83 | + name = ch.id |
| 84 | + self.output_bits.append(ch.data_format.bits) |
| 85 | + self._tx_channel_names.append(name) |
| 86 | + self.channel.append(self._channel(self._ctrl, name)) |
| 87 | + |
| 88 | + tx.__init__(self) |
| 89 | + |
| 90 | + ### Add device attributes here ### |
| 91 | + |
| 92 | + @property |
| 93 | + def sampling_frequency(self): |
| 94 | + """AD353xr sampling frequency config""" |
| 95 | + return self._get_iio_dev_attr_str("sampling_frequency") |
| 96 | + |
| 97 | + @sampling_frequency.setter |
| 98 | + def sampling_frequency(self, value): |
| 99 | + self._set_iio_dev_attr_str("sampling_frequency", value) |
| 100 | + |
| 101 | + @property |
| 102 | + def all_ch_operating_mode_avail(self): |
| 103 | + """AD353xr all channels operating mode available""" |
| 104 | + return self._get_iio_dev_attr_str("all_ch_operating_mode_available") |
| 105 | + |
| 106 | + @property |
| 107 | + def all_ch_operating_mode(self): |
| 108 | + """AD353xr all channels operating mode config""" |
| 109 | + return self._get_iio_dev_attr_str("all_ch_operating_mode") |
| 110 | + |
| 111 | + @all_ch_operating_mode.setter |
| 112 | + def all_ch_operating_mode(self, value): |
| 113 | + if value in self.all_ch_operating_mode_avail: |
| 114 | + self._set_iio_dev_attr_str("all_ch_operating_mode", value) |
| 115 | + else: |
| 116 | + raise ValueError( |
| 117 | + "Error: Operating mode not supported \nUse one of: " |
| 118 | + + str(self.all_ch_operating_mode_avail) |
| 119 | + ) |
| 120 | + |
| 121 | + @property |
| 122 | + def all_ch_input_registers(self): |
| 123 | + """AD353xr all input registers config""" |
| 124 | + return self._get_iio_dev_attr_str("all_ch_input_registers") |
| 125 | + |
| 126 | + @all_ch_input_registers.setter |
| 127 | + def all_input_registers(self, value): |
| 128 | + self._set_iio_dev_attr_str("all_ch_input_registers", value) |
| 129 | + |
| 130 | + @property |
| 131 | + def all_ch_raw(self): |
| 132 | + """AD353xr all dac registers config""" |
| 133 | + return self._get_iio_dev_attr_str("all_ch_raw") |
| 134 | + |
| 135 | + @all_ch_raw.setter |
| 136 | + def all_ch_raw(self, value): |
| 137 | + self._set_iio_dev_attr_str("all_ch_raw", value) |
| 138 | + |
| 139 | + @property |
| 140 | + def reference_select_available(self): |
| 141 | + """AD353xr reference voltage available""" |
| 142 | + return self._get_iio_dev_attr_str("reference_select_available") |
| 143 | + |
| 144 | + @property |
| 145 | + def reference_select(self): |
| 146 | + """AD353xr reference voltage config""" |
| 147 | + return self._get_iio_dev_attr_str("reference_select") |
| 148 | + |
| 149 | + @reference_select.setter |
| 150 | + def reference_select(self, value): |
| 151 | + if value in self.reference_select_available: |
| 152 | + self._set_iio_dev_attr_str("reference_select", value) |
| 153 | + else: |
| 154 | + raise ValueError( |
| 155 | + "Error: Reference select not supported \nUse one of: " |
| 156 | + + str(self.reference_select_available) |
| 157 | + ) |
| 158 | + |
| 159 | + @property |
| 160 | + def sw_ldac_trigger_avail(self): |
| 161 | + """AD353xr sw_ldac_trigger available""" |
| 162 | + return self._get_iio_dev_attr_str("sw_ldac_trigger_available") |
| 163 | + |
| 164 | + @property |
| 165 | + def sw_ldac_trigger(self): |
| 166 | + """AD353xr software ldac trigger config""" |
| 167 | + return self._get_iio_dev_attr_str("sw_ldac_trigger") |
| 168 | + |
| 169 | + @sw_ldac_trigger.setter |
| 170 | + def sw_ldac_trigger(self, value): |
| 171 | + if value in self.sw_ldac_trigger_avail: |
| 172 | + self._set_iio_dev_attr_str("sw_ldac_trigger", value) |
| 173 | + else: |
| 174 | + raise ValueError( |
| 175 | + "Error: Trigger value not supported \nUse one of: " |
| 176 | + + str(self.sw_ldac_trigger_avail) |
| 177 | + ) |
| 178 | + |
| 179 | + @property |
| 180 | + def hw_ldac_trigger_avail(self): |
| 181 | + """AD353xr hw_ldac_trigger available""" |
| 182 | + return self._get_iio_dev_attr_str("hw_ldac_trigger_available") |
| 183 | + |
| 184 | + @property |
| 185 | + def hw_ldac_trigger(self): |
| 186 | + """AD353xr hardware ldac trigger config""" |
| 187 | + return self._get_iio_dev_attr_str("hw_ldac_trigger") |
| 188 | + |
| 189 | + @hw_ldac_trigger.setter |
| 190 | + def hw_ldac_trigger(self, value): |
| 191 | + if value in self.hw_ldac_trigger_avail: |
| 192 | + self._set_iio_dev_attr_str("hw_ldac_trigger", value) |
| 193 | + else: |
| 194 | + raise ValueError( |
| 195 | + "Error: Trigger value not supported \nUse one of: " |
| 196 | + + str(self.hw_ldac_trigger_avail) |
| 197 | + ) |
| 198 | + |
| 199 | + @property |
| 200 | + def range_avail(self): |
| 201 | + """AD353xr range available""" |
| 202 | + return self._get_iio_dev_attr_str("range_available") |
| 203 | + |
| 204 | + @property |
| 205 | + def range(self): |
| 206 | + """AD353xr range config""" |
| 207 | + return self._get_iio_dev_attr_str("range") |
| 208 | + |
| 209 | + @range.setter |
| 210 | + def range(self, value): |
| 211 | + if value in self.range_avail: |
| 212 | + self._set_iio_dev_attr_str("range", value) |
| 213 | + else: |
| 214 | + raise ValueError( |
| 215 | + "Error: Range option not supported \nUse one of: " |
| 216 | + + str(self.range_avail) |
| 217 | + ) |
| 218 | + |
| 219 | + @property |
| 220 | + def mux_out_select_avail(self): |
| 221 | + """AD353xr mux_out_select available""" |
| 222 | + return self._get_iio_dev_attr_str("mux_out_select_available") |
| 223 | + |
| 224 | + @property |
| 225 | + def mux_out_select(self): |
| 226 | + """AD353xr mux out select""" |
| 227 | + return self._get_iio_dev_attr_str("mux_out_select") |
| 228 | + |
| 229 | + @mux_out_select.setter |
| 230 | + def mux_out_select(self, value): |
| 231 | + if value in self.mux_out_select_avail: |
| 232 | + self._set_iio_dev_attr_str("mux_out_select", value) |
| 233 | + else: |
| 234 | + raise ValueError( |
| 235 | + "Error: Mux output option not supported \nUse one of: " |
| 236 | + + str(self.mux_out_select_avail) |
| 237 | + ) |
| 238 | + |
| 239 | + ############################################################################ |
| 240 | + |
| 241 | + class _channel(attribute): |
| 242 | + """AD353xr channel""" |
| 243 | + |
| 244 | + def __init__(self, ctrl, channel_name): |
| 245 | + self.name = channel_name |
| 246 | + self._ctrl = ctrl |
| 247 | + |
| 248 | + ### Add channel attributes here ### |
| 249 | + @property |
| 250 | + def input_register(self): |
| 251 | + """AD353xr channel input register value""" |
| 252 | + return self._get_iio_attr(self.name, "input_register", True) |
| 253 | + |
| 254 | + @input_register.setter |
| 255 | + def input_register(self, value): |
| 256 | + self._set_iio_attr(self.name, "input_register", True, str(int(value))) |
| 257 | + |
| 258 | + @property |
| 259 | + def raw(self): |
| 260 | + """AD353xr channel raw value""" |
| 261 | + return self._get_iio_attr(self.name, "raw", True) |
| 262 | + |
| 263 | + @raw.setter |
| 264 | + def raw(self, value): |
| 265 | + self._set_iio_attr(self.name, "raw", True, str(int(value))) |
| 266 | + |
| 267 | + @property |
| 268 | + def offset(self): |
| 269 | + """AD353xr channel offset""" |
| 270 | + return self._get_iio_attr(self.name, "offset", True) |
| 271 | + |
| 272 | + @offset.setter |
| 273 | + def offset(self, value): |
| 274 | + self._set_iio_attr(self.name, "offset", True, str(Decimal(value).real)) |
| 275 | + |
| 276 | + @property |
| 277 | + def scale(self): |
| 278 | + """AD353xr channel scale""" |
| 279 | + return self._get_iio_attr(self.name, "scale", True) |
| 280 | + |
| 281 | + @scale.setter |
| 282 | + def scale(self, value): |
| 283 | + self._set_iio_attr(self.name, "scale", True, str(Decimal(value).real)) |
| 284 | + |
| 285 | + @property |
| 286 | + def operating_mode_avail(self): |
| 287 | + """AD353xr channel operating mode settings""" |
| 288 | + return self._get_iio_attr_str(self.name, "operating_mode_available", True) |
| 289 | + |
| 290 | + @property |
| 291 | + def operating_mode(self): |
| 292 | + """AD353xr channel operating mode""" |
| 293 | + return self._get_iio_attr_str(self.name, "operating_mode", True) |
| 294 | + |
| 295 | + @operating_mode.setter |
| 296 | + def operating_mode(self, value): |
| 297 | + if value in self.operating_mode_avail: |
| 298 | + self._set_iio_attr(self.name, "operating_mode", True, value) |
| 299 | + else: |
| 300 | + raise ValueError( |
| 301 | + "Error: Operating mode not supported \nUse one of: " |
| 302 | + + str(self.operating_mode_avail) |
| 303 | + ) |
| 304 | + |
| 305 | + ##################################################################### |
0 commit comments