MC9S08AW16 Flash writing problem, can't write value 0x04

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MC9S08AW16 Flash writing problem, can't write value 0x04

1,489 Views
FordFairlane
Contributor I
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:
  • First test:
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 ...
  • Second test:
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
 
Labels (1)
0 Kudos
1 Reply

297 Views
FordFairlane
Contributor I
I've found the problem, I'm readind from flash memory while it is being written. The problem is solved removing one line of code:
 
Code:
static void ROM_CopyRAMtoFlash(unsigned int srcRAM, unsigned int dstROM, unsigned char size){    while(size--)    {//        if(*(unsigned char *)dstROM != *(unsigned char *)srcRAM) //----ERROR-----        {            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);}

 If I remove the line where source is compared to destination it works much better, this line can't be placed before checking FCBEF. I don't know why I wrote this line, because memory must be erased before writing, therefore it will always be 0xFF.
 
Now I have a smaller problem, I can't write more than 50 bytes. If I write 128bytes starting at 0xC000, address 0xC033 has value number 0x32 instead of value number 0x33.
Write: 0x00 0x01 0x02 0x03 0x04 ... 0x30 0x31 0x32 0x33 0x34 0x35
Read: 0x00 0x01 0x02 0x03 0x04 ... 0x30 0x31 0x32 0x32 0x34 0x35
If I call 4 times the same function to write 32bytes each time, it writes 128 bytes correctly.
 
I don't need to write more than 50 bytes, but I'd like to know why it fails.
 
Any help?
 
 
0 Kudos