// Arlotto 2008 UNIVERSITY OF TOULON IUT GEII www : arlotto.univ-tln.fr // Olimex EP930X Handling GPIO from user space // PORTH4 : Button (pulled up) // PORTE0 : Green Led // PORTE1 : Red Led // Compile options : arm-linux-gcc -Wall -mcpu=arm9 // This program unlock device config to allow port H to be used as normal GPIO (NotIDE). // Then waits until button is pressed ( busy waiting ) // Then loops 5 times to blink led while printing Port H and E states. // It then restore initial register values #include #include #include #include #include #include int main(int argc, char **argv) { volatile unsigned int *PEDR, *PEDDR, *PHDR, *PHDDR; volatile unsigned int *DeviceCfg, *SysSWLock ; unsigned int oldPEDR,oldPEDDR,oldPHDR,oldPHDDR,oldDeviceCfg; int i; unsigned int state; unsigned char *start; unsigned char *CfgBase ; int fd = open("/dev/mem", O_RDWR|O_SYNC); start = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x80840000); CfgBase = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x80930000); DeviceCfg = (unsigned int *)(CfgBase + 0x80); SysSWLock = (unsigned int *)(CfgBase + 0xC0); PHDR = (unsigned int *)(start + 0x40); // port h PHDDR = (unsigned int *)(start + 0x44); // port h direction register PEDR = (unsigned int *)(start + 0x20); // port e data PEDDR = (unsigned int *)(start + 0x24); // port e direction register oldPEDR = *PEDR ; oldPEDDR = *PEDDR ; oldPHDR = *PHDR ; oldPHDDR = *PHDDR ; oldDeviceCfg = *DeviceCfg ; //printf("sizeof int :%d sizeof char : %d\n",sizeof(int),sizeof(char)); printf("Etat initial :\n"); printf("PEDR : %x PEDDR : %x : PHDR : %x PHDDR : %x \n",*PEDR,*PEDDR,*PHDR,*PHDDR); printf("DeviceCfg : %x\n",*DeviceCfg); // Unlocking SWLOCK to permit configuration of PORTH to normal GPIO port. if(*SysSWLock == 0x00000001 ) { printf("Sys is open\n"); } else { printf("Sys is locked\n"); printf("Try to open......"); *SysSWLock = 0x000000AA ; } if(*SysSWLock == 0x00000001 ) { printf("Now Sys is open !\n"); } else { printf("Failed to open Sys...\n"); close(fd); return 1 ; } *DeviceCfg = *DeviceCfg | 0x00000800 ; // PORTH is not IDE ! printf("New DeviceCfg : %x\n",*DeviceCfg); *PHDDR = *PHDDR & (~0x00000010); // port h b4 input *PEDDR |= 0x03; // port e b0 and b1 output printf("button demo!\n"); state = *PHDR; // read initial state printf("PHDR=%x PHDDR=%x\n",*PHDR,*PHDDR); printf("PEDR=%x PEDDR=%x\n",*PEDR,*PEDDR); printf("Press Button to continue...."); fflush(stdout); while (state & 0x0010) { // wait until button goes low bit 4 state = *PHDR; // remember bit 0 is pulled up with 4.7k ohm } printf("button Pressed!!!\n"); // blink 5 times, sleep 1 second so it's visible for (i = 0; i < 5; i++) { *PEDR = 0x01; // Green Led On Red Led Off; sleep(1); printf("PHDR=%x :",*PHDR); printf("PEDR=%x\n",*PEDR); *PEDR = 0x02; // Green Led Off Red Led On ; sleep(1); } // Restore initial register values // Relock seems to be done automaticaly *PEDR = oldPEDR ; *PEDDR = oldPEDDR; *PHDR = oldPHDR ; *PHDDR = oldPHDDR ; *DeviceCfg = oldDeviceCfg ; close(fd); printf("Done...\n"); return 0; }