- 积分
- 185
贡献56
飞刀0 FD
注册时间2012-10-6
在线时间148 小时
扫一扫,手机访问本帖
|
本帖最后由 starter 于 2012-12-3 15:39 编辑
其中地址+24是我改的,原文+8
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(void) {
int fd;
void *map_base, *virt_addr;
off_t target = 0x7F008800; //GPKCON0 0x7F008800
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
printf("/dev/mem could not be opened.\n");
perror("open");
exit(1);
} else {
printf("/dev/mem opened.\n");
}
/* 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");
perror("mmap");
} else {
printf("Memory mapped at address %p.\n", map_base);
}
virt_addr = map_base + (target & MAP_MASK);
/* acess remapped region here */
printf("Now try to change the value of the on-board DIP switche\n");
*(unsigned int *)virt_addr=0x11110000;//0x1111<<16;//éè???aê?3?×?ì?
int ab=*(unsigned int *)virt_addr;
//*(unsigned int *)(virt_addr+8)=0xffffffff;
printf("ab=%x\n",ab);
while(1){
*(volatile unsigned int *)(virt_addr+0x24)=0x00;
//printf("1value is 0x%x\n",*(unsigned int *)(virt_addr+0x24));
//printf("2value is 0x%x\n",*(unsigned int *)virt_addr);
//sleep(1);
*(volatile unsigned int *)(virt_addr+0x24)=0xff;
//printf("3value is 0x%x\n",*(unsigned int *)(virt_addr+0x24));
//printf("4value is 0x%x\n",*(unsigned int *)virt_addr);
//sleep(1);
}
/* we'll never get here in this example, but here's how the region is
* unmapped. */
if(munmap(map_base, MAP_SIZE) == -1) {
printf("Memory unmap failed.\n");
}
close(fd);
}
这个代码用4.3.2编译可以动作的,但是我把它mmap 以及驱动LED部分插到QtCreator程序中,编译也应当是4.3.2,运行就退出GUI,为何?
|
|