Chinaunix首页 | 论坛 | 博客
  • 博客访问: 307170
  • 博文数量: 65
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 609
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-06 21:41
个人简介

好好学习,天天向上

文章分类

全部博文(65)

文章存档

2022年(3)

2021年(25)

2020年(1)

2019年(3)

2016年(2)

2015年(3)

2014年(14)

2013年(7)

2012年(7)

我的朋友

分类: LINUX

2021-04-26 20:47:28

简单的验证写入内存和值与读出来的是否相同

点击(此处)折叠或打开

  1. /* this program write mem with rand numbers,
  2. then read to check it, can be used to check resv mem is ok or not*/

  3. #include <stdio.h>
  4. #include <sys/mman.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <sys/ioctl.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <time.h>

  14. int main(int argc,char* argv[])
  15. {
  16.     unsigned char* addrbase;
  17.     volatile unsigned char reg_val;
  18.     int memfd = -1;
  19.     errno = 0;
  20.     int i = 0;
  21.     int j = 0;
  22.     
  23.     if (argc < 5)
  24.         printf("./test phystart(0x800xx) phy_len(0x100000) 1/0(0-read all and print, 1-write and read and check) 5(default wait 5 to check)\n");

  25.     unsigned long start = strtoul(argv[1], NULL, 16);
  26.     unsigned long len = strtoul(argv[2], NULL, 16);
  27.     unsigned long end = start + len;
  28.     
  29.     int set_flag = atoi(argv[3]);
  30.     int wait_s = atoi(argv[4]);
  31.     
  32.     printf("phy start=0x%x, end==0x%x, len=0x%x\n", start, end, len);
  33.     
  34.     srand( (unsigned)time( NULL ) );
  35.     unsigned int rand_base = rand();
  36.     printf("rand() = 0x%x\n", rand_base);
  37.     
  38.     if ((memfd = open("/dev/mem", O_RDWR )) < 0)
  39.     {
  40.         printf("open /dev/mem error\n");
  41.         return -1;
  42.     }
  43.     if (sizeof(unsigned long) == 4){
  44.         printf("mmap !!!\n");
  45.         addrbase = (unsigned char*)mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, start);//0x3a800000 start is absolute phy address
  46.     } else {
  47.         printf("mmap64 !!!\n");
  48.         addrbase = (unsigned char*)mmap64(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, start);
  49.     }
  50.     printf("addrbase=0x%lx\n", addrbase);
  51.     
  52.     if (set_flag == 0) {
  53.         for (i = 0; i < len ; i++) {
  54.             reg_val = *(addrbase+i);
  55.             printf("[read] i=%d, reg_val=0x%x\n", i, reg_val);
  56.         }
  57.     }
  58.     

  59.     
  60.     if (set_flag == 1) {
  61.         printf("before memset, addrbase=0x%lx\n", addrbase);
  62.         for (i = 0; i < 10 ; i++) {
  63.             reg_val = *(addrbase+i);
  64.             printf("[before] i=%d, reg_val=0x%x\n", i, reg_val);
  65.         }
  66.         printf("in memset, addrbase=0x%lx\n", addrbase);
  67.         for (i = 0; i < len; i++) {
  68.             if (i%(1024*1024) == 0) {
  69.                 printf(".");
  70.                 fflush ( stdout );
  71.             }
  72.             memset((addrbase+i), (rand_base*i)%255, 1);
  73.         }
  74.         printf("waiting %d s to scan mem\n", wait_s);
  75.         sleep(wait_s);
  76.         printf("to scan...\n");
  77.         for (i = 0; i < len ; i++) {
  78.             if (i%(1024*1024) == 0) {
  79.                 printf(".");
  80.                 fflush ( stdout );
  81.             }
  82.             reg_val = *(addrbase + i);
  83.             if (reg_val != (rand_base*i)%255)
  84.              printf("[*** after check fail ***] i=%d, reg_val=0x%x, (rand_base*i)%255=0x%x\n", i, reg_val, (rand_base*i)%255);
  85.         }
  86.         printf("check sucess=0x%lx\n", addrbase);
  87.     }
  88.     sleep(1);
  89.     printf("over=0x%lx\n", addrbase);

  90.     if (munmap((void*)addrbase, len))
  91.     {
  92.         printf("munmap fail errno:%d\n", errno);
  93.     }
  94.     
  95.     close(memfd);
  96.     
  97.     printf("return sucess!\n");
  98.     
  99.     return 0;
  100. }

阅读(829) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~