cancel
Showing results for 
Search instead for 
Did you mean: 

Writing in flash is not working

GTolk.1
Associate II

Hi,

im new for Stm32. I want to write 4 numbers in the flash but always get an error message (status = HAL_ERROR while writing in flash). In my function I write the numbers in an array and want to write the individual elements into the flash.

The variables (zahlx) are uint8_t.

Can anyone tell me what I am doing wrong?

void save_to_flash(int wahl)
{
	volatile uint32_t write_cnt = 0, index = 0;
 
	volatile uint32_t data_to_FLASH[4];
 
	memset((uint8_t*)data_to_FLASH, 0,strlen((char*)data_to_FLASH));
 
 
	switch (wahl)
	{
	case 1:
		data_to_FLASH[0] = zahl1;
		data_to_FLASH[1] = zahl2;
		data_to_FLASH[2] = zahl3;
		data_to_FLASH[3] = zahl4;
		write_cnt = 0;
	break;
	case 2:
		data_to_FLASH[0] = zahl5;
		data_to_FLASH[1] = zahl6;
		data_to_FLASH[2] = zahl7;
		data_to_FLASH[3] = zahl8;
		write_cnt = 10;
 
	break;
	}
 
 
	HAL_FLASH_Unlock();			
 
	HAL_FLASH_OB_Unlock();
 
 
	FLASH_EraseInitTypeDef EraseInitStruct;	
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
	EraseInitStruct.Page = 127;
	EraseInitStruct.NbPages = 1;
	uint32_t PageError;
 
 
	volatile HAL_StatusTypeDef status;
	status = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
 
	if(status == HAL_OK)
	{
		while(index < 4)
		{
		        status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,FLASH_STORAGE+write_cnt, data_to_FLASH[index]);
			if(status == HAL_OK)		
                        {			
				write_cnt +=4;	
                                index++;	
			}
		}
	}
	HAL_FLASH_OB_Lock();
	HAL_FLASH_Lock();
 
}

5 REPLIES 5

What STM32 part? Let's narrow it down to 1 or 2, rather than hundreds, across a dozen families.

Make sure you're erasing the right page, and the locations you try to write are blank.

Any of the word write? Or fails immediately?

Not Option Bytes, so don't unlock those.​

Instrument the code, add sanity checking.​

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
GTolk.1
Associate II

.

Hi,

Thank you for your answer. I'm using a STM32L433RC.

The page I want to write on is empty.

0690X00000BuUZvQAN.jpg

I want to write on the last page, so i define 'FLASH_STORAGE = 0x0803F800'.

To detect the problem, i read the flash error code.

...
while(index < 4)
{
	status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,
                  FLASH_STORAGE+write_cnt,data_to_FLASH[index]);
	if(status == HAL_OK)
        {
		write_cnt +=4;
                index++;	
	}else
	{
		printf("Error code: %d for data %d\n",HAL_FLASH_GetError(),index);
	}
}...

It fails immediately, the result is: "Error code: 224 for data 0"

what does this code mean?

Piranha
Chief II

Flash memory on STM32L4 can be programmed only by special 2*32-bit block write procedure and it must be 8-byte aligned.

Read the reference manual section 3.3.7!

In HAL_FLASH_Program(), instead of a million useless code lines of bloat, they could've done address alignment checking, but of course that's not the case...

Pavel A.
Evangelist III
volatile uint32_t data_to_FLASH[4];
memset((uint8_t*)data_to_FLASH, 0,strlen((char*)data_to_FLASH));

Seriously - strlen? Maybe, go learn C first?

-- pa