linux
分类: LINUX
2013-01-24 12:14:36
最后贴出能够在整个擦除块当中,实现以页为单位的均衡处理的代码:
#include#include #include #include #include #include #include #include #include #include #include #include #include #include #include "upgrade.h" #define TEST_MTD "/dev/mtd6" #define TEST_BLOCK (64*2048*4) #ifndef ERASE_SIZE #define ERASE_SIZE (64*2048) #endif #ifdef WRITE_SIZE #define WRITE_SIZE 2048 #endif #define FIRST_EFFC_PAGE 2048 #define DEV_LEN 12 #define LENGTH 6 #define WRITE_LIMIT_PER_BLOCKS 64*4 #define TEST_MALLOC_FAILED(x) do { \\\\ if (NULL == x) { \\\\ printf("malloc failed!\\\\n"); \\\\ exit(1);\\\\ } \\\\ } while(0) int override_flag; /* * get the offset of the effective page */ int get_cur_offset(const char *mtd_dev) { char buf = 0xff; char front,rear; unsigned int offset = TEST_BLOCK+FIRST_EFFC_PAGE; //init printf("offset:%d\\\\n" ,offset); read_mtd(mtd_dev,&buf,1,offset); front = buf; offset = offset + WRITE_SIZE; printf("offset:%d\\\\n" ,offset); read_mtd(mtd_dev,&buf,1,offset); rear = buf; printf("first:%c\\\\n" ,front); printf("second:%c\\\\n" ,rear); //there are datas while (rear=='0' && front=='0') { printf("offset:%d\\\\n" ,offset); offset += WRITE_SIZE; /* get the next mark */ front = rear; read_mtd(mtd_dev,&buf,1,offset); rear = buf; /* system error */ if (offset >= (TEST_BLOCK+ERASE_SIZE)) { printf("arrive at the boundary of block!\\\\n"); break; } } return offset; } /* * get the device node */ void get_mtd_device(char *device) { strcpy(device,TEST_MTD); } /* * whether there are datas or not */ void existed_data(const char *device,char *flag) { unsigned int offset; offset = TEST_BLOCK; read_mtd(device,flag,1,offset); } /* * mark that there are datas */ void mark_existed(const char *device,unsigned int offset) { char flag = '0'; write_mtd(device,&flag,1,offset); } /* * init erase_info */ void _erase_mtd(const char *device,unsigned int offset,unsigned int length) { struct erase_info_user erase; memset(&erase,0,sizeof(struct erase_info_user)); erase.start = offset; erase.length = length; //erase erase_mtd(device,&erase); } /* * write driver */ void write_flash(const char *buf,unsigned int length) { char mtd_dev[DEV_LEN]; char *write_buf = NULL; unsigned int off = 0; unsigned int next_off = 0; char flag = 0; int i; memset(mtd_dev,0,DEV_LEN); write_buf = (char *)malloc(length+1); TEST_MALLOC_FAILED(write_buf); //dirty the latest page get_mtd_device(mtd_dev); empty_block: //data is existed or not existed_data(mtd_dev,&flag); if (0xff == flag) { printf("first\\\\n"); //mark existed mark_existed(mtd_dev,TEST_BLOCK); //write data next_off = TEST_BLOCK + FIRST_EFFC_PAGE; printf("next_off:%d\\\\n" ,next_off); write_buf[0] = '0'; memcpy(write_buf+1,buf,length); write_mtd(mtd_dev,write_buf,length+1,next_off); //read_mtd(mtd_dev,write_buf,length+1,next_off); //printf("read_buf:%s\\\\n" ,write_buf); } else { off = get_cur_offset(mtd_dev); if (off == TEST_BLOCK+ERASE_SIZE) { override_flag = 1; printf("\\\\n\\\\n\\\\nthird\\\\n\\\\n"); //erase the block and the state convert to empty _erase_mtd(mtd_dev,TEST_BLOCK,ERASE_SIZE); //exit(1); goto empty_block; } else { printf("second\\\\n"); //write the data and dirty the page //next_off = off + WRITE_SIZE; write_buf[0] = '0'; memcpy(write_buf+1,buf,length); printf("write_buf:%s\\\\n" ,write_buf); printf("next_off:%d\\\\n" ,next_off); write_mtd(mtd_dev,write_buf,length+1,off); read_mtd(mtd_dev,write_buf,length+1,off); printf("read_buf:%s\\\\n" ,write_buf); } } printf("\\\\n"); } /* * middle layer of write test */ void write_test_data(const char *buf,unsigned int length) { write_flash(buf,length); } /* * read driver */ void read_flash(char *buf,unsigned int length) { char mtd_dev[DEV_LEN]; unsigned int off = 0; //unsigned int length = 0; memset(mtd_dev,0,12); get_mtd_device(mtd_dev); off = get_cur_offset(mtd_dev); read_mtd(mtd_dev,buf,length,off-WRITE_SIZE); } /* * middle layer of read test */ void read_test_data(char *buf,unsigned int length) { if (length > WRITE_SIZE || NULL == buf) exit(1); read_flash(buf,length); } /* * main */ int main() { int length = LENGTH; char buffer[LENGTH]; // length <= 2048 memset(buffer,0,LENGTH); while (1) { write_test_data("12345",5); if (override_flag == 1) { write_test_data("woshi",5); break; } } read_test_data(buffer,7); printf("\\\\nbuffer:%s\\\\n" ,buffer); return 0; }