下面是读写K9F1208一个page的程序. 在int Nand_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare){ 函数中出错了.
当 data!=NULL, spare!=NULL(写整个页,包括Spare)时能够写正确. 但当date==NULL, spare!=NUL(只写SPare)L时会出错.
int Nand_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare) { int i; NAND_ENABLE_CE(0); //Enable Flash
if(data) { // read main data WRITE_NAND_COMMAND(0x00, 0); // ponit to C eara WRITE_NAND_ADDRESS(0x00, 0); // start from 0 WRITE_NAND_ADDRESS(0xff&(chunkInNAND), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>8), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>16), 0); for(i=0; i<10; i++); NAND_WAIT_READY(0); //wait for(i=0; i<BYTES_PER_PAGE; i++) *data++ = READ_NAND(0); if(spare) {// and spare data = (__u8 *)spare; for(i=0; i<BYTES_OF_SPARE; i++) *data++ = READ_NAND(0); } } else if(spare) { // only spare WRITE_NAND_COMMAND(0x50, 0); // ponit to C eara WRITE_NAND_ADDRESS(0x00, 0); // start from 0 WRITE_NAND_ADDRESS(0xff&(chunkInNAND), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>8), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>16), 0); for(i=0; i<10; i++); NAND_WAIT_READY(0); //wait data = (__u8 *)spare; for(i=0; i<BYTES_OF_SPARE; i++) *data++ = READ_NAND(0); } NAND_DISABLE_CE(0); //Enable Flash
return YAFFS_OK; }
int Nand_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare){
__u8 *readback, *pwrite; int i; int rc = YAFFS_OK;
if((readback = malloc(BYTES_PER_PAGE + BYTES_OF_SPARE)) == NULL) { printf ("malloc return NULL
"); return YAFFS_FAIL; }
NAND_ENABLE_CE(0); //Enable Flash if(data) { // write data WRITE_NAND_COMMAND(0x00, 0); // ponit to A eara WRITE_NAND_COMMAND(0x80, 0); // sequence command WRITE_NAND_ADDRESS(0x00, 0); // start from 0 WRITE_NAND_ADDRESS(0xff&(chunkInNAND), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>8), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>16), 0); pwrite = data; for(i=0; i<BYTES_PER_PAGE; i++) WRITE_NAND(*pwrite++, 0); if(spare) { // and spare pwrite = (__u8 *)spare; for(i=0; i<BYTES_OF_SPARE; i++) WRITE_NAND(*pwrite++, 0); } WRITE_NAND_COMMAND(0x10, 0); // program command for(i=0; i<10; i++); NAND_WAIT_READY(0); //wait WRITE_NAND_COMMAND(0x70, 0); // read status command if(0x00 != (READ_NAND(0) & 0x01)) { rc = YAFFS_FAIL; } } else if(spare) { // only spare WRITE_NAND_COMMAND(0x50, 0); // ponit to C eara WRITE_NAND_COMMAND(0x80, 0); // sequence command WRITE_NAND_ADDRESS(0x00, 0); // start from 0 WRITE_NAND_ADDRESS(0xff&(chunkInNAND), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>8), 0); WRITE_NAND_ADDRESS(0xff&(chunkInNAND>>16), 0); pwrite = (__u8 *)spare; for(i=0; i<BYTES_OF_SPARE; i++) WRITE_NAND(*pwrite++, 0); WRITE_NAND_COMMAND(0x10, 0); // program command for(i=0; i<10; i++); NAND_WAIT_READY(0); //wait WRITE_NAND_COMMAND(0x70, 0); // read status command if(0x00 != (READ_NAND(0) & 0x01)) { rc = YAFFS_FAIL; } } NAND_DISABLE_CE(0); //Enable Flash
Nand_ReadChunkFromNAND((struct yaffs_DeviceStruct *)0, chunkInNAND, readback, (yaffs_Spare *)&readback[BYTES_PER_PAGE]); if(data) { if(0 != memcmp(readback, data, BYTES_PER_PAGE)) { printf("write Data error
"); rc = YAFFS_FAIL; } else printf("write Data ok
"); } if(spare) { if(0 != memcmp(readback+BYTES_PER_PAGE, (__u8 *)spare, BYTES_OF_SPARE)) { printf("write Spare error
"); rc = YAFFS_FAIL; } else printf("write Spare ok
"); } free(readback); return rc; } |
|