Quantcast
Channel: EngineerZone: Message List
Viewing all articles
Browse latest Browse all 22625

Re: Trouble writing some AD9361 PHY registers using libiio

$
0
0

Some private helper methods I wrote:

 

static int get_channel_name_from_index(

        // Channel index

        unsigned int chan,

        // Channel name string

        char * chan_name)

{

    if (chan == 0) {

        strcpy(chan_name, CHN_ID_0);

    } else if (chan == 1) {

        strcpy(chan_name, CHN_ID_1);

    } else {

        fprintf(stderr, "Invalid channel index\n");

        return 1;

    }

 

    return 0;

 

}; // static int get_channel_name_from_index(...)

 

static int get_attribute(

        // Context

        const struct iio_context * ctx,

        // Channel index (0-based index)

        unsigned int chan,

        // Channel is output (transmit), else input (receive)

        bool output,

        // Attribute to find

        const char * atr_to_find,

        // Attribute channel, pass by reference

        const struct iio_channel ** pchn,

        // Attribute name, pass by reference

        const char ** patr_name)

{

    char chan_name[64];

    struct iio_device * dev;

 

    if (get_channel_name_from_index(chan, chan_name) != 0) {

        return 1;

    }

    dev = iio_context_find_device(ctx, DEV_FMCOMMS2_PHY);

    if (dev == NULL) {

        fprintf(stderr, "Unable to find device\n");

        return 2;

    }

    *pchn = iio_device_find_channel(dev, chan_name, output);

    if (*pchn == NULL) {

        fprintf(stderr, "Unable to find channel for name: %s\n", chan_name);

        return 3;

    }

    *patr_name = iio_channel_find_attr(*pchn, atr_to_find);

    if (*patr_name == NULL) {

        fprintf(stderr, "Unable to find attribute: %s, for channel: %s\n",

                atr_to_find, chan_name);

        return 4;

    }

 

    return 0;

} // get_attribute(...)

 

Here's how I get/set the RX RF bandwidth:

 

int get_rx_rf_bandwidth(

        // Context

        struct iio_context * ctx,

        // Channel index (0-based index)

        unsigned int chan,

        // RF bandwidth (Hz), pass by reference

        double *pvalue)

{

    const struct iio_channel * chn;

    const char * atr_name;

    ssize_t ret;

    const char atr_to_find[64] = ATR_RF_BW;

    char atr_value[1024];

   

    if (get_attribute(ctx, chan, false, atr_to_find, &chn, &atr_name) != 0) {

        fprintf(stderr, "Unable to find attribute: %s\n", atr_to_find);

        return 1;

    }

    ret = iio_channel_attr_read(chn, atr_to_find, atr_value, sizeof(atr_value));

    if (ret <= 0) {

        fprintf(stderr, "Unable to read channel attribute: %s, returned errno code: %zd, string: %s\n",

                atr_to_find, ret, strerror(ret));

        return 2;

    }

    *pvalue = atof(atr_value);

   

    return 0;

} // int get_rx_rf_bandwidth(...)

 

int set_rx_rf_bandwidth(

        // Context

        struct iio_context * ctx,

        // Channel index (0-based index)

        unsigned int chan,

        // RF bandwidth (Hz)

        double value)

{

    const struct iio_channel * chn;

    const char * atr_name;

    ssize_t ret;

    const char atr_to_find[64] = ATR_RF_BW;

    char atr_value[1024];

 

    if (get_attribute(ctx, chan, false, atr_to_find, &chn, &atr_name) != 0) {

        fprintf(stderr, "Unable to find attribute: %s\n", atr_to_find);

        return 1;

    }

    if (sprintf(atr_value, "%f", value) < 0) {

        fprintf(stderr, "Unable to convert value to string\n");

        return 2;

    }

    ret = iio_channel_attr_write(chn, atr_name, atr_value);

    if (ret <= 0) {

        fprintf(stderr, "Unable to read channel attribute: %s, returned errno code: %zd, string: %s\n",

                atr_to_find, ret, strerror(ret));

        return 3;

    }

   

    return 0;

} // int set_rx_rf_bandwidth(...)


Viewing all articles
Browse latest Browse all 22625

Trending Articles