[英]Write to a physical address in Linux on ARM
I would like to write an integer(just one value, it can be also other type) to a specific register (for example: 0x60006666) on a Linux machine using the ARM platform. 我想在使用ARM平台的Linux机器上将整数(只是一个值,也可以是其他类型)写入特定寄存器(例如:0x60006666)。
There are many examples using the mmap(2), but it is not clear how to write just one value to a specific address using the mmap(). 有很多使用mmap(2)的示例,但不清楚如何使用mmap()仅将一个值写入特定地址。 Having a look at the mmap() manual, it does not specify what value to write to the specific register: http://man7.org/linux/man-pages/man2/mmap.2.html
看看mmap()手册,它没有指定要写入特定寄存器的值: http : //man7.org/linux/man-pages/man2/mmap.2.html
Here is the function: 这是函数:
void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
It is clear that *addr is the address, but where do we insert the value is written to this address? 很明显,* addr是地址,但是我们将值插入到哪里呢?
In my case I would like to write an int to a specific address, how would mmap look like? 就我而言,我想将int写入特定地址,mmap会是什么样子?
#define _WRITE_ADDR 0x60006666 //address where to write
unsigned int value_addr = 0x00000080 //value to be written to the address
I would like to write the above mentioned value to the specified address. 我想将上述值写入指定的地址。 It should be trivial, but not very clear since it has been some time since I worked with this type of questions, hopefully somebody has some hints.
这应该是微不足道的,但不是很清楚,因为自从我处理此类问题已经有一段时间了,希望有人能提供一些提示。 Thanks!
谢谢!
Similar question: 类似的问题:
WRITE and READ registers in Linux on ARM Linux在ARM上的WRITE和READ寄存器
It is rather simple. 这很简单。 Example based on RPI
基于RPI的示例
first you need to: 首先,您需要:
mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
then allocate memory for the map. 然后为地图分配内存。 For example for one BCM RPi peritheral it will me 4K + 4K
例如,对于一个BCM RPi腹膜,它将是4K + 4K
periph_mem = malloc( 8*1024 - 1); periph_mem = malloc(8 * 1024-1);
then make sure that it is 4k aligned and mmap
it: 然后确保它对齐4k并
mmap
它:
gpio_map = (unsigned char *)mmap((caddr_t)poriph_mem, BLOCK_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, mem_fd, PERIPH_BASE );
where PERIPH_BASE is the base address of the peripheral (for example GPIO 0x20000000 (BCM peripherals base) + 0x200000) 其中PERIPH_BASE是外设的基地址(例如GPIO 0x20000000(BCM外设基)+ 0x200000)
then you can access them as normal pointers (but remember they have to volatile
) 那么您可以将它们作为普通指针访问(但请记住,它们必须是
volatile
)
*(volatile uint32_t *)(periph_mem + OFFSET) = VALUE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.