Skip to content

Commit

Permalink
treewide: move to libiio 1.0i API
Browse files Browse the repository at this point in the history
While at it, increased cmake minimum version to 3.10 as in other
projects.

Signed-off-by: Nuno Sa <[email protected]>
  • Loading branch information
nunojsa committed Sep 12, 2023
1 parent a24e263 commit c947b8a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 67 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.10)
project(ad9361 C)

set(LIBAD9361_VERSION_MAJOR 0)
Expand Down Expand Up @@ -78,7 +78,7 @@ endif()
add_definitions(-D_POSIX_C_SOURCE=200809L -D__XSI_VISIBLE=500 -DLIBAD9361_EXPORTS=1)

find_library(LIBIIO_LIBRARIES iio)
find_path(LIBIIO_INCLUDEDIR iio.h)
find_path(LIBIIO_INCLUDEDIR iio/iio.h)

set(LIBAD9361_HEADERS ad9361.h)

Expand Down
4 changes: 2 additions & 2 deletions ad9361_baseband_auto_rate.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "ad9361.h"

#include <errno.h>
#include <iio.h>
#include <iio/iio.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -146,7 +146,7 @@ int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate)
int dacrate, txrate, max;
char readbuf[100];

ret = iio_device_attr_read(dev, "tx_path_rates", readbuf, sizeof(readbuf));
ret = iio_device_attr_read_raw(dev, "tx_path_rates", readbuf, sizeof(readbuf));
if (ret < 0)
return ret;
ret = sscanf(readbuf, "BBPLL:%*d DAC:%d T2:%*d T1:%*d TF:%*d TXSAMP:%d", &dacrate, &txrate);
Expand Down
4 changes: 2 additions & 2 deletions ad9361_design_taps.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "ad9361.h"
#include "filterdesigner/internal_design_filter_cg.h"
#include <errno.h>
#include <iio.h>
#include <iio/iio.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -260,7 +260,7 @@ int apply_custom_filter(struct iio_device *dev, unsigned dec_tx,

int dacrate, txrate, max;
char readbuf[100];
ret = iio_device_attr_read(dev, "tx_path_rates", readbuf, sizeof(readbuf));
ret = iio_device_attr_read_raw(dev, "tx_path_rates", readbuf, sizeof(readbuf));
if (ret < 0)
return ret;
ret = sscanf(readbuf, "BBPLL:%*d DAC:%d T2:%*d T1:%*d TF:%*d TXSAMP:%d",
Expand Down
66 changes: 44 additions & 22 deletions ad9361_fmcomms5_phase_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "ad9361.h"

#include <errno.h>
#include <iio.h>
#include <iio/iio.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -74,6 +74,8 @@ static struct iio_channel *dds_out[2][8];
static struct iio_buffer *rxbuf;
static struct iio_channel *rxa_chan_real, *rxa_chan_imag;
static struct iio_channel *rxb_chan_real, *rxb_chan_imag;
static struct iio_channels_mask *dev_rx_mask;
static struct iio_stream *dev_rx_stream;

static void ad9361_sleep_ms(void)
{
Expand Down Expand Up @@ -292,62 +294,75 @@ int streaming_interfaces(bool enable)
if (!(rxa_chan_real && rxa_chan_imag && rxb_chan_real && rxb_chan_imag))
streaming_interfaces(false);

iio_channel_enable(rxa_chan_real);
iio_channel_enable(rxa_chan_imag);
iio_channel_enable(rxb_chan_real);
iio_channel_enable(rxb_chan_imag);
rxbuf = iio_device_create_buffer(dev_rx, SAMPLES, false);
if (!rxbuf)
iio_channel_enable(rxa_chan_real, dev_rx_mask);
iio_channel_enable(rxa_chan_imag, dev_rx_mask);
iio_channel_enable(rxb_chan_real, dev_rx_mask);
iio_channel_enable(rxb_chan_imag, dev_rx_mask);
rxbuf = iio_device_create_buffer(dev_rx, 0, dev_rx_mask);
if (iio_err(rxbuf)) {
rxbuf = NULL;
streaming_interfaces(false);
}
dev_rx_stream = iio_buffer_create_stream(rxbuf, 4, SAMPLES);
if (iio_err(dev_rx_stream)) {
dev_rx_stream = NULL;
streaming_interfaces(false);
}
} else {
if (dev_rx_stream) {
iio_stream_destroy(dev_rx_stream);
}
if (rxbuf) {
iio_buffer_destroy(rxbuf);
}
if (rxa_chan_real) {
iio_channel_disable(rxa_chan_real);
iio_channel_disable(rxa_chan_real, dev_rx_mask);
}
if (rxa_chan_imag) {
iio_channel_disable(rxa_chan_imag);
iio_channel_disable(rxa_chan_imag, dev_rx_mask);
}
if (rxb_chan_real) {
iio_channel_disable(rxb_chan_real);
iio_channel_disable(rxb_chan_real, dev_rx_mask);
}
if (rxb_chan_imag) {
iio_channel_disable(rxb_chan_imag);
iio_channel_disable(rxb_chan_imag, dev_rx_mask);
}
return -1;
}
return 0;
}

