AnsweredAssumed Answered

Hard Faults reading program Flash memory

Question asked by ryanmonfils on Aug 22, 2016
Latest reply on Aug 23, 2016 by EARL GOODRICH


I have custom firmware consisting of a Bootloader and application on the Kinetis K22FN1M0VLL12.  My Bootloader goes through the following sequence. 

 

1) Send a command to erase enough Flash to program the new application.

2) Send the application in packets via the UART.  Write each packet into Flash as it comes in.

3) Reboot

4) Compute a CRC over the entire application in Flash and compare against a pre-calculated value.

5) If the CRCs match, jump to the application.

 

I don't believe I'm doing anything unusual.  I've been testing with corrupt application images to ensure that the system recovers properly and have discovered an issue.  Sometimes after an update while looping through the CRC calculation, I will jump into a Hard Fault.  I can't understand why this is happening and am not sure what to try next.  The Hard Fault seems to coincide with the portion of the application I corrupted but it shouldn't matter since I am just reading the Flash location.  Any suggestions?  Below is the snippet of CRC code.

 

uint16_t calulate16BitCrc (uint32_t startAddress, uint32_t length)
{
    volatile char* dataPtr;
    char newData;
    uint16_t crc;

    dataPtr = (uint8_t*)startAddress;
    crc = 0x5555;

    for (int i = 0; i < length; i++)
    {
        newData = *dataPtr;
        crc ^= newData;
        crc &= 0xFFFF;
        crc = (crc >> 15) | (crc << 1);
        dataPtr++;
    }
    return crc;
}

Outcomes