1

I currently have this code setup to erase a specific sector of the STM32F207 internal flash (in a freeRTOS environment):

    static void eraseSector(uint32_t sector)
    {
        FLASH_EraseInitTypeDef eraseInitStruct = { 0 };
        HAL_StatusTypeDef status = HAL_OK;
     
        if( HAL_FLASH_Unlock() != HAL_OK )
        {
            printDebug("Error unlocking Flash\r\n");
        }
     
        eraseInitStruct.TypeErase = TYPEERASE_SECTORS;
        eraseInitStruct.VoltageRange = VOLTAGE_RANGE_3;
        eraseInitStruct.Sector = sector;
        eraseInitStruct.NbSectors = 1;
     
        status = HAL_FLASHEx_Erase_IT(&eraseInitStruct);
        if (status != HAL_OK)
        {
            print("Unable to erase Sector: %d\n", status);
        }
        else
        {
            print("Flash sector %d erased. Status: %d\n", sector, status);
        }

        /* Wait for the erase operation to complete */
        osSemaphoreWait(FlashOperation_sem, osWaitForever);

        if( HAL_FLASH_Lock() != HAL_OK )
        {
            print("Error locking Flash\n");
        }
    }

When this code executes, the application resets/crashes whenever the FLASH_CR Start bit is set in FLASH_Erase_Sector() (called from the HAL_FLASHEx_Erase_IT() function). I've attempted this with multiple unused flash sectors and they all crash.

I've also attempted directly calling FLASH_Erase_Sector() and the same persists.

Anviori
  • 15
  • 8
  • 1
    you are running this code from ram yes? check with the part as to whether or not you can run from flash while flashing. often the design will have two banks or more if this is possible and you can erase parts of one while running off the other. the common solution is to just run from ram, copy the code required to flash then jump. – old_timer Oct 30 '20 at 15:09
  • The [STM32F2xx Flash Programming Manual](https://www.st.com/resource/en/programming_manual/CD00233952-.pdf) says, "code or data fetches cannot be performed while a write/erase operation is ongoing." I believe that means you must copy your flash programming routines to RAM and execute them from RAM. – kkrambo Oct 30 '20 at 15:19
  • Following the instructions here: https://www.keil.com/support/docs/3228.htm I placed the file with the code above, as well as the HAL file with the FLASH_Erase_Sector() function in RAM and the erase still fails to execute. – Anviori Oct 30 '20 at 18:55

1 Answers1

0

Turns out my crashes were caused by my Watchdog dying (terrible oversight on my part). I temporarily disable the watchdog during the flash erase & write operations and it works fine.

The better option, though, would be to place the functionality in RAM (as suggested by @old_timer & @kkrambo) to prevent the CPU from being blocked for too long.

Anviori
  • 15
  • 8
  • 1
    I dont see that this part has banked flash memories so if it happened to work it was dumb luck likely based on ST's cache/prefetch thing. In general a bad idea to try to run from flash and erase flash at the same time, unless the documentation clearly states how to do it safely. – old_timer Nov 02 '20 at 07:43
  • @old_timer, yeah that appears to be the case. It isn't working 100% of the time. The solution for this part seems to be executing from RAM as you suggested. – Anviori Nov 03 '20 at 12:46