From GumstixDocsWiki
/* This program writes a byte with a shifting bit to */
/* GPIO 59, 60, 61, 62, 81, 82, 83, 84 */
/* These bits are available on the STUART 20 pin solder pads */
/* by Tim Crawford */
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#define MAP_SIZE 4096
#define MAP_MASK ( MAP_SIZE - 1 )
#define GAFR1_U 0x40E00060
#define GAFR2_U 0x40E00068
#define GPDR1 0x40E00010
#define GPDR2 0x40E00014
#define GPCR1 0x40E00028
#define GPCR2 0x40E0002C
#define GPSR1 0x40E0001C
#define GPSR2 0x40E00020
typedef unsigned int u32;
void *map, *regaddr;
static void putmem(u32 addr, u32 val)
{
regaddr = map + (addr & MAP_MASK);
*(u32*) regaddr = val;
}
static int getmem(u32 addr)
{
u32 val;
regaddr = map + (addr & MAP_MASK);
val = *(u32*) regaddr;
return val;
}
void clearbits(unsigned char uc)
{
putmem(GPCR1, (uc & 0x0F) << 27);
putmem(GPCR2, (uc & 0xF0) << 13);
}
void setbits(unsigned char uc)
{
putmem(GPSR1, (uc & 0x0F) << 27);
putmem(GPSR2, (uc & 0xF0) << 13);
}
void setlights(unsigned char uc)
{
clearbits(~uc);
setbits(uc);
}
int main( int argc, char **argv )
{
unsigned int i;
int fd;
unsigned char lights;
struct timespec tv;
unsigned char bulb[8] = {0, 1, 2, 3, 4, 5, 6, 7};
unsigned char patt[8];
fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd<0) {
perror("open(\"/dev/mem\")");
exit(1);
}
map = mmap(0,
MAP_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0x40E00000 & ~MAP_MASK
);
if (map == (void*)-1 ) {
perror("mmap()");
exit(1);
}
/* Set alternate function to GPIO */
i = getmem(GAFR1_U);
i &= 0x87FFFFFF;
putmem(GAFR1_U, i);
i = getmem(GAFR2_U);
i &= 0xFFFFE0;
putmem(GAFR2_U, i);
/* Set the GPIO direction */
i = getmem(GPDR1);
i |= 0x78000000;
putmem(GPDR1, i);
i = getmem(GPDR2);
i |= 0x001E0000;
putmem(GPDR2, i);
printf( "Mello World\n" );
for (i = 0; i < count; i++){
nanosleep(&tv, &tv);
setlights(lights);
lights <<= 1;
if (!lights)
lights = 1;
printf("i = %d lights = %X\n", i, lights);
};
munmap(0,MAP_SIZE);
return 0;
}