Author Topic: Need help with using GPIO  (Read 32798 times)

Offline patwood

  • Linux geek
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1660
  • Karma: +129/-0
    • View Profile
Re: Need help with using GPIO
« Reply #30 on: July 01, 2013, 11:23:58 am »
You know, I had planned on writing up a short tutorial after I had this all working and tested -- then promptly forgot!

So, here we go:

First, I'm assuming you're using the newer gpio_sunxi.ko GPIO drivers, not the deprecated sun4i_gpio drivers.

Next, I'll assume you've read through this thread and understand the basics: how to add GPIO pins to script.bin, how to set them up for input/output with the /sys files, how to read/write them, etc.

Now, only some GPIO pins are hooked up to the A10 interrupt controller.  Look for EINT here: http://linux-sunxi.org/A10/PIO.  I used PH14 and PH15, so my FEX entry looks like this:

Code: [Select]
[gpio_para]
gpio_used = 1
gpio_num = 2
gpio_pin_1 = port:PH14<0><default><default><default>
gpio_pin_2 = port:PH15<0><default><default><default>

If you use a pin that's not connected to the interrupt controller, you won't see an edge file in /sys/class/gpio/gpio1_ph14.

Once you have a gpio sys device with an edge file, you can set edge triggering to rising, falling, or both (default is none, so you have to set it to something), e.g.,

Code: [Select]
echo both > /sys/class/gpio/gpio1_ph14/edge
I also set active_low to 1, as that's simply how the polarity on my button was wired up.

At this point, you can poll() on the value /sys file.  See the attached example for how I'm doing it.  Just note that I'm polling on error, not input.  I don't know why this works and input doesn't, but that's what I determined empirically.

The attached python program prints a message when the button is pressed and when it is released, along with the elapsed time.  It's run like this:

Code: [Select]
python /priscilla/checkswitch.py /sys/devices/platform/gpio-sunxi/gpio/gpio1_ph14
« Last Edit: May 02, 2014, 11:38:23 pm by patwood »

Offline chris

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Need help with using GPIO
« Reply #31 on: August 19, 2013, 11:57:47 am »
I know this thread is a bit old, but I also wanted to thank you for your info.

And I would like to comment on this:
At this point, you can poll() on the value /sys file.  See the attached example for how I'm doing it.  Just note that I'm polling on error, not input.  I don't know why this works and input doesn't, but that's what I determined empirically.

This behaviour is documented here:  https://www.kernel.org/doc/Documentation/gpio.txt
(Recommended reading for anybody interested in the /sys/class/gpio/ interface.)
Quote:
Code: [Select]
If you use poll(2), set the events POLLPRI and POLLERR.

Offline patwood

  • Linux geek
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1660
  • Karma: +129/-0
    • View Profile
Re: Need help with using GPIO
« Reply #32 on: August 19, 2013, 01:40:29 pm »
I know this thread is a bit old, but I also wanted to thank you for your info.

And I would like to comment on this:
At this point, you can poll() on the value /sys file.  See the attached example for how I'm doing it.  Just note that I'm polling on error, not input.  I don't know why this works and input doesn't, but that's what I determined empirically.

This behaviour is documented here:  https://www.kernel.org/doc/Documentation/gpio.txt
(Recommended reading for anybody interested in the /sys/class/gpio/ interface.)
Quote:
Code: [Select]
If you use poll(2), set the events POLLPRI and POLLERR.
Thanks for pointing that out.  Somehow I missed it the first time I read that document.

Offline dvanf

  • Newbie
  • *
  • Posts: 20
  • Karma: +0/-2
    • View Profile
Re: Need help with using GPIO
« Reply #33 on: December 02, 2013, 10:48:29 am »
Patwood,

On my CB2 I have Linux 3.4.43-sub7i, distribution GNU/Linux 7.2
When I do 'ls /sys/class/gpio', I only see two files, export and unexport.
What should I do to get gpiochip1 there (if it has to be there at all)?
I would like to use all the pins of port D (PD0 - PD27) available on the 50-pin connector, as I will not use an LCD display.
For this I have set [lcd0_para] to unused in script.bin using bin2fex and fex2bin.
And as I understand there have to be files like gpio32_pd0 and gpio31_pd1 (in my case up to gpio56_pd27) in /sys/class/gpio, how can I make these?

Thanks in advance, dvanf (Dick van Fulpen, Houten [NL])

Offline patwood

  • Linux geek
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1660
  • Karma: +129/-0
    • View Profile
Re: Need help with using GPIO
« Reply #34 on: December 02, 2013, 10:11:26 pm »
First, read this: http://lxr.free-electrons.com/source/Documentation/gpio.txt

You create the gpio files by writing numbers starting at one to the export file, like this: http://www.cubieforums.com/index.php/topic,103.msg545.html#msg545

Nester

  • Guest
Re: Need help with using GPIO
« Reply #35 on: December 07, 2013, 06:02:08 pm »
Hi guys,

I'm interested in this topic to.
Actually i'm trying to port the rpi_lirc driver  for cubieboard.

Source: http://svn.stmlabs.com/svn/raspbmc/patches/lirc/drivers/lirc_rpi/lirc_rpi.c

The aim is to make lirc work on GPIOs for cubieboard as well, to catch RF 433mhz sensors signals with Lirc.

The lirc_rpi module, uses the gpio library, which is actually implemented also by the gpio-sunxi.
It works in kernel space registering the GPIOs interrupts in kernel space through the gpio library to measure the time between each gpio pulse (basically sensors signatures).


I've been able to modify the driver but still seems it is not catching the IRQ.
Seems the problem is related to a bug in the gpio-sunxi which i've discovered it has been patched 3 months ago.

I'm recompiling the latest kernel version.
If anyone is interested let me know.

Cheers.
Marco.



ADBensi

  • Guest
Re: Need help with using GPIO
« Reply #36 on: April 06, 2014, 04:24:41 pm »
Please, re-attach the files, all not found :(
I wold like to use Interrupt on GPIO and one example code in C ou Python

Offline vivekgb

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Need help with using GPIO
« Reply #37 on: February 23, 2015, 04:48:35 am »
yes plz update the files :(
i am struck as i am unable to use interrupt in my project