cancel
Showing results for 
Search instead for 
Did you mean: 

Can't erase flash on STM32407VG Discovery

Alan Chambers
Associate II

I'm a bit lost by this one. I can write to the flash but cannot erase a sector. I can erase sectors with the ST-Link Utility. The result codes at every step indicate success, but the flash is not actually erased. I am using the Standard Peripheral Library as follows (the status checks with while (true) are just part of the debugging effort):

void FlashDriver::erase_sector(uint8_t sector_index)
{
    DisableInterrupts di;
    // Unlock the Flash Program Erase controller 
    if (FLASH_GetStatus() != FLASH_COMPLETE)
        while (true);            
    FLASH_Unlock();
 
    // Clear pending flags (if any)
    FLASH_ClearFlag(
        FLASH_FLAG_EOP |
        FLASH_FLAG_OPERR |
        FLASH_FLAG_WRPERR |
        FLASH_FLAG_PGAERR |
        FLASH_FLAG_PGPERR |
        FLASH_FLAG_PGSERR
    );
 
    // Erase the given logical sector.
    if (FLASH_GetStatus() != FLASH_COMPLETE)
        while (true);
    if (FLASH_EraseSector(sector_index, VoltageRange_3) != FLASH_COMPLETE)
    {
        ...
    }
 
    if (FLASH_GetStatus() != FLASH_COMPLETE)
        while (true);
    FLASH_Lock();   
}

I'd be grateful for any insights on this one.

Al

3 REPLIES 3
Alan Chambers
Associate II

I should add that I converted the Discovery board to work with J-Link (using a utility from SEGGER). I don't know if this might have some impact on fuses or other features which might affect the flash. Kind of clutching at straws.

Alan Chambers
Associate II

Fixed it. The variable sector_index is a logical index in my implementation: 0, 1, 2, ... but FLASH_EraseSector() expects FLASH_Sector_0, FLASH_Sector_1, FLASH_Sector_2 which are completely different values. Silly error that was far too easy to make. Feeling sheepish.

I started writing a custom HAL some time ago that would have made this bug almost impossible - the register field values are typesafe enum classes. Perhaps I should return to that project. I won't use Cube's HAL under any circumstances, but I don't suppose SPL is going to support STM32MP1 (say).

>>but FLASH_EraseSector() expects...

ST seems to favour bit patterns it can apply directly rather than indexes it can shift into place.

For the F2/F4 I'd built most of this in assembler, and had to code routines to compute the variable/non-uniform sector sizes. Extra fun on the 2MB parts, where the 16K/64K/128K patterns were repeated at the beginning of the second bank, and the index was discontinuous.

The code actually turned out reasonably compactly, I was aiming for something I could copy to RAM without a rabbit warren of call dependencies.

Hope for SPL is rather grim, we'll just have to frankenstein the usable parts from HAL...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..