How to determine GPIO numbers?
#1
I've been experimenting with some of the GPIOs on my Pine, and I think I figured out how to talk to them using /sys/class/gpio. Unlike the Pi, which I am coming from, the GPIO numbers are different. I found another resource on the web that said in order to get the GPIO number for a different chip, you need to take the letter from the Pin number, convert it to a number, subtract 1, multiply by 32, then add the listed GPIO number. I thought that was strange, but figured I'd give it a try. For example, I wanted to write to the Pi 2 pin #16, which is GPIO 23. I did the folllowing:

Using the document attached to this post, I determined the pin I want is PC13.

P="Pin", we don't care about this.
C=3rd letter in alphabet, but we're nerds, we count from zero, so C=2 (subtract 1)
13=GPIO 23 We just use the raw number, I can't math.

So we take 2 and multiply it by 32, which seems to be a magic number. I'm sure it has significance to someone, but not a n00b like me. 2*32 = 64. Now we add 64+13 from the pin out document to get a final result of 77.

Then, as root, I changed into /sys/class/gpio and enabled the GPIO:
Code:
cd /sys/class/gpio
echo 77 > export
cd gpio77
echo "out" > direction
echo 1 > value
After doing that, hurrah, my LED lit up. The anode (positive) of the LED is connected to the pin mentioned above, the cathode (negative) is connected to one of the pins labeled GND.

I just wanted to share this with everyone because it took me an hour or so of google searches until I figured this out. It may be documented somewhere else on this site, but so far I couldn't find anything. For reference, I'm doing all my experimentation in longsleep's 3/20 build of Ubuntu 16.04 "Xenial."
  Reply
#2
(03-22-2016, 09:38 PM)joey Wrote: I've been experimenting with some of the GPIOs on my Pine, and I think I figured out how to talk to them using /sys/class/gpio. Unlike the Pi, which I am coming from, the GPIO numbers are different. I found another resource on the web that said in order to get the GPIO number for a different chip, you need to take the letter from the Pin number, convert it to a number, subtract 1, multiply by 32, then add the listed GPIO number. I thought that was strange, but figured I'd give it a try. For example, I wanted to write to the Pi 2 pin #16, which is GPIO 23. I did the folllowing:

Using the document attached to this post, I determined the pin I want is PC13.

P="Pin", we don't care about this.
C=3rd letter in alphabet, but we're nerds, we count from zero, so C=2 (subtract 1)
13=GPIO 23

So we take 2 and multiply it by 32, which seems to be a magic number. I'm sure it has significance to someone, but not a n00b like me. 2*32 = 64. Now we add 64+23 from the pin out document to get a final result of 77.

Then, as root, I changed into /sys/class/gpio and enabled the GPIO:
Code:
cd /sys/class/gpio
echo 77 > export
cd gpio77
echo "out" > direction
echo 1 > value
After doing that, hurrah, my LED lit up. The anode (positive) of the LED is connected to the pin mentioned above, the cathode (negative) is connected to one of the pins labeled GND.

I just wanted to share this with everyone because it took me an hour or so of google searches until I figured this out. It may be documented somewhere else on this site, but so far I couldn't find anything. For reference, I'm doing all my experimentation in longsleep's 3/20 build of Ubuntu 16.04 "Xenial."

Just an fyi, the 32 comes from the fact that gpio pins are grouped into banks of 32 pins. So pins in bank 0 are numbered 0 to 31, pins in bank 1  32 to 63, and so on.
  Reply
#3
(03-23-2016, 03:44 PM)patrickhwood Wrote: Just an fyi, the 32 comes from the fact that gpio pins are grouped into banks of 32 pins. So pins in bank 0 are numbered 0 to 31, pins in bank 1  32 to 63, and so on.

Thanks Patrick. That makes perfect sense.
  Reply
#4
(03-23-2016, 04:39 PM)joey Wrote:
(03-23-2016, 03:44 PM)patrickhwood Wrote: Just an fyi, the 32 comes from the fact that gpio pins are grouped into banks of 32 pins. So pins in bank 0 are numbered 0 to 31, pins in bank 1  32 to 63, and so on.

Thanks Patrick. That makes perfect sense.


Hi

So what am I doing wrong?  I don't have /sys/class/gpio - i have class but no gpio

however the pi2 pins seems to be live anyway, i can get an led to light running off the 5V and GND pins whenever the pine is running.

Is this a quirk for the android setup?  Am I going to have to install linux for the first time in my life?
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use dts or other setup to declare gpio pin Interrupt (e.g. a button)? dkebler 2 3,999 11-26-2024, 09:58 AM
Last Post: disesob294
  RPi.GPIO python module for Pine A64/A64+ aquilegia 98 148,017 10-26-2024, 07:17 AM
Last Post: Garesdq1d
  fm transmitter with gpio weasel18 2 5,473 09-10-2019, 04:28 AM
Last Post: desai_amogh
Lightbulb Sample GPIO codes highlighting RPi.GPIO-PineA64 and the PI bus MarkHaysHarris777 6 12,382 06-07-2019, 12:37 AM
Last Post: tllim
Star GPIO, SPI and I2C C++ Lib databit 7 12,456 02-04-2019, 05:45 AM
Last Post: Jeff R
Information Howto: Controlling Pine64 GPIO via the filesystem (sysfs) on linux pfeerick 4 12,776 01-24-2019, 03:36 AM
Last Post: Fifth
  GPIO and SPI SamR1 20 35,058 03-15-2018, 10:32 AM
Last Post: jomoengineer
Question GPIO shockr 7 16,298 03-11-2018, 01:52 AM
Last Post: jomoengineer
  Read GPIO problem shworker 14 23,794 08-17-2017, 01:21 PM
Last Post: martinayotte
  GPIO fiq capability joseph 3 6,774 11-10-2016, 06:07 PM
Last Post: joseph

Forum Jump:


Users browsing this thread: 1 Guest(s)