[Voyage-linux] [Off Topic] GPIO Interupt Programming

Roger Eisenecher (spam-protected)
Mon Jul 25 12:04:10 HKT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all

find attached a small c programm to access the led gpio directly without
a kernel driver as a little example. It is not perfect but it does work
for my purposes.

kindly regards
rOger

Punky Tse schrieb:
> Please find the attached message from soekris for GPIO support on 4801. 
> And does any body knows this patch also works on wrap?  But anyway, this
> patch will go into the next kernel update.
> 
> Mirko Buholzer wrote:
> 
>> Does some one of you have some experience in programming gpio and
>> interrupts on a wrap board.
>> I would like to do a frequency counter on one of the GPIO ports and
>> cant find some good resources.
>>
>> Thanks in advance!
>> Regards, Mirko _______________________________________________
>> Voyage-linux mailing list
>> Voyage-linux at list.voyage.hk
>> http://list.voyage.hk/mailman/listinfo/voyage-linux
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Voyage-linux mailing list
> Voyage-linux at list.voyage.hk
> http://list.voyage.hk/mailman/listinfo/voyage-linux


+---------------------------------------------------------------------
| PGP Public Key -> http://www.icer.ch/contact/eisenecher_pub_key.txt
+---------------------------------------------------------------------
| Roger Eisenecher                     mailto:roger.eisenecher at icer.ch
| ic engineering & research GmbH       Tel: +41 (0)52/223'13'70
| Grondweg 15                          Fax: +41 (0)52/223'13'71
| CH-8266 Steckborn                    http://www.icer.ch/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFC5GS6pF3l9rYt4bARAkMvAJ9iVF6uRPaMlIW0yloO+nhgraNmoACeOpQ6
H0NNa/CCwGh4A/MtG19GSpA=
=PZex
-----END PGP SIGNATURE-----
-------------- next part --------------
/*
 * led.c - a simple program to read/write led states of WRAP board.
 *         (c) 2005, ic engineering & research GmbH, Roger Eisenecher
 *
 *         Usage: ./led [xyz]
 *
 *                If called without params the program will display the
 *                current state of the Onboard LEDs.
 *
 *                If you specify xyz, the corresponding leds will be
 *                modified: for each position (x=D1, y=D2, z=D3) you could
 *                specify 0 to switch off, 1 to swicht on, i or I for
 *                inverting or - to modify nothing. 
 */
 
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>

#define GPIOBASE 0xF400
#define GPIO2    2 /* LED 1 (D3) */
#define GPIO3    3 /* LED 2 (D2) */
#define GPIO18   18 /* LED 3 (D1) */

int checkSC1100()
{
	return (1);
}

void invertGPIO(const unsigned int uiGPIO)
{
	unsigned char ucReg = inb(GPIOBASE+uiGPIO/8);
	outb((ucReg & 1<<uiGPIO%8) ? ucReg & ~(1<<uiGPIO%8) : ucReg | 1<<uiGPIO%8, GPIOBASE+uiGPIO/8);
	return;
}

void setGPIO(const unsigned int uiGPIO)
{
	unsigned char ucReg = inb(GPIOBASE+uiGPIO/8);
	outb(ucReg | 1<<uiGPIO%8, GPIOBASE+uiGPIO/8);
	return;
}

void resetGPIO(const unsigned int uiGPIO)
{
	unsigned char ucReg = inb(GPIOBASE+uiGPIO/8);
	outb(ucReg & ~(1<<uiGPIO%8), GPIOBASE+uiGPIO/8);
	return;
}

int getGPIO(const unsigned int uiGPIO)
{
	return (int)((inb(GPIOBASE+uiGPIO/8) & 1<<uiGPIO%8)>>uiGPIO%8 ? 1 : 0);
}


int main(int argc, char **argv)
{
	if (!checkSC1100())
	{
		printf("Sorry: This programm supports only SC1100 processor!!!\n");
		exit(1);
	}

    /* Get access to the ports */
    if (ioperm(GPIOBASE, 3, 1)) { perror("ioperm"); exit(1); }

	/* if no argument given we will give out the actual state of the leds... */
	if (argc == 1)
	{
		printf("LED states D1:%s D2:%s D3:%s\n", getGPIO(GPIO18) ? "off" : "on", getGPIO(GPIO3) ? "off" : "on", getGPIO(GPIO2) ? "off" : "on");
	}
	else
	{
		int i;
		for(i=0; i<strlen(argv[1]); i++)
		{	
			switch (argv[1][i])
			{
				case '-': /* Do nothing */
					break;
				case '0': /* Switch off led */
					switch (i+1)
					{
						case 1: /* LED 3 (D1) */
							setGPIO(GPIO18);
							break;
						case 2: /* LED 2 (D2) */
							setGPIO(GPIO3);
							break;
						case 3: /* LED 1 (D3) */
							setGPIO(GPIO2);
							break;
						default:
							break;
					}
					break;
				case '1': /* Switch on led */
					switch (i+1)
					{
						case 1: /* LED 3 (D1) */
							resetGPIO(GPIO18);
							break;
						case 2: /* LED 2 (D2) */
							resetGPIO(GPIO3);
							break;
						case 3: /* LED 1 (D3) */
							resetGPIO(GPIO2);
							break;
						default:
							break;
					}
					break;
				case 'I': /* invert led */
				case 'i': /* invert led */
					switch (i+1)
					{
						case 1: /* LED 3 (D1) */
							invertGPIO(GPIO18);
							break;
						case 2: /* LED 2 (D2) */
							invertGPIO(GPIO3);
							break;
						case 3: /* LED 1 (D3) */
							invertGPIO(GPIO2);
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}			
		}
	}

    /* We don't need the ports anymore */
    if (ioperm(GPIOBASE, 3, 0)) { perror("ioperm"); exit(1); }
    
    exit(0);
}


More information about the Voyage-linux mailing list