write2ddr
功能:应用层向DDR指定地址写入指定长度的指定数据(/dev/mem和mmap函数)
第一个参数:ddr的基地址
第二个参数:编移地址
第三个参数:要写入多大内存
第四个参数:写入入的数据
实现:dev/mem+mmap函数
写好后,32位机器可以用,但是64位机器不可以用,比如基她址传入0x100000000,8个0,已经超出32位地址所能表达的最大数ox1fffffff。怎么在64机器上传入超32位的Oxfffffff的地数据?
strtoull西数似乎只能转换到32位的。
用sscanf函数进行数据转换。
/* 2019.10.15 by zhilu */
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* help information */
void do_help(char* argv0)
{
printf("%s -h help information\n", argv0);
printf("%s <base address > <offset address> <limit> <data>\n", argv0);
}
/ manipulation function »/
int do_write_func(unsigned long long base_addr, unsigned long long offset_addr, unsigned long long limit, char data)
{
unsigned char *map_base:
unsigned long long mem_base_addr = 0;
unsigned long long tmp_addr = 0;
int len = 0;
int fd = -1;
int i = 0:
unsigned long addr;
unsigned char content;
fd = open ("/dev/mem" , O_RDWR|0_SYNC);
if(fd < 0)
return -1;
mem_base_addr = base_addr + offset_addr;
tmp_addr = mem_base_addr % (0x1000);
mem_base_addr = mem_base_addr - tmp_addr:
map_base = mmap(NULL, limit, PROT_READ| PROT_WRITE, MAP_SHARED, fd, mem_base_addr);
//printf("map_base = Ox%p\n",map_base );
if (map_base == 0) {
printf("NULL pointer!\n");
return -1;
} else {
printf("init ok!\n");
}
for (i=0 ; i < limit; ++i)
{
//content = * (map_base+tmp_addr+i);
//printf("address: Ox%p content Ox%x\t\t", &map_base[tmp_addr+i), content);
*(map_base+tmp_addr+i) = (unsigned char)data;
//content = *(map_base+tmp_addr+i);
//printf("updated address: Ox%p content 0x%x\n", &map_base[tmp_addr+1], content);
}
close (fd);
munmap(map_base, limit);
return 0;
}
/* main function */
int main(int argc, char** argv)
{
int ret;
unsigned long long base_addr;
if(argc != 5) {
do_help(argv[0]);
} else {
sscanf(argv[1], "%11x", &base_addr);
ret = do_write_func(base_addr, strtoul1(argv[2], 0, 16), strtoul1(argv[3], 0, 16), strtoul1(a rgv[4], 0, 16));
if (ret < 0) {
printf("write failed!\n");
}
printf("write successful!\n\n");
return 0;
}