#include
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
int munmap(void *start, size_t length);
1. start與offset一樣填寫為page size倍數位的位置,如page size=4k, 則可填寫0x56000000,
若填寫非page size的倍數,會發生錯誤。
2. length可以填寫getpagesize();
3. mmap執行正確,應該return一個ponitor P,printf P時會發現是0x56000000,亦即P是指向
0x56000000位址,所以可以用P來存取IO memory: *((unsigned short*)(P+0x14))
也可以直接指定位址來存取IO memory: *((unsigned short*)0x56000014) 若沒有使用mmap而
直接指定位址存取IO memory會發生segmentation fault。
For example:
int mem_fd;
volatile void* pMem;
UINT32 tmp;
if((mem_fd=open("/dev/mem", O_RDWR)) < 0){
printf("PDKHW.C: can't open /dev/mem.\n");
return;
}
pMem = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED,
mem_fd, 0x1E788000);
if(pMem == MAP_FAILED){
printf("PDKHW.C: can't get the adress poniter of PUART: %s\n", strerror(errno));
close(mem_fd);
return;
}
printf("Register Value: 0x%x.\n", *((volatile unsigned short*)(pMem+0x20)));
沒有留言:
張貼留言