This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Using ROM_FlashErase causes hard faults later.

Other Parts Discussed in Thread: TM4C1294NCPDT, EK-TM4C1294XL

I am using a TM4C1294NCPDT and am currently porting a bootloader that was developed for the LM3S9D90. The problem that has me stuck is that after a call to ROM_FlashErase, subsequent code will cause a hard fault. I have used two methods to track down this hard fault: printing to UART2 and toggling an LED. For example, line 10 will cause a hard fault when the UART2 interrupt is enabled. Line 9 will likewise cause a fault when GPIOPinWrite is called (the GPIOPinRead part of that macro seems to work?). If both lines 9 and 10 are removed from the loop, the loop will complete, then I will get a hard fault in line 12 (if in) or line 13 (if 12 is removed). If both line 12 and 13 are removed, I will get a hard fault later. The NVIC_VTABLE is always 0.

Here is the code fragment.

  1.     PRINTF("About to start flash erase.\n"); //This prints OK
  2.     MID_LED_TGL; //This works
  3.     //Note: MID_LED_TGL is GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_1, (GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_1) ^ GPIO_PIN_1))
  4.     // Enter a loop to erase all the requested flash pages beginning at the
  5.     // application start address (above the USB stick updater).
  6.     for (ulIdx = APP_START_ADDRESS; ulIdx < ulFlashEnd; ulIdx += FLASH_PAGE_SIZE) {
  7.         ROM_FlashErase (ulIdx);
  8.         //MID_LED_TGL; //this will cause a hard fault if used
  9.         //PRINTF("Erased a page.\n"); //this will cause a hard fault if used (when the UART interrupt is enabled)
  10.     }
  11.     MID_LED_OFF; //if just ROM_FlashErase is in previous loop, then this will cause a hard fault
  12.     PRINTF("Finished flash erase.\n"); //This will fault when interrupt is enabled

This may or may not be related, but it might be a clue: when I get this fault, I cannot restart the code in the debugger. If the code is stopped and I do a System Reset and/or Core Reset and press the Restart button (which is what I normally do to re-start code) code starts immediately (without me pressing the "Play" button) and the uC will be in the hard fault handler. Forcing a reload of the uC is the only why I've found to re-run the code.

  • Hello Mark,

    Did you try replacing it with the Flash based erase? Also what are the value of APP_START_ADDRESS, ulFlashEnd and FLASH_PAGE_SIZE

    Regards
    Amit
  • Not sure what you mean by “Flash based erase”. If I call FlashErase instead of ROM_ FlashErase, the resulting .bin file is about 40 bytes larger (so there must be some extra code somewhere) but the behavior is the same. The APP_START_ADDRESS is 0x4000, the end address is the end of 512K flash. I don’t think it is an address problem as the first address is 0x4000 and once that first erase is done, I cannot, for example, turn on an LED. However, I can erase more pages without problem (i.e. finish the erase loop).
  • Hello Mark,

    Let me share the source code with you for the a simple test program that I have and may be we can use that as a start point to debug the issue. Are you using the EK-TM4C1294 or a custom board? If the latter, can you let me know which pin the LED and does it switch on when high or low?

    Regards
    Amit
  • Hello Amit,
    It is running on a custom board. However, since the bootloader is so small, most of the features of the board are not used. I could port it to run on the EK-TM4C1294. An LED pin is PJ1 (see line 3 in the fragment in my original post), active high.
    Mark
  • Mark,
    Perhaps you have the NVIC_VTABLE_R register pointed to the flash you just erased at 0x4000?

    Randy
  • Hello Mark,

    My bad. I was using a DK-TM4C1924 for a USB Boot Loader. But no worries. I will retrofit it for EK-TM4C1294XL, test it and send it across.

    Regards
    Amit
  • Amit,
    Thank you for looking into this. I have a video showing the odd behavior I am experiencing. This bootloader gets it's Firmware.bin image from an attached SPI Flash with a file system. So the first thing I would normally do it make sure such a file exists and then get it's size. I noticed that when I took this out (and set the file size so that all of flash (except the bootloader) would be erased), I did not get a hard fault after erasing the flash. Interesting. When I added the code to open the file after the erase was done, I got a hard fault *at the erase*--the new code never ran. When I commented out this code, the erase works fine. In either case the code never runs, but if it is included after the erase section, the erase section faults. Here is a link to the video: dl.dropboxusercontent.com/.../ROM_FlashErase1.avi

    Thank you,
    mark
  • Hello Mark,

    I went through the video. Unfortunately I am not sure why the removal of the code for file check create a problem, unless a part of the same is in the section you have erased.

    Regards
    Amit
  • Mark Kirkwold said:
    he problem that has me stuck is that after a call to ROM_FlashErase, subsequent code will cause a hard fault. I have used two methods to track down this hard fault: printing to UART2 and toggling an LED.

    Based upon the fact that peripheral access cause a hard fault, have you checked the SYSCTL registers to see if the peripherals are being unexpectedly disabled by the ROM_FlashErase call?

    With an EK-TM4C129XL which makes use of ROM_FlashErase, FlashErase, ROM_FlashProgram and FlashProgram while toggling LEDs. I wasn't able to repeat the problem of getting a hard fault when toggling LEDs on GPIO pins, following a call to ROM_FlashErase. 

  • Amit,
    OK, never mind. For any others that may be interested, the second clue in my original post was in fact a big one: being unable to restart the debugger. The reason? Part of the code had been erased. Having a look at the .map file showed that a few bytes extended past the 0x4000 limit, thus, once the first page was erased, the bootloader itself could no longer run.

    Thank you,
    mark
  • Hello Mark,

    Good catch and thanks for posting back.

    Regards
    Amit