"Device or resource busy" when attempting to add a third buffer with iio_device_create_buffer to ad9361-iiostream.c example code

Hello-

I am  modifying the ad9361-iiostream.c https://github.com/analogdevicesinc/libiio/blob/master/examples/ad9361-iiostream.c example as a simple experiment to create a third iio buffer.

I have no competing applications such as iio-oscilloscope using the resources, but console output shows that it  "Could not create RF buffer: Device or resource busy" on the first attempt to run the code.

In this thread, https://ez.analog.com/linux-device-drivers/linux-software-drivers/f/q-a/87769/libiio-device-or-resource-busy-when-creating-buffers/313187#313187 mahkoe seems to have successfully created the buffers , but can't open them after running another program that uses the resources.. I cant create them at all even though it is the only program I run, not even the first time after a fresh boot from power off of target and host.. I am using zc702 and FMCOMMS2.

Console output below:

root@analog:~/tmp# ./ad9361-iiostream
* Acquiring IIO context
* Acquiring AD9361 streaming devices
* Configuring AD9361 for streaming
* Acquiring AD9361 phy channel 0
* Acquiring AD9361 RX lo channel
* Acquiring AD9361 phy channel 0
* Acquiring AD9361 TX lo channel
* Initializing AD9361 IIO streaming channels
* Enabling IIO streaming channels
* Creating non-cyclic IIO buffers with 1 MiS
* Receive buffer created
* Transmit buffer created
Could not create RF buffer: Device or resource busy
* Destroying buffers
* Disabling streaming channels
* Destroying context

It seems straightforward and common to create multiple buffers, can anyone see what the issue is here? I appreciate your suggestions.

I've attached my code and anything that I thought should be modified to create the third buffer (which I call RF) is commented with //RF or /*RF*/

Full modified code is copied below:

/*
* libiio - AD9361 IIO streaming example
*
* Copyright (C) 2014 IABG mbH
* Author: Michael Feilen <feilen_at_iabg.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
**/

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>

#ifdef __APPLE__
#include <iio/iio.h>
#else
#include <iio.h>
#endif

/* helper macros */
#define MHZ(x) ((long long)(x*1000000.0 + .5))
#define GHZ(x) ((long long)(x*1000000000.0 + .5))

#define ASSERT(expr) { \
if (!(expr)) { \
(void) fprintf(stderr, "assertion failed (%s:%d)\n", __FILE__, __LINE__); \
(void) abort(); \
} \
}

/* RX is input, TX is output */
enum iodev { RX, TX };

/* common RX and TX streaming params */
struct stream_cfg {
long long bw_hz; // Analog banwidth in Hz
long long fs_hz; // Baseband sample rate in Hz
long long lo_hz; // Local oscillator frequency in Hz
const char* rfport; // Port name
};

/* static scratch mem for strings */
static char tmpstr[64];

/* IIO structs required for streaming */
static struct iio_context *ctx = NULL;
static struct iio_channel *rx0_i = NULL;
static struct iio_channel *rx0_q = NULL;
static struct iio_channel *tx0_i = NULL;
static struct iio_channel *tx0_q = NULL;
static struct iio_channel *rf0_i = NULL; //RF
static struct iio_channel *rf0_q = NULL; //RF

static struct iio_buffer *rxbuf = NULL;
static struct iio_buffer *txbuf = NULL;

static struct iio_buffer *rfbuf = NULL; /*RF*/

static bool stop;

/* cleanup and exit */
static void shutdown()
{
printf("* Destroying buffers\n");
if (rxbuf) { iio_buffer_destroy(rxbuf); }
if (txbuf) { iio_buffer_destroy(txbuf); }
if (rfbuf) { iio_buffer_destroy(rfbuf); } /*RF*/

printf("* Disabling streaming channels\n");
if (rx0_i) { iio_channel_disable(rx0_i); }
if (rx0_q) { iio_channel_disable(rx0_q); }
if (tx0_i) { iio_channel_disable(tx0_i); }
if (tx0_q) { iio_channel_disable(tx0_q); }
if (rf0_i) { iio_channel_disable(rf0_i); } //RF
if (rf0_q) { iio_channel_disable(rf0_q); } //RF

printf("* Destroying context\n");
if (ctx) { iio_context_destroy(ctx); }
exit(0);
}

