Hi,
the issue I see in the following lines:
while((FSTAT & FSTAT_CCIF_MASK) == 0){
_FEED_COP();
}; //wait for done
There is a rule for command processing: The command must not be executed from the Flash block containing space which is Erased/Written.
Because of this, the part of the code waiting for CCIF is executed out of the RAM to avoid accidental E/W of the bock from which the code is executed.
Plese look into an example XEP100-FLASH-PFLASH-CW51.ZIP which can be found in LAMA's S12XE unofficial examples.
There is function stored in RAM (see module main.c)
//==============================================================================
//PFLASH Send_Command
//==============================================================================
//this function is stored in nonbanked RAM memory. Must be called by JSR
//in C language:
// {
// FSTAT_CCIF = 1; //launch command
// while(FSTAT_CCIF == 0); //wait for done
// }
static unsigned char Send_Command[]=
{
0x1C, 0x01, 0x06, 0x80, 0x1F, 0x01, 0x06, 0x80, 0xFB, 0x3D
};
The function is then called:
asm JSR Send_Command;
Moreover, I suggest you to disable interrupts during execution of this critical part.
Note: Of course the CodeWarrior IDE provides also possibility to create critical function directly in the RAM space by means of placement "#pragma CODE_SEG DEFAULT_RAM". The you do not have to use assembler instructions. Moreover, the function can be called in standard way PFLASH_Send_command(void); .
#pragma CODE_SEG DEFAULT_RAM
PFLASH_Send_command(void)
{
asm PSHC // store CCR
DisableInterrupts; // asm SEI
FSTAT_CCIF = 1; //launch command
while(FSTAT_CCIF == 0); //wait for done
asm PULC // restore CCR
}
#pragma CODE_SEG DEFAULT
As you can see I made small modification and moved interrupts handling from flash command preparation function to this function.
I have attache mentioned example also here.
Best regards,
Ladislav