3
\$\begingroup\$

I´m trying to save some data to flash memory on my STM32F407 board. Before I can save them, I need to erase a memory sector. I chose 16 Kbytes Sector1 starting with address 0x08004000 and chose 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 choose wrong voltage range or number of sectors?

Thanks for your answers.

\$\endgroup\$
  • 1
    \$\begingroup\$ Define "stops responding". Are you trying to debug through this? Don't expect that to work, as flash programming has tight hardware requirements. \$\endgroup\$ – Chris Stratton Nov 15 '15 at 22:19
  • \$\begingroup\$ I placed my breakpoint on line with HAL_FLASH_Program() function but my program has never reached it. After erasing memory it stops responding and I can wait how long I want. \$\endgroup\$ – Jardo421 Nov 16 '15 at 9:59
2
\$\begingroup\$

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, start_address, data) != HAL_OK) {
    HAL_FLASH_Lock();
    return;
}
HAL_FLASH_Lock();

Thanks for your help.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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