Skip to content

Commit 9bc1e8b

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 9bc1e8b

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
/*
3+
* libiio - Library for interfacing industrial I/O (IIO) devices
4+
*
5+
* Copyright (C) 2023 Analog Devices, Inc.
6+
* Author: Adrian Stanea <[email protected]>
7+
*/
8+
9+
using System;
10+
using System.Globalization;
11+
using System.Reflection;
12+
using iio;
13+
14+
[assembly: AssemblyTitle("Pluto_Example")]
15+
[assembly: AssemblyVersion("1.0.0.0")]
16+
[assembly: System.Runtime.InteropServices.ComVisible(false)]
17+
[assembly: CLSCompliant(true)]
18+
namespace IIOCSharp
19+
{
20+
static class PlutoExample
21+
{
22+
const int INT16_BYTE_STEP = 2;
23+
const string URI = "ip:192.168.2.1";
24+
const string TXLO = "1000000000";
25+
const string TXBW = "5000000";
26+
const string TXFS = "3000000";
27+
const string RXLO = TXLO;
28+
const string RXBW = TXBW;
29+
const string RXFS = TXFS;
30+
const string GAIN_CTRL_MODE = "slow_attack";
31+
const string HW_GAIN = "-30";
32+
33+
const string CHN_VOLTAGE0 = "voltage0";
34+
const string CHN_VOLTAGE1 = "voltage1";
35+
36+
static void Main(string[] args)
37+
{
38+
Context ctx = new Context(URI);
39+
if (ctx == null)
40+
{
41+
Console.WriteLine("Unable to create IIO context");
42+
return;
43+
}
44+
// get reference to devices
45+
var ctrl = ctx.find_device("ad9361-phy");
46+
var tx_dac = ctx.find_device("cf-ad9361-dds-core-lpc");
47+
var rx_adc = ctx.find_device("cf-ad9361-lpc");
48+
49+
rx_config(ctrl, RXLO, RXBW, RXFS, GAIN_CTRL_MODE);
50+
tx_config(ctrl, TXLO, TXBW, TXFS, HW_GAIN);
51+
52+
// Enable all IQ channels
53+
enable_tx(tx_dac);
54+
enable_rx(rx_adc);
55+
56+
// Cyclic buffer to output perdiodic waveforms
57+
uint samples_per_channel = Convert.ToUInt16(Math.Pow(2, 15));
58+
59+
var tx_buf = new IOBuffer(tx_dac, samples_per_channel, true);
60+
var rx_buff = new IOBuffer(rx_adc, samples_per_channel, false);
61+
62+
byte[] iqBytes = generate_sine(Convert.ToInt32(samples_per_channel), RXFS);
63+
64+
// Send data to TX buffer
65+
tx_buf.fill(iqBytes);
66+
tx_buf.push();
67+
68+
// read data on rx buffer
69+
const int num_readings = 10;
70+
(List<int> reals, List<int> imags) = read_rx_data(rx_buff, num_readings, Convert.ToInt32(samples_per_channel));
71+
}
72+
73+
static void rx_config(Device device, string rx_lo, string rx_bw, string rx_fs, string gain_ctrl_mode)
74+
{
75+
var rx_chn = device.find_channel(CHN_VOLTAGE0, false);
76+
77+
device.find_channel("RX_LO", true).find_attribute("frequency").write(rx_lo);
78+
rx_chn.find_attribute("rf_bandwidth").write(rx_bw);
79+
rx_chn.find_attribute("sampling_frequency").write(rx_fs);
80+
rx_chn.find_attribute("gain_control_mode").write(gain_ctrl_mode);
81+
}
82+
83+
static void tx_config(Device device, string tx_lo, string tx_bw, string tx_fs, string hw_gain)
84+
{
85+
var tx_chn = device.find_channel(CHN_VOLTAGE0, true);
86+
87+
device.find_channel("TX_LO", true).find_attribute("frequency").write(tx_lo);
88+
tx_chn.find_attribute("rf_bandwidth").write(tx_bw);
89+
tx_chn.find_attribute("sampling_frequency").write(tx_fs);
90+
tx_chn.find_attribute("hardwaregain").write(hw_gain);
91+
}
92+
93+
static void enable_tx(Device tx_adc)
94+
{
95+
tx_adc.find_channel(CHN_VOLTAGE0, true).enable();
96+
tx_adc.find_channel(CHN_VOLTAGE1, true).enable();
97+
}
98+
99+
static void enable_rx(Device rx_dac)
100+
{
101+
rx_dac.find_channel(CHN_VOLTAGE0, false).enable();
102+
rx_dac.find_channel(CHN_VOLTAGE1, false).enable();
103+
}
104+
105+
static byte[] generate_sine(int samples_per_channel, string rx_fs)
106+
{
107+
const int fc = 10000;
108+
double ts = 1.0 / int.Parse(rx_fs, CultureInfo.CurrentCulture);
109+
double[] t = Enumerable.Range(0, samples_per_channel).Select(i => i * ts).ToArray();
110+
111+
double[] i = t.Select(x => (Math.Sin(2 * Math.PI * x * fc) * Math.Pow(2, 14))).ToArray();
112+
double[] q = t.Select(x => (Math.Cos(2 * Math.PI * x * fc) * Math.Pow(2, 14))).ToArray();
113+
114+
Int16[] iq = new Int16[i.Length + q.Length];
115+
for (int j = 0; j < i.Length; j++)
116+
{
117+
iq[j * INT16_BYTE_STEP] = Convert.ToInt16(i[j]);
118+
iq[j * INT16_BYTE_STEP + 1] = Convert.ToInt16(q[j]);
119+
}
120+
121+
byte[] iqBytes = iq.SelectMany(BitConverter.GetBytes).ToArray();
122+
return iqBytes;
123+
}
124+
125+
static (List<int> reals, List<int> imags) read_rx_data(IOBuffer rx_buff, int num_readings, int samples_per_channel)
126+
{
127+
List<int> reals = new List<int>();
128+
List<int> imags = new List<int>();
129+
130+
byte[] data = new byte[samples_per_channel];
131+
for (int k = 0; k < num_readings; k++)
132+
{
133+
rx_buff.refill();
134+
rx_buff.read(data);
135+
136+
Int16[] data_casted = new Int16[data.Length / INT16_BYTE_STEP];
137+
Buffer.BlockCopy(data, 0, data_casted, 0, data.Length);
138+
for (int idx = 0; idx < data_casted.Length; idx += INT16_BYTE_STEP)
139+
{
140+
reals.Add(data_casted[idx]);
141+
imags.Add(data_casted[idx + 1]);
142+
}
143+
}
144+
return (reals, imags);
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)