Code Monkey home page Code Monkey logo

akospasztor / stm32-bootloader Goto Github PK

View Code? Open in Web Editor NEW
850.0 68.0 295.0 2.54 MB

Customizable Bootloader for STM32 microcontrollers. This example demonstrates how to perform in-application-programming of a firmware located on an external SD card with FAT32 file system.

Home Page: https://akospasztor.github.io/stm32-bootloader

License: Other

C 98.80% Assembly 0.99% Python 0.17% Makefile 0.04%
bootloader in-app-programming iap flasher stm32 stm32l4 stm32l476 mcu microcontroller flash

stm32-bootloader's Introduction

STM32 Bootloader

Customizable Bootloader for STM32 microcontrollers. This project includes demonstrations for various hardware how to perform in-application-programming of a firmware located on external SD card with FAT32 file system.

Each example uses the same bootloader library located in the lib/stm32-bootloader folder. The examples are located in the projects folder and they come with a separate, dedicated README file with description related to that specific implementation.

Update: the STM32L496-Discovery example supports compiling and building the project with the GNU Arm Embedded Toolchain (ARM GCC) out-of-the-box, in addition to IAR EWARM. Check out the project README for further information.

Please refer to https://akospasztor.github.io/stm32-bootloader for complete documentation of the bootloader library source code.

Build Status

Table of Contents

Bootloader features

  • Configurable application space
  • Flash erase
  • Flash programming
  • Flash verification after programming
  • Checksum verification
  • Flash protection check, write protection enable/disable
  • Extended error handling, fail-safe design
  • Bootloader firmware update and the ability to perform full chip re-programming: enter ST's built-in bootloader from software (without triggering the BOOT pin)
  • Serial tracing over SWO for easier debugging and development
  • Easy to customize and port to other microcontrollers

Source code organization

Repository
├── docs
├── drivers
│   ├── CMSIS
│   └── STM32L4xx_HAL_Driver
├── lib
│   ├── fatfs
│   └── stm32-bootloader
└── projects
    ├── STM32L476-CustomHw
    ├── STM32L496-CustomHw
    └── STM32L496-Discovery

The docs folder contains the generated documentation of the bootloader source code and other documentation-related static files.

The drivers folder contains the CMSIS (Cortex Microcontroller Software Interface Standard) as well as the HAL (Hardware Abstraction Layer) drivers from ST.

The bootloader source code and corresponding header file can be found in lib/stm32-bootloader folder. Additionally, the lib folder contains the FatFs library as well.

The various demonstrations reside in the projects folder. Each example project contains an include and source folder where the header and source files are located respectively. The compiler and SDK-specific files are located in their respective subfolders. Furthermore, every example project has a dedicated README file explaining its functionality in detail.

Examples

This repository contains the following examples.

Microcontroller Hardware Project path
STM32L476VG Custom projects/STM32L476-CustomHw
STM32L496VG Custom projects/STM32L496-CustomHw
STM32L496AG 32L496GDISCOVERY projects/STM32L496-Discovery

How to use

The bootloader can be easily customized and tailored to the required hardware and environment, i.e. to perform firmware updates over various interfaces or even to implement over-the-air (OTA) updates if the hardware incorporates wireless communication modules. In order to perform successful in-application-programming, the following sequence has to be kept:

  1. Check for flash write protection and disable it if necessary.
  2. Initialize flash with Bootloader_Init().
  3. Erase application space with Bootloader_Erase().
  4. Prepare for programming by calling Bootloader_FlashBegin().
  5. Perform programming by repeatedly calling the Bootloader_FlashNext() function. The programming procedure requires 8 bytes of data (double word) to be programmed at once into the flash. This function automatically increases the address where the data is being written.
  6. Finalize programming by calling Bootloader_FlashEnd().

The application image has to be in binary format. If the checksum verification is enabled, the binary must include the checksum value at the end of the image. When creating the application image, the checksum has to be calculated over the entire image (except the checksum area) with the following parameters:

  • Algorithm: CRC32
  • Size: 4 bytes
  • Initial value: 0xFFFFFFFF
  • Bit order: MSB first

