AnsweredAssumed Answered

IAP failure on writing in STM32f429

Question asked by harry anders on May 23, 2018

I am writing a bootloader application that resides in sectors_0 (0x08000000-0x08003FFF) and sector_1 (0x08004000-0x08007FFF) of stm32f429. When the bootloader application starts up it erases sector_2(0x08008000-0x0800BFFF) and sector_3(0x0800C000-0x0800FFFF) of the flash memory. Then loads the main code from an SDCARD and writes it to sector_2 and sector_3.

 


I can successfully erase sector_2 and sector_3 but when trying to write to them, I get HAL_ERROR.

 


This is the relevant code:

 

 

  /* Unlock the Flash to enable the flash control register access *************/
     

     volatile uint32_t main_addr = 0x08008000;

     FLASH_If_Init();

 


      FlashProtection = FLASH_If_GetWriteProtectionStatus();

 

      pEraseInit.TypeErase = TYPEERASE_SECTORS;
      pEraseInit.Sector = FLASH_SECTOR_2;
      pEraseInit.NbSectors = 2;
      pEraseInit.VoltageRange = VOLTAGE_RANGE_3;
      if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK)
      {
         /* Error occurred while page erase */
         while (1);
      }

 

      if(FLASH_If_Write(main_addr, (uint32_t *)buffer, (fno.fsize)/4)!= FLASHIF_OK) //writes are 32 bit
        {
         //error    
         while(1);

 

        }

 

 


Functions being used have been defined in the CUBE project section: STM32Cube\Repository\STM32Cube_FW_F4_V1.21.0\Projects\STM32446E_EVAL :

/**
  * @brief  Unlocks Flash for write access
  * @param  None
  * @retval None
  */
void FLASH_If_Init(void)
{
  HAL_FLASH_Unlock();

 

  /* Clear pending flags (if any) */  
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
                         FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
}

 


/**
  * @brief  This function writes a data buffer in flash (data are 32-bit aligned).
  * @note   After writing data buffer, the flash content is checked.
  * @param  FlashAddress: start address for writing data buffer
  * @param  Data: pointer on data buffer
  * @param  DataLength: length of data buffer (unit is 32-bit word)   
  * @retval 0: Data successfully written to Flash memory
  *         1: Error occurred while writing data in Flash memory
  *         2: Written Data in flash memory is different from expected one
  */
uint32_t FLASH_If_Write(uint32_t FlashAddress, uint32_t* Data ,uint32_t DataLength)
{
  uint32_t i = 0;

 

  for (i = 0; (i < DataLength) && (FlashAddress <= (USER_FLASH_END_ADDRESS-4)); i++)
  {
    /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
       be done by word */
    if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK)   //<<<<<<<<<This line returns a HAL_ERROR
    {
     /* Check the written value */
      if (*(uint32_t*)FlashAddress != *(uint32_t*)(Data+i))
      {
        /* Flash content doesn't match SRAM content */
        return(FLASHIF_WRITINGCTRL_ERROR);
      }
      /* Increment FLASH destination address */
      FlashAddress += 4;
    }
    else
    {
      /* Error occurred while writing data in Flash memory */
      return (FLASHIF_WRITING_ERROR);
    }
  }

 

  return (FLASHIF_OK);
}

Outcomes