nand flash 读写测试
生活随笔
收集整理的這篇文章主要介紹了
nand flash 读写测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
參考鏈接:https://www.cnblogs.com/pengdonglin137/p/3468953.html
測試程序:
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <unistd.h> #include <linux/ioctl.h> #include <sys/time.h> #include <mtd/mtd-user.h> #include <errno.h>static char Buffer[128*1024] = {0};int main () {int fd;fd = open ("/dev/mtd0", O_SYNC | O_RDWR);struct mtd_info_user { __u8 type; __u32 flags;__u32 size; // Total size of the MTD__u32 erasesize;__u32 writesize;__u32 oobsize;// Amount of OOB data per block (e.g. 16)/* The below two fields are obsolete and broken, do not use them * (TODO: remove at some point) */__u32 ecctype;__u32 eccsize;};struct mtd_info_user mtd;ioctl(fd, MEMGETINFO,&mtd) ;struct erase_info_user {__u32 start;__u32 length;};typedef struct erase_info_user erase_info_t;erase_info_t erase;int isNAND, bbtest = 1;struct mtd_info_user *DevInfo = &mtd;erase.length = DevInfo->erasesize;// erase.length 表示的是擦除大小,也就是一塊的大小,如128KB// DevInfo->size 為某個/dev/mtdx的大小// erasse.start應該是按塊對齊遞增isNAND = (DevInfo->type == MTD_NANDFLASH) ? 1 : 0;for (erase.start = 0; erase.start < DevInfo->size; erase.start += DevInfo->erasesize) {if (bbtest) {loff_t offset = erase.start;int ret = ioctl(fd, MEMGETBADBLOCK, &offset); //判斷是不是壞塊if (ret > 0) {//if (!quiet)printf ("\nSkipping bad block at 0x%08x\n", erase.start);continue;//發(fā)現(xiàn)是壞塊,應該跳過} #if 0else if (ret < 0) {if (errno == EOPNOTSUPP) {bbtest = 0;if (isNAND) {fprintf(stderr, "%s: Bad block check not available\n", DevInfo->dir);return 1;}} else {fprintf(stderr, "\n%s: MTD get bad block failed: %s\n", DevInfo->dir, strerror(errno));return 1;}}#endif}//if (!quiet){fprintf(stderr, "\rErasing %d Kibyte @ %x -- %2llu %% complete.", \(DevInfo->erasesize) / 1024, erase.start,(unsigned long long) erase.start * 100 / (DevInfo->size));}if (ioctl(fd, MEMERASE, &erase) != 0) //執(zhí)行擦除操作{//fprintf(stderr, "\n%s: MTD Erase failure: %s\n", DevInfo->dir,strerror(errno));printf("MTD Erase failure\n");continue;}}//writechar IPLFile[] = "IPL.bin";int readfd = 0;int readlen;printf("%s read\n",IPLFile);readfd = open(IPLFile, O_RDONLY);if(readfd == 0){printf("OPEN %s ERROR, not to upgrade\n",IPLFile);}memset(Buffer , 0 , sizeof(Buffer));readlen = read(readfd, Buffer, sizeof(Buffer));printf("readlen = %d\n",readlen);if(readlen <= 0){printf("buffer fread error \n");close(readfd);readfd = 0;return -1;}close(readfd);readfd = 0; //清零//write{int num = (readlen + 2*1024-1)/(2*1024);int j = 0x0;int alwlen = 0x0;int cur_write_pos = 0;int write_ret = 0;int wlen = 0x0;int write_cnt = 0;printf("Start write\n");for(j=0x0; j<num; j++){alwlen = 0x0;loff_t offset;cur_write_pos = 0;write_ret = 0;wlen = 0x0;offset = 0/*FlashDevice.MtdDevice[i].StartAddress*/ + j * 2*1024;lseek(fd, offset, 0);wlen = ((readlen - alwlen)>(2*1024)) ? (2*1024) : (readlen-alwlen);printf("+");while(cur_write_pos != wlen){#if 1printf("wlen: %d,cur_write_pos: %d\n",wlen,cur_write_pos);write_ret = write(fd, Buffer + offset + cur_write_pos, wlen - cur_write_pos);if (write_ret < 0){printf(" (%d)\nFlashWrite failed, not erase? (%d)\n", write_cnt, write_ret);return -1;}else if (write_ret == 0){break;}#endifcur_write_pos += write_ret;}write_cnt++;}printf("write (%d)*2k\n", write_cnt);printf("finish write\n");}return 0; }測試腳本:
#!/bin/bashecho "write flash test run!"while true doif test -e /dev/sda; thenif test -e /mnt/flash_erease; then/mnt/flash_ereaseelseecho "/dev/sda mount"mount -t vfat /dev/sda /mntfielseecho "/dev/sda not exist"fisleep 10sdoneexit 0?
總結(jié)
以上是生活随笔為你收集整理的nand flash 读写测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IIS5 IIS6 IIS7区别
- 下一篇: IIS 部署,发布 报错 500.19