Important notes:

  • In order to perform a successful application jump from the bootloader, the vector table of the application firmware should be relocated. On system reset, the vector table is fixed at address 0x00000000. When creating an application, the microcontroller startup code sets the vector table offset to 0x0000 in the system_stm32xxxx.c file by default. This has to be either disabled (the bootloader can be configured to perform the vector table relocation before the jump) or manually set the vector table offset register (VTOR) to the appropriate offset value which is the start address of the application space. For more information, please refer to [1].
  • The linker settings of the application firmware need to be adjusted from their default settings so that the start address of flash reflects the actual start address of the application space.

Configuration

The bootloader can be widely configured in the bootloader.h file. The file includes detailed comments and descriptions related to the configurable parameters and definitions.

References

[1] PM0214, "STM32F3 Series, STM32F4 Series, STM32L4 Series and STM32L4+ Series Cortex®-M4 Programming Manual", http://www.st.com/resource/en/programming_manual/dm00046982.pdf

stm32-bootloader's People

Contributors

akospasztor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stm32-bootloader's Issues

Error : Jump to application

Hi,
I am trying this example for my STM32F429zi, and I can erase and flash the memory but I am not able to run the application code, please can you suggest to me where I am doing wrong.

I am attaching my code. if any other details are required please let me know.

Thanks
BootTest.zip

stm32-bootloader for stm32f103xxxx series

hello akospasztor,

first of all thanks for ur driver.
my application is also same as ur application but for stm32f103xxxx series.

can u suggest in writing this driver for my controlller....please...

thanks,
sankar

How to compute the RAM_SIZE?

Hi,

In your bootloader.h file you have the following:

/* MCU RAM size, used for checking accurately whether flash contains valid application */
#if (STM32L496xx)
#define RAM_SIZE        (uint32_t)0x00040000
#elif (STM32L476xx)
#define RAM_SIZE        (uint32_t)0x00018000
#endif

My question is - how did you compute these values? I'm not sure how to get from 320K / 128K of RAM to those numbers.

Cheers

Shared SDIO+fatfs

The Bootloader currently uses SDIO+fatfs to read the new firmware file. If I want to use SDIO+fatfs in application code, it will be in memory twice. Is there a way to share the SDIO+fatfs through some clever linking?

HardFault at f_read

I tried with different clock settings (divider) for SD IO, but no success as I always get HardFault error at:
fr = f_read(&SDFile, &data, 8, &num);
(@ reading from file)

Do I need to check some other configuration as-well?

ad.1:
for the test, I put the f_read at different places, and I found that if it is called before Bootloader_Erase() it is ok, but if it was placed right after it, the Hard_Fault appears

STM32 Flash HAL: HAL_FLASH_Program

Hi Akos,

When I use the below function, it writes the bytes in the correct flash address, however bytes are written in LSB

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_ptr, data)

e.g.:
when I write 0x0A1B2C3D4C5E6F55
I see that this 0x556F5E4C3D2C1B0A was written on the memory

Could you please confirm if that is also the case with you?
If that is the case, is there a way to write the data in MSB without updating the flash HAL?

Many thanks in advance

Port on STM32F4 Error!

I have an STM32F4 Discovery board. And i am porting this code to my board. But it have so much error, mainly related to HAL FLASH. I am using HAL library. Anyone have bootloader for STM32F4?

Check for app bug?

return ( ((*(__IO uint32_t*)APP_ADDRESS) & ~(RAM_SIZE-1)) == 0x20000000 ) ? BL_OK : BL_NO_APP;

Perhaps I'm missing something but if RAM_SIZE = 0x00040000 then (RAM_SIZE - 1) = 0x0003ffff, then inverted it equals 0xfffc0000. Then 0x20040000 & 0xfffc0000 = 0x20040000 which is not equal to 0x20000000. This assumes that the first 4 bytes of the app point to the end of the RAM space which I think it does at least on my app.

How to build the firmware image

Hello,
thank you for your great bootloader code, which is the first code, which runs perfectly for me and is easy understandable.
But I have one question. How can I build the firmware update image?
CubeIDE gives me an Intel hex file, .ELF file or an binary file which is almost 140MB large.
Thank you for your help

Is it a bug in function Bootloader_Erase()?

OK, here is your soure code, if I use a MCU only 256KB like stm32l476rc, all the flash in the bank1 from page1 to 128(every page is 2K), so the NbrOfPages must be < FLASH_PAGE_NBPERBANK(256), but in your program, it will skip the if(NbrOfPages > FLASH_PAGE_NBPERBANK).

