Home About us Questions Send money Contacts Terms


http://acme.systems

Please note:This article is obsolete or related to an obsolete 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:

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




Acme Systems srl
Via Aldo Moro 53 - 00055 Ladispoli (RM) - Italy
P.IVA/C.F. 08114831004
Tel +39.06.99.12.187 - Fax +39.06.622.765.31
http://www.acmesystems.it
Iscritta al Registro delle Imprese di Roma al n. 08114831004


Atmel© Certified Partner

Documentation Terms of Use
Acme Systems provides this documentation "as is" without warranty or guarantees of any kind. The maintainer of this site (Sergio Tanzilli), has gone to a great deal of effort into making this documentation as correct as possible. Acme Systems does not provide any direct support for the Open Source preinstalled software but does provide, through these pages and forum posts, all of the information required to obtain the sources, install, use and update the Open Source software that can be run on the Acme platforms. Please note that all of the preinstalled software, used on the Acme Systems products, are Open Source and you will need to check the license terms provided (usually the GPL) by each author before using it in any commercial or non-commercial product, by yourself. Before sending emails or calling the Acme staff please note that WE ARE MAINLY HARDWARE DESIGNERS and NOT LINUX GURUS and therefore ask that you post your questions directly to the forum listed below and ensure that all of the site contributors and large software community will read and reply to your questions.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Creative Commons License