2024-12-18 08:58 PM - last edited on 2024-12-19 12:10 AM by SofLit
Split from this thread: https://community.st.com/t5/stm32-mcus-embedded-software/flash-writing/td-p/753438
Hi @SofLit thanks and
void StartUpdateFWBlueNRG(uint32_t SizeOfUpdate, uint32_t uwCRCValue)
{
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);
// BootInfo* boot_info = (BootInfo*)BOOT_INFO_ADDRESS;
// uint32_t target_address = (boot_info->active_app == 1) ? 0x0801C000 : 0x08005000;
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SectorError = 0;
OTA_PRINTF("Start FLASH Erase\r\n");
SizeOfUpdateBlueFW = SizeOfUpdate;
AspecteduwCRCValue = uwCRCValue;
// Compute first and last pages
uint32_t FirstPage = GetPage(0x0801C000);
uint32_t LastPage = GetPage(0x08022EE0-1);
uint32_t NbPages = LastPage - FirstPage + 1;
// Compute number of pages to erase
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = 0x0801C000;
EraseInitStruct.NbPages = NbPages;
// Unlock the Flash
HAL_FLASH_Unlock();
// Clear all flash error flags
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
uint16_t errorcode = HAL_FLASH_GetError();
OTA_PRINTF("Flash Erase Error: 0x%08lX\r\n", (long)errorcode);
OTA_ERROR_FUNCTION();
} else {
OTA_PRINTF("End FLASH Erase %ld Pages of %d Bytes\r\n", (long)EraseInitStruct.NbPages, FLASH_PAGE_SIZE);
}
// Lock the Flash
HAL_FLASH_Lock();
}
now im able to write the flash however my erase flash doesnt work and hence writting flash as well but if that area is clear i can able to write also there is no issue in address selection also bcz same procedure i directly use in main.c file it works well.
2024-12-18 09:00 PM
its working by using _I0 macro however im not able to erase the flash and there is not problrm in address selection also i believe bcz same approcch if i do in main.c file it works
2024-12-19 12:05 PM - edited 2024-12-19 09:06 PM
Hi I have put above reply for flash write it as an solution accepted but please help me out in while erasing the flash fn. the point it will not erase the selected reason but if do hard code Nb.pages (256) instead for getting it from Getpage first and last.
the pages of this stm32l072 198kb are 128bytes each page and 4kb per sector.
uint32_t GetPage(uint32_t Addr)
{
uint32_t page = 0;
if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
{
/* Bank 1 */
page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
}
else
{
/* Bank 2 */
page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
}
return page;
}
void StartUpdateFWBlueNRG(uint32_t SizeOfUpdate, uint32_t uwCRCValue)
{
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);
// BootInfo* boot_info = (BootInfo*)BOOT_INFO_ADDRESS;
// uint32_t target_address = (boot_info->active_app == 1) ? 0x0801C000 : 0x08005000;
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SectorError = 0;
OTA_PRINTF("Start FLASH Erase\r\n");
SizeOfUpdateBlueFW = SizeOfUpdate;
AspecteduwCRCValue = uwCRCValue;
WritingAddress = OTA_ADDRESS_START;
// WritingAddress = target_address;
// Compute first and last pages
uint32_t FirstPage = GetPage(0x0801C000);
// uint32_t LastAddress = WritingAddress + SizeOfUpdate - 1;
uint32_t LastPage = GetPage(0x0802300-1);
uint32_t NbPages = LastPage - FirstPage + 1;
// Compute number of pages to erase
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = 0x0801C000;
EraseInitStruct.NbPages = 256/*((LastPage - FirstPage) / FLASH_PAGE_SIZE) + 1*/;
// Unlock the Flash
HAL_FLASH_Unlock();
// Clear all flash error flags
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);
// __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGFLASH_FLAG_EOPAERR);
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
uint16_t errorcode = HAL_FLASH_GetError();
OTA_PRINTF("Flash Erase Error: 0x%08lX\r\n", (long)errorcode);
OTA_ERROR_FUNCTION();
} else {
OTA_PRINTF("End FLASH Erase %ld Pages of %d Bytes\r\n", (long)EraseInitStruct.NbPages, FLASH_PAGE_SIZE);
}
// Lock the Flash
HAL_FLASH_Lock();
}