Skip to content

Commit 5ff070d

Browse files
committed
bindings/csharp: pluto example
- How to use C# bindings to control libiio devices. - Configure Pluto and use RX and TX interfaces to send and receive data. - Nice to have for EZ questions such as: https://ez.analog.com/adieducation/university-program/f/q-a/574101/adalam-pluto-sdr-via-c Signed-off-by: Adrian Stanea <[email protected]>
1 parent cfb6249 commit 5ff070d

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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.Collections.Generic;
22+
using System.Linq;
23+
using System.Text;
24+
using System.Threading.Tasks;
25+
using iio;
26+
27+
namespace IIOCSharp
28+
{
29+
class PlutoExample
30+
{
31+
static void Main(string[] args)
32+
{
33+
Context ctx = new Context("192.168.2.1");
34+
if (ctx == null)
35+
{
36+
Console.WriteLine("Unable to create IIO context");
37+
return;
38+
}
39+
40+
string TXLO = "1000000000";
41+
string TXBW = "5000000";
42+
string TXFS = "3000000";
43+
string RXLO = TXLO;
44+
string RXBW = TXBW;
45+
string RXFS = TXFS;
46+
string gain_ctrl_mode = "slow_attack";
47+
string hw_gain = "-30";
48+
49+
// get reference to devices
50+
var ctrl = ctx.find_device("ad9361-phy"); //configure transciever
51+
var tx_dac = ctx.find_device("cf-ad9361-dds-core-lpc");
52+
var rx_adc = ctx.find_device("cf-ad9361-lpc");
53+
54+
rx_config(ctrl, RXLO, RXBW, RXFS, gain_ctrl_mode);
55+
tx_config(ctrl, TXLO, TXBW, TXFS, hw_gain);
56+
57+
// Enable all IQ channels
58+
enable_tx(tx_dac);
59+
enable_rx(rx_adc);
60+
61+
// Cyclic buffer to output perdiodic waveforms
62+
int samples_per_channel = (int)Math.Pow(2, 15);
63+
64+
var tx_buf = new IOBuffer(tx_dac, (uint)samples_per_channel, true);
65+
var rx_buff = new IOBuffer(rx_adc, (uint)samples_per_channel, false);
66+
67+
byte[] iqBytes = generate_sine(samples_per_channel, RXFS);
68+
69+
// Send data to TX buffer
70+
tx_buf.fill(iqBytes);
71+
tx_buf.push();
72+
73+
// read data on rx buffer
74+
(List<int> reals, List<int> imags) = read_rx_data(rx_buff, 10, samples_per_channel);
75+
}
76+
77+
static void rx_config(Device device, string rx_lo, string rx_bw, string rx_fs, string gain_ctrl_mode)
78+
{
79+
var rx_chn = device.find_channel("voltage0", false);
80+
81+
device.find_channel("RX_LO", true).find_attribute("frequency").write(rx_lo);
82+
rx_chn.find_attribute("rf_bandwidth").write(rx_bw);
83+
rx_chn.find_attribute("sampling_frequency").write(rx_fs);
84+
rx_chn.find_attribute("gain_control_mode").write(gain_ctrl_mode);
85+
}
86+
87+
static void tx_config(Device device, string tx_lo, string tx_bw, string tx_fs, string hw_gain)
88+
{
89+
var tx_chn = device.find_channel("voltage0", true);
90+
91+
device.find_channel("TX_LO", true).find_attribute("frequency").write(tx_lo);
92+
tx_chn.find_attribute("rf_bandwidth").write(tx_bw);
93+
tx_chn.find_attribute("sampling_frequency").write(tx_fs);
94+
tx_chn.find_attribute("hardwaregain").write(hw_gain);
95+
}
96+
97+
static void enable_tx(Device tx_adc)
98+
{
99+
tx_adc.find_channel("voltage0", true).enable();
100+
tx_adc.find_channel("voltage1", true).enable();
101+
}
102+
103+
static void enable_rx(Device rx_dac)
104+
{
105+
rx_dac.find_channel("voltage0", false).enable();
106+
rx_dac.find_channel("voltage1", false).enable();
107+
}
108+
109+
static byte[] generate_sine(int samples_per_channel, string rx_fs)
110+
{
111+
int fc = 10000;
112+
double ts = 1.0 / int.Parse(rx_fs);
113+
double[] t = Enumerable.Range(0, samples_per_channel).Select(i => i * ts).ToArray();
114+
115+
double[] i = t.Select(x => (Math.Sin(2 * Math.PI * x * fc) * Math.Pow(2, 14))).ToArray();
116+
double[] q = t.Select(x => (Math.Cos(2 * Math.PI * x * fc) * Math.Pow(2, 14))).ToArray();
117+
118+
int[] iq = new int[i.Length + q.Length];
119+
for (int j = 0; j < i.Length; j++)
120+
{
121+
iq[j * 2] = Convert.ToInt16(i[j]);
122+
iq[j * 2 + 1] = Convert.ToInt16(q[j]);
123+
}
124+
125+
byte[] iqBytes = iq.SelectMany(BitConverter.GetBytes).ToArray();
126+
return iqBytes;
127+
}
128+
129+
static (List<int> reals, List<int> imags) read_rx_data(IOBuffer rx_buff, int num_readings, int samples_per_channel)
130+
{
131+
List<int> reals = new List<int>();
132+
List<int> imags = new List<int>();
133+
134+
byte[] data = new byte[samples_per_channel];
135+
for (int k = 0; k < num_readings; k++)
136+
{
137+
rx_buff.refill();
138+
rx_buff.read(data);
139+
140+
short[] data_casted = new short[data.Length / 2];
141+
Buffer.BlockCopy(data, 0, data_casted, 0, data.Length);
142+
for (int idx = 0; idx < data_casted.Length; idx += 2)
143+
{
144+
reals.Add(data_casted[idx]);
145+
imags.Add(data_casted[idx + 1]);
146+
}
147+
}
148+
return (reals, imags);
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)