libiio

这篇博客详细介绍了如何在Ubuntu台机上编译libiio库,包括禁用特定功能的步骤,并探讨了交叉编译的过程。此外,还提供了libiio相关测试程序的使用方法,如iiod、iio_info和iio_attr等,涵盖本地和IP连接方式的数据读取。

目录

参考资料

Ubuntu台机编译

交叉编译

libxml

libiio

CMakeLists.txt修改

编译

测试程序

iiod

iio_info

local方式

 IP方式

iio_attr

显示可用上下文

显示可用设备 

显示设备属性

 显示可用通道

显示通道属性

 IP方式显示可用设备

iio_readdev

local方式读取数据

IP方式显示读取数据

于libiio开发


参考资料

libiio

Ubuntu台机编译

Ubuntu 16.04.4 LTS系统, 使用cmake编译,禁用AIO、DNS_SD。

$ git clone https://github.com/analogdevicesinc/libiio.git
$ cd libiio
$ git log -1
commit 1e9e1647809bc0bdeab22753d7e61a22e4030d07
Author: Michael Hennerich <michael.hennerich@analog.com>
Date:   Thu Jul 22 13:37:57 2021 +0200

    bindings: python iio.py: find_device() also use label

    When multiple devices with the same name exist, label is used to
    differentiate them. Update find_device to also support labels.

    Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
$ mkdir build
$ cd build
$ cmake ../ -DWITH_AIO=OFF -DHAVE_DNS_SD=OFF
-- cmake version: 3.5.1
-- The C compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Performing Test HAS_WPEDANTIC
-- Performing Test HAS_WPEDANTIC - Success
-- Performing Test HAS_WSHADOW
-- Performing Test HAS_WSHADOW - Success
-- Looking for strdup
-- Looking for strdup - found
-- Looking for strndup
-- Looking for strndup - found
-- Looking for strerror_r
-- Looking for strerror_r - found
-- Looking for newlocale
-- Looking for newlocale - found
-- Looking for in6addr_any
-- Looking for in6addr_any - found
-- Looking for libusb-1.0 : Found
-- Looking for libusb_get_version
-- Looking for libusb_get_version - found
-- Found Git: /usr/bin/git (found version "2.7.4")
-- Building with Network back end support
-- Performing Test WITH_NETWORK_EVENTFD
-- Performing Test WITH_NETWORK_EVENTFD - Success
-- Building without DNS-SD (ZeroConf) support
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.3")
-- Found BISON: /usr/bin/bison (found version "3.0.4")
-- Found FLEX: /usr/bin/flex (found version "2.6.0")
-- Looking for pthread_setname_np
-- Looking for pthread_setname_np - found
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/work/libiio/build
$ make
Scanning dependencies of target iio
[  2%] Building C object CMakeFiles/iio.dir/backend.c.o
[  4%] Building C object CMakeFiles/iio.dir/channel.c.o
[  6%] Building C object CMakeFiles/iio.dir/device.c.o
[  9%] Building C object CMakeFiles/iio.dir/context.c.o
[ 11%] Building C object CMakeFiles/iio.dir/buffer.c.o
[ 13%] Building C object CMakeFiles/iio.dir/utilities.c.o
[ 15%] Building C object CMakeFiles/iio.dir/scan.c.o
[ 18%] Building C object CMakeFiles/iio.dir/sort.c.o
[ 20%] Building C object CMakeFiles/iio.dir/usb.c.o
[ 22%] Building C object CMakeFiles/iio.dir/local.c.o
[ 25%] Building C object CMakeFiles/iio.dir/libini/libini.c.o
[ 27%] Building C object CMakeFiles/iio.dir/network.c.o
[ 29%] Building C object CMakeFiles/iio.dir/network-unix.c.o
[ 31%] Building C object CMakeFiles/iio.dir/xml.c.o
[ 34%] Building C object CMakeFiles/iio.dir/lock.c.o
[ 36%] Building C object CMakeFiles/iio.dir/iiod-client.c.o
[ 38%] Linking C shared library libiio.so
[ 38%] Built target iio
Scanning dependencies of target iio_tests_helper
[ 40%] Building C object tests/CMakeFiles/iio_tests_helper.dir/iio_common.c.o
[ 43%] Building C object tests/CMakeFiles/iio_tests_helper.dir/gen_code.c.o
[ 45%] Linking C static library libiio_tests_helper.a
[ 45%] Built target iio_tests_helper
Scanning dependencies of target iio_attr
[ 47%] Building C object tests/CMakeFiles/iio_attr.dir/iio_attr.c.o
[ 50%] Linking C executable iio_attr
[ 50%] Built target iio_attr
Scanning dependencies of target iio_genxml
[ 52%] Building C object tests/
libiio 是一个用于与 IIO(Industrial I/O)子系统交互的库,广泛用于 Linux 系统中与硬件传感器、ADC、DAC 等设备进行通信。它提供了一种高级接口,允许用户通过网络或本地访问 IIO 设备,适用于开发嵌入式系统、数据采集系统和测试工具。 以下是一些常见的 libiio 使用示例: ### 初始化 IIO 上下文 libiio 的核心是 `iio_context`,它是与设备通信的起点。可以通过本地或远程方式创建上下文: ```c #include <iio.h> struct iio_context *ctx; // 创建本地上下文 ctx = iio_create_default_context(); if (!ctx) { // 处理错误 } ``` ### 枚举设备和通道 在获得上下文后,可以枚举上下文中的所有设备及其通道: ```c unsigned int dev_count = iio_context_get_devices_count(ctx); for (unsigned int i = 0; i < dev_count; i++) { const struct iio_device *dev = iio_context_get_device(ctx, i); const char *dev_name = iio_device_get_name(dev); printf("Device %u: %s\n", i, dev_name ? dev_name : "Unnamed device"); unsigned int chn_count = iio_device_get_channels_count(dev); for (unsigned int j = 0; j < chn_count; j++) { const struct iio_channel *chn = iio_device_get_channel(dev, j); const char *chn_name = iio_channel_get_name(chn); printf(" Channel %u: %s (%s)\n", j, chn_name ? chn_name : "Unnamed channel", iio_channel_is_output(chn) ? "output" : "input"); } } ``` ### 读取输入通道数据 对于输入通道,可以使用 `iio_channel_read` 函数读取原始数据: ```c const struct iio_channel *chn = iio_device_find_channel(dev, "voltage0", false); if (chn) { char buf[1024]; ssize_t bytes_read = iio_channel_read(chn, buf, sizeof(buf)); if (bytes_read > 0) { printf("Read %zd bytes: %s\n", bytes_read, buf); } } ``` ### 写入输出通道数据 对于输出通道,可以使用 `iio_channel_write` 函数设置值: ```c const struct iio_channel *chn = iio_device_find_channel(dev, "voltage0", true); if (chn) { const char *value = "1000"; // 设置为 1000 µV ssize_t bytes_written = iio_channel_write(chn, value, strlen(value)); if (bytes_written > 0) { printf("Wrote %zd bytes\n", bytes_written); } } ``` ### 使用缓冲区进行高效数据采集 对于需要高吞吐量的应用,可以使用缓冲区机制进行高效数据采集: ```c struct iio_buffer *buf = iio_device_create_buffer(dev, 1024, false); if (buf) { while (1) { // 填充缓冲区 iio_buffer_refill(buf); // 获取数据指针 void *data = iio_buffer_start(buf); size_t length = iio_buffer_end(buf) - iio_buffer_start(buf); // 处理数据 process_data(data, length); // 标记数据已处理 iio_buffer_mark_consumed(buf, length); } iio_buffer_destroy(buf); } ``` ### 清理资源 使用完毕后,应释放相关资源: ```c iio_context_destroy(ctx); ``` 上述示例涵盖了 libiio 的基本使用方法,包括初始化上下文、枚举设备和通道、读写通道数据、使用缓冲区进行数据采集以及资源清理。这些功能使得 libiio 成为开发与 IIO 子系统交互应用程序的强大工具。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值