Skip to main content
GTolk.1
Associate II
December 11, 2019
Question

Writing in flash is not working

  • December 11, 2019
  • 5 replies
  • 2017 views

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();
 
}

This topic has been closed for replies.

5 replies

Tesla DeLorean
Guru
December 11, 2019

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
GTolk.1
GTolk.1Author
Associate II
December 12, 2019

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?

GTolk.1
GTolk.1Author
Associate II
December 11, 2019

.

Piranha
Principal III
December 14, 2019

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.
December 14, 2019
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