0
\$\begingroup\$

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ I can confirm that FLASH_PageErase(0, FLASH_BANK_2) successfully erases data. I used STM32CubeProgrammer to edit that memory area, and the data gets erased when this method is executed. \$\endgroup\$ Commented May 6 at 12:42

1 Answer 1

1
\$\begingroup\$

I got answer for this question here:

https://community.st.com/t5/stm32-mcus-products/stm32l4s5-write-data-to-2mb-flash-memory-stm32cubeide/m-p/800395#M278455


At the link above, STMicroelectronics Community member TDK posted:

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

The relevant code at that link is:

...
  /* Program the user Flash area word by word
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  Address = FLASH_USER_START_ADDR;

  while (Address < FLASH_USER_END_ADDR)
  {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
    {
      Address = Address + 8;
    }
   else
    {
      /* Error occurred while writing data in Flash memory.
         User can add here some code to deal with this error */
      while (1)
      {
        /* Make LED2 blink (100ms on, 2s off) to indicate error in Write operation */
        BSP_LED_On(LED2);
        HAL_Delay(100);
        BSP_LED_Off(LED2);
        HAL_Delay(2000);
      }
    }
  }
...
\$\endgroup\$
1
  • \$\begingroup\$ landas - Hi, Thanks for coming back with the answer that you got on the STM forum. Answers here should not be link-only, since if that link dies in future, the answer becomes useless. Therefore I copied over what I think is the relevant information, with attribution to its author, and added that to your answer. You edit what I added to improve it, if needed, of course. \$\endgroup\$ Commented May 8 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.