Hello, I've written some code to write in flash, I've tested it with all values from 0x00 to 0xFE, and all addresses from 0xC000 to 0xC07F (first flash page). I can write and read every value in any address, except for value 0x04.
This is the source code:
Code:
typedef struct { unsigned char code[100];}FuncInRAM;typedef void (*pRAM_CopyRAMtoFlash)(unsigned int, unsigned int, unsigned char);typedef void (*pRAM_ErasePage)(unsigned int);static void ROM_CopyRAMtoFlash(unsigned int srcRAM, unsigned int dstROM, unsigned char size){ while(size--) { if(*(unsigned char *)dstROM != *(unsigned char *)srcRAM) { while(!FSTAT_FCBEF); *(unsigned char *)dstROM = *(unsigned char *)srcRAM; FCMD = FCMD_BURST_PROGRAM; FSTAT = 0x80; if(FSTAT & BM_FLASH_ERR_MASK) size=0; } dstROM++; srcRAM++; } while(!FSTAT_FCCF);}void CopyRAMtoFlash(unsigned int srcRAM, unsigned int dstROM, unsigned char size){ #pragma MESSAGE DISABLE C1805 FuncInRAM RAM_CopyRAMtoFlash=*(FuncInRAM *)(ROM_CopyRAMtoFlash); if (FSTAT & BM_FLASH_ERR_MASK) FSTAT = BM_FLASH_ERR_MASK; { asm PSHA; asm TPA; asm SEI; asm STA CCR_reg; asm PULA; } ((pRAM_CopyRAMtoFlash)&RAM_CopyRAMtoFlash)(srcRAM,dstROM,size); { asm PSHA; asm LDA CCR_reg; asm TAP; asm PULA; }}unsigned char VerifyFlash(unsigned int srcRAM, unsigned int dstROM, unsigned char size){ unsigned char c; for(c=0;c<size;c++) { if(((unsigned char *)srcRAM)[c] != ((unsigned char *)dstROM)[c]) return 1;//Error } return 0;//OK}
I've written this code to test the application code above :
Code:
#pragma CONST_SEG MY_DATA_ROMconst unsigned char DataInROM[2][512];//segment defined @ 0xC000#pragma CONST_SEG DEFAULT... unsigned char c; unsigned char d; //FIRTS TEST, WRITE CONSTANT VALUE for(d=0;d<255;d++) { for(c=0;c<128;c++) { DataInRAM[c]=d; } ErasePage((unsigned int)&DataInROM[0][0]); CopyRAMtoFlash((unsigned int)&DataInRAM[0],(unsigned int)&DataInROM[0][0],128); if (VerifyFlash((unsigned int)&DataInRAM[0],(unsigned int)&DataInROM[0][0],128)) ERROR(); WatchDog(); } //SECOND TEST, WRITE PROGRESSIVE VALUE for(d=0;d<255;d++) { for(c=0;c<128;c++) { DataInRAM[c]=c+d; } ErasePage((unsigned int)&DataInROM[0][0]); CopyRAMtoFlash((unsigned int)&DataInRAM[0],(unsigned int)&DataInROM[0][0],128); if (VerifyFlash((unsigned int)&DataInRAM[0],(unsigned int)&DataInROM[0][0],128)) ERROR(); WatchDog(); }
These are the results:
data written--> data read
0x00 0x00 0x00 ... --> 0x00 0x00 0x00 ...
0x01 0x01 0x01 ... --> 0x01 0x01 0x01 ...
0x02 0x02 0x02 ... --> 0x02 0x02 0x02 ...
0x03 0x03 0x03 ... --> 0x03 0x03 0x03 ...
0x04 0x04 0x04 ... --> 0x04 0xFF 0xFF 0x04 0xFF 0xFF 0x04 0xFF 0xFF...
0x05 0x05 0x05 ... --> 0x05 0x05 0x05 ...
...
0xFE 0xFE 0xFE ... --> 0xFE 0xFE 0xFE ...
data written--> data read
0x00 0x01 0x02 0x03 0x04 0x05 ... --> 0x00 0x01 0x02 0x03 0x05 0x06 0x07 ... 0x40 0xFF 0x41 ...
0x01 0x02 0x03 0x04 0x05 0x06 ... --> 0x01 0x02 0x03 0xFF 0x05 0x06 ...
0x02 0x03 0x04 0x05 0x06 0x07 ... --> 0x02 0x03 0x05 0x06 ... 0x42 0xFF 0x43 0x44 ...
0x03 0x04 0x05 0x06 0x07 0x08 ... --> 0x03 0xFF 0x05 0x06 0x07 0x08 ...
...
Stack size is 0x120, I use HCS08 multilink USB to debug and read memory contents.
If I run the tests step by step, there is not error, it writes correctly.
Bus speed is 2.09MHz, FCDIV=10 --> 2.09MHz/(10+1)=190KHz
Could anybody tell me what is wrong?
Thank you in advance