再次导出同一个 GPIO 时删除 GPIO 文件夹

The GPIO folder is deleted when the same GPIO is exported again

我基本上是在我的应用程序中导出任何 gpios。我开始在 beaglebone black 上实现它。没有问题后,我继续在 beaglebone blue 上实现应用程序。所以,我意识到应用程序第一次可以运行,然后第二次就失败了。之后,它再次可以 运行 第三次。

经过一些搜索,我意识到在 beaglebone blue 上导出任何 gpio 时会出现问题,它不仅不会覆盖所需的 GPIO 值,还会删除现有的 gpio。例如,简单的 echo 23 > /sys/class/gpio/export 命令给出 beaglebone black 和 beaglebone blue 的输出如下:

Beaglebone 黑色 (3.8.13-bone86)

root@beaglebone# echo 23 > /sys/class/gpio/export
root@beaglebone# ls /sys/class/gpio/
export  gpio23  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
root@beaglebone# echo 23 > /sys/class/gpio/export
bash: echo: write error: Device or resource busy
root@beaglebone# ls /sys/class/gpio/
export  gpio23  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport

Beaglebone 蓝色 (4.9.105-ti-rt-r113)

root@beaglebone# echo 23 > /sys/class/gpio/export
root@beaglebone# ls /sys/class/gpio/
export  gpio23  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
root@beaglebone# echo 23 > /sys/class/gpio/export
bash: echo: write error: Operation not permitted
root@beaglebone# ls /sys/class/gpio/
export  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport

那么,如何解决 beaglebone blue 上的这个问题?

我在 BeagleBone Black (4.14.71-ti-r80) 中重复了这个 'bug'。

它出现在从 Derek Molloy Issue 1 优秀的 Exploring BeagleBone 一书中复制的一些 C++ 中,该书是在 3.8 时编写的内核是最新的。

我查了新版的书(今年出的),没有提到这个问题。

我 'fixed' 它通过测试 gpio# 目录是否存在,然后将 gpio 编号写入导出 sysfs 文件。

即我检查了“/sys/class/gpio/gpio7”

是否存在

在将 7 写入“/sys/class/gpio/export”之前

我将 Molloy 的 GPIO.cpp 中现有的 exportGPIO 函数更改为:

int GPIO::exportGPIO(){
    return write(GPIO_PATH, "export", this->number);
}

至:

int GPIO::exportGPIO(){
    // APM - 2019-05-27 - export twice kills it

    if (checkDirectoryExists(GPIO_PATH + this->name)) {
        cout << std::string("GPIO: export ") + GPIO_PATH + this->name + " already exists" << endl;
        return 0;
    }
    return write(GPIO_PATH, "export", this->number);
}

我根据 stat() 调用编写的 checkDirectoryExists 函数。

类似的方法在 shell 脚本中应该非常简单。