1

I´m trying to save some data to flash memory on my STM32F407 board. Before I can save them I need to erase memory sector. I choosed 16 Kbytes Sector1 starting with address 0x08004000 and choosed Voltage range 2.1-2.7 V. I'm using HAL library.

Program stops responding after FLASH->CR |= FLASH_CR_STRT; line inside HAL_FLASHEx_Erase() -> FLASH_Erase_Sector() function.

I'm pretty sure it's my fault but I can't find out what is wrong.

HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
                           FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.Sector = FLASH_SECTOR_1;
EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_2;
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {     
    HAL_FLASH_Lock();
    return;
}

uint16_t data = 300;
//----------------------------write data
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, start_address, data) != HAL_OK) {
    HAL_FLASH_Lock();
    return;
}
HAL_FLASH_Lock();

Did I choosed wrong voltage range or number of sectors?

Thanks for your answers.

1

2 Answers 2

2

If your program is bigger than 16k, then you've just managed to erase a part of it from flash. You should pick a sector from the end of the flash (but then the erase times will be longer), or rearrange the sections a bit in your linker configuration.

2

I found the solution. I used HAL_FLASH_Lock() function instead of HAL_FLASHEx_Erase() function and it works fine. I also changed SECTOR because I was accidently erasing my program.

unit32_t address = 0x0800C000;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

FLASH_Erase_Sector(FLASH_SECTOR_3, VOLTAGE_RANGE_3);

//----------------------------write data  
uint8_t data = 'A';
if (HAL_FLASH_Program(TYPEPROGRAM_BYTE, address, data) != HAL_OK) {
    HAL_FLASH_Lock();
    return;
}
HAL_FLASH_Lock();

Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.