|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Analog Devices, Inc. |
| 3 | + * Author: Adrian Stanea <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + * */ |
| 19 | + |
| 20 | +using System; |
| 21 | +using System.Globalization; |
| 22 | +using System.Reflection; |
| 23 | +using iio; |
| 24 | + |
| 25 | +[assembly: AssemblyTitle("Pluto_Example")] |
| 26 | +[assembly: AssemblyVersion("1.0.0.0")] |
| 27 | +[assembly: System.Runtime.InteropServices.ComVisible(false)] |
| 28 | +[assembly: CLSCompliant(true)] |
| 29 | +namespace IIOCSharp |
| 30 | +{ |
| 31 | + static class PlutoExample |
| 32 | + { |
| 33 | + const int INT16_BYTE_STEP = 2; |
| 34 | + const string URI = "ip:192.168.2.1"; |
| 35 | + const string TXLO = "1000000000"; |
| 36 | + const string TXBW = "5000000"; |
| 37 | + const string TXFS = "3000000"; |
| 38 | + const string RXLO = TXLO; |
| 39 | + const string RXBW = TXBW; |
| 40 | + const string RXFS = TXFS; |
| 41 | + const string GAIN_CTRL_MODE = "slow_attack"; |
| 42 | + const string HW_GAIN = "-30"; |
| 43 | + |
| 44 | + const string CHN_VOLTAGE0 = "voltage0"; |
| 45 | + const string CHN_VOLTAGE1 = "voltage1"; |
| 46 | + |
| 47 | + static void Main(string[] args) |
| 48 | + { |
| 49 | + Context ctx = new Context(URI); |
| 50 | + if (ctx == null) |
| 51 | + { |
| 52 | + Console.WriteLine("Unable to create IIO context"); |
| 53 | + return; |
| 54 | + } |
| 55 | + // get reference to devices |
| 56 | + var ctrl = ctx.find_device("ad9361-phy"); |
| 57 | + var tx_dac = ctx.find_device("cf-ad9361-dds-core-lpc"); |
| 58 | + var rx_adc = ctx.find_device("cf-ad9361-lpc"); |
| 59 | + |
| 60 | + rx_config(ctrl, RXLO, RXBW, RXFS, GAIN_CTRL_MODE); |
| 61 | + tx_config(ctrl, TXLO, TXBW, TXFS, HW_GAIN); |
| 62 | + |
| 63 | + // Enable all IQ channels |
| 64 | + enable_tx(tx_dac); |
| 65 | + enable_rx(rx_adc); |
| 66 | + |
| 67 | + // Cyclic buffer to output perdiodic waveforms |
| 68 | + uint samples_per_channel = Convert.ToUInt16(Math.Pow(2, 15)); |
| 69 | + |
| 70 | + var tx_buf = new IOBuffer(tx_dac, samples_per_channel, true); |
| 71 | + var rx_buff = new IOBuffer(rx_adc, samples_per_channel, false); |
| 72 | + |
| 73 | + byte[] iqBytes = generate_sine(Convert.ToInt32(samples_per_channel), RXFS); |
| 74 | + |
| 75 | + // Send data to TX buffer |
| 76 | + tx_buf.fill(iqBytes); |
| 77 | + tx_buf.push(); |
| 78 | + |
| 79 | + // read data on rx buffer |
| 80 | + const int num_readings = 10; |
| 81 | + (List<int> reals, List<int> imags) = read_rx_data(rx_buff, num_readings, Convert.ToInt32(samples_per_channel)); |
| 82 | + } |
| 83 | + |
| 84 | + static void rx_config(Device device, string rx_lo, string rx_bw, string rx_fs, string gain_ctrl_mode) |
| 85 | + { |
| 86 | + var rx_chn = device.find_channel(CHN_VOLTAGE0, false); |
| 87 | + |
| 88 | + device.find_channel("RX_LO", true).find_attribute("frequency").write(rx_lo); |
| 89 | + rx_chn.find_attribute("rf_bandwidth").write(rx_bw); |
| 90 | + rx_chn.find_attribute("sampling_frequency").write(rx_fs); |
| 91 | + rx_chn.find_attribute("gain_control_mode").write(gain_ctrl_mode); |
| 92 | + } |
| 93 | + |
| 94 | + static void tx_config(Device device, string tx_lo, string tx_bw, string tx_fs, string hw_gain) |
| 95 | + { |
| 96 | + var tx_chn = device.find_channel(CHN_VOLTAGE0, true); |
| 97 | + |
| 98 | + device.find_channel("TX_LO", true).find_attribute("frequency").write(tx_lo); |
| 99 | + tx_chn.find_attribute("rf_bandwidth").write(tx_bw); |
| 100 | + tx_chn.find_attribute("sampling_frequency").write(tx_fs); |
| 101 | + tx_chn.find_attribute("hardwaregain").write(hw_gain); |
| 102 | + } |
| 103 | + |
| 104 | + static void enable_tx(Device tx_adc) |
| 105 | + { |
| 106 | + tx_adc.find_channel(CHN_VOLTAGE0, true).enable(); |
| 107 | + tx_adc.find_channel(CHN_VOLTAGE1, true).enable(); |
| 108 | + } |
| 109 | + |
| 110 | + static void enable_rx(Device rx_dac) |
| 111 | + { |
| 112 | + rx_dac.find_channel(CHN_VOLTAGE0, false).enable(); |
| 113 | + rx_dac.find_channel(CHN_VOLTAGE1, false).enable(); |
| 114 | + } |
| 115 | + |
| 116 | + static byte[] generate_sine(int samples_per_channel, string rx_fs) |
| 117 | + { |
| 118 | + const int fc = 10000; |
| 119 | + double ts = 1.0 / int.Parse(rx_fs, CultureInfo.CurrentCulture); |
| 120 | + double[] t = Enumerable.Range(0, samples_per_channel).Select(i => i * ts).ToArray(); |
| 121 | + |
| 122 | + double[] i = t.Select(x => (Math.Sin(2 * Math.PI * x * fc) * Math.Pow(2, 14))).ToArray(); |
| 123 | + double[] q = t.Select(x => (Math.Cos(2 * Math.PI * x * fc) * Math.Pow(2, 14))).ToArray(); |
| 124 | + |
| 125 | + Int16[] iq = new Int16[i.Length + q.Length]; |
| 126 | + for (int j = 0; j < i.Length; j++) |
| 127 | + { |
| 128 | + iq[j * INT16_BYTE_STEP] = Convert.ToInt16(i[j]); |
| 129 | + iq[j * INT16_BYTE_STEP + 1] = Convert.ToInt16(q[j]); |
| 130 | + } |
| 131 | + |
| 132 | + byte[] iqBytes = iq.SelectMany(BitConverter.GetBytes).ToArray(); |
| 133 | + return iqBytes; |
| 134 | + } |
| 135 | + |
| 136 | + static (List<int> reals, List<int> imags) read_rx_data(IOBuffer rx_buff, int num_readings, int samples_per_channel) |
| 137 | + { |
| 138 | + List<int> reals = new List<int>(); |
| 139 | + List<int> imags = new List<int>(); |
| 140 | + |
| 141 | + byte[] data = new byte[samples_per_channel]; |
| 142 | + for (int k = 0; k < num_readings; k++) |
| 143 | + { |
| 144 | + rx_buff.refill(); |
| 145 | + rx_buff.read(data); |
| 146 | + |
| 147 | + Int16[] data_casted = new Int16[data.Length / INT16_BYTE_STEP]; |
| 148 | + Buffer.BlockCopy(data, 0, data_casted, 0, data.Length); |
| 149 | + for (int idx = 0; idx < data_casted.Length; idx += INT16_BYTE_STEP) |
| 150 | + { |
| 151 | + reals.Add(data_casted[idx]); |
| 152 | + imags.Add(data_casted[idx + 1]); |
| 153 | + } |
| 154 | + } |
| 155 | + return (reals, imags); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments