使用libgpio V1,如何在Raspberry Pi 4上设置给定线路的内部上拉?

问题描述 投票:0回答:1

我使用的是Raspberry Pi 4,我想将我的线路设置为使用内部PULL_UP(默认8以上的Raspberry线路都是pull_down):

使用 C++ 和 libgpiod:

try {
    gpiod::chip chip("gpiochip0");
    gpiod::line_bulk lines;

    const std::vector<io_gpio_t> offsets = {
        {1, 21},
        {2, 26},
        {3, 16},
        {4, 19}};

    for (io_gpio_t offset : offsets) {
        auto line = chip.get_line(offset.gpio_line);
        line.set_bias(gpiod::line::bias::pull_up); // <<---- ERROR HERE!!!!
        line.request({"io", gpiod::line_request::EVENT_BOTH_EDGES, 100000});
        lines.append(line);
    }

    while (true) {
        gpiod::line_bulk changed_lines = lines.event_wait(std::chrono::seconds(10));
        for (gpiod::line line : changed_lines) {
            auto event = line.event_read();
            int offset = line.offset();
            process_digital_in(offset, event); // Process event
        }
    }
} catch (const std::exception &e) {
    std::cerr << "Error: " << e.what() << std::endl;
}

}

看到libgpiod 1.6版本不支持set_bias:

src/IO.cpp:79:18: error: ‘class gpiod::line’ has no member named ‘set_bias’; did you mean ‘set_flags’?
   79 |             line.set_bias(gpiod::line::bias::pull_up);
      |                  ^~~~~~~~
      |                  set_flags
src/IO.cpp:79:40: error: ‘gpiod::line::bias’ is not a class, namespace, or enumeration
   79 |             line.set_bias(gpiod::line::bias::pull_up);
      |                                        ^~~~

编译器建议 set_flags,但我找不到相关文档。

如何正确设置输入以通过编程方式使用内部上拉电阻?

c++ raspberry-pi libgpiod
1个回答
0
投票

首先,

set_bias()
libgpiod v2调用,而不是v1,因此看起来您正在尝试将v2文档或示例应用于v1。那是行不通的。

只要您拥有最新版本的 libgpiod (1.5+) 和内核 (5.5+),就可以使用 libgpiod v1 设置偏差。

您需要在传递给请求的

struct line_request
中设置适当的标志 - 您在其中传递“100000” - 无论这意味着什么。应该是:

line.request({"io", gpiod::line_request::EVENT_BOTH_EDGES, gpiod::line_request::FLAG_BIAS_PULL_UP});

此外,IIRC,

gpiod::line_bulk
适用于您一次请求多行的情况,而不是您单独请求并手动添加到批量中的情况,因此这也不会按您的预期工作。您需要将行添加到批量中,然后请求它,所以类似于:

for (io_gpio_t offset : offsets) {
    lines.append(chip.get_line(offset.gpio_line));
}
lines.request({"io", gpiod::line_request::EVENT_BOTH_EDGES, gpiod::line_request::FLAG_BIAS_PULL_UP});

但是帮自己一个忙,切换到 libgpiod v2 ,它有更好的 API、更好的文档和示例,并且正在积极开发。它尚未针对 Raspberry PiOS 进行打包,但从源代码安装并不困难。 libgpiod v1 使用的内核 API 已被弃用,就像旧的 sysfs 接口一样,最终将被删除。新的开发应该使用当前的 API,因此 libgpiod v2

© www.soinside.com 2019 - 2024. All rights reserved.