AnsweredAssumed Answered

Kinetis K10FX512 read flash hard fault

Question asked by Dario Antolovic on Oct 6, 2015

Hello friends,


we are working with MK10FX512VLQ12 on our custom developed board. We completed almost the firmware for the intended application, and had no problem until now.


The problem started while trying to implement the bootloader functionality. The bootloader FW writes some parameters in flash memory, that are used by both applications (bootloader and user). After the write is done, we check if the parameters are really in flash on the desired locations, but while accessing the flash location, the MCU goes to hard fault.


I will try to clarify all the steps that we make before the hard fault.


  • After reset we check a flag in memory (0x9000), that indicates if the bootloader application should be started, or should we jump to the user application. The read function looks like:


#define BL_RUN_FLAG_ADDRESS         0x9000   bootloader_run_flag = *((uint32_t*) BL_RUN_FLAG_ADDRESS);


The read function returns the correct value, and is set to enter the bootloader application if no user application is present.

  • In the bootloader application (after the PE_init) we first check if there are 5 parameters writen i memory (0x9004, 0x9008, 0x900C, 0x9010 and 0x9014), if not (and they are not at first start of the bootloader application) we write the parameters to this locations. To write this parameters we use the IntFLASH PE component from Configuration Data: Using the Internal FLASH instead of an external EEPROM | MCU on Eclipse. Below is the code to check if the data is written, and to write the data.


if(BL_CheckMsgIDinFlash()){      BL_WriteMsgIDtoFlash();       Cpu_SystemReset(); }  bool BL_CheckMsgIDinFlash(void) {   uint32_t rtrn = 0;   uint32_t i = 0;    for (i = 0; i < MSGID_NR; i++) {        if (Flash_MSGID[i] != *((uint32_t*) (BL_RUN_FLAG_ADDRESS + ((i + 1) * 4)))) {             rtrn++;        }   }   return (bool) rtrn; }  bool BL_WriteMsgIDtoFlash(void) {   uint32_t i = 0;   uint32_t err = 0;   uint32_t tmp32 = 0;   uint32_t addr = 0;    for (i = 0; i < MSGID_NR; i++) {        tmp32 = Flash_MSGID[i];        addr = BL_RUN_FLAG_ADDRESS + (i + 1) * 4;        err = IFsh1_SetBlockFlash((IFsh1_TDataAddress) &tmp32, addr, 4); // write data to specified location in flash    } return err; }


When I run this part of code in debugger, the memory locations are not written correctly, at least the debugger shows it incorrectly. While single stepping trough BL_WriteMsgIDtoFlash() i have this in my memory locations:




On the third step the first two locations are erased, and at the end the two last locations are correct.


  • After that I reset the CPU and do again the whole procedure. First I check the Bootloader flag on location 0x9000, and at this point the CPU goes in the hard fault handler.


bootloader_run_flag = *((uint32_t*) BL_RUN_FLAG_ADDRESS);



I read the flowing values in the system registers:




The PC shows the last location where it broke, and this is the part of the code:


222         bootloader_run_flag = *((uint32_t*) BL_RUN_FLAG_ADDRESS); 000036fa:   mov r3,#0x9000 000036fe:   ldr r3,[r3,#0] 00003700:   str r3,[r7,#4]


The same problem happens when I try immediately read the memory locations after the write command, without resetting the CPU.


I attached also the InitFLASH source files for reference.


I'm very confused now, because we implemented this bootloader firmware on two other devices (MK10DX128 and MK40DN512) and it works without any problems.


Does anyone have a suggestion what could be the problem here?


Best regards,


Original Attachment has been moved to: Generated_Code.rar