Raspberry PI,GPIO使用SYSFS上拉/下拉电阻

os *_*erd 7 c linux sysfs gpio raspberry-pi

刚刚开始,我想说明我已经了解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 /类/ GPIO/GPIO/17 /方向

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

/ SYS /类/ GPIO/gpio17 /值

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

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

use*_*890 3

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

您必须修改并编译 dts(源),将其放置在 /boot/overlays 中,并在 config.txt 中启用它。说明位于源头中。(感谢 PhillE 的帮助!)

/*
* 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";
  };
};
Run Code Online (Sandbox Code Playgroud)