cancel
Showing results for 
Search instead for 
Did you mean: 

Erasing a page of flash on STM32L476RG

fab04
Associate II

Hello everybody,

I'm working on an STM32L476RG. For the moment, I'm using a nucleo-476RG board.

I'm trying to write and read some data in the flash memory.

I'm trying to write at the address : 0x080FF800 (first address of last page of the memory).

I do the following operations : erase page / write 16 bytes / read 16 bytes.

When I'm in debug mode, I see that the erase operation doesn't work (flash memory contains the old data) and then writing operation doesn't work well.

By adding the instruction "monitor flash mass_erase" in my run configurations, I can see that the page is well erased and then writing and reading are working well.

So I think something is wrong in my code but I don't know what. Moreover, the HAL_FLASHEx_Erase function gave me HAL_OK as return (and it seems not to be right).

/* Déclarations et initialisations des variables */
	volatile uint64_t data_to_FLASH[(strlen((char*)data)/8)	+ (int)((strlen((char*)data) % 8) != 0)];
 
	memset((uint8_t*)data_to_FLASH, 0, strlen((char*)data_to_FLASH));
	strcpy((char*)data_to_FLASH, (char*)data);
 
	volatile uint32_t data_length = (strlen((char*)data_to_FLASH) / 8) + (int)((strlen((char*)data_to_FLASH) % 8) != 0);
	volatile uint16_t pages = (strlen((char*)data)/page_size) + (int)((strlen((char*)data)%page_size) != 0);
 
	HAL_FLASH_Unlock();
	HAL_FLASH_OB_Unlock();
 
	/* Fill EraseInit structure */
	FLASH_EraseInitTypeDef EraseInitStruct;
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
	EraseInitStruct.Page = FLASH_STORAGE;
	EraseInitStruct.NbPages = pages;
	uint32_t PageError;
 
	volatile uint32_t write_cnt=0, index=0;
 
	volatile HAL_StatusTypeDef status;
 
	status = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
 
	while(index < data_length) {
	  if (status == HAL_OK) {
	      status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_STORAGE + write_cnt, data_to_FLASH[index]);
		  if(status == HAL_OK) {
			  write_cnt += 8;
			  index++;
		  }
	  }
	}
 
	HAL_FLASH_OB_Lock();
	HAL_FLASH_Lock();

7 REPLIES 7
Bob S
Principal

FIrst - what is with all the "volatile" modifiers? Not harmful but there is no need for those.

Second - Is it probable that the debugger is NOT refreshing the data from Flash in its "memory" window, since it "knows" that Flash memory shouldn't normally be changing.

And finally, what do you mean "writing and reading don't work well"? Does that mean that you code that attempts to read back the data you just wrote shows unexpected data? Or does that mean that the memory window in the debugger doesn't show what you expect (see "second" above).

fab04
Associate II

Thank you for your answer.

Concerning your second remark, it's what I was expected, so I have added my read function directly after erasing memory and I have red again my old data which confirm that the erasing is not working.

I mean that the software stay blocked in the do-while because HAL_FLASH_Program return HAL_ERROR (value of FLASH_SR = 0x00A8) so that means that I have PROGERR, PGAERR & PGSERR.

 I suppose that I have PROGERR because the registers contain value different from 0xFFFF FFFF.

Bob S
Principal

With some STM32 families and versions of HAL, I've had to add this line before all calls to __HAL_FLASH_CLEAR_FLAG() before calling the HAL flash functions, such as:

// Code to write a block of data to FLASH, params not shown
HAL_FLASH_Unlock();
while ( len-- > 0 && sts == HAL_OK ) {
    __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_ALL_ERRORS );
   sts = HAL_FLASH_Program( ..... );
}
HAL_FLASH_Unlock();

And also before calling HAL_FLASHEx_Erase().

fab04
Associate II

I've tried to add this instruction : __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_ALL_ERRORS ); but the result is the same, the flash is not erased.

Is anything else that I can try ?

Pavel A.
Evangelist III

In your code, is FLASH_STORAGE address of the page? It should be the page number (small integer).

fab04
Associate II

yes FLASH_STORAGE is the address of the page.

fab04
Associate II

and now it works ! thank you very much !