Skip to main content
Explorer
May 8, 2025
Solved

STM32L4S5 write data to 2MB flash memory (STM32CubeIDE)

  • May 8, 2025
  • 1 reply
  • 383 views

I am trying to write to the flash memory of the STM32L4S5QIIX microcontroller on the EVAL-ADIN1110EBZ development board. I have also tried similar code on an STM32L4S5VITX microcontroller on a custom circuit board.

Short example code:

uint32_t flash_memory_address = 0x08100000;
uint64_t flash_write_data = 0x12345678;
uint8_t flash_read_data[4]= {0};

HAL_FLASH_Unlock();
FLASH_PageErase(0, FLASH_BANK_2);
HAL_FLASH_Lock();

HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,
 flash_memory_address, 
 flash_write_data);
HAL_FLASH_Lock();

 

The full code and project files can be found at: https://github.com/landas/stm32l4s5-flash-memory/blob/e8304c3d17e27ee9f27f8a8243ceb7f73ba077bf/Core/Src/main.c#L135

FLASH_PageErase(0, FLASH_BANK_2) operation was completed successfully. This was verified by using STM32CubeProgrammer to write to the flash memory and confirm the changes in the specified memory area.

HAL_FLASH_Program(...) returns the error codes:

  • HAL_FLASH_ERROR_PGA
  • HAL_FLASH_ERROR_PGS

I get similar code to work with a NUCLEO-F303RE develpoement board without any errors.

I would appreciate any hints on how to resolve this issue.

Note: I have tried the same code without 

HAL_FLASH_Lock();
HAL_FLASH_Unlock();

between erase and program.

 

    This topic has been closed for replies.
    Best answer by TDK

    Use HAL_FLASHEx_Erase to erase flash pages. FLASH_PageErase is not meant to be called from user programs, only HAL_* functions.

    Here is an example project which erases and writes to flash:

    STM32CubeL4/Projects/NUCLEO-L452RE/Examples/FLASH/FLASH_EraseProgram/Src/main.c at 874612f28ce541e585f253cdcfd434dd8eb94914 · STMicroelectronics/STM32CubeL4

    1 reply

    TDKAnswer
    Super User
    May 8, 2025

    Use HAL_FLASHEx_Erase to erase flash pages. FLASH_PageErase is not meant to be called from user programs, only HAL_* functions.

    Here is an example project which erases and writes to flash:

    STM32CubeL4/Projects/NUCLEO-L452RE/Examples/FLASH/FLASH_EraseProgram/Src/main.c at 874612f28ce541e585f253cdcfd434dd8eb94914 · STMicroelectronics/STM32CubeL4