GPIO on Linux user-space

GPIO on Linux user-space

In linux system you have the privilege to drive digital outputs/inputs from user land with no more than a simple script. You also have the ability of catching hardware external interrupts through GPIO (although, it needs a bit of code and your hardware obviously should support this feature) doing these in user space is easy, but if you have safety/time critical functions, it might not be the best idea to use user space driver.

Like PWM, go to "/sys/class/gpio/" then you would see "gpiochipX" where X is the number of port (chip of gpio device), each port is 32 bit (might be different in different machines, mostly 32 bit) also ther are "export" and "unexport" files. you need to know the number of the gpio you want to use, 54 for instance, is the 22nd gpio from second gpiochip. or number 83 is 32+32+19 it means the 19th gpio of the third gpiochip. Anyhow, if this gpio is not used in the kernel (it's free, in the most SOCs you need to config some kind of IOMUX to get the gpio function of a pin/pad it is done in device tree nowadays. if you are using rpi or beagle bone they have done it for you!) so easily do: "echo 83 > /sys/class/gpio/export" then the directory gpio83 will appear in "/sys/class/gpio" (you can make it go away by unexport it the same way!)

Under gpioX (X is your gpio number) you would find: "value", "direction" and "edge" ("edge" is for those gpios that have the interrupt ability), so write "in" or "out" string in "direction" to make the gpio input or output! awesome.. right! read the "value" (in case you set it as input). Likewise write 1 or 0 to it (in case you have set it as output). In "edge" file (if exists) you should write one of these strings: {"none", "falling", "rising", "both"} well, obviously "none" means no interrupt, "falling" means interrupt on falling edge (1 to 0). "rising" means interrupt on rising edge (0 to 1) and "both" means interrupt on change (any). Then catching this is with function poll(2) in code. Just this interrupt, need code but simple gpio is doable with script. Using poll(2) is not hard. take a look at "man 2 poll"

At the end:1- What I explained often called "gpiolib", but I don't call it that! because it is not a library and so lead to confusion! 2- There is another more efficient way to do gpio control in user space. "gpio-cdev" I will explain it in a different talk.

Raha Rajabi

Senior C++ Programmer at Ubisoft

4 年

Thank you for sharing useful notes vahid, if you can share them on github site too, it will be searched more easily by others.

要查看或添加评论,请登录

Vahid Gharaee的更多文章

  • Linux Kernel Configuration

    Linux Kernel Configuration

    There are about 30 million lines of code in Linux kernel because it caries support for too many architectures and…

    3 条评论
  • PWM on Linux user-space

    PWM on Linux user-space

    What if you need to have PWM on your Embedded linux system? of course one way is to go to the kernel level, make your…

社区洞察

其他会员也浏览了