static void handle_sig(int sig)
{
printf("Waiting for process to finish...\n");
stop = true;
}

/* 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)
{
errchk(iio_channel_attr_write_longlong(chn, what, val), what);
}

/* write attribute: string */
static void wr_ch_str(struct iio_channel *chn, const char* what, const char* str)
{
errchk(iio_channel_attr_write(chn, what, str), what);
}

/* 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 ad9361 phy device */
static struct iio_device* get_ad9361_phy(struct iio_context *ctx)
{
struct iio_device *dev = iio_context_find_device(ctx, "ad9361-phy");
ASSERT(dev && "No ad9361-phy found");
return dev;
}

/* finds AD9361 streaming IIO devices */
static bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev)
{
switch (d) {
case TX: *dev = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); return *dev != NULL;
case RX: *dev = iio_context_find_device(ctx, "cf-ad9361-lpc"); return *dev != NULL;
default: ASSERT(0); return false;
}
}

/* finds AD9361 streaming IIO channels */
static bool get_ad9361_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn)
{
*chn = iio_device_find_channel(dev, get_ch_name("voltage", chid), d == TX);
if (!*chn)
*chn = iio_device_find_channel(dev, get_ch_name("altvoltage", chid), d == TX);
return *chn != NULL;
}

/* finds AD9361 phy IIO configuration channel with id chid */
static bool get_phy_chan(struct iio_context *ctx, enum iodev d, int chid, struct iio_channel **chn)
{
switch (d) {
case RX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("voltage", chid), false); return *chn != NULL;
case TX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("voltage", chid), true); return *chn != NULL;
default: ASSERT(0); return false;
}
}

/* finds AD9361 local oscillator IIO configuration channels */
static bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn)
{
switch (d) {
// LO chan is always output, i.e. true
case RX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("altvoltage", 0), true); return *chn != NULL;
case TX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("altvoltage", 1), true); return *chn != NULL;
default: ASSERT(0); return false;
}
}

/* applies streaming configuration through IIO */
bool cfg_ad9361_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, enum iodev type, int chid)
{
struct iio_channel *chn = NULL;

// Configure phy and lo channels
printf("* Acquiring AD9361 phy channel %d\n", chid);
if (!get_phy_chan(ctx, type, chid, &chn)) { return false; }
wr_ch_str(chn, "rf_port_select", cfg->rfport);
wr_ch_lli(chn, "rf_bandwidth", cfg->bw_hz);
wr_ch_lli(chn, "sampling_frequency", cfg->fs_hz);

// Configure LO channel
printf("* Acquiring AD9361 %s lo channel\n", type == TX ? "TX" : "RX");
if (!get_lo_chan(ctx, type, &chn)) { return false; }
wr_ch_lli(chn, "frequency", cfg->lo_hz);
return true;
}

/* simple configuration and streaming */
int main (int argc, char **argv)
{
// Streaming devices
struct iio_device *tx;
struct iio_device *rx;
struct iio_device *rf; /*RF */

// RX and TX sample counters
size_t nrx = 0;
size_t ntx = 0;
size_t nrf = 0; /*RF */

// Stream configurations
struct stream_cfg rxcfg;
struct stream_cfg txcfg;
struct stream_cfg rfcfg;

// Listen to ctrl+c and ASSERT
signal(SIGINT, handle_sig);

// RX stream config
rxcfg.bw_hz = MHZ(2); // 2 MHz rf bandwidth
rxcfg.fs_hz = MHZ(2.5); // 2.5 MS/s rx sample rate
rxcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency
rxcfg.rfport = "A_BALANCED"; // port A (select for rf freq.)

// TX stream config
txcfg.bw_hz = MHZ(1.5); // 1.5 MHz rf bandwidth
txcfg.fs_hz = MHZ(2.5); // 2.5 MS/s tx sample rate
txcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency
txcfg.rfport = "A"; // port A (select for rf freq.)

printf("* Acquiring IIO context\n");
ASSERT((ctx = iio_create_default_context()) && "No context");
ASSERT(iio_context_get_devices_count(ctx) > 0 && "No devices");

printf("* Acquiring AD9361 streaming devices\n");
ASSERT(get_ad9361_stream_dev(ctx, TX, &tx) && "No tx dev found");
ASSERT(get_ad9361_stream_dev(ctx, RX, &rx) && "No rx dev found");
ASSERT(get_ad9361_stream_dev(ctx, RX, &rf) && "No rf dev found"); //RF

printf("* Configuring AD9361 for streaming\n");
ASSERT(cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0) && "RX port 0 not found");
ASSERT(cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0) && "TX port 0 not found");
//ASSERT(cfg_ad9361_streaming_ch(ctx, &rfcfg, RX, 0) && "RF port 0 not found"); // RF


