Skip to main content
JPikk.1
Associate
Jun 5, 2023 10:05 AM UTC
Question

MCU:STM32G0B1 fails flash page erase with HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)

Why using:

/* Fill EraseInit structure*/
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
	EraseInitStruct.Banks		= FLASH_BANK_1;
	EraseInitStruct.Page 		= StartPage;
	EraseInitStruct.NbPages = ((EndPage - StartPage)/FLASH_PAGE_SIZE) +1; /* For example 32 */

HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)

causes HAL_Error and calling

uint32_t HAL_FLASH_GetError(void)

shows that bits:

HAL_FLASH_ERROR_PGA
HAL_FLASH_ERROR_SIZ
HAL_FLASH_ERROR_PGS

are set?

And how to fix it?

EDIT1:

Calculate the number of pages to be erased correctly.

	EraseInitStruct.NbPages = (EndPage - StartPage) +1;

This topic has been closed for replies.

2 replies

Piranha
Principal III
Jun 6, 2023 9:02 PM UTC

You are dividing page numbers by FLASH_PAGE_SIZE...

JPikk.1
JPikk.1Author
Associate
Jun 7, 2023 6:09 AM UTC

Hello @Piranha​ and thank you for your answer.

You are correct that that calculation made no sense.

It caused number of pages to always be 1, after the +1 was added to the calculation.