11. Flash Loader
Motivation
In this step, we will discuss how to load data into external flash memory. The compiler compiles the application code and assets in your project into a binary or hex file. Depending on the specifications in the linker script, parts of the binary or hex file will be placed in internal flash, while other parts will be placed in external flash. Typically, assets such as images and fonts are stored in external flash, whereas the application code is stored in internal flash.
During development we need a way to write data to the external flash, but this is not necessary during runtime where we only read from the flash.
Two ways are common for writing data to the external flash:
- Write a flashloader for STM32CubeProgrammer
- Use a proprietary application-based solution
Note
Goal
The goal in this section is to select and develop a mechanism for loading data to the external flash.
Verification
Here are the verification points for this section:
Verification Point | Rationale |
---|---|
Data can be flashed to the external flash | External flash can be used for storage |
Prerequisites
The following are the prerequisites for this step:
- Information about the flash, typically a datasheet
- Information about the connections between the MCU and the external flash
Do
Flash loader for STM32CubeProgrammer
The STM32CubeProgrammer comes with flash loaders for the various STM32 Evaluation kits. The flash loaders are small programs that are loaded to the RAM of the MCU and executed during flashing to facilitate the programming of the flash.
The flash loader consists of two parts:
- Configuration of the GPIOs and flash interface that are required to communicate with the flash
- The flashing algorithm that knows the sequence of commands required to write in the flash
These parts can often be based on an existing flash loader. If you can find a flash loader for the same flash that you are using, take that as starting point and modify the GPIO part. If you design your hardware by copying the flash circuit from an evaluation kit, then you can use the flash loader for that kit directly.
The flash loader projects provided with STM32CubeProgrammer are found in the installation folder, which is C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\ExternalLoader if it is installed in the default directory.
Flash loader projects can also be found here on github: STM32 External Flashloaders on GitHub.
Further reading
- Section 3.9 in UM0892: STM32 ST-LINK utility software description.
- STMicroelectronics Community: How to add your SPI flash into the STM32CubeProgrammer's external loader Part 1.
- STMicroelectronics Community: How to add your SPI flash into the STM32CubeProgrammer's external loader Part 2.
Proprietary application-based solution
Another solution is to include flash loading into the application itself. The idea is that you already have the flash configuration inside your application (to be able to load from it), and maybe you know how to write a block to the flash from your previous testing. You then just need a way of transferring the new flash data to your application. One way is through a UART. The application receives the data stream, and writes the data to the flash, block by block.
The flash cannot be in memory mapped mode while this is running, so the application must typically be put in a special mode.
Open source solutions for the transmission of bytes can be found on the Internet. The Y-modem protocol for example provides 16-bit CRC on the data.
Testing
After the data has been written to the flash, test that it can be read correctly. Use the small test programs developed in the previous sections.
It is advised to test the whole flash thoroughly now, to find any bugs early.