printf("* Initializing AD9361 IIO streaming channels\n");
ASSERT(get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i) && "RX chan i not found");
ASSERT(get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q) && "RX chan q not found");
ASSERT(get_ad9361_stream_ch(ctx, TX, tx, 0, &tx0_i) && "TX chan i not found");
ASSERT(get_ad9361_stream_ch(ctx, TX, tx, 1, &tx0_q) && "TX chan q not found");
ASSERT(get_ad9361_stream_ch(ctx, RX, rf, 0, &rf0_i) && "RX chan i not found"); //RF
ASSERT(get_ad9361_stream_ch(ctx, RX, rf, 1, &rf0_q) && "RX chan q not found"); //RF


printf("* Enabling IIO streaming channels\n");
iio_channel_enable(rx0_i);
iio_channel_enable(rx0_q);
iio_channel_enable(tx0_i);
iio_channel_enable(tx0_q);
iio_channel_enable(rf0_i); //RF
iio_channel_enable(rf0_q); //RF


printf("* Creating non-cyclic IIO buffers with 1 MiS\n");

rxbuf = iio_device_create_buffer(rx, 1024, false);//RF reduce size by *1024
if (!rxbuf) {
perror("Could not create RX buffer");
shutdown();
}

printf("* Receive buffer created\n");





txbuf = iio_device_create_buffer(tx, 1024, false); //RF reduce size by *1024
if (!txbuf) {
perror("Could not create TX buffer");
shutdown();
}

printf("* Transmit buffer created\n");




//RF trouble using rx device
rfbuf = iio_device_create_buffer(rx, 1024, false);
if (!rfbuf) {
perror("Could not create RF buffer");
shutdown();
}

printf("* Aux buffer created\n");


printf("* Starting IO streaming (press CTRL+C to cancel)\n");
while (!stop)
{
ssize_t nbytes_rx, nbytes_tx, nbytes_rf;
char *p_dat, *p_end;
ptrdiff_t p_inc;

// Schedule TX buffer
nbytes_tx = iio_buffer_push(txbuf);
if (nbytes_tx < 0) { printf("Error pushing buf %d\n", (int) nbytes_tx); shutdown(); }

// Refill RX buffer
nbytes_rx = iio_buffer_refill(rxbuf);
if (nbytes_rx < 0) { printf("Error refilling buf %d\n",(int) nbytes_rx); shutdown(); }

/* Refill RF buffer
nbytes_rf = iio_buffer_refill(rfbuf);
if (nbytes_rf < 0) { printf("Error refilling buf %d\n",(int) nbytes_rf); shutdown(); }
*/

printf("* round 2\n");

// READ: Get pointers to RX buf and read IQ from RX buf port 0
p_inc = iio_buffer_step(rxbuf);
p_end = iio_buffer_end(rxbuf);
for (p_dat = (char *)iio_buffer_first(rxbuf, rx0_i); p_dat < p_end; p_dat += p_inc) {
// Example: swap I and Q
const int16_t i = ((int16_t*)p_dat)[0]; // Real (I)
const int16_t q = ((int16_t*)p_dat)[1]; // Imag (Q)
((int16_t*)p_dat)[0] = q;
((int16_t*)p_dat)[1] = i;
}

// WRITE: Get pointers to TX buf and write IQ to TX buf port 0
p_inc = iio_buffer_step(txbuf);
p_end = iio_buffer_end(txbuf);
for (p_dat = (char *)iio_buffer_first(txbuf, tx0_i); p_dat < p_end; p_dat += p_inc) {
// Example: fill with zeros
// 12-bit sample needs to be MSB alligned so shift by 4
// wiki.analog.com/.../basic_iq_datafiles
((int16_t*)p_dat)[0] = 0 << 4; // Real (I)
((int16_t*)p_dat)[1] = 0 << 4; // Imag (Q)
}

// Sample counter increment and status output
nrx += nbytes_rx / iio_device_get_sample_size(rx);
ntx += nbytes_tx / iio_device_get_sample_size(tx);

nrf += nbytes_rf / iio_device_get_sample_size(rf); //RF


printf("\tRX %8.2f MSmp, TX %8.2f MSmp\n, RF %8.2f MSmp\n", nrx/1e6, ntx/1e6, nrf/1e6);
}

