Sample Code/C/LED

/* 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                                                    */


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 
 * 7) include 
 * 8) include 
 * 9) include 
 * 10) include 
 * 11) include 
 * 12) define MAP_SIZE 4096
 * 13) define MAP_MASK ( MAP_SIZE - 1 )
 * 14) define GAFR1_U 0x40E00060
 * 15) define GAFR2_U 0x40E00068
 * 16) define GPDR1 0x40E00010
 * 17) define GPDR2 0x40E00014
 * 18) define GPCR1 0x40E00028
 * 19) define GPCR2 0x40E0002C
 * 20) define GPSR1 0x40E0001C
 * 21) 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; }