cancel
Showing results for 
Search instead for 
Did you mean: 

Can't erase on certain flash memory address using STM32G030J6

Candra SN.
Associate III

I want to store some data at start address (0x0800F800) until end address (0x0800F820) on flash memory of STM32G030J6. I save data when the address is empty. When the data has reached the end address, I want to erase all data stored from start to end address. But after several tests, the data was not successfully removed from the address.

is there a special configuration in the code for the function of deleting data on flash memory STM32G030J6 ? Or maybe there is a little mistake in my code ?

Here is my code:

Flash memory address

#define DATA_PAGE_STR ((uint32_t)0x0800F800)
#define DATA_PAGE_END ((uint32_t)0x0800F820)
uint32_t Dstr = DATA_PAGE_STR;

Erase function

void F_Erase_Proccess(void)
{
	uint8_t page = 0;
	uint32_t str = 0;
 
	page = 1;
	str = DATA_PAGE_STR;
 
	HAL_FLASH_Unlock();
 
	uint32_t PageError = 0;
	FLASH_EraseInitTypeDef EraseInit;
	EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
	EraseInit.Page = str;
	EraseInit.NbPages = page;
 
	HAL_FLASHEx_Erase(&EraseInit, &PageError);
	HAL_FLASH_Lock();
}

Write function

void F_Write_Proccess(uint32_t val)
{
	uint32_t* str = 0;
	uint32_t end = 0;
	uint32_t def = 0;
 
	str = &Dstr;
	end = DATA_PAGE_END;
	def = DATA_PAGE_STR;
 
	HAL_FLASH_Unlock();
	uint32_t value = 0;
	while (1)
	{
		value = *(__IO uint32_t*)(*str);
		if (value == 0xFFFFFFFF)
		{
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, *str, val);
			break;
		}
		else
		{
			*str += 8;
			if (*str > end)
			{
				*str = def;
				F_Erase_Proccess();
			}
		}
	}
	HAL_FLASH_Lock();
}

Read function

void F_ReadL_Proccess(uint32_t* var)
{
	uint32_t* str = 0;
	uint32_t def = 0;
		
        str = &Dstr;
	def = DATA_PAGE_STR;
 
	uint32_t value = 0;
	uint32_t vaddr = def;
 
	while (1)
	{
		value = *(__IO uint32_t*)vaddr;
		if (value == 0xFFFFFFFF)
		{
			*str = vaddr-8;
			if (*str < def)
			{
				*var = 0;
				*str = def;
			}
			else *var = *(__IO uint32_t*)(*str);
			break;
		}
		else vaddr += 8;
	}
}

2 REPLIES 2
KnarfB
Principal III

Dstr is an uint32_t holding the address you need. Taking &Dstr is wrong.

hth

knarfB

Hello @KnarfB​ ,thanks for your advice. I only use &Dstr for writing and reading functions. And it works fine. My problem is not being able to erase the data on that address. Does &Dstr affect the erase function ?