Raspberry PI,GPIO使用SYSFS

时间:2015-11-10 09:17:20

标签: c linux raspberry-pi gpio sysfs

刚刚开始我想说明我已经知道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设置为输入,请将“”中的字符串'写入文件句柄:

/ SYS /类/ GPIO / GPIO / 17 /方向

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

/ SYS /类/ GPIO / gpio17 /值

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

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

2 个答案:

答案 0 :(得分:2)

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

您必须修改并编译dts(source),将其放在/ 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";
  };
};

答案 1 :(得分:0)

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安装中可能没有。