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.
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.
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);
}
}
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);
}
|
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.
while (1) {
gpiosetbits(PORTA, PA0);
gpioclearbits(PORTA, PA0);
}
As shown on figure the maximum frequency reached is about 158 KHz.
|
|
Atmel© Certified Partner |
Documentation Terms of Use
Acme Systems provides this documentation "as is" without warranty or guarantees of any kind.
The mantainer of this site (Sergio Tanzilli), has gone to a great deal
of effort into making this documentation as correct as possible.
Acme Systems doesn't provide any direct support for the Open Source preinstalled software but provides, through
these pages and forum posts, all the information required to obtain the sources, install, use and update the
Open Source softwares runnable on the FOX Board, NetusG20, AriaG25 and Terra platforms.
Please note that all the preinstalled softwares, used on the Acme Systems products, are Open Source and you will
have 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 here are our contacts
please note that WE ARE MAINLY HARDWARE DESIGNERS and NOT LINUX GURUS so could be better to post
your questions directly to the forum listed below to be sure that all the contributors of this site and
the large software developers community will read and reply to your questions.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.