I've tried to erase and write data to PFlash, but it will occur ILLEGAL_BP. The following list is my test environment.
MCU: MC9S12XS128
IDE : CodeWarrior IDE version 5.9.0 Build 2836
BDM: TBDML
Here is my Code
void PFlash_Write(word *ptr){ unsigned char i; while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait if command in progress FSTAT = 0x30; //clear ACCERR and PVIOL FCCOBIX = 0x00; FCCOB = 0x067E; //program 0x7E0000 FCCOBIX = 0x01; FCCOB = 0x0000; for(i=2; i<6; i++) //fill data (4 words) to FCCOB register { FCCOBIX = i; FCCOB = *ptr; ptr++; } FSTAT = 0x80; //launch command while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait for done}/******************************************************************************/void PFlash_Erase(void){ while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait if command in progress FSTAT = 0x30; //clear ACCERR and PVIOL FCCOBIX = 0x00; FCCOB = 0x0A7E; //erase 0x7E0000 FCCOBIX = 0x01; FCCOB = 0x0000; FSTAT = 0x80; //launch command while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait for done}void main{ unsigned short u16Data[4]; for( ; { .... if( getkey() == 'w' ) { u16Data[0] = 100; u16Data[1] = 110; u16Data[2] = 120; u16Data[3] = 130; DisableInterrupts; PFlash_Erase(); PFlash_Write(u16Data); EnableInterrupts; } }}
Could you have any suggestion for me to solve this problem? Thanks a lot.
Regards,
Pogo
Solved! Go to Solution.
In the code:
#pragma CODE_SEG __NEAR_SEG ramcodeseg
static void executeflashcmd(void)
{
asm
{
PSHC // remember I-bit state
SEI // disable interrupts
}
FSTAT = 0x80; //launch command
while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait for done
asm PULC; // restore I-bit
}
#pragma CODE_SEG DEFAULT
In PRM file you need to add following line into PLACEMENT block
ramcodeseg INTO RAM;
To make it working you need to use ANSI C startup routine, which will copy routine from flash to RAM at startup. Else you will need to copy routine by your own code.
It is because flash memory is not readable while any flash command is being executed (flash/erase/etc). This part of your code should execute from RAM:
FSTAT = 0x80; //launch command
while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait for done
Also, since interrupts can happen at any time, even while writing/erasing flash, you also need to disable interrupts before launching flash command. (It is possible to move critical ISR's and interrupt vectors table to RAM and disable only less time critical interrupts).
Thanks for your reply.
Could you show me how to copy the code into RAM as function and how to call it?
I use the same code to erase or program D-Flash, but it doesn't have this issue.
Thanks again.
Regards,
Pogo
In the code:
#pragma CODE_SEG __NEAR_SEG ramcodeseg
static void executeflashcmd(void)
{
asm
{
PSHC // remember I-bit state
SEI // disable interrupts
}
FSTAT = 0x80; //launch command
while((FSTAT & FSTAT_CCIF_MASK) == 0); //wait for done
asm PULC; // restore I-bit
}
#pragma CODE_SEG DEFAULT
In PRM file you need to add following line into PLACEMENT block
ramcodeseg INTO RAM;
To make it working you need to use ANSI C startup routine, which will copy routine from flash to RAM at startup. Else you will need to copy routine by your own code.
Hi Kef,
It works fine.
Thanks for your help.
Regards,
Pogo