#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
off_t target = 0xB7A01084;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
printf("/dev/mem could not be opened.\n");
exit(1);
} else {
printf("/dev/mem opened.\n");
}
fflush(stdout);
/* Map one page */
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target + & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Memory map failed.\n");
} else {
printf("Memory mapped at address %p.\n", map_base);
}
fflush(stdout);
virt_addr = map_base + (target & MAP_MASK);
/* acess remapped region here */
if(munmap(map_base, MAP_SIZE) == -1) {
printf("Memory unmap failed.\n");
}
close(fd);
}
Once a mapping has been achieved the registers may be accessed as required, to ensure the correct type of access is performed by the CPU the void * pointer must be cast to the correct type, for instance in the above the following could be used
/* for a byte wide access */
read_result = *((volatile unsigned char *) virt_addr);
/* for a sixteen bit access */
read_result = *((volatile unsigned short *) virt_addr);
/* for a thirty two bit access */
read_result = *((volatile unsigned long *) virt_addr);
使用 /dev/port 设备文件控制 LED 闪动。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。