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
Options:
  • None
Major device id:
  • 7
Devices:
  • /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.
Programs:
  • /bin/portio

Usage:

portio operation...

Operations:

-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

Ports:

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 
Examples:

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*
oo----o-oooo--oo
oooooooooooooooo
oooo-------oooo-
ooooooooooo--ooo
------oooooooooo
--oo---o--oooooo
----------------

Configure all ports as input:

# portio -i a b c d e f g
# cat /dev/conf*
ii----i-iiii--ii
iiiiiiiiiiiiiiii
iiii-------iiii-
iiiiiiiiiii--iii
------iiiiiiiiii
--ii---i--iiiiii
iiii--iiii----ii

Read pins of all ports:

# cat /dev/port*
11----1-1111--11
1111111111111111
1111-------1111-
11111111111--111
------1111111111
--11---1--111111
1111--1111----11
API:

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);