Skip to main content
FVarg.1
Visitor II
May 19, 2021
Question

HAL_ERROR when i try to erase the flash memory

  • May 19, 2021
  • 2 replies
  • 2446 views

Hello, i'm using STM32G071RB and i need to write on the Flash Memory.

i check if i have wrote on it before. If i did, i try erase the page to re-write. At this point, i just get a HAL_ERROR of the function HAL_FLASHEx_Erase(&EraseInitStruct, &PageError).

i'm doing this:

#define FLASH_USER_START_ADDR  0x08000000

#define USEDPAGES ((uint8_t) 64)

HAL_FLASH_Unlock(); //Unlock memory write permission

FLASH_EraseInitTypeDef EraseInitStruct;

/**Define Erase Struct**/

EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

EraseInitStruct.Page = FLASH_USER_START_ADDR;

EraseInitStruct.NbPages = USEDPAGES;

if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK){//Erase Pages

printf("Fail Init Erasing\n");

return 0;

}

i did the same funtions with stm32f0 and i had no troubles with it

This topic has been closed for replies.

2 replies

TDK
May 19, 2021

> #define FLASH_USER_START_ADDR 0x08000000

> EraseInitStruct.Page = FLASH_USER_START_ADDR;

It needs a page number, not an address.

See an example here:

https://github.com/STMicroelectronics/STM32CubeG0/blob/5cb06333a6a43cefbe145f10a5aa98d3cc4cffee/Projects/STM32G071B-DISCO/Examples/FLASH/FLASH_EraseProgram/Src/main.c

Also in the code:

/**
 * @brief FLASH Erase structure definition
 */
typedef struct
{
 uint32_t TypeErase; /*!< Mass erase or page erase.
 This parameter can be a value of @ref FLASH_Type_Erase */
 uint32_t Banks; /*!< Select bank to erase.
 This parameter must be a value of @ref FLASH_Banks
 (FLASH_BANK_BOTH should be used only for mass erase) */
 uint32_t Page; /*!< Initial Flash page to erase when page erase is enabled
 This parameter must be a value between 0 and (FLASH_PAGE_NB - 1) */
 uint32_t NbPages; /*!< Number of pages to be erased.
 This parameter must be a value between 1 and (FLASH_PAGE_NB - value of initial page)*/
} FLASH_EraseInitTypeDef;

"If you feel a post has answered your question, please click ""Accept as Solution""."
Ons KOOLI
Associate
May 20, 2021

Hi @FVarg.1​ ,

Please refer to the FLASH_EraseProgram example under directory STM32Cube_FW_G0_V1.4.0\Projects\NUCLEO-G071RB\Examples\FLASH of the STM32CubeG0.

Best Regards,

Ons.

Note: If the problem is resolved, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster.