就是利用 /dev/mem,把datasheet 裡面 soc 的 register 位址map 出來存取 (所以要 root 權限).
include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#define MAPSIZE 1024
unsigned long *getMappedAddr(unsigned long addr)
{
int fd;
void *map;
unsigned int offset = addr & (MAPSIZE-1);
fd = open("/dev/mem",O_RDWR | O_SYNC);
if(fd<0){
printf("open dev/mem error\n");
return (unsigned long*)-1;
}
map = mmap(0, MAPSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr & ~(MAPSIZE-1));
if(map == MAP_FAILED){
printf("mmap error\n");
return (unsigned long*)-1;
}
return (unsigned long*)((char*)map+offset);
}
int main(int argc, char **argv)
{
unsigned long regaddr;
unsigned long *mapaddr;
char *end;
if(argc<2){
printf("please specify address\n");
return 0;
}
regaddr = strtoul(argv[1],&end,16);
mapaddr = getMappedAddr(regaddr);
if(mapaddr!=(unsigned long*)-1){
printf("%X = %X\n",regaddr,*(mapaddr));
}
return 0;
}
.. 這個拿來讀 GPIO STATE 應該很方便吧...不知道 overhead 大不大...
沒有留言:
張貼留言