使用linux/gpio.h从Linux用户空间控制GPIO。

huangapple go评论75阅读模式
英文:

control gpio from linux userspace with linux/gpio.h

问题

我想写入 GPIO 128。在 linux/gpio.h 中有一个 struct gpiohandle_request,其中包含关于 GPIO 句柄请求的信息。我看到了这个示例

> 假设我们想要将引脚 17 和 27 配置为输出,并且我们想在引脚 17 上写入高电平 (1),在引脚 27 上写入低电平 (0)。

<!-- langyuage: lang-c -->

struct gpiohandle_request rq;
rq.lineoffsets[0] = 17;
rq.lineoffsets[1] = 27;
rq.lines = 2;
rq.flags = GPIOHANDLE_REQUEST_OUTPUT;

现在我对 lineoffsets 部分感到困惑。文档指定:

> * @lineoffsets: 由关联的 GPIO 设备的偏移索引指定的所需线路数组

什么是一条线呢?如果我想要配置 gpio0gpio127,我可以这样做一个循环吗?

<!-- language: lang-c -->

for (int i = 0; i &lt; 128; i++) {
    rq.lineoffsets[i] = i;
}
英文:

I want to write to GPIO 128. Inside linux/gpio.h there is struct gpiohandle_request which hold information about a GPIO handle request. I saw this example:

> Let say we want to configure pin 17 and 27 as OUTPUT and we want to write HIGH (1) on pin 17 and LOW (0) on pin 27.

<!-- langyuage: lang-c -->

struct gpiohandle_request rq;
rq.lineoffsets[0] = 17;
rq.lineoffsets[1] = 27;
rq.lines = 2;
rq.flags = GPIOHANDLE_REQUEST_OUTPUT;

Now I am confused about lineoffsets. Documentation specify that:

> * @lineoffsets: an array desired lines, specified by offset index for
> the associated GPIO device

What does a line mean? If I want to configure gpio0 to gpio127 can I make a loop this way:

<!-- language: lang-c -->

for (int i = 0; i &lt; 128; i++) {
    rq.lineoffsets[i] = i;
}

答案1

得分: 1

以下是您要翻译的内容:

Let say we want to configure pin 17 and 27 as OUTPUT and we want to write HIGH (1) on pin 17 and LOW (0) on pin 27.

让我们假设我们想将引脚 17 和 27 配置为输出,并且我们想在引脚 17 上写入高电平 (1),在引脚 27 上写入低电平 (0)。

Setting that structure does not set any GPIOs levels. When you call ioctl() it will only set the mode of the appropriate GPIOs.

设置该结构不会设置任何GPIO电平。当您调用ioctl()时,它只会设置适当GPIO的模式。

BTW this structure is depreciated.

顺便说一下,这个结构已经过时了。

What does a line mean

一行是硬件相关的,您需要查阅您的实现文档来了解其含义。

If i want to configure gpio0 to gpio127

如果您想配置gpio0到gpio127,请检查您是否有足够的GPIO并且lineoffsets字段足够大。即使您拥有足够的物理GPIO,控制结构也不必一次性处理它们。最大处理数在GPIOHANDLES_MAX中定义。

英文:

> Let say we want to configure pin 17 and 27 as OUTPUT and we want to
> write HIGH (1) on pin 17 and LOW (0) on pin 27.

Setting that structure does not set any GPIOs levels. When you call ioctl() it will only set the mode of the appropriate GPIOs.

BTW this structure is depreciated.

> What does a line mean

It is hardware dependent and you need to consult your implementation.

> If i want to configure gpio0 to gpio127

Check if you have enough GPIOs and if the lineoffsets field is large enough. Even if you have enough physical GPIOs the control structure does not have to handle them in "one go". The maximum handles number is defined in GPIOHANDLES_MAX.

huangapple
  • 本文由 发表于 2023年5月22日 22:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定