libiio: adrv9009-iiostream.c

libiio 1.0 Library for interfacing with IIO devices

Loading...
Searching...
No Matches
adrv9009-iiostream.c

This example libiio program is meant to exercise the features of IIO functionality on the ADRV9009.

// SPDX-License-Identifier: GPL-2.0-or-later
/*
* libiio - ADRV9009 IIO streaming example
*
* Copyright (C) 2014 IABG mbH
* Author: Michael Feilen <feilen_at_iabg.de>
* Copyright (C) 2019 Analog Devices Inc.
**/
#include "iiostream-common.h"
#include <errno.h>
#include <iio/iio.h>
#include <iio/iio-debug.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
/* helper macros */
#define MHZ(x) ((long long)(x*1000000.0 + .5))
#define GHZ(x) ((long long)(x*1000000000.0 + .5))
#define IIO_ENSURE(expr) { \
if (!(expr)) { \
(void) fprintf(stderr, "assertion failed (%s:%d)\n", __FILE__, __LINE__); \
(void) abort(); \
} \
}
#define BLOCK_SIZE (1024 * 1024)
/* RX is input, TX is output */
enum iodev { RX, TX };
/* common RX and TX streaming params */
struct stream_cfg {
long long lo_hz; // Local oscillator frequency in Hz
};
/* static scratch mem for strings */
static char tmpstr[64];
/* IIO structs required for streaming */
static struct iio_context *ctx = NULL;
static struct iio_buffer *rxbuf = NULL;
static struct iio_buffer *txbuf = NULL;
static struct iio_stream *rxstream = NULL;
static struct iio_stream *txstream = NULL;
static struct iio_channels_mask *rxmask = NULL;
static struct iio_channels_mask *txmask = NULL;
/* cleanup resources */
static void cleanup(void)
{
printf("* Destroying streams\n");
if (rxstream) {iio_stream_destroy(rxstream); }
if (txstream) { iio_stream_destroy(txstream); }
printf("* Destroying channel masks\n");
if (rxmask) { iio_channels_mask_destroy(rxmask); }
if (txmask) { iio_channels_mask_destroy(txmask); }
printf("* Destroying context\n");
if (ctx) { iio_context_destroy(ctx); }
}
/* cleanup and exit with error */
static void shutdown(void)
{
cleanup();
exit(1);
}
static void handle_sig(int sig)
{
printf("Waiting for process to finish... Got signal %d\n", sig);
stop_stream();
}
/* check return value of attr_write function */
static void errchk(int v, const char* what) {
if (v < 0) { fprintf(stderr, "Error %d writing to channel \"%s\"\nvalue may not be supported.\n", v, what); shutdown(); }
}
/* write attribute: long long int */
static void wr_ch_lli(struct iio_channel *chn, const char* what, long long val)
{
const struct iio_attr *attr = iio_channel_find_attr(chn, what);
errchk(attr ? iio_attr_write_longlong(attr, val) : -ENOENT, what);
}
/* read attribute: long long int */
static long long rd_ch_lli(struct iio_channel *chn, const char* what)
{
const struct iio_attr *attr = iio_channel_find_attr(chn, what);
long long val = 0;
errchk(attr ? iio_attr_read_longlong(attr, &val) : -ENOENT, what);
printf("\t %s: %lld\n", what, val);
return val;
}
/* helper function generating channel names */
static char* get_ch_name_mod(const char* type, int id, char modify)
{
snprintf(tmpstr, sizeof(tmpstr), "%s%d_%c", type, id, modify);
return tmpstr;
}
/* helper function generating channel names */
static char* get_ch_name(const char* type, int id)
{
snprintf(tmpstr, sizeof(tmpstr), "%s%d", type, id);
return tmpstr;
}
/* returns adrv9009 phy device */
static struct iio_device* get_adrv9009_phy(void)
{
struct iio_device *dev = iio_context_find_device(ctx, "adrv9009-phy");
IIO_ENSURE(dev && "No adrv9009-phy found");
return dev;
}
/* finds adrv9009 streaming IIO devices */
static bool get_adrv9009_stream_dev(enum iodev d, struct iio_device **dev)
{
switch (d) {
case TX: *dev = iio_context_find_device(ctx, "axi-adrv9009-tx-hpc"); return *dev != NULL;
case RX: *dev = iio_context_find_device(ctx, "axi-adrv9009-rx-hpc"); return *dev != NULL;
default: IIO_ENSURE(0); return false;
}
}
/* finds adrv9009 streaming IIO channels */
static bool get_adrv9009_stream_ch(enum iodev d, struct iio_device *dev, int chid, char modify, struct iio_channel **chn)
{
*chn = iio_device_find_channel(dev, modify ? get_ch_name_mod("voltage", chid, modify) : get_ch_name("voltage", chid), d == TX);
if (!*chn)
*chn = iio_device_find_channel(dev, modify ? get_ch_name_mod("voltage", chid, modify) : get_ch_name("voltage", chid), d == TX);
return *chn != NULL;
}
/* finds adrv9009 phy IIO configuration channel with id chid */
static bool get_phy_chan(enum iodev d, int chid, struct iio_channel **chn)
{
switch (d) {
case RX: *chn = iio_device_find_channel(get_adrv9009_phy(), get_ch_name("voltage", chid), false); return *chn != NULL;
case TX: *chn = iio_device_find_channel(get_adrv9009_phy(), get_ch_name("voltage", chid), true); return *chn != NULL;
default: IIO_ENSURE(0); return false;
}
}
/* finds adrv9009 local oscillator IIO configuration channels */
static bool get_lo_chan(struct iio_channel **chn)
{
// LO chan is always output, i.e. true
*chn = iio_device_find_channel(get_adrv9009_phy(), get_ch_name("altvoltage", 0), true); return *chn != NULL;
}
/* applies streaming configuration through IIO */
bool cfg_adrv9009_streaming_ch(struct stream_cfg *cfg, int chid)
{
struct iio_channel *chn = NULL;
// Configure phy and lo channels
printf("* Acquiring ADRV9009 phy channel %d\n", chid);
if (!get_phy_chan(true, chid, &chn)) { return false; }
rd_ch_lli(chn, "rf_bandwidth");
rd_ch_lli(chn, "sampling_frequency");
// Configure LO channel
printf("* Acquiring ADRV9009 TRX lo channel\n");
if (!get_lo_chan(&chn)) { return false; }
wr_ch_lli(chn, "frequency", cfg->lo_hz);
return true;
}
/* simple configuration and streaming */
/* usage:
* Default context, assuming local IIO devices, i.e., this script is run on a platform to
* which adrv9009 is connected.
$./a.out
* URI context, find out the uri by typing `iio_info -s` at the command line of the host PC
$./a.out usb:x.x.x
*/
int main (int argc, char **argv)
{
// Streaming devices and channels
struct iio_device *tx;
struct iio_device *rx;
struct iio_channel *rx0_i = NULL;
struct iio_channel *rx0_q = NULL;
struct iio_channel *tx0_i = NULL;
struct iio_channel *tx0_q = NULL;
// RX and TX sample size
size_t rx_sample_sz, tx_sample_sz;
// Stream configuration
struct stream_cfg trxcfg;
int err;
// Listen to ctrl+c and IIO_ENSURE
signal(SIGINT, handle_sig);
// TRX stream config
trxcfg.lo_hz = GHZ(2.5);
printf("* Acquiring IIO context\n");
if (argc == 1) {
ctx = iio_create_context(NULL, NULL);
err = iio_err(ctx);
IIO_ENSURE(!err && "No context");
} else if (argc == 2) {
ctx = iio_create_context(NULL, argv[1]);
err = iio_err(ctx);
IIO_ENSURE(!err && "No context");
}
IIO_ENSURE(iio_context_get_devices_count(ctx) > 0 && "No devices");
printf("* Acquiring ADRV9009 streaming devices\n");
IIO_ENSURE(get_adrv9009_stream_dev(TX, &tx) && "No tx dev found");
IIO_ENSURE(get_adrv9009_stream_dev(RX, &rx) && "No rx dev found");
printf("* Configuring ADRV9009 for streaming\n");
IIO_ENSURE(cfg_adrv9009_streaming_ch(&trxcfg, 0) && "TRX device not found");
printf("* Initializing ADRV9009 IIO streaming channels\n");
IIO_ENSURE(get_adrv9009_stream_ch(RX, rx, 0, 'i', &rx0_i) && "RX chan i not found");
IIO_ENSURE(get_adrv9009_stream_ch(RX, rx, 0, 'q', &rx0_q) && "RX chan q not found");
IIO_ENSURE(get_adrv9009_stream_ch(TX, tx, 0, 0, &tx0_i) && "TX chan i not found");
IIO_ENSURE(get_adrv9009_stream_ch(TX, tx, 1, 0, &tx0_q) && "TX chan q not found");
rxmask = iio_create_channels_mask(iio_device_get_channels_count(rx));
if (!rxmask) {
fprintf(stderr, "Unable to alloc RX channels mask\n");
shutdown();
}
txmask = iio_create_channels_mask(iio_device_get_channels_count(tx));
if (!txmask) {
fprintf(stderr, "Unable to alloc TX channels mask\n");
shutdown();
}
printf("* Enabling IIO streaming channels\n");
iio_channel_enable(rx0_i, rxmask);
iio_channel_enable(rx0_q, rxmask);
iio_channel_enable(tx0_i, txmask);
iio_channel_enable(tx0_q, txmask);
printf("* Creating non-cyclic IIO buffers with 1 MiS\n");
rxbuf = iio_device_get_buffer(rx, 0);
if (!rxbuf) {
ctx_perror(ctx, -ENODEV, "Could not get RX buffer");
shutdown();
}
txbuf = iio_device_get_buffer(tx, 0);
if (!txbuf) {
ctx_perror(ctx, -ENODEV, "Could not get TX buffer");
shutdown();
}
rxstream = iio_buffer_create_stream(rxbuf, 4, BLOCK_SIZE, rxmask);
if (iio_err(rxstream)) {
rxstream = NULL;
ctx_perror(ctx, iio_err(rxstream), "Could not create RX stream");
shutdown();
}
txstream = iio_buffer_create_stream(txbuf, 4, BLOCK_SIZE, txmask);
if (iio_err(txstream)) {
txstream = NULL;
ctx_perror(ctx, iio_err(txstream), "Could not create TX stream");
shutdown();
}
rx_sample_sz = iio_device_get_sample_size(rx, rxmask);
tx_sample_sz = iio_device_get_sample_size(tx, txmask);
printf("* Starting IO streaming (press CTRL+C to cancel)\n");
stream(rx_sample_sz, tx_sample_sz, BLOCK_SIZE,
rxstream, txstream, rx0_i, tx0_i);
cleanup();
return 0;
}
iio_channel_enable
void iio_channel_enable(const struct iio_channel *chn, struct iio_channels_mask *mask)
Enable the given channel.
Definition channel.c:444
iio_channel_find_attr
const struct iio_attr * iio_channel_find_attr(const struct iio_channel *chn, const char *name)
Try to find a channel-specific attribute by its name.
Definition channel.c:392
iio_context_find_device
struct iio_device * iio_context_find_device(const struct iio_context *ctx, const char *name)
Try to find a device structure by its ID, label or name.
Definition compat.c:710
iio_create_context
struct iio_context * iio_create_context(const struct iio_context_params *params, const char *uri)
Create a context from a URI description.
Definition context.c:716
iio_context_destroy
void iio_context_destroy(struct iio_context *ctx)
Destroy the given context.
Definition compat.c:434
iio_context_get_devices_count
unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
Enumerate the devices found in the given context.
Definition compat.c:698
iio_create_channels_mask
struct iio_channels_mask * iio_create_channels_mask(unsigned int nb_channels)
Create a new empty channels mask.
Definition mask.c:14
iio_channels_mask_destroy
void iio_channels_mask_destroy(struct iio_channels_mask *mask)
Destroy a channels mask.
Definition mask.c:40
iio_device_get_buffer
struct iio_buffer * iio_device_get_buffer(const struct iio_device *dev, unsigned int index)
Get the buffer present at the given index.
Definition device.c:254
iio_device_find_channel
struct iio_channel * iio_device_find_channel(const struct iio_device *dev, const char *name, bool output)
Try to find a channel structure by its ID, label or name.
Definition compat.c:969
iio_device_get_channels_count
unsigned int iio_device_get_channels_count(const struct iio_device *dev)
Enumerate the channels of the given device.
Definition compat.c:857
iio_stream_destroy
void iio_stream_destroy(struct iio_stream *stream)
Destroy the given stream object.
Definition stream.c:86
iio_buffer_create_stream
struct iio_stream * iio_buffer_create_stream(struct iio_buffer *buffer, size_t nb_blocks, size_t samples_count, struct iio_channels_mask *mask)
Create a iio_stream object for the given iio_buffer.
Definition stream.c:26
iio.h
Public interface.
iio_attr
Structure holding meta-data for an attribute.
Definition iio-backend.h:83
iio_buffer
An input or output buffer, used to read or write samples.
iio_channel
Represents an input or output channel of a device.
iio_context
Contains the representation of an IIO context.
iio_device
Represents a device in the IIO context.