General Purpose Input/Output Driver

The GPIO driver provides simple access to the individual IO ports on the PIC32 chip.

Enable with:
  • DRIVER_GPIO = yes
  • None
Major device id:
  • 7
  • /dev/confa /dev/confb /dev/confc /dev/confd /dev/confe /dev/conff/dev/confg
  • /dev/porta /dev/portb /dev/portc /dev/portd /dev/porte /dev/portf /dev/portg

The /dev/confX devices configure the various aspects of the IO ports, and the /dev/portX devices allow access to the actual data.

Write to /dev/confX:

  • 'i' - configure the corresponding port pin as an input;
  • 'o' - configure the corresponding port pin as an output;
  • 'd' - configure the corresponding port pin as an open-drain output;
  • 'x' - deconfigure the corresponding port pin;
  • '.' - no action.

Write to /dev/portX:

  • '0' - set output pin low;
  • '1' - set output pin high;
  • '+' - invert the value of output pin;
  • '.' - no action.
  • /bin/portio


portio operation...


-i ports configure ports as input
-o ports configure ports as output
-d ports configure ports as open-drain output
-x ports deconfigure ports
-a port value assign port value
-s ports set ports to 1
-c ports clear ports (set to 0)
-r ports reverse ports (invert)
-g ports get port values
-m msec delay in milliseconds
-v verbose mode


a0 single pin
b3-7,11 list of pins
c same as c0-15

You can use several operations on one portio call, for example:

# portio -o a1-2 c15 -i b0-3 d -s a1 c15 -c a2 -g b d 

Configure pins PA0 and PA1 as input, PA2 as output and PA3 as open-drain output:

# echo .............doii > /dev/confa

Set pins A2 and A3 to high:

# echo .............11.. > /dev/porta

Configure all ports as output:

# portio -o a b c d e f g

Display configuration of all ports:

# cat /dev/conf*

Configure all ports as input:

# portio -i a b c d e f g
# cat /dev/conf*

Read pins of all ports:

# cat /dev/port*

Use ioctl() on any of /dev/portN devices to control pins from the user program. The generic interface is:

#include <sys/gpio.h>
ioctl(fd, port_ident | operation, pinmask);

Port identifier is:

  • GPIO_PORTA or GPIO_PORT(0) for port A;
  • GPIO_PORTB or GPIO_PORT(1) for port B;
  • GPIO_PORTC or GPIO_PORT(2) for port C;
  • GPIO_PORTD or GPIO_PORT(3) for port D;
  • GPIO_PORTE or GPIO_PORT(4) for port E;
  • GPIO_PORTF or GPIO_PORT(5) for port F;
  • GPIO_PORTG or GPIO_PORT(6) for port G.

Operation is one of:

  • GPIO_PORTA or GPIO_PORT(0) for port A;
  • GPIO_CONFIN - configure pins as input
  • GPIO_CONFOUT - configure pins as output
  • GPIO_CONFOD - configure pins as open drain output
  • GPIO_DECONF - deconfigure pins
  • GPIO_STORE - set values of all pins, configures as output
  • GPIO_SET - set output pins to 1
  • GPIO_CLEAR - set output pins to 0
  • GPIO_INVERT - invert output pins by mask
  • GPIO_POLL - get input values (pinmask ignored)

Pins are specified as bitmask: bit 0 corresponds to pin 0, bit 1 - pin 1 etc. Several operations can be combined in one call. For example, to toggle pin A2 high then low, and get value of all PORTA pins:

val = ioctl(fd, GPIO_PORTA | GPIO_SET | GPIO_CLEAR | GPIO_POLL, 1 << 3);