8

刚开始,我想声明我知道 Python 和其他用于在 Raspberry PI 上操作 GPIO 的高级实现。我也一直在使用 WiringPI C API,并且在 Raspbian Jessie 上遇到了问题,即使我没有更改任何代码行,我在 Raspbian Wheezy 上也没有。WiringPI C API 开发人员还表示,他没有立即支持 Raspbian Jessie 的计划,所以我有点像没有桨的小溪。

出于这个原因,我一直在阅读以下关于使用sysfs访问 Raspberry PI GPIO 的教程(以及其他教程),因为这似乎是一种在不使用 WiringPI 且不编写我自己的 GPIO 库的情况下寻址 GPIO 的方法:

http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html

根据本教程,要将 GPIO17 设置为输入,请将字符串“in”写入文件句柄:

/sys/class/gpio/gpio/17/direction

...然后我可以从以下位置读取 GPIO 输入值:

/sys/class/gpio/gpio17/值

这一切都很好,但我没有选择在我的生产板上重新安装上拉电阻。是否可以使用sysfs设置 Raspberry PI 的内置上拉和下拉电阻?

此外,如果无法通过sysfs设置上拉和下拉电阻,我是否正确假设即使在最新的 Raspbian Jessie 中,唯一的其他方法是直接写入 GPIO 寄存器?即即使在 Raspbian Jessie 中也没有用于 GPIO 编程的官方 C API?

4

2 回答 2

3

您可以使用设备树覆盖在启动时激活上拉和端口方向。

您必须修改和编译 dts(源代码),将其放在 /boot/overlays 中,并在 config.txt 中启用它。说明在源标题中。(感谢菲尔的帮助!)

/*
* Overlay for enabling gpio's to pull at boot time
* this overlay uses pincctrl to initialize the pull-up register for the the listed gpios
* the compatible="gpio-leds" forces a module probe so the pinctrl does something
*
* To use this dts:
* copy this to a file named gpio_pull-overlay.dts
* modify the brcm,pins, brcm,function, and brcm,pull values
* apt-get install device-tree-compiler
* dtc -@ -I dts -O dtb -o gpio_pull-overlay.dtb gpio_pull-overlay.dts
* sudo cp gpio_pull-overlay.dtb /boot/overlays
* add this line to the end config.txt: dtoverlay=gpio_pull
* reboot
*/

/dts-v1/;
/plugin/;
/ {
  compatible = "brcm,bcm2835", "brcm,bcm2708";
  fragment@0 {
    target = <&gpio>;
    __overlay__ {
       gpio_pins: gpio_pins {
          brcm,pins = <30 31 32 33>; /* list of gpio(n) pins to pull */
          brcm,function = <0 1 0 1>; /* boot up direction:in=0 out=1 */
          brcm,pull = <2 0 1 0>; /* pull direction: none=0, 1 = down, 2 = up */
       };
    };
  };
  fragment@1 {
    target-path = "/soc";
    __overlay__ {
       gpiopull:gpiopull {
          compatible = "gpio-leds";
          pinctrl-names = "default";
          pinctrl-0 = <&gpio_pins>;
          status = "okay";
       };
    };
  };
  __overrides__ {
     gpio_pull = <&gpiopull>,"status";
  };
};
于 2015-11-12T15:26:48.487 回答
1

user1967890 的解决方案对我来说运行良好,直到 2020 年 5 月 28 日或 29 日左右,然后我进行了适当的更新/升级,我相信有一个 Raspberry Pi 内核升级,之后它停止工作。无论如何,我不得不求助于使用 Python 解决方案。所以我现在在启动时使用以下命令在 GPIO 引脚 17 和 18 上设置上拉电阻,并确保在 GPIO 22 到 25 上都没有设置上拉电阻和下拉电阻。是的,我意识到这段代码可以通过使用来缩短设置 GPIO_PIN_NUMBER 的循环,但它只在启动时运行一次,所以我不太担心。

#!/usr/bin/python
import RPi.GPIO as GPIO

GPIO_PIN_NUMBER=17
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN_NUMBER, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO_PIN_NUMBER=18
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN_NUMBER, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO_PIN_NUMBER=22
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN_NUMBER, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

GPIO_PIN_NUMBER=23
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN_NUMBER, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

GPIO_PIN_NUMBER=24
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN_NUMBER, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

GPIO_PIN_NUMBER=25
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN_NUMBER, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

显然,如果我想拉下 GPIO 引脚,我可以使用 GPIO.PUD_DOWN 而不是 GPIO.PUD_UP。我不必安装任何尚未出现在我的 Raspberry Pi 上的东西,尽管在以前的软件设置过程中可能已经安装了一些东西,而这些东西可能不会出现在全新的 Raspbian 安装中。

于 2020-06-04T20:49:24.327 回答