cancel
Showing results for 
Search instead for 
Did you mean: 

FLASH_Erase_Sector

Stefano Ugolini
Associate III
Posted on December 22, 2017 at 10:53

I'm trying to store some data using HAL library.

HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );

FLASH_Erase_Sector(ADDR_FLASH_SECTOR_10, VOLTAGE_RANGE_3);

uint64_t testVar = 0XFAFAFAFAFAFAFAFA;

HAL_FLASH_Program(TYPEPROGRAM_WORD, ADDR_FLASH_SECTOR_10, testVar);

HAL_FLASH_Lock();

Now

FLASH_Erase_Sector does not erase the sector and that's why I can't program it. Am I missing something?

I tried the same function from STM32F4xx_StdPeriph_Driver library and it works fine. I can see that the two fuctions are different even if they should look the same. Few things I don't understand.

1.In the HAL version of the function FLASH_WaitForLastOperation() is not used. 

2.This part is missing

/* if the erase operation is completed, disable the SER Bit */

FLASH->CR &= (~FLASH_CR_SER);

FLASH->CR &= SECTOR_MASK;

3. This comment seems a bad copy and paste from the 

STM32F4xx_StdPeriph_Driver  version.

/* if the previous operation is completed, proceed to erase the sector */

4. I can't figure out where this come from, could someone point me to a datasheet?

/* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */

if(Sector > FLASH_SECTOR_11)

{

Sector += 4U;

}

#hal_flash_program-stm32f43 #flash_erase_sector #hal_flash
1 REPLY 1
Malith Yapa
Associate
Posted on March 19, 2018 at 07:36

I think you're supposed to use HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError) which calls

FLASH_Erase_Sector

internally along with the missing parts from the peripheral library that you have mentioned here.

But I'm still having some issue where it is not erased consistently. Sometimes requires multiple calls to successfully erase. Have you had any success with this?