cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to erase flash memory of STM32G030 using HAL_FLASHEx_Erase

Gladson
Associate III

I am working on a project which requires storing values in flash memory so that the values will be retained next time the device is turned on. I tried this on STM32F103C8T6 and it worked without any issues. But on STM32G030C8T6 I'm getting error while trying to erase a page.

 

The following is the code that I wrote for erasing a flash memory page.

if (HAL_FLASHEx_Erase(&eis, &pageError) != HAL_OK){
	HAL_FLASH_Lock();
	return HAL_FLASH_GetError();
} else {
	HAL_FLASH_Lock();
	return 0;
}

But it is returning 0x80. I did some debugging and found out that it is "HAL_FLASH_ERROR_PGS". Which by the function's comment, it is a "Programming sequence error". What is this error? how can it be solved? why did it not appear while I was working on F103?

 

This is the link for the project.

1 ACCEPTED SOLUTION

Accepted Solutions

Check if G HAL require .Page as address or as page number.

View solution in original post

4 REPLIES 4
LCE
Principal

I guess the ancient F1 series is completely different from G0.
Maybe the G0 series requires unlocking flash before erasing or programming? So check the reference manual.

At least in H7 there are extra key unlock registers which must be written with certain codes before full flash access.

Gladson
Associate III

I have used HAL_FLASH_Unlock() to unlock the flash memory, this is before the code I put in the original question.

This is the whole function for erasing.

uint32_t eraseData(){
	static FLASH_EraseInitTypeDef eis;
	uint32_t pageError;
	uint32_t address = baseAddress;
	HAL_FLASH_Unlock();
	eis.TypeErase = FLASH_TYPEERASE_PAGES;
	eis.Page = address;
	eis.NbPages = 1;

	if (HAL_FLASHEx_Erase(&eis, &pageError) != HAL_OK){
		HAL_FLASH_Lock();

		return HAL_FLASH_GetError();
	} else {

		HAL_FLASH_Lock();
		return 0;
	}
}

 I saw a reference program for H7 series microcontroller in this link. They also used only HAL_FLASH_Unlock() to erase the program.

Check if G HAL require .Page as address or as page number.

Gladson
Associate III

Thanks a lot @MM..1 for the answer, It was exactly the problem.

In F103, I had to write the page address.

In G030, After writing page number it worked.