我想在 Linux 用户空间中写入 OMAP3530 上的硬件寄存器。请找到下面的代码。Mmap 返回错误:
内存映射失败。错误-1
mmap:权限被拒绝
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define BASE_ADDRESS 0x48050000
int main()
{
int mem;
unsigned int *value;
void *ptr;
/* Open /dev/mem */
if ((mem = open ("/dev/mem", O_RDONLY | O_SYNC)) == -1)
fprintf(stderr, "Cannot open /dev/mem\n"), exit(1);
ptr = mmap (0, 8192, PROT_READ|PROT_WRITE, MAP_SHARED, mem, BASE_ADDRESS);
if(ptr == (void *) -1) {
printf("Memory map failed. error %i\n", ptr);
perror("mmap");
}
value = (unsigned int*)(ptr + 0x4BC+((1-1)*0x90)+(0*0x04));
*value = 479*799*3-4;
printf("value = %i", value);
value = (unsigned int*)(ptr + 0x4C4+((-1)*0x90));
*value = 1;
printf("value = %i", value);
value = (unsigned int*)(ptr + 0x4C8+((-1)*0x90));
*value = 1;
printf("value = %i", value);
return 0;
}