Hi Alice,
i tought that may be a general problem, so i didnt attach many details.
Curious, in singlestep mode it works fine. It seems to be a conflict, maybe the MCU try to read the next command while the flash write command is still running. I dont know if so, but the Flowchart in the Reference Manual suggest to check the CCIF Bit to check the command is finished. If not two actions at same time are running, the next command will be done after the flash command is done and we not have to check the CCIF Bit.
Error bits i cannot readout, because the MCU reboots directly when it should execute the command.
To my setup: I have a board, but there is another MCU implemented as i wants to work with. So i attached a MKV10Z32VLC7 directly to the NXP LPC-Link 2 Programmer and like to program baremetal. I use the "KV10 Sub-Family Reference Manual" Document Number: KV10P48M75RM. I already programmed the clock, interrupts, PWM, UART..
I also looked into the sample code of "SDK_2.2_MKV10Z32xxx7" and others. But in this case i didnt found anything about this theme. The code in this examples is automatic generated and often too complicated to read for me. Beseides, i like to change it for my special requirements.
Here my nonworking code to write flash:
int main(void)
{
SetClock75MHz(); // Sets the system Clock to 75 MHz and the Bus Clock/Flash Clock to 25MHz
FTFA_FCNFG = 0x00; // Command Complete Interrupt and Read Collision Error Interrupt off
while(((FTFA_FSTAT & 0x10) | (FTFA_FSTAT & 0x20)) > 0){} // check on ACCERR und FPVIOL
//SIM_SCGC6 |= 0x00000001; // Clockgate enable, but not needed
// Erase
while((FTFA_FSTAT & 0x80) == 0){} // check CCIF Bit, whether a command is still running
FTFA_FSTAT = 0x70; // delete all old Errorbits (also 0x30 may be correct)
FTFA_FCCOB0 = 0x09; //load "Erase Flash Sector" Command
FTFA_FCCOB1 = 0x00; // Adresse High Byte
FTFA_FCCOB2 = 0x7C; // Adresse, Middle Byte
FTFA_FCCOB3 = 0x00; // Adresse Low Byte (must be longword aligned)
FTFA_FSTAT = 0x80; // write CCIF Bit to clear it and execute the command
//while((FTFA_FSTAT & 0x80) != 0){Wait(5);} // from a forum, its needly ?
while((FTFA_FSTAT & 0x80) == 0){} // check CCIF Bit
// Write
while((FTFA_FSTAT & 0x80) == 0){} // check CCIF Bit, whether a command is still running
FTFA_FSTAT = 0x70; // delete all old Errorbits
FTFA_FCCOB0 = 0x06; // load "Program Longword" Command
FTFA_FCCOB1 = 0x00; // Adresse High Byte
FTFA_FCCOB2 = 0x7C; // Adresse, Middle Byte
FTFA_FCCOB3 = 0x00; // Adresse Low Byte (must be longword aligned)
FTFA_FCCOB4 = 0xAB; // load 4. Byte
FTFA_FCCOB5 = 0xCD; // load 3. Byte
FTFA_FCCOB6 = 0xEF; // load 2. Byte
FTFA_FCCOB7 = 0x01; // load 1. Byte
FTFA_FSTAT = 0x80; // write CCIF Bit to clear it and execute the command
while((FTFA_FSTAT & 0x80) == 0){} // check CCIF Bit
I also tried with the associated enabled interrupt (and an IRQ Handler Routine):
NVIC_EnableIRQ(5); // enable Interrupt Vektor
FTFA_FCNFG = 0x80; // Command Complete Interrupt