cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l451 error when trying to write to the Flash

lucasrj
Associate II

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 ?

12 REPLIES 12

Hey, what was also my understading, can you see any reasion why the write function do not seam to work in the code i send ealier 

Khaled_DHIF
ST Employee

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
Associate II

here is the full project