cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U575 Bank1 Flashing Problem from Bank2

SDembededDev
Associate

I am trying to do OTA programming modifications using the DUAL bank feature of my STM32U575 IC with 1Mbyte of memory.

I have successfully done this when the program starts in BANK 1 and I reflash BANK 2. I change the User Option byte to OB_SWAP_BANK_ENABLE and the board reboots and successfully runs from BANK 2.

Now, on the next firmware update, programming BANK 1 when I am operating from BANK 2 the HAL_FLASH_Program command fails on the first byte.

The erase code is attached and it is very similar to others I have found on GIT for other products. Does the U5 have an inherent defect?

 

any help would be greatly appreciated.

9 REPLIES 9
NFern.1
Associate III

I am facing the same issue. Please share the fix if you have managed to solve this.

Unfortunately I was unable to solve this problem
rulin
Associate II

I am facing the same issue too. Please share the fix if you have managed to solve this.

I could not solve it. My other post on the same issue is unanswered by ST.

can't erase correct Flash Bank when attempting to ... - STMicroelectronics Community

有一个点你是否注意到,就是flash擦除时,bank和page这两个参数,这里的bank是指物理bank,与是否从bank2启动无关。

I google translated @rulin : One point you might have noticed is that during flash erase, the "bank" and "page" parameters refer to the physical bank, regardless of whether the program starts from bank2.

I encountered the same issue and just resolved it. The reason was that when erasing the flash, the specified bank should be the physical bank. For example, if the program starts from physical bank2 and wants to erase physical bank1, the bank in the erase operation should be specified as bank1. I previously thought that when starting from bank2, the physical bank1 should be the logical bank2, so I specified bank2 during the erase operation, which caused the previous issue.

Please share the function / data-structure you use to determine the Active Flash Bank with the currently executing code. Also please share the function call with arguments for the Erase function.

uint8_t isBootFromBank1(void)
{
    FLASH_OBProgramInitTypeDef OBInit;
    HAL_FLASHEx_OBGetConfig(&OBInit);
    return (OBInit.USERConfig & OB_SWAP_BANK_ENABLE) ? 0 : 1;
}
static uint32_t GetBank(uint32_t addr)
{
    if (isBootFromBank1())
    {
        if (addr < (STM32_FLASH_BASE + BOARD_BANK_SIZE))
        {
            return FLASH_BANK_1;
        }
        else
        {
            return FLASH_BANK_2;
        }
    }
    else
    {
        if (addr < (STM32_FLASH_BASE + BOARD_BANK_SIZE))
        {
            return FLASH_BANK_2;
        }
        else
        {
            return FLASH_BANK_1;
        }
    }
}

static uint32_t GetPage(uint32_t addr)
{
    uint32_t page;
    if (addr < (STM32_FLASH_BASE + BOARD_BANK_SIZE))
        page = (addr - STM32_FLASH_BASE) / BOARD_PAGE_SIZE;
    else
        page = (addr - STM32_FLASH_BASE - BOARD_BANK_SIZE) / BOARD_PAGE_SIZE;
    return page;
}

HAL_StatusTypeDef flash_erase_page(uint32_t addr)
{
    FLASH_EraseInitTypeDef FlashEraseInit;
    uint32_t SectorError = 0;
    HAL_StatusTypeDef status;

    FlashEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
    FlashEraseInit.Banks = GetBank(addr);
    FlashEraseInit.Page = GetPage(addr); 
    FlashEraseInit.NbPages = 1;

    status = HAL_FLASHEx_Erase(&FlashEraseInit, &SectorError);

    return status;
}