And the program will not erase the BANK1, cause it will be skipped.

Maybe i was wrong, Thank you for your contribution, it help me a lot, it write very clearly and easy reading.

uint8_t Bootloader_Erase(void)
{
    uint32_t NbrOfPages = 0;
    uint32_t PageError  = 0;
    FLASH_EraseInitTypeDef pEraseInit;
    HAL_StatusTypeDef status = HAL_OK;

    HAL_FLASH_Unlock();

    /* Get the number of pages to erase */
    NbrOfPages = (FLASH_BASE + FLASH_SIZE - APP_ADDRESS) / FLASH_PAGE_SIZE;

    if(NbrOfPages > FLASH_PAGE_NBPERBANK)
    {
        pEraseInit.Banks     = FLASH_BANK_1;
        pEraseInit.NbPages   = NbrOfPages % FLASH_PAGE_NBPERBANK;
        pEraseInit.Page      = FLASH_PAGE_NBPERBANK - pEraseInit.NbPages;
        pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
        status               = HAL_FLASHEx_Erase(&pEraseInit, &PageError);

        NbrOfPages = FLASH_PAGE_NBPERBANK;
    }

    if(status == HAL_OK)
    {
        pEraseInit.Banks     = FLASH_BANK_2;
        pEraseInit.NbPages   = NbrOfPages;
        pEraseInit.Page      = FLASH_PAGE_NBPERBANK - pEraseInit.NbPages;
        pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
        status               = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
    }

    HAL_FLASH_Lock();

    return (status == HAL_OK) ? BL_OK : BL_ERASE_ERROR;
}

Not an issue - a question - SCB->VTOR issue with F103?

Hi,

This is a shot in the dark and a cry for help.
I am writing a simple bootloader for STM32 F103 series using Arduino IDE.
I figured all the settings and am pretty sure I have set the linker loader script and VTOR table right.
I am confident of that because when I load the target firmware to 0x80080000 address with the cube programmer it works fine and VTOR is set correctly.

However, when I jump to the firmware from the bootloader, the VTOR is not set to 0x8008000! It remains 0x8000000. No idea why!
It is such a simple code and yet, something does not work.
Have you encountered any issues with F103 line?

Thank you!

void boot_jump2Address(const uint32_t address)
{
  uint32_t appStack;
  pFunction appEntry;
 
//  Serial.print("boot_jump2Address=0x"); Serial.println((uint32_t)address, HEX);

  // get the application stack pointer (1st entry in the app vector table)
  appStack = (uint32_t)*((__IO uint32_t*)address);
//  Serial.print("appStack=0x"); Serial.println((uint32_t)appStack, HEX);
 
  // Get the app entry point (2nd entry in the app vector table
  appEntry = (pFunction)*(__IO uint32_t*)(address + 4);
//  Serial.print("appEntry=0x"); Serial.println((uint32_t)appEntry, HEX);

  delay(1000);



  
  HAL_RCC_DeInit();
  HAL_DeInit();
 
  SysTick->CTRL = 0;
  SysTick->LOAD = 0;
  SysTick->VAL  = 0;
 
  // Reconfigure vector table offset to match the app location
  delay(100);
  
  __disable_irq();
  SCB->VTOR = 0x8008000;
  __DSB();
  __enable_irq();


  __set_MSP(appStack); // Set app stack pointer

  appEntry(); // Start the app
 
  while (1); // never reached
}

Support for STM32F4 series

Hi, I am trying to adapt this library for STM32F401 MCU and it seems like there are a ton of configurations that are different. Could you suggest what's the best way to port this implementation to F4?

Linker Script Question

Hello, I am trying to port this to work on a STM32F107 based custom board and was hoping for some clarification on how to setup the .ld file. From your example I see that the start address is mapped to 8000000 with a length of 8000.

Do you also have to setup a flash partition for the application memory? It seems like the code is able to jump to the main application at the 80008000 address but that memory region is not defined in the .ld file.

My end goal is to have it set up with the Bootloader, app memory, an update cache (for storing downloaded bin files) and a small region at the end for storing some user data.

Could you please clarify if the .ld file needs this information or not?

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.