cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASHEx_Erase Problem

Bahadir(Canceled)
Associate III

Hello everyone !

I am converting one of the my projects from F0 to G0. While debugging, when i come to HAL_FLASHEx_Erase line, it jumps to HardFault. I checked the memory, flash address is empty. I don't erase the program. Here is the some picture during the debug session and code.

What is the problem or my fail?

//-------Error flags after the Erase function 

0693W00000JPQT5QAP.jpg0693W00000JPQT0QAP.jpg 

int flash_par_adr1[ParSize] __attribute__((at(0x0800F000)));	// 4096 Byte =4KB
int flash_par_adr2[ParSize] __attribute__((at(0x0800E000)));	// 4096 Byte =4KB
 
//_____________________________________________________________________________ SaveParamToFlash
HAL_StatusTypeDef SaveParamToFlash(int page)
{
uint32_t tFlashAddress	= 0x00;
uint32_t tParAddress		= 0x00;
FLASH_EraseInitTypeDef	FLASH_EraseInit;
uint32_t								PageError;
HAL_StatusTypeDef				feedback;
	
	if(page == 0)
		FlashAddress= (uint32_t)&flash_par_adr1;
	else
		FlashAddress= (uint32_t)&flash_par_adr2;
 
/* Erase the FLASH pages */	
	ParAddress	= (uint32_t)∥
	EndAddress	= ParAddress + ParSize;
	NbrOfPage		= (EndAddress - ParAddress) / FLASH_PAGE_SIZE + 1;
	
	FLASH_EraseInit.TypeErase		= FLASH_TYPEERASE_PAGES;
	//FLASH_EraseInit.Banks				= 0x00;
	FLASH_EraseInit.Page	      =	FlashAddress;
	FLASH_EraseInit.NbPages			= NbrOfPage;
	HAL_FLASH_Unlock();
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_MISERR | FLASH_FLAG_PGSERR | FLASH_FLAG_FASTERR);
  
	feedback = HAL_FLASHEx_Erase(&FLASH_EraseInit,&PageError);
	HAL_FLASH_Lock();
	if(feedback != HAL_OK)
		return(feedback);
 
/* Program the user Flash area word by word*/
	tFlashAddress	= FlashAddress;
	tParAddress		= ParAddress;
	HAL_FLASH_Unlock();
	while ((tParAddress < EndAddress) && (feedback == 0))
	{
		feedback = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST,tFlashAddress, *(__IO uint32_t *)tParAddress);
		if(feedback != HAL_OK)
			return(feedback);
		tFlashAddress	= tFlashAddress + 4;
		tParAddress		= tParAddress + 4;
	}
	HAL_FLASH_Lock();
	
/* Check if the programmed data is OK */
	tFlashAddress	= FlashAddress;
	tParAddress		= ParAddress;
  while ((tParAddress < EndAddress) && (feedback == 0))
  {
    if (*(__IO uint32_t *)tParAddress	!= *(__IO uint32_t *)tFlashAddress)
    {
			feedback = HAL_ERROR;
    }
 
    tFlashAddress	= tFlashAddress + 4;
		tParAddress		= tParAddress + 4;
  }
	return(feedback);
}

0693W00000JPQSbQAP.jpg

4 REPLIES 4
TDK
Guru

> //FLASH_EraseInit.Banks = 0x00;

Why is this commented out? You need to initialize this field.

> FLASH_EraseInit.Page = FlashAddress;

This is asking for a page, not an address.

Print out the values in FLASH_EraseInit at the time of the call to HAL_FLASHEx_Erase.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for reply TDK !

Actually I used codes which worked on F0. I guess i am missing something.

I will try on Monday what you suggest.

Bahadir(Canceled)
Associate III

SOLVED...

First of all thank you @TDK​ you opened my eyes !

It is not PageAddress it is Page as you said so I added this code my function to calculate page number.

uint32_t  FlashPage;
FlashPage = (FlashAddress & 0x000FFFFF) / 0x800 ;

After that, I added this code below all of the flash operations.

FLASH_WaitForLastOperation(1000);

And it SOLVED. Whole codes below for anyone who needs help.

HAL_StatusTypeDef SaveParamToFlash(int page)
{
uint32_t tFlashAddress	= 0x00;
uint32_t tParAddress		= 0x00;
FLASH_EraseInitTypeDef	FLASH_EraseInit;
uint32_t								PageError;
uint32_t  FlashPage;
HAL_StatusTypeDef				feedback;
	
	if(page == 0)
		FlashAddress= (uint32_t)&flash_par_adr1;
	else
		FlashAddress= (uint32_t)&flash_par_adr2;
  
  FlashPage = (FlashAddress & 0x000FFFFF) / 0x800 ;
  
 
/* Erase the FLASH pages */	
  
	ParAddress	= (uint32_t)&PAR;
	EndAddress	= (uint32_t)ParAddress + ParSize;
	NbrOfPage		= (uint32_t)(EndAddress - ParAddress) / FLASH_PAGE_SIZE + 1;
	
	FLASH_EraseInit.TypeErase		= FLASH_TYPEERASE_PAGES;
	FLASH_EraseInit.Banks				= 0x00;
	FLASH_EraseInit.Page	      =	FlashPage;
	FLASH_EraseInit.NbPages			= NbrOfPage;
	__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR
  |  FLASH_FLAG_PGSERR  ); 
  HAL_FLASH_Unlock();  
	feedback = HAL_FLASHEx_Erase(&FLASH_EraseInit,&PageError);
  FLASH_WaitForLastOperation(1000);
	HAL_FLASH_Lock();
	if(feedback != HAL_OK)
		return(feedback);
 
 
	tFlashAddress	= FlashAddress;
	tParAddress		= ParAddress;
	HAL_FLASH_Unlock();
	while ((tParAddress < EndAddress) && (feedback == 0))
	{
		feedback = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,tFlashAddress, *(__IO uint64_t *)tParAddress);
		if(feedback != HAL_OK)
			return(feedback);
		tFlashAddress	= tFlashAddress + 8;
		tParAddress		= tParAddress + 8;
	}
  FLASH_WaitForLastOperation(1000);
	HAL_FLASH_Lock();
	
/* Check if the programmed data is OK */
	tFlashAddress	= FlashAddress;
	tParAddress		= ParAddress;
  while ((tParAddress < EndAddress) && (feedback == 0))
  {
    if (*(__IO uint32_t *)tParAddress	!= *(__IO uint32_t *)tFlashAddress)
    {
			feedback = HAL_ERROR;
    }
 
    tFlashAddress	= tFlashAddress + 4;
		tParAddress		= tParAddress + 4;
  }
  FLASH_WaitForLastOperation(1000);
	return(feedback);
}

Also;

void LoadParamFromFlash(int page)
{
	ReloadWDT();
	if(page == 0)
		FlashAddress= (uint32_t)&flash_par_adr1;
	else
		FlashAddress= (uint32_t)&flash_par_adr2;
	ParAddress	= (uint32_t)&PAR;
	EndAddress	= (uint32_t)ParAddress + ParSize;
  while (ParAddress < EndAddress)
  {
		*(__IO uint64_t *)ParAddress	= *(__IO uint64_t *)FlashAddress;
    FlashAddress	= FlashAddress + 8;
		ParAddress		= ParAddress + 8;
    __NOP();
  }
  FLASH_WaitForLastOperation(2500);
}