Skip to main content
lucasrj
Associate II
March 24, 2025
Question

stm32l451 error when trying to write to the Flash

  • March 24, 2025
  • 13 replies
  • 2039 views

i am trying to implement openBootloader for an stm32l451,

but i cant get the write to work i get the following errors HAL_FLASH_ERROR_PROG | HAL_FLASH_ERROR_PGA |HAL_FLASH_ERROR_PGS

i am trying to write to 800c000 in the flash

this is my code( i used https://github.com/STMicroelectronics/stm32wl-openbl-apps to assist in how to write the interfaces)

void OPENBL_FLASH_Unlock(void) { HAL_FLASH_Unlock(); }

static void OPENBL_FLASH_Program(uint32_t Address, uint64_t Data) {
 /* Clear all FLASH errors flags before starting write operation */
 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

 HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, Data);
}


void OPENBL_FLASH_Write(uint32_t Address, uint8_t *Data, uint32_t DataLength) { 
 uint32_t index;
 __ALIGNED(4) uint8_t buffer[FLASH_PROG_STEP_SIZE] = {0x0U};
 uint8_t remaining;

 if ((Data != NULL) && (DataLength != 0U)) {
 /* Unlock the flash memory for write operation */
 OPENBL_FLASH_Unlock();

 /* Program double-word by double-word (8 bytes) */
 while ((DataLength >> 3U) > 0U) {
 for (index = 0U; index < FLASH_PROG_STEP_SIZE; index++) {
 buffer[index] = *(Data + index);
 }

 OPENBL_FLASH_Program(Address,
 *((uint64_t *)buffer));

 Address += FLASH_PROG_STEP_SIZE;
 Data += FLASH_PROG_STEP_SIZE;
 DataLength -= FLASH_PROG_STEP_SIZE;
 }

 /* If remaining count, go back to fill the rest with 0xFF */
 if (DataLength > 0U) {
 remaining = FLASH_PROG_STEP_SIZE - DataLength;

 /* Copy the remaining bytes */
 for (index = 0U; index < DataLength; index++) {
 buffer[index] = *(Data + index);
 }

 /* Fill the upper bytes with 0xFF */
 for (index = 0U; index < remaining; index++) {
 buffer[index + DataLength] = 0xFFU;
 }

 /* FLASH word program */
 OPENBL_FLASH_Program(Address,
 (uint64_t)(*((uint64_t *)((uint32_t)buffer))));
 }

 /* Lock the Flash to disable the flash control register access */
 OPENBL_FLASH_Lock();
 }

 is where something i am missing ?

This topic has been closed for replies.

13 replies

ST Technical Moderator
March 28, 2025

Hello @lucasrj 

Is the OPENBL_FLASH_Erase() work fine please?

Could you please share your entire code then we can assist you more effectively. 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
lucasrj
lucasrjAuthor
Associate II
April 4, 2025

hey @Saket_Om 

Sorry for the late reply

I have not tried the OPENBL_FLASH_Erase() as I have not implemented it in the Python script on the host side,

The host device is an NX Jetson using an arm processor, so it can not run the Cube programmer.

Is there an ARM version of Cube Programmer?

Then, I used the debugger. I could see it got the correct data in OPENBL_FLASH_Write() and called OPENBL_FLASH_Program() with the expected data, so I don't see how it should make a difference. Am I missing something?

I have attached my flash interface files

 

ST Technical Moderator
April 7, 2025

Hello @lucasrj 

Is the Write process work fine with STM32Cube programmer please? 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
lucasrj
lucasrjAuthor
Associate II
April 7, 2025

I can write to it using a st-link, I am unable to connected the serial to an x64 computer.

it is connected to an jetson nx running arm

Is where a version of stm32cube programmer working on arm? 

ST Employee
April 10, 2025

Hello @lucasrj 

Could you please share the complete code for your open bootloader project? It would help me gain a comprehensive understanding of the project settings.

Thank you!

Khaled

"Please mark my answer as best by clicking on the “Accept as solution"" button if it fully answered your question. This will help other users find this solution faster.​"
lucasrj
lucasrjAuthor
Associate II
May 13, 2025

Hey, 

Did you have the opportunity to look at my code? and se what is wrong

I have reattached the code

 

 

lucasrj
lucasrjAuthor
Associate II
April 10, 2025

here is the full project