diff --git a/ad9361_baseband_auto_rate.c b/ad9361_baseband_auto_rate.c index 9dd0bb6..3ef59e5 100644 --- a/ad9361_baseband_auto_rate.c +++ b/ad9361_baseband_auto_rate.c @@ -53,23 +53,32 @@ static int16_t fir_64_2[] = { int ad9361_set_trx_fir_enable(struct iio_device *dev, int enable) { + const struct iio_attr *attr; int ret = iio_device_attr_write_bool(dev, "in_out_voltage_filter_fir_en", !!enable); - if (ret < 0) - ret = iio_channel_attr_write_bool(iio_device_find_channel(dev, "out", false), - "voltage_filter_fir_en", !!enable); + if (ret < 0) { + attr = iio_channel_find_attr( + iio_device_find_channel(dev, "out", false), + "voltage_filter_fir_en"); + if (attr) + ret = iio_attr_write_bool(attr, !!enable); + } return ret; } int ad9361_get_trx_fir_enable(struct iio_device *dev, int *enable) { + const struct iio_attr *chAttr; bool value; int ret = iio_device_attr_read_bool(dev, "in_out_voltage_filter_fir_en", &value); if (ret < 0) - ret = iio_channel_attr_read_bool(iio_device_find_channel(dev, "out", false), - "voltage_filter_fir_en", &value); + chAttr = iio_channel_find_attr( + iio_device_find_channel(dev, "out", false), + "voltage_filter_fir_en"); + if (chAttr) + ret = iio_attr_read_bool(chAttr, &value); if (!ret) *enable = value; @@ -80,6 +89,7 @@ int ad9361_get_trx_fir_enable(struct iio_device *dev, int *enable) int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate) { struct iio_channel *chan; + const struct iio_attr *v0_samplfreq_attr; long long current_rate; int dec, taps, ret, i, enable, len = 0; int16_t *fir; @@ -107,7 +117,11 @@ int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate) if (chan == NULL) return -ENODEV; - ret = iio_channel_attr_read_longlong(chan, "sampling_frequency", ¤t_rate); + v0_samplfreq_attr = iio_channel_find_attr(chan, "sampling_frequency"); + if (!v0_samplfreq_attr) + return -ENODEV; + + ret = iio_attr_read_longlong(v0_samplfreq_attr, ¤t_rate); if (ret < 0) return ret; @@ -117,7 +131,7 @@ int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate) if (enable) { if (current_rate <= (25000000 / 12)) - iio_channel_attr_write_longlong(chan, "sampling_frequency", 3000000); + iio_attr_write_longlong(v0_samplfreq_attr, 3000000); ret = ad9361_set_trx_fir_enable(dev, false); if (ret < 0) @@ -158,16 +172,16 @@ int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate) max = (dacrate / txrate) * 16; if (max < taps) - iio_channel_attr_write_longlong(chan, "sampling_frequency", 3000000); + iio_attr_write_longlong(v0_samplfreq_attr, 3000000); ret = ad9361_set_trx_fir_enable(dev, true); if (ret < 0) return ret; - ret = iio_channel_attr_write_longlong(chan, "sampling_frequency", rate); + ret = iio_attr_write_longlong(v0_samplfreq_attr, rate); if (ret < 0) return ret; } else { - ret = iio_channel_attr_write_longlong(chan, "sampling_frequency", rate); + ret = iio_attr_write_longlong(v0_samplfreq_attr, rate); if (ret < 0) return ret; ret = ad9361_set_trx_fir_enable(dev, true); diff --git a/ad9361_design_taps.c b/ad9361_design_taps.c index 97eba78..fe392d1 100644 --- a/ad9361_design_taps.c +++ b/ad9361_design_taps.c @@ -210,6 +210,7 @@ int apply_custom_filter(struct iio_device *dev, unsigned dec_tx, unsigned long wnom_tx, unsigned long wnom_rx) { struct iio_channel *chanTX, *chanRX; + const struct iio_attr *tx_samplfreq_attr, *attr; long long current_rate; int ret, i, enable, len = 0; char *buf; @@ -218,7 +219,11 @@ int apply_custom_filter(struct iio_device *dev, unsigned dec_tx, if (chanTX == NULL) return -ENODEV; - ret = iio_channel_attr_read_longlong(chanTX, "sampling_frequency", ¤t_rate); + tx_samplfreq_attr = iio_channel_find_attr(chanTX, "sampling_frequency"); + if (!tx_samplfreq_attr) + return -ENODEV; + + ret = iio_attr_read_longlong(tx_samplfreq_attr, ¤t_rate); if (ret < 0) return ret; @@ -228,7 +233,7 @@ int apply_custom_filter(struct iio_device *dev, unsigned dec_tx, if (enable) { if (current_rate <= (25000000 / 12)) - iio_channel_attr_write_longlong(chanTX, "sampling_frequency", 3000000); + iio_attr_write_longlong(tx_samplfreq_attr, 3000000); ret = ad9361_set_trx_fir_enable(dev, false); if (ret < 0) @@ -271,17 +276,17 @@ int apply_custom_filter(struct iio_device *dev, unsigned dec_tx, return -EINVAL; max = (dacrate / txrate) * 16; if (max < taps) - iio_channel_attr_write_longlong(chanTX, "sampling_frequency", 3000000); + iio_attr_write_longlong(tx_samplfreq_attr, 3000000); ret = ad9361_set_trx_fir_enable(dev, true); if (ret < 0) return ret; - ret = iio_channel_attr_write_longlong(chanTX, "sampling_frequency", rate); + ret = iio_attr_write_longlong(tx_samplfreq_attr, rate); if (ret < 0) return ret; } else { - ret = iio_channel_attr_write_longlong(chanTX, "sampling_frequency", rate); + ret = iio_attr_write_longlong(tx_samplfreq_attr, rate); if (ret < 0) return ret; ret = ad9361_set_trx_fir_enable(dev, true); @@ -292,12 +297,18 @@ int apply_custom_filter(struct iio_device *dev, unsigned dec_tx, chanRX = iio_device_find_channel(dev, "voltage0", false); if (chanRX == NULL) return -ENODEV; - ret = iio_channel_attr_write_longlong(chanTX, "rf_bandwidth", wnom_tx); - if (ret < 0) - return ret; - ret = iio_channel_attr_write_longlong(chanRX, "rf_bandwidth", wnom_rx); - if (ret < 0) - return ret; + attr = iio_channel_find_attr(chanTX, "rf_bandwidth"); + if (attr) { + ret = iio_attr_write_longlong(attr, wnom_tx); + if (ret < 0) + return ret; + } + attr = iio_channel_find_attr(chanRX, "rf_bandwidth"); + if (attr) { + ret = iio_attr_write_longlong(attr, wnom_rx); + if (ret < 0) + return ret; + } return 0; } diff --git a/ad9361_fmcomms5_phase_sync.c b/ad9361_fmcomms5_phase_sync.c index 1d512bd..4ebd201 100644 --- a/ad9361_fmcomms5_phase_sync.c +++ b/ad9361_fmcomms5_phase_sync.c @@ -103,6 +103,7 @@ double scale_phase_0_360(double val) void dds_tx_phase_rotation(struct iio_device *dev, double val) { + const struct iio_attr *attr; long long i, q; int d, j; @@ -120,10 +121,14 @@ void dds_tx_phase_rotation(struct iio_device *dev, double val) case 1: case 4: case 5: - iio_channel_attr_write_longlong(dds_out[d][j], "phase", i); + attr = iio_channel_find_attr(dds_out[d][j], "phase"); + if (attr) + iio_attr_write_longlong(attr, i); break; default: - iio_channel_attr_write_longlong(dds_out[d][j], "phase", q); + attr = iio_channel_find_attr(dds_out[d][j], "phase"); + if (attr) + iio_attr_write_longlong(attr, q); } } } @@ -166,6 +171,7 @@ void configure_ports(unsigned val) { unsigned lp_slave, lp_master, sw; char *rx_port, *tx_port; + const struct iio_attr *attr; // https://wiki.analog.com/resources/eval/user-guides/ad-fmcomms5-ebz/multi-chip-sync#rf_phase_difference @@ -224,21 +230,28 @@ void configure_ports(unsigned val) sw); #endif // Map ports to switch orientation - iio_channel_attr_write(iio_device_find_channel(dev_phy, "voltage0", false), - "rf_port_select", rx_port); - iio_channel_attr_write(iio_device_find_channel(dev_phy, "voltage0", true), - "rf_port_select", tx_port); - iio_channel_attr_write( - iio_device_find_channel(dev_phy_slave, "voltage0", false), - "rf_port_select", rx_port); - iio_channel_attr_write( - iio_device_find_channel(dev_phy_slave, "voltage0", true), - "rf_port_select", tx_port); + attr = iio_channel_find_attr(iio_device_find_channel(dev_phy, "voltage0", + false), "rf_port_select"); + if (attr) + iio_attr_write(attr, rx_port); + attr = iio_channel_find_attr(iio_device_find_channel(dev_phy, "voltage0", + true), "rf_port_select"); + if (attr) + iio_attr_write(attr, tx_port); + attr = iio_channel_find_attr(iio_device_find_channel(dev_phy_slave, + "voltage0", false), "rf_port_select"); + if (attr) + iio_attr_write(attr, rx_port); + attr = iio_channel_find_attr(iio_device_find_channel(dev_phy_slave, + "voltage0", true), "rf_port_select"); + if (attr) + iio_attr_write(attr, rx_port); } int trx_phase_rotation(struct iio_device *dev, double val) { struct iio_channel *out0, *out1; + const struct iio_attr *attr; double phase, vcos, vsin; unsigned offset; int ret; @@ -271,14 +284,26 @@ int trx_phase_rotation(struct iio_device *dev, double val) return -ENODEV; if (out1 && out0) { - ret = iio_channel_attr_write_double(out0, "calibscale", (double)vcos); - CHECK(ret); - ret = iio_channel_attr_write_double(out0, "calibphase", (double)(-1.0 * vsin)); - CHECK(ret); - ret = iio_channel_attr_write_double(out1, "calibscale", (double)vcos); - CHECK(ret); - ret = iio_channel_attr_write_double(out1, "calibphase", (double)vsin); - CHECK(ret); + attr = iio_channel_find_attr(out0, "calibscale"); + if (attr) { + iio_attr_write_double(attr, (double)vcos); + CHECK(ret); + } + attr = iio_channel_find_attr(out0, "calibphase"); + if (attr) { + iio_attr_write_double(attr, (double)(-1.0 * vsin)); + CHECK(ret); + } + attr = iio_channel_find_attr(out1, "calibscale"); + if (attr) { + iio_attr_write_double(attr, (double)vcos); + CHECK(ret); + } + attr = iio_channel_find_attr(out1, "calibphase"); + if (attr) { + iio_attr_write_double(attr, (double)vsin); + CHECK(ret); + } } } return 0; @@ -431,21 +456,29 @@ int calibrate_chain(struct iio_device *dev, double scale, double *phase) int quad_tracking(bool enable) { + const struct iio_attr *attr; struct iio_channel *chn = iio_device_find_channel(dev_phy, "voltage0", enable); if (chn == NULL) return -ENODEV; - iio_channel_attr_write(chn, "quadrature_tracking_en", "0"); + attr = iio_channel_find_attr(chn, "quadrature_tracking_en"); + if (attr) + iio_attr_write_string(attr, "0"); + chn = iio_device_find_channel(dev_phy_slave, "voltage0", enable); if (chn == NULL) return -ENODEV; - iio_channel_attr_write(chn, "quadrature_tracking_en", "0"); + attr = iio_channel_find_attr(chn, "quadrature_tracking_en"); + if (attr) + iio_attr_write_string(attr, "0"); + return 0; } int configure_transceiver(struct iio_device *dev, long long bw_hz, long long fs_hz, long long lo_hz) { + const struct iio_attr *attr; int ret = 0; // Set up channels struct iio_channel *chnRX1; @@ -457,10 +490,16 @@ int configure_transceiver(struct iio_device *dev, long long bw_hz, chnTX1 = iio_device_find_channel(dev, "altvoltage1", true); if (!(chnRX1 && chnTX1)) return -ENODEV; - ret = iio_channel_attr_write_longlong(chnRX1, "frequency", lo_hz); - CHECK(ret); - ret = iio_channel_attr_write_longlong(chnTX1, "frequency", lo_hz); - CHECK(ret); + attr = iio_channel_find_attr(chnRX1, "frequency"); + if (attr) { + ret = iio_attr_write_longlong(attr, lo_hz); + CHECK(ret); + } + attr = iio_channel_find_attr(chnTX1, "frequency"); + if (attr) { + ret = iio_attr_write_longlong(chnTX1, lo_hz); + CHECK(ret); + } // Set up gains to know good values chnRX1 = iio_device_find_channel(dev, "voltage0", false); chnTX1 = iio_device_find_channel(dev, "voltage0", true); @@ -468,19 +507,36 @@ int configure_transceiver(struct iio_device *dev, long long bw_hz, chnTX2 = iio_device_find_channel(dev, "voltage1", true); if (!(chnRX1 && chnTX1 && chnRX2 && chnTX2)) return -ENODEV; - ret = iio_channel_attr_write(chnRX1, "gain_control_mode", "manual"); - CHECK(ret); - ret = iio_channel_attr_write(chnRX2, "gain_control_mode", "manual"); - CHECK(ret); - ret = iio_channel_attr_write_double(chnRX1, "hardwaregain", 32.0); - CHECK(ret); - - ret = iio_channel_attr_write_double(chnRX2, "hardwaregain", 32.0); - CHECK(ret); - ret = iio_channel_attr_write_double(chnTX1, "hardwaregain", -20); - CHECK(ret); - ret = iio_channel_attr_write_double(chnTX2, "hardwaregain", -20); - CHECK(ret); + attr = iio_channel_find_attr(chnRX1, "gain_control_mode"); + if (attr) { + ret = iio_attr_write_string(attr, "manual"); + CHECK(ret); + } + attr = iio_channel_find_attr(chnRX2, "gain_control_mode"); + if (attr) { + ret = iio_attr_write_string(attr, "manual"); + CHECK(ret); + } + attr = iio_channel_find_attr(chnRX1, "hardwaregain"); + if (attr) { + ret = iio_attr_write_double(attr, 32.0); + CHECK(ret); + } + attr = iio_channel_find_attr(chnRX2, "hardwaregain"); + if (attr) { + ret = iio_attr_write_double(attr, 32.0); + CHECK(ret); + } + attr = iio_channel_find_attr(chnTX1, "hardwaregain"); + if (attr) { + ret = iio_attr_write_double(attr, -20); + CHECK(ret); + } + attr = iio_channel_find_attr(chnTX2, "hardwaregain"); + if (attr) { + ret = iio_attr_write_double(attr, -20); + CHECK(ret); + } return 0; } @@ -489,11 +545,16 @@ int configure_dds(double fs, double scale) { long long freq = (long long)fs * 0.01; int i, j, ret = 0; + const struct iio_attr *attr; for (i = 0; i < 2; i++) { for (j = 0; j < 8; j++) { - ret |= iio_channel_attr_write_longlong(dds_out[i][j], "frequency", freq); - ret |= iio_channel_attr_write_double(dds_out[i][j], "scale", scale); + attr = iio_channel_find_attr(dds_out[i][j], "frequency"); + if (attr) + ret |= iio_attr_write_longlong(attr, freq); + attr = iio_channel_find_attr(dds_out[i][j], "scale"); + if (attr) + ret |= iio_attr_write_double(attr, scale); } dds_tx_phase_rotation(i ? dev_tx_slave : dev_tx, 0.0); @@ -609,6 +670,7 @@ int ad9361_fmcomms5_phase_sync(struct iio_context *ctx, long long lo) { struct iio_channel *chan; struct iio_device *dev; + const struct iio_attr *attr; long long sample_rate; int ret; @@ -619,8 +681,11 @@ int ad9361_fmcomms5_phase_sync(struct iio_context *ctx, long long lo) chan = iio_device_find_channel(dev, "voltage0", true); if (chan == NULL) return -ENODEV; - ret = iio_channel_attr_read_longlong(chan, "sampling_frequency", &sample_rate); - CHECK(ret); + attr = iio_channel_find_attr(chan, "sampling_frequency"); + if (attr) { + ret = iio_attr_read_longlong(attr, &sample_rate); + CHECK(ret); + } ret = phase_sync(ctx, sample_rate, lo); diff --git a/ad9361_multichip_sync.c b/ad9361_multichip_sync.c index 268d269..f977b26 100644 --- a/ad9361_multichip_sync.c +++ b/ad9361_multichip_sync.c @@ -45,6 +45,7 @@ int ad9361_multichip_sync(struct iio_device *master, struct iio_device **slaves, char ensm_mode[MAX_AD9361_SYNC_DEVS][20]; unsigned int i, step; bool mcs_is_debug_attr = !iio_device_find_attr(master, "multichip_sync"); + const struct iio_attr *attr; if (num_slaves >= MAX_AD9361_SYNC_DEVS || num_slaves < 1) return -EINVAL; @@ -54,18 +55,23 @@ int ad9361_multichip_sync(struct iio_device *master, struct iio_device **slaves, long long tx_sample_master_freq, tx_sample_slave_freq; tx_sample_master = iio_device_find_channel(master, "voltage0", true); - iio_channel_attr_read_longlong(tx_sample_master, "sampling_frequency", &tx_sample_master_freq); + attr = iio_channel_find_attr(tx_sample_master, "sampling_frequency"); + if (attr) + iio_attr_read_longlong(attr, &tx_sample_master_freq); for (i = 0; i < num_slaves; i++) { tx_sample_slave = iio_device_find_channel(slaves[i], "voltage0", true); if (tx_sample_slave == NULL) return -ENODEV; - - iio_channel_attr_read_longlong(tx_sample_slave, "sampling_frequency", &tx_sample_slave_freq); + attr = iio_channel_find_attr(tx_sample_slave, "sampling_frequency"); + if (attr) + iio_attr_read_longlong(attr, &tx_sample_slave_freq); if (tx_sample_master_freq != tx_sample_slave_freq) { fprintf(stderr, "tx_sample_master_freq != tx_sample_slave_freq\nUpdating...\n"); - iio_channel_attr_write_longlong(tx_sample_slave, "sampling_frequency", tx_sample_master_freq); + attr = iio_channel_find_attr(tx_sample_slave, "sampling_frequency"); + if (attr) + iio_attr_write_longlong(attr, tx_sample_master_freq); } } }