gpt4 book ai didi

linux - 在 Linux 用户空间访问硬件寄存器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:19 24 4
gpt4 key购买 nike

我想在 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;
}

最佳答案

mmap() 的文档说(强调我的):

The mmap() function shall fail if:

EACCES

The fildes argument is not open for read, regardless of the protection specified, or fildes is not open for write and PROT_WRITE was specified for a MAP_SHARED type mapping.

由于您指定了 PROT_WRITE,因此文件应该打开以供写入。您的代码只打开它以供阅读。

关于linux - 在 Linux 用户空间访问硬件寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com