cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong behaviour: Writing on FLASH works only the first time I change the page

RLeon.1
Associate II

Hi to everybody,

I'm newbie with STM.

I'm using:

Board: NUCLEO-L4R5ZI

MCU: STM32L4R5ZI

IDE: STM32CubeIDE_1.0.0

I'm trying to write into FLASH by using HAL library and I'm getting the following unwanted behaviour (CAPITAL LETTERS):

- The writing works fine ONLY THE FIRST TIME I change the page, with or WITHOUT erasing the page before. THE SUBSEQUENT TIMES, I ALWAYS GET THE FOLLOWING ERRORS, even though restarting the device:

   HAL_FLASH_ERROR_PROG HAL_FLASH_ERROR_PGA HAL_FLASH_ERROR_PGS

- The written value can be read succesfully EVEN AFTER THE PAGE ERASING and even after restarting the device.

- The erasing returns HAL_OK and PageError does not report errors on the specified page.

An extract of my code follows. I'm calling it inside main loop.

Any help is appreciated.

Thank you.

#define FLASH_BANK			FLASH_BANK_1 // I also tried FLASH_BANK_2
#define FLASH_BANK_ADDRESS	((FLASH_BANK == FLASH_BANK_1) ? 0x08000000 : 0x08080000)
#define FLASH_PAGE_N		250   // I also tried many other pages
#define FLASH_PAGE_BYTES_N	0x800 // 2kb
#define FLASH_PAGE_ADDRESS	(FLASH_BANK_ADDRESS + (FLASH_PAGE_N * FLASH_PAGE_BYTES_N))
#define DOUBLEWORD_BYTES_N	8 // 8*8 == 64 bit == sizeof(uint64_t)
#define FLASH_ADDRESS_VALUE_1	(FLASH_PAGE_ADDRESS + (DOUBLEWORD_BYTES_N * 0))
#define FLASH_ADDRESS_VALUE_2	(FLASH_PAGE_ADDRESS + (DOUBLEWORD_BYTES_N * 1))
 
int value_1 = 1000;
int value_2 = 10;
 
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_FLASH_CLK_ENABLE();
 
HAL_FLASH_Unlock();
 
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
 
FLASH_EraseInitTypeDef flash_erase_init =
{
	.TypeErase = FLASH_TYPEERASE_PAGES,
	.Banks     = FLASH_BANK,
	.Page      = FLASH_PAGE_N,
	.NbPages   = 1
};
uint32_t flash_page_erase_error = 0;
// ret is HAL_OK_
uint32_t ret = HAL_FLASHEx_Erase(&flash_erase_init, &flash_page_erase_error);
 
// This works fine (ret == HAL_OK) only the first time after changing FLASH_PAGE_N,
// also if I comment the HAL_FLASHEx_Erase() above:
ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_ADDRESS_VALUE_1, (uint64_t)(value_1));
// This always fails (ret == HAL_ERROR):
ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_ADDRESS_VALUE_2, (uint64_t)(value_2));
 
// value_1_read is == value_1:
uint64_t value_1_read = (*(__IO uint64_t*)FLASH_ADDRESS_VALUE_1);
// value_2_read is wrong:
uint64_t value_2_read = (*(__IO uint64_t*)FLASH_ADDRESS_VALUE_2);
 
HAL_FLASH_Lock();

4 REPLIES 4
Mike_ST
ST Employee

Hello, there are examples for nucleo L4R5ZI / Flash, in L4 firmware package.

Please check them.

RLeon.1
Associate II

SOLVED

I initially referred to [https://www.st.com/content/ccc/resource/training/technical/product_training/91/e3/aa/26/e6/69/4f/de/STM32L4_Memory_Flash.pdf/files/STM32L4_Memory_Flash.pdf/jcr:content/translations/en.STM32L4_Memory_Flash.pdf] which says that a page is 2KB (pag.6).

Now in stm32l4xx_hal_flash.h I see that for STM32L4R5xx it is 4KB (FLASH_PAGE_SIZE).

Did I misunderstand STM32L4_Memory_Flash.pdf or should it supply a diffent information?

Thank you

From page 2 of the presentation:

Please note that this presentation has been written for

STM32L47x/48x devices.

Key differences with other devices are indicated at the end of

the presentation unless otherwise specified.

There's no 'L4R5 at the end of the presentation, it's obviously older than 'L4R5.

The DS+RM to given part is *always* the authoritative information source.

JW

RLeon.1
Associate II

My mistake.

Thank you for your support.

RL