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, I’m getting the following unwanted behaviour (CAPITAL LETTERS) and I’m not finding out what I’m doing wrong:
- 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.
Any help is appreciated.
Thank you.
An extract of my code follows:
I’m calling it inside main loop.
`
#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 always 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,
// even though 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();
`