cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to write to internal flash - STM32F4Discovery

dan239955_stm1_st
Associate II
Posted on November 11, 2015 at 17:59

I have the following code:

 FLASH_EraseInitTypeDef EraseInitStruct

;

    

__HAL_FLASH_INSTRUCTION_CACHE_RESET

();

    

__HAL_FLASH_INSTRUCTION_CACHE_ENABLE

();

    

HAL_FLASH_Unlock

();

    /* Get the 1st sector to erase */

    // FirstSector = GetSector (Start_Addr);

    /* Get the number of sector to erase from 1st sector*/

    // NbOfSectors = GetSector (End_Addr) - FirstSector + 1;

    uint32_t SectorError;

    /* Fill EraseInit structure*/

    EraseInitStruct.TypeErase = TYPEERASE_SECTORS;

    EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3;

    EraseInitStruct.Sector = FLASH_SECTOR_1;

    EraseInitStruct.NbSectors = 1;

    

__HAL_FLASH_CLEAR_FLAG

(FLASH_FLAG_EOP | FLASH_FLAG_OPERR |

                            FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);

    if (

HAL_FLASHEx_Erase

(&EraseInitStruct, &SectorError) != HAL_OK)

    {

        return false;

    }

    

FLASH_WaitForLastOperation

(10000);

    uint32_t* ptr = (uint32_t*)0x8004000;

    uint32_t value = *ptr;

    if (value == 0xFFFFFFFF)

    {

        return true;

    }

    else

    {

        return false;

    }

I expect this to  make the value at address 0x08004000 read 0xFFFFFFFF but it doesn't.

    Any ideas why not? I'm running this and stepping through to verify.

Any help is much appreciated.

D

4 REPLIES 4
Posted on November 11, 2015 at 22:14

Do you have any of the locks set?

What is at that address?

JW

Posted on November 12, 2015 at 00:32

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/Unable%20to%20erase%20Flash%20sector%20-%20STM32F4Discovery&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/AllItems.aspx&currentviews=6]Dupe

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dan239955_stm1_st
Associate II
Posted on November 12, 2015 at 10:20

''Do you have any of the locks set?''

   I assume '' 

HAL_FLASH_Unlock

();'' should have cleared any locks?

''What is at that address?''

Nothing at the moment...

Perhaps its not erasing the address range I think it is... Are sectors defined in hardware or by the HAL library?

Dan

dhiry2k1
Associate II
Posted on November 12, 2015 at 19:18

From where you are running the code. Seems like you are running from 0x8000000 and 0x8004000 might be in your code itself and probably that is why you are unable to erase the memory.