# STM32 Bootloader demo on 32L496GDISCOVERY
This example utilizes the 32L496GDISCOVERY discovery board from ST [[1]](#references), equipped with a STM32L496AG microcontroller [[2]](#references). This project demonstrates how to perform in-application-programming of a firmware located on external SD card with FAT32 file system.
Please refer to the main [README](../../README.md) for the complete description about the functionality and operation of the bootloader library.
## Table of Contents
- [Description](#description)
- [Operation](#operation)
- [Compile & Build](#compile--build)
- [References](#references)
## Description
This demo is implemented on the 32L496GDISCOVERY board from ST, equipped with a STM32L496AG microcontroller [[2]](#references). Please refer to the user manual of the discovery board for the complete schematics and board description [[3]](#references).
The microSD card is connected to the MCU over SDIO interface. The implementation uses the official HAL library of ST [[4]](#references) and can be compiled with IAR EWARM or GNU Arm Embedded Toolchain (ARM GCC). Programming and debugging are performed over the on-board ST-LINK debugger.
There are two LEDs located on the discovery board, which are easily accessible, and they don't require the use of the IO expander. These LEDs are used for signaling various status to the user and they are labeled as LD2 and LD3.
The middle (center) button of the blue joystick located on the discovery board is used for user interaction. This center button is simply referred as "button" in the following sections.
The UART2 interface of the MCU is connected directly to the ST-LINK on the board. When the ST-LINK is connected to the computer via USB, an additional virtual com port (USB VCP) is initialized by the debugger. The assigned COM port number can be checked in the Device Manager on Windows. This example project uses this virtual com port to output various debug messages. This functionality can be disabled or enabled with the `USE_VCP` definition located in the `main.h` file.
A compiled application image named `app-demo.bin` is provided with this example project. This file can be copied directly to the SD card of the discovery board and it can be used for testing the functionality of the bootloader. This small application does nothing but flashes the LD2 and LD3 LEDs repeatedly, in the following pattern:
1. LD2 blinks twice
2. LD3 blinks twice
3. LD2 and LD3 LEDs blink twice, simultaneously
The microcontroller flash is organized as follows: by default, the first 32kBytes (16 pages) of the flash is reserved for the bootloader and the rest of the flash is the application space.
![Flash organization](../../docs/img/flash-organization.png)
*Figure 2: Flash organization*
## Operation
After power-up, the bootloader starts. The bootloader checks for user-interaction:
- If the button is not pressed, then the bootloader tries to launch the application: First it checks the application space. If there is a firmware located in the application space, the bootloader calculates the checksum over the application space and compares with the application checksum (if the checksum feature is enabled). Finally, the bootloader prepares for the jump by resetting the peripherals, disabling the SysTick, setting the vector table and stack pointer, then the bootloader performs a jump to the application.
- If the button is pressed and released within 4 seconds: LD2 is blinking during this interval and the bootloader tries to update the application firmware by performing the following sequence:
1. Checks for write protection. If the application space is write-protected, then both LD2 and L3 LEDs are blinking for five seconds. If the button is pressed within this interval, the bootloader disables the write protection by re-programming the flash option bytes and performs a system reset (required after flash option bytes programming). Please note that after disabling the write protection, the user must invoke the application update procedure again by pressing the button in order to continue the firmware update.
2. Initializes SD card, looks for application binary and opens the file.
3. Checks the file size whether it fits the application space in the microcontroller flash.
4. Initializes microcontroller flash.
5. Erases the application space. During erase, the LD3 LED is on. If the user presses the button and keeps it pressed until the end of the flash erase procedure, the bootloader then interrupts the firmware update and does not perform flash programming after the erase operation. This feature is useful if the user only wants to erase the application space.
6. Performs flash programming. During flashing the LD2 LED is blinking.
7. Verifies flash programming by re-opening the firmware file located on the SD card and comparing the content of the file with the flash content.
8. Enables write protection of application space if this feature is enabled in the configuration.
9. After successful in-application-programming, the bootloader launches the application.
- If the button is pressed for more than 4 seconds: LD3 is blinking during this interval and the bootloader launches ST's built-in bootloader located in the internal boot ROM (system memory) of the chip. For more information, please refer to [[5]](#references). With this method, the bootloader can be updated or even a full chip re-programming can be performed easily, for instance by connecting the hardware to the computer via USB and using DFU mode [[6, 7]](#references).
- If the button is kept pressed for more than 9 seconds: the LEDs are switched off and the bootloader tries to launch the application located in the flash. This scenario is fully equivalent to the case when the user does not press the button after power-up (see above).
![Bootloader sequence](../../docs/img/bootloader-sequence.png)
*Figure 3: Bootloader sequence*
## Compile & Build
The project can be built out-of-the-box with either IAR EWARM or GNU Arm Embedded Toolchain. The `EWARM` subfolder contains the required files to compile and build the demo with the IAR EWARM toolchain.
The `GCC` subfolder contains the compiler-specific files, a `Makefile` and a `SConscript` file to easily compile and build the project with the GNU Arm Embedded Toolchain.
### IAR EWARM
1. Open the `Project.eww` workspace file with IAR.
2. Build the project and download to the target.
Note: The IAR EWARM project is already configured with the required parameters and options in order to compile and build the demo with a single click.
### GNU Arm Embedded Toolchain
Prerequisites:
- GNU Arm Embedded Toolchain, recommended version: 8-2019-q3-update
- At least one of the followings:
- GNU Make (for Windows, see: [Make for Windows](http://gnuwin32.sourceforge.net/packages/make.htm))
- Python with pip
#### Build with Make
Steps to compile and build with GNU Make:
1. If the GNU Arm Embedded Toolchain has not been added to PATH: Edit the `CUSTOMPATH` variable in the `Makefile` so that it points to the `bin` folder of the installed GNU Arm Embedded Toolchain.
2. Open up your favorite terminal and navigate to the `GCC` subfolder where the makefile is located.
3. Type `make` and hit enter.
4. The `build` subfolder should contain the binary, ELF and HEX output files, named `stm32-bootloader.bin`, `stm32-bootloader.elf` and `stm32-bootloader.hex` respectively.
#### Build with SCons
This project currently supports two build configurations: debug (default) and release. Follow these steps to compile and build the project with SCons. Please note that the recommended usage is within a virtualenv.
1. Install the requirements: `pip install -r requirements.txt`
2. If the `bin` folder of the GNU Arm Embedded Toolchain does not exist in the PATH, it can be specified in the `SConstruct` file.
3. To build the project with the default debug configuration, execute: `scons -j8`
4. To build a
没有合适的资源?快使用搜索试试~ 我知道了~
stm32-bootloader源代码-stm32-在线升级.7z
共163个文件
h:69个
c:49个
py:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 11 下载量 143 浏览量
2021-07-20
16:55:33
上传
评论 1
收藏 885KB 7Z 举报
温馨提示
stm32-bootloader源代码-stm32-在线升级.7z
资源推荐
资源详情
资源评论
收起资源包目录
stm32-bootloader源代码-stm32-在线升级.7z (163个子文件)
app-demo.bin 6KB
cc936.c 697KB
cc949.c 546KB
cc950.c 434KB
cc932.c 240KB
ff.c 201KB
stm32l4xx_hal_rcc_ex.c 119KB
stm32l4xx_hal_uart.c 112KB
stm32l4xx_hal_sd.c 108KB
stm32l4xx_hal_rcc.c 62KB
stm32l4xx_ll_sdmmc.c 52KB
stm32l4xx_hal_flash_ex.c 48KB
stm32l4xx_hal_can.c 46KB
stm32l4xx_hal_pwr_ex.c 44KB
stm32l4xx_hal_dma.c 37KB
stm32l4xx_hal_uart_ex.c 29KB
stm32l4xx_hal_flash.c 28KB
stm32l4xx_hal_pwr.c 26KB
ccsbcs.c 24KB
stm32l4xx_hal.c 22KB
stm32l4xx_hal_cortex.c 22KB
stm32l4xx_hal_gpio.c 20KB
stm32l4xx_hal_sd_ex.c 18KB
main.c 18KB
stm32l4xx_hal_crc.c 18KB
main.c 15KB
main.c 15KB
bootloader.c 14KB
bsp_driver_sd.c 14KB
bsp_driver_sd.c 14KB
bsp_driver_sd.c 14KB
system_stm32l4xx.c 13KB
system_stm32l4xx.c 13KB
system_stm32l4xx.c 13KB
stm32l4xx_hal_dma_ex.c 11KB
stm32l4xx_hal_flash_ramfunc.c 9KB
sd_diskio.c 9KB
sd_diskio.c 9KB
sd_diskio.c 9KB
stm32l4xx_hal_crc_ex.c 9KB
diskio.c 6KB
syscall.c 6KB
ff_gen_drv.c 6KB
stm32l4xx_it.c 3KB
stm32l4xx_it.c 3KB
stm32l4xx_it.c 3KB
fatfs.c 1KB
fatfs.c 1KB
fatfs.c 1KB
unicode.c 386B
.clang-format 4KB
Doxyfile 110KB
.editorconfig 241B
stm32-bootloader.ewd 51KB
stm32-bootloader.ewd 51KB
stm32-bootloader.ewd 51KB
stm32-bootloader.ewp 42KB
stm32-bootloader.ewp 42KB
stm32-bootloader.ewp 42KB
stm32-bootloader.ewt 90KB
stm32-bootloader.ewt 90KB
stm32-bootloader.ewt 90KB
Project.eww 153B
Project.eww 153B
Project.eww 153B
.gitignore 132B
stm32l496xx.h 1.45MB
stm32l476xx.h 1.35MB
arm_math.h 239KB
stm32l4xx_hal_rcc.h 211KB
stm32_hal_legacy.h 179KB
stm32l4xx_hal_rcc_ex.h 137KB
core_cm7.h 134KB
core_cm4.h 110KB
core_cm3.h 100KB
core_sc300.h 99KB
stm32l4xx_hal_uart.h 81KB
stm32l4xx_ll_sdmmc.h 71KB
cmsis_armcc_V6.h 52KB
stm32l4xx_hal_flash.h 52KB
core_sc000.h 43KB
core_cm0plus.h 43KB
stm32l4xx_hal_sd.h 42KB
stm32l4xx_hal_uart_ex.h 40KB
cmsis_gcc.h 39KB
stm32l4xx_hal_gpio_ex.h 37KB
core_cm0.h 36KB
stm32l4xx_hal_pwr_ex.h 35KB
stm32l4xx_hal_dma.h 35KB
stm32l4xx_hal_can_legacy.h 35KB
stm32l4xx_hal.h 32KB
cmsis_armcc.h 23KB
stm32l4xx_hal_cortex.h 18KB
stm32l4xx_hal_pwr.h 16KB
stm32l4xx_hal_crc.h 16KB
stm32l4xx_hal_dma_ex.h 15KB
stm32l4xx_hal_gpio.h 14KB
ff.h 13KB
stm32l4xx_hal_conf.h 12KB
stm32l4xx_hal_conf.h 12KB
共 163 条
- 1
- 2
应用市场
- 粉丝: 456
- 资源: 3808
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页