- 积分
- 79
贡献366
飞刀108 FD
注册时间2016-3-27
在线时间10 小时
扫一扫,手机访问本帖
|
参照手册,如图,直接编写应用程序来对nandflash进行数据的存储操作。运行程序后能正常的读取和写入,但是掉电后再次读写,数据丢失了。是怎么回事?
是不是块设备的读和写的应用函数写的不正确呢????
- #include<sys/types.h>
- #include<fcntl.h>
- #include<sys/stat.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<string.h>
- #include<errno.h>
- #include<stdlib.h>
- //#define BUF_SIZE 256
- #define ROW 28
- #define COLUMN 6
- /* ./nandflash -w
- ./nandflash -r */
- int main(int argc, char **argv)
- {
- int ret = 0;
- int i,j;
- //char arr[BUF_SIZE] = {'0'};
- //char buf[BUF_SIZE] = {'0'};
- float data_base_write[ROW][COLUMN] = {
- 1.891052295,1.880637028,1.880662672,1.880770901,1.879450873,1.880700352,
- 1.879935252,1.881508812,1.869690787,1.87005238, 1.870044239,1.859651899,
- 1.859074656,1.859008458,1.859362259,1.858735545,1.847817291,1.848525292,
- 1.848378952,1.880780544,1.88147216, 1.881095431,1.871008965,1.870199515,
- 1.870671536,1.859612464,1.86003391, 1.859165311,1.848139818,1.849201702,
- 1.883467744,1.862821112,1.85208504, 1.846950465,1.836828431,1.837161962,
- 1.831672258,1.821147095,1.816506092,1.81138156, 1.811605633,1.806054896,
- 1.813204183,1.806264194,1.795831025,1.78588526, 1.78032686, 1.81507569,
- 1.815702515,1.800243218,1.790447621,1.780343032,1.77532877, 1.770262973,
- 1.760081062,1.749647594,1.749650075,1.745028739,1.739645746,1.728564228,
- 1.723900325,1.713978878,1.7139702, 1.703652117,1.704013747,1.693571616,
- 1.687657736,1.683074436,1.677927014,1.673206028,1.667436721,1.662266263,
- 1.657424307,1.652538685,1.647217463,1.636413681,1.636255431,1.641119179,
- 1.631570079,1.621213283,1.616427321,1.61582351, 1.61672418, 1.610865586,
- 1.600660337,1.595917449,1.596217952,1.595806511,1.590650782,1.688821116,
- 1.625993828,1.615743386,1.605662676,1.594974692,1.590410244,1.585351706,
- 1.579973715,1.575218547,1.569798485,1.570256538,1.565249874,1.559633934,
- 1.564982053,1.5749795,1.565281117, 1.559755497,1.549897686,1.543877153,
- 1.545018749,1.544594015,1.534308672,1.528760394,1.528926025,1.52909165,
- 1.523905932,1.518667542,1.513648227,1.513605019,1.513947061,1.533959919,
- 1.53913728, 1.523624797,1.513581304,1.507785075,1.503196068,1.49782206,
- 1.492848535,1.49241439, 1.487482777,1.482535149,1.482787432,1.47697118,
- 1.481901555,1.49813662, 1.482077521,1.477115531,1.472077278,1.472248014,
- 1.467010667,1.46176725, 1.456695267,1.451354973,1.451615926,1.446816133,
- 1.446757061,1.441379748,1.436245625,1.436815415,1.436294515,1.441857532,
- 1.467206348,1.451864037,1.447137975,1.441790587,1.431630948,1.431808425,
- 1.426256259,1.426087753,1.421263351,1.416327275,1.416578183,1.410840173,
- 1.410994963,1.406171429,1.406335848,1.40045524, 1.421786505,1.432455272
- };
-
- float data_base_read[ROW][COLUMN] = {0.0};
-
- int fd = open("/dev/mtdblock7", O_RDWR);
- if (fd < 0) {
- perror("open nandflash device:");
- exit(1);
- }
-
- if(argc < 2)
- {
- printf("please input correct argument\n");
- exit(1);
- }
- if(!strcmp(argv[1],"-w"))
- {
- /*if(argc != 3)
- {
- printf("please input like this : ./nandflash -w forlinx\n");
- exit(1);
- }
- strcpy(arr,argv[2]);
- ret = write(fd,arr,strlen(arr));
- */
-
- ret = write(fd, data_base_write, sizeof(data_base_write));
- printf("%d data has been writen\n",sizeof(data_base_write)/sizeof(float));
-
- if(ret < 0)
- {
- fprintf(stderr,"error nandflash_write: %s\n",strerror(errno));
- exit(1);
- }
- // printf("write ret = %d\n",ret);
- // ret = lseek(fd,30,SEEK_SET);
- // ret = write(fd,arr,strlen(arr) + 1);
- // if(ret < 0)
- // {
- // fprintf(stderr,"error eeprom_write: %s\n",strerror(errno));
- // exit(1);
- // }
- // printf("write ret = %d\n",ret);
- }
- if(!strcmp(argv[1],"-r"))
- {
- if(argc != 2)
- {
- printf("please input like this : ./nandflash -r\n");
- exit(1);
- }
- //ret = read(fd,buf,BUF_SIZE);
-
- ret = read(fd, data_base_read, sizeof(data_base_write));
- if(ret < 0)
- {
- fprintf(stderr,"error nandflash_read: %s\n",strerror(errno));
- exit(1);
- }
- //printf("the value in buf is %s\n",buf);
-
- for(i = 0;i < ROW;i++)
- for(j = 0;j < COLUMN;j++)
- {
- printf(" %.9f",data_base_read[i][j]);
- if (j==COLUMN-1)
- printf("\n");
- }
- /* ret = lseek(fd,6,SEEK_SET);
- ret = read(fd,buf,BUF_SIZE);
- if(ret < 0)
- {
- fprintf(stderr,"error nandflash_read: %s\n",strerror(errno));
- exit(1);
- }
- printf("lseek to the seventh bytes in buf is %s\n",buf);
- */
- }
- close(fd);
- return 0;
-
复制代码 |
-
|