From bc02a4c1abf329565f139180d8533f1e0eafcbaa Mon Sep 17 00:00:00 2001 From: Trecia Agoylo Date: Wed, 23 Aug 2023 11:08:44 +0800 Subject: [PATCH] ad4630: updates on class, test, examples Signed-off-by: Trecia Agoylo --- adi/ad4630.py | 92 ++++++------------- examples/ad4630/ad4630_example_all_mode.py | 27 +++--- examples/ad4630/ad4630_example_simple_plot.py | 11 ++- test/test_ad4630.py | 34 ++++++- 4 files changed, 83 insertions(+), 81 deletions(-) diff --git a/adi/ad4630.py b/adi/ad4630.py index e1e60811a..cd9039864 100644 --- a/adi/ad4630.py +++ b/adi/ad4630.py @@ -26,7 +26,7 @@ class ad4630(rx, context_manager, attribute): """ AD4630 is low power 24-bit precision SAR ADC """ _complex_data = False - _data_type = "voltage" + _data_type = np.uint32 _device_name = "" _rx_channel_names = [] @@ -54,17 +54,15 @@ def __init__(self, uri="", device_name="ad4630-24"): for ch in self._ctrl.channels: self.output_bits.append(ch.data_format.bits) self._rx_channel_names.append(ch.id) - if "differential" in ch.name: - _channels.append((ch.id, self._diff_channel(self._ctrl, ch.id))) - if "0" in ch.name: - self.chan0 = self._diff_channel(self._ctrl, ch.name) - if "1" in ch.name: - self.chan1 = self._diff_channel(self._ctrl, ch.name) + _channels.append((ch.id, self._channel(self._ctrl, ch.id))) + if "0" in ch.id: + self.chan0 = self._channel(self._ctrl, ch.id) + if "1" in ch.id: + self.chan1 = self._channel(self._ctrl, ch.id) rx.__init__(self) def rx(self): - if not self._rx__rxbuf: self._rx_init_channels() self._rx__rxbuf.refill() @@ -108,45 +106,13 @@ def output_data_mode(self): @property def sample_rate(self): - """Get/Set the sampling frequency.""" + """Get the sampling frequency.""" return self._get_iio_dev_attr("sampling_frequency") @sample_rate.setter def sample_rate(self, rate): - """Get/Set the sampling frequency.""" - if str(rate) in str(self.sample_rate_avail): - self._set_iio_dev_attr("sampling_frequency", str(rate)) - else: - raise ValueError( - "Error: Sample rate not supported \nUse one of: " - + str(self.sample_rate_avail) - ) - - @property - def sample_rate_avail(self): - """Get list of all the sampling frequency available.""" - return self._get_iio_dev_attr("sampling_frequency_available") - - @property - def operating_mode_avail(self): - """Get list of all the operating mode available.""" - return self._get_iio_dev_attr_str("operating_mode_available") - - @property - def operating_mode(self): - """Get/Set the operating mode.""" - return self._get_iio_dev_attr_str("operating_mode") - - @operating_mode.setter - def operating_mode(self, mode): - """Get/Set the operating mode.""" - if mode in self.operating_mode_avail: - self._set_iio_dev_attr_str("operating_mode", mode) - else: - raise ValueError( - "Error: Operating mode not supported \nUse one of: " - + str(self.operating_mode_avail) - ) + """Set the sampling frequency.""" + self._set_iio_dev_attr("sampling_frequency", str(rate)) @property def sample_averaging_avail(self): @@ -155,12 +121,12 @@ def sample_averaging_avail(self): @property def sample_averaging(self): - """Get/Set the sample averaging. Only available in 30bit averaged mode.""" + """Get the sample averaging. Only available in 30bit averaged mode.""" return self._get_iio_dev_attr_str("sample_averaging") @sample_averaging.setter def sample_averaging(self, n_sample): - """Get/Set the sample averaging. Only available in 30bit averaged mode.""" + """Set the sample averaging. Only available in 30bit averaged mode.""" if str(self.sample_averaging) != "OFF": if str(n_sample) in str(self.sample_averaging_avail): self._set_iio_dev_attr("sample_averaging", str(n_sample)) @@ -172,7 +138,7 @@ def sample_averaging(self, n_sample): else: raise Exception("Sample Averaging only available in 30bit averaged mode.") - class _diff_channel(attribute): + class _channel(attribute): """AD4x30 differential channel.""" def __init__(self, ctrl, channel_name): @@ -180,21 +146,23 @@ def __init__(self, ctrl, channel_name): self._ctrl = ctrl @property - def hw_gain(self): - """Get/Set the hardwaregain of differential channel.""" - return self._get_iio_attr(self.name, "hardwaregain", False) - - @hw_gain.setter - def hw_gain(self, gain): - """Get/Set the hardwaregain of differential channel.""" - self._set_iio_attr(self.name, "hardwaregain", False, int(gain)) + def calibbias(self): + """Get calibration bias/offset value.""" + return self._get_iio_attr(self.name, "calibbias", False, self._ctrl) + + @calibbias.setter + def calibbias(self, calibbias): + """Set calibration bias/offset value.""" + self._set_iio_attr( + self.name, "calibbias", False, int(calibbias), self._ctrl + ) @property - def offset(self): - """Get/Set the offset of differential channel.""" - return self._get_iio_attr(self.name, "offset", False, self._ctrl) - - @offset.setter - def offset(self, offset): - """Get/Set the offset of differential channel.""" - self._set_iio_attr(self.name, "offset", False, int(offset), self._ctrl) + def calibscale(self): + """Get calibration scale value.""" + return self._get_iio_attr(self.name, "calibscale", False, self._ctrl) + + @calibscale.setter + def calibscale(self, calibscale): + """Set calibration scale value.""" + self._set_iio_attr(self.name, "calibscale", False, calibscale, self._ctrl) diff --git a/examples/ad4630/ad4630_example_all_mode.py b/examples/ad4630/ad4630_example_all_mode.py index 41c6b619f..26804d4c4 100644 --- a/examples/ad4630/ad4630_example_all_mode.py +++ b/examples/ad4630/ad4630_example_all_mode.py @@ -10,7 +10,7 @@ import numpy as np import sin_params as sp -device_name = "ad4030-24" +device_name = "ad4630-24" fs = 2000000 # Sampling Frequency N = 65536 # Length of rx buffer @@ -18,14 +18,11 @@ def main(): """ Instantiate the device and set th parameters.""" adc = adi.ad4630( - uri="ip:169.254.92.202", device_name=device_name - ) # To connect via ip address 169.254.92.202 + uri="ip:192.168.10.171", device_name=device_name + ) # To connect via ip address adc.rx_buffer_size = N adc.sample_rate = fs - """To switch the device b/w low_power_mode and normal_operating_mode.""" - adc.operating_mode = "normal_operating_mode" - """sample_averaging is only supported by 30bit mode. and in this mode it cannot be OFF.""" if adc.output_data_mode == "30bit_avg": adc.sample_averaging = 16 @@ -34,11 +31,11 @@ def main(): print(adc.output_data_mode) """ Differential Channel attributes""" - adc.chan0.hw_gain = 2 - adc.chan0.offset = 2 + adc.chan0.calibscale = 1 + adc.chan0.calibbias = 2 if device_name == "ad4630-24": - adc.chan1.hw_gain = 2 - adc.chan1.offset = 2 + adc.chan1.calibscale = 1 + adc.chan1.calibbias = 2 data = adc.rx() # Receive the data adc.rx_destroy_buffer() # Destroy the remaining data in buffer @@ -110,11 +107,11 @@ def analysis(bits, op_data): print("Binwidth = ", bin_width) print("SNR (dB) = ", snr) print("SNR of Adjacent chan (dB) =", snr_adj) - print("thd = " + str(thd) + " calculated thd = " + thd_calc) - print("sfdr = " + str(sfdr) + " adjacent chan sfdr = " + sfdr_adj) - print("ENOB = " + str(enob) + " calculated ENOB = " + enob_calc) - print("sinad = " + str(sinad) + " calculated sinad = " + sinad_calc) - print("Max code = " + str(max_code) + "Min code = " + str(min_code)) + print("thd = " + str(thd) + " calculated thd = " + str(thd_calc)) + print("sfdr = " + str(sfdr) + " adjacent chan sfdr = " + str(sfdr_adj)) + print("ENOB = " + str(enob) + " calculated ENOB = " + str(enob_calc)) + print("sinad = " + str(sinad) + " calculated sinad = " + str(sinad_calc)) + print("Max code = " + str(max_code) + " Min code = " + str(min_code)) print("Lent of each captured array =", len(op_data)) diff --git a/examples/ad4630/ad4630_example_simple_plot.py b/examples/ad4630/ad4630_example_simple_plot.py index e6673af6f..2571b2c22 100644 --- a/examples/ad4630/ad4630_example_simple_plot.py +++ b/examples/ad4630/ad4630_example_simple_plot.py @@ -3,13 +3,20 @@ # SPDX short identifier: ADIBSD +import sys + import adi import matplotlib.pyplot as plt import numpy as np -device_name = "ad4630-23" +# Optionally pass URI as command line argument, +# else use default context manager search +my_uri = sys.argv[1] if len(sys.argv) >= 2 else None +print("uri: " + str(my_uri)) + +device_name = "ad4630-24" -adc = ad4630(uri="ip:192.168.0.130", device_name=device_name) +adc = adi.ad4630(uri=my_uri, device_name=device_name) adc.rx_buffer_size = 500 adc.sample_rate = 2000000 diff --git a/test/test_ad4630.py b/test/test_ad4630.py index 04323ebf1..35ab3ba37 100644 --- a/test/test_ad4630.py +++ b/test/test_ad4630.py @@ -3,10 +3,11 @@ hardware = ["ad4030-24", "ad4630-24"] classname = "adi.ad4630" + ######################################### @pytest.mark.iio_hardware(hardware) @pytest.mark.parametrize("classname", [(classname)]) -@pytest.mark.parametrize("channel", [0]) +@pytest.mark.parametrize("channel", [[0, 1]]) def test_ad4630_rx_data(test_dma_rx, iio_uri, classname, channel): test_dma_rx(iio_uri, classname, channel) @@ -24,4 +25,33 @@ def test_ad4630_rx_data(test_dma_rx, iio_uri, classname, channel): ], ) def test_ad4630_attr(test_attribute_multiple_values, iio_uri, classname, attr, val): - test_attribute_multiple_values(iio_uri, classname, attr, val, 0) + test_attribute_multiple_values(iio_uri, classname, attr, val, 1) + + +######################################### +@pytest.mark.iio_hardware(hardware) +@pytest.mark.parametrize("classname", [(classname)]) +@pytest.mark.parametrize( + "attr, start, stop, step, tol, repeats, sub_channel", + [ + ("calibbias", 0, 2, 1, 0, 2, "chan0"), + ("calibbias", 0, 2, 1, 0, 2, "chan1"), + ("calibscale", 0, 1, 0.5, 0, 2, "chan0"), + ("calibscale", 0, 1, 0.5, 0, 2, "chan1"), + ], +) +def test_ad4630_channel_attrs( + test_attribute_single_value, + iio_uri, + classname, + attr, + start, + stop, + step, + tol, + repeats, + sub_channel, +): + test_attribute_single_value( + iio_uri, classname, attr, start, stop, step, tol, repeats, sub_channel + )