Giter VIP home page Giter VIP logo

Comments (3)

KiraVerSace avatar KiraVerSace commented on May 10, 2024

The blow is my program inherit from you.

bool bootLoaderEraseFlash(void)
{
    /**
     * @brief 定义FLASH每个Bank有多少Pages
     *        STM32L4系列分为两个Bank,每个Bank=256Pages 每个Page=2K
     */
    uint32_t numberOfPages = 0;
    uint32_t pageError = 0;

    FLASH_EraseInitTypeDef flashEraseInit;
    HAL_StatusTypeDef halStatus = HAL_OK;

    HAL_FLASH_Unlock();

    numberOfPages = (FLASH_BASE+FLASH_SIZE-USR_APP_ADDRESS)/FLASH_PAGE_SIZE;

    flashEraseInit.Banks     = FLASH_BANK_1;                                        // 擦除指定的Bank 目前用的L476RC只有一个Bank
    flashEraseInit.NbPages   = numberOfPages % (FLASH_BANK_SIZE/FLASH_PAGE_SIZE);   // 一次性擦除多少页
    flashEraseInit.Page      = (USR_APP_ADDRESS-FLASH_BASE)/FLASH_PAGE_SIZE;        // 擦除的起始页,从除去BootLoader开始
    flashEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;                               // 擦除类型是“Page Erase”

    halStatus = HAL_FLASHEx_Erase(&flashEraseInit, &pageError);

    if ((halStatus == HAL_OK)&&(numberOfPages > (FLASH_BANK_SIZE/FLASH_PAGE_SIZE)))
    {
        flashEraseInit.Banks     = FLASH_BANK_2;
        flashEraseInit.NbPages   = numberOfPages - flashEraseInit.NbPages;          // 减去上次擦除的Pages得到剩余的
        flashEraseInit.Page      = USR_APP_ADDRESS-(FLASH_BASE+FLASH_BANK_SIZE)/FLASH_PAGE_SIZE;
        flashEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;

        halStatus = HAL_FLASHEx_Erase(&flashEraseInit, &pageError);
    }
    HAL_FLASH_Lock();

    if (halStatus == HAL_OK)
    {
        logInfo("IAP Erase the flash [0x%08X-0x%08X] successfully!", USR_APP_ADDRESS, FLASH_SIZE);

        return true;
    }
    else
    {
        logError("IAP Erase the flash error!");
        return false;
    }
}

from stm32-bootloader.

akospasztor avatar akospasztor commented on May 10, 2024

Hi @KiraVerSace,

If you use a microcontroller that has flash banks with number of pages other than 256, then you need to change that in the bootloader.h file.

I quickly double-checked the datasheet of the STM32L476RC, and it states explicitly that each flash bank contains 256 pages. See page 19:

STM32L476xx devices feature up to 1 Mbyte of embedded Flash memory available for storing programs and data. The Flash memory is divided into two banks allowing read-while-write operations. [...] Each bank contains 256 pages of 2 Kbyte.

However, digging into the reference manual of the microcontroller, this does not seem to be true for devices with 512KB flash size... See page 98 here: https://www.st.com/resource/en/reference_manual/rm0351-stm32l47xxx-stm32l48xxx-stm32l49xxx-and-stm32l4axxx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

So yes, you need to adapt the configuration in the bootloader.h so that the FLASH_PAGE_NBPERBANK reflects the correct number of flash pages in a bank.

from stm32-bootloader.

stale avatar stale commented on May 10, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from stm32-bootloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.