AnsweredAssumed Answered

STM32F103RF Dual Boot Problem

Question asked by rovasata on Jul 1, 2015
Latest reply on Jul 1, 2015 by Clive One
Hi,
I am using STM32F103RF (XL density) micro and want to use the flash banks for firmware update. 
I took "FLASH\Dual_Boot" project of STM32F10x_StdPeriphLib as an example.
My design is like that: 
If my code is working on bank1, I erase bank2. 
Then I download the new firmware from a gprs module and save the data word by word to bank2.  
Then I program my micro to start from bank2 and reset. 
So new firmware starts running on bank2.
My code is like that:

#define BANK1_START_ADDRESS       (0x08000000)
#define BANK2_START_ADDRESS       (0x08080000)
  
int main(void)
    ...
    #if defined(BOOT_FROM_BANK1)
    /* Set the vector table to the Bank1 start address */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK1_START_ADDRESS);
    #elif defined(BOOT_FROM_BANK2)
    /* Set the vector table to the Bank1 start address */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK2_START_ADDRESS);
    #endif
    ...
}
  
void Update(void)
{
    ...
    #if defined(BOOT_FROM_BANK1)              
    u32FlashDestination = BANK2_START_ADDRESS;
    #elif defined(BOOT_FROM_BANK2)            
    u32FlashDestination = BANK1_START_ADDRESS;
    #endif
    u32NumberOfPage = Calculate_Page_Amounth(0x10000); //64KB space
    if(SUCCESS != Erase_MemoryArea(u32FlashDestination, u32NumberOfPage)){
        FWUpdate_ErrorFunction(ERROR_IN_FLASH_ERASE);
        return;
    }
    ...
    // Here I take the data and save to Bank2 by using FLASH_ProgramWord(...)
    // I didn't put the code here because it is unnecessary.
    ...
    FLASH_Unlock();
    FLASH_EraseOptionBytes();
    #if defined(BOOT_FROM_BANK1)
    if (FLASH_BootConfig(FLASH_BOOT_Bank2) == FLASH_COMPLETE)             
    #elif defined(BOOT_FROM_BANK2)
    if (FLASH_BootConfig(FLASH_BOOT_Bank1) == FLASH_COMPLETE)
    #endif
    
        NVIC_SystemReset();
    }
}
   
ErrorStatus Erase_MemoryArea(u32 u32FlashDestAdr, u32 u32NoOfPage)
{
    u32 u32EraseCounter;   
    for (u32EraseCounter = 0; u32EraseCounter < u32NoOfPage; u32EraseCounter++){
        if(FLASH_COMPLETE != FLASH_ErasePage(u32FlashDestAdr + (u32EraseCounter * PAGE_SIZE)) )  
        {   return ERROR;
        }        
    }
    return SUCCESS;
}

The code works fine if the scenario is as explained in the example.
But when the code is working on bank2 and if I want to update the firmware, say erase bank1 and program it, I see a problem.
Just after erasing page1 of bank1, the micro stops working.
Even if I turn the micro off and turn it on again, just after a couple of micro seconds the micro stops working.
Page1 of bank1 is from the address 0x0800 0000 to 0x0800 07FF
Is it an illegal action to erase that part even if the code is running on bank2?

Outcomes