void read_buffer_data(struct iio_channel *chn, struct iio_buffer *buf,
void read_buffer_data(struct iio_channel *chn, const struct iio_block *block,
void *dst, size_t len)
{
uintptr_t src_ptr, dst_ptr = (uintptr_t)dst, end = dst_ptr + len;
unsigned int bytes = iio_channel_get_data_format(chn)->length / 8;
uintptr_t buf_end = (uintptr_t)iio_buffer_end(buf);
ptrdiff_t buf_step = iio_buffer_step(buf);
uintptr_t buf_end = (uintptr_t)iio_block_end(block);
const struct iio_device *rx = iio_channel_get_device(chn);
ptrdiff_t buf_step = iio_device_get_sample_size(rx, dev_rx_mask);

for (src_ptr = (uintptr_t)iio_buffer_first(buf, chn);
for (src_ptr = (uintptr_t)iio_block_first(block, chn);
src_ptr < buf_end && dst_ptr + bytes <= end;
src_ptr += buf_step, dst_ptr += bytes)
iio_channel_convert(chn, (void *)dst_ptr, (const void *)src_ptr);
}

double estimate_phase_diff(double *estimate)
{
ssize_t nbytes_rx = iio_buffer_refill(rxbuf);
if (!nbytes_rx)
return nbytes_rx;
const struct iio_block *rxblock;

rxblock = iio_stream_get_next_block(dev_rx_stream);
if (iio_err(rxblock))
return iio_err(rxblock);

int16_t myData0_i[SAMPLES], myData0_q[SAMPLES];
int16_t myData2_i[SAMPLES], myData2_q[SAMPLES];

// Read data from all channels
read_buffer_data(rxa_chan_real, rxbuf, myData0_i, SAMPLES * sizeof(int16_t));
read_buffer_data(rxa_chan_imag, rxbuf, myData0_q, SAMPLES * sizeof(int16_t));
read_buffer_data(rxb_chan_real, rxbuf, myData2_i, SAMPLES * sizeof(int16_t));
read_buffer_data(rxb_chan_imag, rxbuf, myData2_q, SAMPLES * sizeof(int16_t));
read_buffer_data(rxa_chan_real, rxblock, myData0_i, SAMPLES * sizeof(int16_t));
read_buffer_data(rxa_chan_imag, rxblock, myData0_q, SAMPLES * sizeof(int16_t));
read_buffer_data(rxb_chan_real, rxblock, myData2_i, SAMPLES * sizeof(int16_t));
read_buffer_data(rxb_chan_imag, rxblock, myData2_q, SAMPLES * sizeof(int16_t));

ad9361_sleep_ms();

Expand Down Expand Up @@ -508,6 +523,7 @@ int phase_sync(struct iio_context *ctx, long long sample_rate, long long lo)
{
// Set analog bandwidth same as sample rate
long long bw = sample_rate;
unsigned int n_channels;

// Set up devices
if (!setup_iio_devices(ctx))
Expand Down Expand Up @@ -545,6 +561,11 @@ int phase_sync(struct iio_context *ctx, long long sample_rate, long long lo)
ret = trx_phase_rotation(dev_tx_slave, 0.0);
CHECK(ret);

n_channels = iio_device_get_channels_count(dev_rx);
dev_rx_mask = iio_create_channels_mask(n_channels);
if (!dev_rx_mask)
return -ENOMEM;

// Align receiver on Chip A (TX from chip A) with BIST loopback
configure_ports(1); // Chip A -> Chip A | FPGA Loopback on B
double phase_est_rx_slave = 0, phase_est = 0;
Expand All @@ -569,6 +590,7 @@ int phase_sync(struct iio_context *ctx, long long sample_rate, long long lo)
// Set rotation of chip B receiver to originally measured
ret = trx_phase_rotation(dev_rx_slave, phase_est_rx_slave);
CHECK(ret);
iio_channels_mask_destroy(dev_rx_mask);

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions ad9361_multichip_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "ad9361.h"

#include <errno.h>
#include <iio.h>
#include <iio/iio.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -82,11 +82,11 @@ int ad9361_multichip_sync(struct iio_device *master, struct iio_device **slaves,
}

/* Move the parts int ALERT for MCS */
iio_device_attr_read(master, "ensm_mode", ensm_mode[0], sizeof(ensm_mode));
iio_device_attr_read_raw(master, "ensm_mode", ensm_mode[0], sizeof(ensm_mode));
iio_device_attr_write(master, "ensm_mode", "alert");

for (i = 0; i < num_slaves; i++) {
iio_device_attr_read(slaves[i], "ensm_mode", ensm_mode[i + 1], sizeof(ensm_mode));
iio_device_attr_read_raw(slaves[i], "ensm_mode", ensm_mode[i + 1], sizeof(ensm_mode));
iio_device_attr_write(slaves[i], "ensm_mode", "alert");
}

Expand Down
6 changes: 1 addition & 5 deletions test/auto_rate_test_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
#include <string.h>
#include <errno.h>

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

#define RATE_TOLERANCE_HZ 2

Expand Down Expand Up @@ -55,7 +51,7 @@ int main(void)
const char* uri = getenv("URI_AD9361");
if (uri == NULL)
exit(0);// Cant find anything don't run tests
ctx = iio_create_context_from_uri(uri);
ctx = iio_create_context(NULL, uri);
if (ctx == NULL) {
printf("No device found... skipping test");
exit(0);// Cant find anything don't run tests
Expand Down
6 changes: 1 addition & 5 deletions test/filter_designer_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
#include <stdlib.h>
#include <string.h>

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

int main(void)
{
Expand All @@ -25,7 +21,7 @@ int main(void)
const char* uri = getenv("URI_AD9361");
if (uri == NULL)
exit(0);// Cant find anything don't run tests
ctx = iio_create_context_from_uri(uri);
ctx = iio_create_context(NULL, uri);
if (ctx == NULL)
exit(0);// Cant find anything don't run tests
dev = iio_context_find_device(ctx, "ad9361-phy");
Expand Down
Loading

0 comments on commit c947b8a

Please sign in to comment.