shutdown();

return 0;
}



.
[edited by: RFImage at 3:46 AM (GMT 0) on 7 Jan 2019]
Parents Reply
  • That is all information I had been trying to learn from this experiment. My intent was to refill the buffers on a trigger (interrupt) but first I have to see if I can even create two buffers fed by the same device channel. If what you say is true ( I have no reason to doubt) then perhaps this scheme won't work. I appreciate your suggestion, that is good information to know because I do want fresh data.. But still, can I retain the old data in one buffer (freeze it) then feed another from the same device at a later time to a different buffer?

Children
  • Does anyone from AD know the answer to this? Can I freeze a buffer, retain it's content while switching to another buffer to fill from the same device/channel in real time? My capture has to be optimized for speed, then I have all the time in the world to analyze the data, so I would like it to be there (scattered in various memory locations captured at varying times), for post analysis and know where to find it. I am trying to avoid memory copys during event captures. Each buffer refill is triggered by a real time event. In short I have a series of events that could be very tightly spaced and I dont have enough processing time between them. So I want to store a segment of the event related stream for post analysis but I don't have time to copy the memory from the buffer to some place that is static.

  • 0
    •  Analog Employees 
    on Jan 9, 2019 4:53 AM over 1 year ago in reply to RFImage

    You might be able to just manage the pointers returned by the buffer refills assuming you don't cycle back on the memory ring. Have you tried benchmarking the memcopys to see if they are too slow?

    -Travis

  • Hi Travis - that's pretty much what I had in mind in the first place, creating a non cyclical buffer, refilling it then moving on to another buffer. However  I think I've learned from this discussion that I can't have two buffers associated with the same device input stream (rx input stream  from ad9361  in this case) , correct? 

    So I've moved on to looking at channel disable as a possible way of preventing overwrite of captured data,

    I am thinking that instead of multiple buffers I could use multiple channels and when I do the refill, only fill one channel at a time, because the others are disabled. So I still have the question above re channels. what does iio_channel_disable do exactly, does it prevent writes to memory for that channel? or does it somehow destroy the data in that channel?

    Unless you have a better suggestion.

    Memcopys will be my fallback, but I think it will limit  front end processing so I am exploring other options first.

  • 0
    •  Analog Employees 
    on Jan 9, 2019 7:24 AM over 1 year ago in reply to RFImage

    Having multiple buffers (multiple reader support) is a feature provided by IIOD, it won't work at the local backend. And I'm honestly not sure what it will provide you.

    Disabling channels, prevents certain channels from being captured by the DMA. It's basically a HW mask, and an action that need to be done prior in creating the buffer. 

    If you need more successive samples, you can increase the number of kernel buffer count. 

    If you want to flush all queued kernel buffers (since they are automatically filled after the buffer is created), you need to destroy the buffer object.

    -Michael  

  • Thank you mhennerich, so a channel cannot be enabled or disabled in real time then, is what I hear you saying. Is there any way at all to have control of what is getting written to the buffer and when, or is this whole thing just designed to be a passive stream consumer? As for what it will provide me, it avoids memcopys. Why stream data to one place in memory, just to have to copy it somewhere else at a later time? It isn't optimal, but with no other option that is what I will have to do I suppose.