2022-05-12 04:29 AM
Hello,
cn anyone help me with flash writing.
I am using bootloader to update the application code in STM32F407, But the code isnt working i tried to debug the code, the code is not erasing the sectors.
i found HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) erasing function returning the error status.
So i also have tried to do the FLASH_Erase_Sector(StartSector, FLASH_VOLTAGE_RANGE_3); After this the sector was getting erased successfully but the
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartSectorAddress, Data[sofar]) is giving error .
So i tried to debug this function too and i observed FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); this function was givinig eror status.
Can someone help me solve this problem
The code works fine when i run it in debug mode , the flash erase and write works properly
2022-05-12 04:46 AM
Hello @suraj,
Before calling HAL_FLASHEx_Erase you need to unlock the Flash using HAL_FLASH_Unlock();
It should work fine.
Fell free to ask for more details.
Best Regards,
Gwénolé
2022-05-12 04:52 AM
uint32_t Flash_Write_Data (uint32_t StartSectorAddress, uint32_t *Data, uint16_t numberofwords)
{
uint32_t SECTORError;
int sofar=0;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
if(erase_flag == 0)
{
/* Erase the user Flash area */
/* Get the number of sector to erase from 1st sector */
uint32_t StartSector = GetSector(StartSectorAddress);
uint32_t EndSectorAddress = StartSectorAddress + numberofwords*4;
uint32_t EndSector = GetSector(EndSectorAddress);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = StartSector;
EraseInitStruct.NbSectors = (EndSector - StartSector) + 1;
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
DCRST and ICRST bits in the FLASH_CR register. */
// HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
// FLASH_Erase_Sector(StartSector, FLASH_VOLTAGE_RANGE_3);
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
{
HAL_UART_Transmit(&huart3, &lol, 1, 3000);
return HAL_FLASH_GetError ();
}
}
/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
while (sofar<numberofwords)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartSectorAddress, Data[sofar]) == HAL_OK)
{
StartSectorAddress += 4; // use StartPageAddress += 2 for half word and 8 for double word
sofar++;
}
else
{
// HAL_UART_Transmit(&huart3, &lol, 1, 3000);
/* Error occurred while writing data in Flash memory*/
return HAL_FLASH_GetError ();
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
return 0;
}
As soon as I call the function Flash_Write_Data() I am unlocking the flash. I have attached the code for your reference.
Thank you,