无法在 Adafruit FT232H 上使用 gpiod_line_request_output() 访问 gpio 线

Can't access gpio line with gpiod_line_request_output() on Adafruit FT232H

我正在使用 Adafruit Ft232H breakout 将 GPIO 端口添加到我的 Linux 电脑。虽然我在 libftdi 和 bitbang 模式下成功地闪烁了一个 led,但我在 libgpiod 上没有同样的运气,因为 gpiod_line_request_output 失败了。

我系统的一些gpio信息:

sudo gpiodetect
gpiochip0 [ftdi-cbus] (4 lines)

sudo gpioinfo
gpiochip0 - 4 lines:
        line   0:      unnamed       unused   input  active-high 
        line   1:      unnamed       unused   input  active-high 
        line   2:      unnamed       unused   input  active-high 
        line   3:      unnamed       unused   input  active-high

这是试图访问第 0 行的 C 程序。

#include <stdio.h>
#include <stdlib.h>

#include <gpiod.h>

#define LINE_NUM 0

void gpio_fatal(struct gpiod_chip* chip, const char msg[20]);

int main(int argc, char** argv)
{
    struct gpiod_chip*  chip;
    struct gpiod_line*  line;
    const char path[] = "/dev/gpiochip0";

    chip = gpiod_chip_open(path);
    if(!chip)
    {
        fprintf(stderr, "Error opening path\n");
        return EXIT_FAILURE;
    }

    line = gpiod_chip_get_line(chip, LINE_NUM);
    if(!line)
    {
        fprintf(stderr, "error getting this line\n");
        return EXIT_FAILURE;
    }

    int ret = gpiod_line_request_output(line,
                        "ftdi-cbus",
                        1);
    if(ret != 0)
        gpio_fatal(chip, "Request output failed");

    for(;;)
    {
        gpiod_line_set_value(line, 1);
        printf("On\n");
        sleep(1);
        gpiod_line_set_value(line, 0);
        printf("Off\n");
        sleep(1);
    }

    gpiod_line_release(line);
    gpiod_chip_close(chip);

    return EXIT_SUCCESS;
}

void gpio_fatal(struct gpiod_chip* chip, const char* msg)
{
    fprintf(stderr, "%s\n", msg);
    gpiod_chip_close(chip);
    exit(EXIT_FAILURE);
}

运行 带有 sudo 的可执行文件给我:

sudo g_gpiod/build/g_gpiod 
Password: 
Request output failed

gpiod.h 声明失败函数如下:

/**
 * @brief Reserve a single line, set the direction to output.
 * @param line GPIO line object.
 * @param consumer Name of the consumer.
 * @param default_val Initial line value.
 * @return 0 if the line was properly reserved, -1 on failure.
 */
int gpiod_line_request_output(struct gpiod_line *line,
                  const char *consumer, int default_val) GPIOD_API;

参数似乎是正确的,为什么会失败?使用 libftdiCircuitPython 的其他示例可以访问端口并正常工作。

您需要烧写EEPROM来设置C5管脚的功能,例如使用来自 libftdi 的 ftdi_eeprom 命令。首先卸载ftdi_sio模块,保存原来的EEPROM:

$ sudo rmmod ftdi_sio
$ sudo ftdi_eeprom --verbose --device i:0x0403:0x6014 ft232h-orig.conf

FTDI eeprom generator v0.17
(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>
FTDI read eeprom: 0
EEPROM size: 256
VID:     0x0403
PID:     0x6014
Release: 0x0900
Bus Powered: 100 mA
Manufacturer: ÿÿÿÿÿÿÿÿ
Product:      ÿÿÿÿÿÿ
Serial:       ÿÿÿÿÿÿÿÿ
Checksum      : ffff
PNP: 1
Channel A has Mode UART
FT1284 Mode Clock is idle LOW, MSB first, No Flow Control
ACBUS has 4 mA drive
ADBUS has 4 mA drive
C0 Function: TRISTATE
C1 Function: TRISTATE
C2 Function: TRISTATE
C3 Function: TRISTATE
C4 Function: TRISTATE
C5 Function: TRISTATE
C6 Function: TRISTATE
C7 Function: TRISTATE
C8 Function: DRIVE_1
C9 Function: DRIVE_0
FTDI close: 0

文件 ft232h-orig.conf 仅包含一行 filename="ft232h-orig.bin"

您可以使用ftdi_sio模块控制FT232H的4个GPIO引脚:

| line | pin | remarks   |
==========================
| 0    | C5  | -         |
| 1    | C6  | -         | 
| 2    | C8  | red led   |
| 3    | C9  | green led |

Adafruit 板有两个 LED(红色和绿色)的阴极连接到引脚 C8 和 C9。默认的 EEPROM 将 C9 设置为 DRIVE_0,以便在电路板上电时绿色 LED 灯“亮起”。

这是我的 ftdi_eeprom 配置文件:

$ cat ft232h-libgpiod.conf
vendor_id="0x0403"
product_id="0x6014"
manufacturer="Adafruit"
product="FT232H Breakout"

# whatever
serial="20211223"
use_serial=true

max_power=100
self_powered=false
remote_wakeup=false

cha_type=UART
cha_vcp=false

cbush5=IOMODE
cbush6=IOMODE

# red led
cbush8=IOMODE

# green led
cbush9=DRIVE_0 # power-on indicator
#cbush9=IOMODE

$ sudo ftdi_eeprom --verbose --device i:0x0403:0x6014 --flash-eeprom ft232h-libgpiod.conf

您的程序现在可以在引脚 C5 上的第 0 行正常工作。作为奖励,您现在可以使用 libgpiod 命令控制红色 LED:

# turn red LED on (low active)
sudo gpioset gpiochip0 2=0