/sys/class/gpio

GPIO lines can be read and written via pseudo-files in the /sys/class/gpio directory on most Linux systems.

GPIO-numbering

( found info here in the forum.pine64.org )

There are several banks of gpios, identified with letters, so A=0, B=1, C=2 etc. Multiply this letter-code by 32 and add the number following as per:


/* Convert the hardware spec strings such as PC7 into equivalent GPIOnnn names. 
	Returns the calculated gpio number. */ 

int convertgpioname(char *gname,    /* String with PC15 or similar string in it */
	char *gpiofile,                 /* Name of the GPIOnnn file */
	size_t gpiofilemax)             /* Available length of this */
{
	int gpiono;

	gpiono = (gname[1] - 'A') * 32 + strtol(gname+2, NULL, 10); 
	snprintf(gpiofile, gpiofilemax, "gpio%u", gpiono);
	return(gpiono);
}

This same basic numbering scheme applies to gpio-lines references inside kernel modules.

The pseudo-file /sys/kernel/debug/gpio lists all the gpios allocated in the kernel, either via a module or via sysfs, and shows their states.

Use with /sys/class/gpio

Thus, for example, pin P11, that is, the Pi-2 connector's pin 11, which is named PC7, has C=2, times 32 is 64, then add the 7, for a total of 71, so this is identified as GPIO71.

Write 71 to /sys/class/gpio/export. The system makes the /sys/class/gpio/gpio71/ directory for us with the usual contents.

We now have /sys/class/gpio/gpio71/direction to be set to in or out -- even if it starts out saying in it doesn't read inputs until written. This is because exporting the gpio line doesn't set its function. (see notes here). Then value will contain 0 for L and 1 for H or vice versa according to the setting of active_low

active_lowWirevalue
0 L 0
0 H 1
1 L 1
1 H 0

We notice that there is no edge for gpio71 -- it doesn't support interrupts. Another higher-numbered one, PH9, or GPIO233, does support them, so we have to do some discovery on this.

The GPIOS that have this "edge" are the ones in the PB, PG, PH and PL series: PB0 thru PB9, 32-41, PG0 thru PG13, 192-205 PH2-226, PH3-227, PH5 thru PH9, 229-233, PL8 thru PL11, 360-363, PL2 thru PL6, 354-358 (not tested)

Also, PH7 (pin P26) has been seen to be used for something else in some kernels -- trying to export GPIO231 (231 = 7 + 7*32) returns "device busy".

Looking at /sys/kernel/debug/gpio shows that this gpio line is being used for a codec or sound-card output.

Note that exporting a gpio doesn't set or change its direction: on initial exporting the gpio is likely disabled, and will remain so until the direction is set to in or out as desired. Reading the direction bit will return in even though the gpio is disabled. Similarly, if a gpio is unexported and then later re-exported, it will remain set the same as it was before it was unexported. There has been some discussion of whether this is a bug or should be expected behavior, seeing as there are no automatic setting changes taking place.

To Pine 64+ main page


Powered by Apache

Valid XHTML 1.0!