/*
* gpio.c: Simple program to configure/read/write GPIO's.
*
* Copyright (C) 2003, Jamey Hicks <jamey.hicks@hp.com>
* Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
*
*
* This software has been developed for the LART computing board
* (http://www.lart.tudelft.nl/). The development has been sponsored by
* the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
* and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
* projects.
*
* The author can be reached at:
*
* Jan-Derk Bakker
* Information and Communication Theory Group
* Faculty of Information Technology and Systems
* Delft University of Technology
* P.O. Box 5031
* 2600 GA Delft
* The Netherlands
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc, char **argv)
{
int fd;
volatile unsigned long *gpio_base;
off_t gpio_nr;
int dir = 0, set_dir = 0;
int clear = 0, set = 0;
if (argc < 2) {
fprintf(stderr, "\nUsage:\t%s gpio\n",
argv[0]);
exit(1);
}
int c = getopt_long(argc, argv, "cios", NULL, NULL);
switch (c) {
case 'c':
clear = 1;
break;
case 'i':
dir = 1;
set_dir = 1;
break;
case 'o':
dir = 0;
set_dir = 1;
break;
case 's':
set = 1;
break;
default:
break;
}
gpio_nr = strtoul(argv[optind], 0, 0);
if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
/* printf("/dev/mem opened.\n"); */
fflush(stdout);
/* Map one page */
gpio_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0xc0804000 & ~MAP_MASK);
if (gpio_base == (void *) -1) FATAL;
if (clear) {
printf("Clearing GPIO%d\n", gpio_nr);
*(gpio_base + 0x0000) &= ~(1 << (gpio_nr&0x1f));
}
if (set) {
printf("Setting GPIO%d\n", gpio_nr);
*(gpio_base + 0x0000) |= (1 << (gpio_nr&0x1f));
}
if (set_dir) {
if (dir) {
*(gpio_base + 0x0004) |= (1 << (gpio_nr&0x1f));
printf("GPIO%d is now an input\n", gpio_nr);
}
else {
*(gpio_base + 0x0004) &= ~(1 << (gpio_nr&0x1f));
printf("GPIO%d is now an output\n", gpio_nr);
}
}
if (*(gpio_base + 0x0004) & (1 << (gpio_nr&0x1f))) {
printf("GPIO%d is an input", gpio_nr);
if (*(gpio_base + 0x0008) & (1 << (gpio_nr&0x1f))) {
printf(" and is set\n");
}
else {
printf(" and is cleared\n");
}
}
else {
printf("GPIO%d is an output", gpio_nr);
if (*(gpio_base + 0x0000) & (1 << (gpio_nr&0x1f))) {
printf(" and is set\n");
}
else {
printf(" and is cleared\n");
}
}
printf("GPOUTR = 0x%08x\n", *(gpio_base + 0x0000));
printf("GPOER = 0x%08x\n", *(gpio_base + 0x0004));
printf("GPINR = 0x%08x\n", *(gpio_base + 0x0008));
printf("GPISR = 0x%08x\n", *(gpio_base + 0x000c));
printf("GPIT1R = 0x%08x\n", *(gpio_base + 0x0010));
printf("GPIT2R = 0x%08x\n", *(gpio_base + 0x0014));
printf("GPCLKR = 0x%08x\n", *(gpio_base + 0x0018));
if (munmap((void *)gpio_base, MAP_SIZE) == -1) FATAL;
close(fd);
return 0;
}