The Acme Systems shipping office will be closed for holidays from 14 august to 22 august
PLEASE NOTE: This article is obsolete or related to a discontinued product.

Using the I/O lines in C

How to use the I/O line in C

The general purpose I/O line of the FOX Board can be used in C in two ways:

File descriptor method

With this method, the ports A,B and G can be opened using the following device names:

  • /dev/gpioa Port A
  • /dev/gpiob Port B
  • /dev/gpiog Port G

Before any operation on the I/O lines you have to open one of these files using the open() function:

if ((fd = open("/dev/gpiog", O_RDWR))<0) {
  printf("Open error on /dev/gpiog\n");
  exit(0);
}
And close it at the end with:
  close(fd)

Any other operation like set, reset, get and direction can be done with the ioctl() functions.

Set an output line

The ioctl() functions is used to set or reset an I/O line are:

// Set the lines specified by the bit mask "iomask" to 1
ioctl(fd,_IO(ETRAXGPIO_IOCTYPE,IO_SETBITS),iomask); 

// Set the lines specified by the bit mask "iomask" to 0
ioctl(fd,_IO(ETRAXGPIO_IOCTYPE,IO_CLRBITS),iomask); 

iomask is a bit mask used to define which bits have to be set or reset by means of the ioctl() call.

For example to set the lines 3,4 and 5 on Port G the value of iomask will be:

iomask = 1<<5 | 1<<4 | 1<<3; // iomask=00000000 00000000 00000000 00111000

Following is a C sample program to blink each second for 10 times the external LED on OG25:

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"    
#include "sys/ioctl.h"
#include "fcntl.h"     
#include "asm/etraxgpio.h"

int main(void) {
  int fd;
  int i;
  int iomask;

  if ((fd = open("/dev/gpiog", O_RDWR))<0) {
    printf("Open error on /dev/gpiog\n");
    exit(0);
  }

  iomask=1<<25;

  for (i=0;i<10;i++) {
    printf("Led ON\n");
    ioctl(fd,_IO(ETRAXGPIO_IOCTYPE,IO_SETBITS),iomask);
    sleep(1);

    printf("Led OFF\n");
    ioctl(fd,_IO(ETRAXGPIO_IOCTYPE,IO_CLRBITS),iomask);
    sleep(1);
  }
  close(fd);
  exit(0);
}

The direction of IOG8-15 and IOG16-23 lines can be set as a whole 8 bit bus and not bit by bit. The direction of IOG0,IOG24, PA0-7 and PB0-7 lines can be set indipendently.

Read an input line

To read an input line the format of the ioctl() call is a little bit different:
value=ioctl(fd, _IO(ETRAXGPIO_IOCTYPE, IO_READBITS));
the return value of this function is a bitmask with all the current input line status. The following example reads the state of an external switch (see schematic on fig.3) and prints a message when the switch is pressed:
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"    
#include "sys/ioctl.h"
#include "fcntl.h"     
#include "asm/etraxgpio.h"

int main(void) {
  int fd;
  int value;
  int iomask;

  if ((fd = open("/dev/gpiog", O_RDWR))<0) {
    printf("Open error on /dev/gpiog\n");
    exit(0);
  }

  iomask=1<<16;

  printf("Press the switch...\n",iomask);
  while (1) {
    value=ioctl(fd, _IO(ETRAXGPIO_IOCTYPE, IO_READBITS));
    if ((value&iomask)==0) {
      printf("Switch pressed\n");
    }
    sleep(1);
  }
}

GPIO syscalls method

This method has been introduced with Prhozen patch to the SDK and uses the syscalls to allow userspace applications to access directly to the Kernel functions. In this way is not requested to open and close a file description and the operation is quite faster.

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "time.h"
#include "string.h"
#include "linux/gpio_syscalls.h"

int main(int argc, char **argv) {

  // set PA0 as output
  gpiosetdir(PORTA, DIROUT, PA0);

  // set PB7 as output
  gpiosetdir(PORTB, DIROUT, PB7);

  // set IOG24 as output
  gpiosetdir(PORTG, DIROUT, PG24);

  // set IOG8-15 as output
  gpiosetdir(PORTG, DIROUT, PG8_15);

  while(1) {
    gpiosetbits(PORTA, PA0);
    gpiosetbits(PORTB, PB7);
    gpiotogglebit(PORTG, PG24);
    gpiosetbits(PORTG, PG8);
    printf("%d\n", (gpiogetbits(PORTG, PG2))?(1):(0));
    sleep(1);

    gpioclearbits(PORTA, PA0);
    gpioclearbits(PORTB, PB7);
    gpiotogglebit(PORTG, PG24);
    gpioclearbits(PORTG, PG8);
    printf("%d\n", (gpiogetbits(PORTG, PG2))?(1):(0));

    sleep(1);
  }
  return(0);
}

How fast is an ioctl() function ?

To provide a general idea on how fast is an ioctl() function, we measured the signal frequency generated with this loop:
while (1) {
  ioctl(fd,_IO(ETRAXGPIO_IOCTYPE,IO_SETBITS),iomask);
  ioctl(fd,_IO(ETRAXGPIO_IOCTYPE,IO_CLRBITS),iomask);
}
As shown on figure the maximum frequency reached is about 46 KHz.


Max frequency obtained with ioctl() in a userspace application

How fast is a GPIO syscalls function ?

To provide a general idea on how fast is a GPIO syscalls, we measured the signal frequency generated with this loop:
while (1) {
  gpiosetbits(PORTA, PA0);
  gpioclearbits(PORTA, PA0);
}
As shown on figure the maximum frequency reached is about 158 KHz.


Max frequency obtained with syscalls() in a userspace application

Related links