Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raspberry PI, GPIO Pull UP/DOWN resistors with SYSFS

Just off the bat I'd like to state that I'm aware of Python and other high level implementations for manipulating GPIO on the Raspberry PI. I've also been using the WiringPI C API and am experiencing problems with it on Raspbian Jessie that I was not having on Raspbian Wheezy even though I have not changed a single line of code. Also the WiringPI C API developer says he has no immediate plans to support Raspbian Jessie so I'm kind of up a creek without a paddle.

For this reason I've been reading the following tutorial (among others) on accessing Raspberry PI GPIOs using sysfs since this seems to be one way of addressing GPIO without using WiringPI and without writing my own GPIO library:

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

According to this tutorial, to set GPIO17 as an input, you write the string 'in' to the file handle:

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

...and then I can read GPIO input values from:

/sys/class/gpio/gpio17/value

This is all well and good but I do not have the option of retro fitting pull-up resistors to my production boards. Is it possible to set the Raspberry PI's built in pull-up and pull-down resistors using sysfs?

Also, if setting the pull-up and pull-down resistors via sysfs is not possible am I correct in assuming that even in the latest Raspbian Jessie the only other way to do this is write directly to GPIO registers? i.e. even in Raspbian Jessie there is no official C API for GPIO programming?

like image 321
os x nerd Avatar asked Nov 10 '15 09:11

os x nerd


1 Answers

You can use a device-tree overlay to activate the pull-ups and port direction at boot up.

You will have to modify and compile the dts (source), place it in /boot/overlays, and enable it in config.txt. The instructions are in the source header. (Thanks to PhillE for his help!)

/*
* 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";
  };
};
like image 165
user1967890 Avatar answered Oct 01 '22 19:10

user1967890