In this step we will discuss loading data to the external flash. The compiler will compile the text, fonts, and images in your project and produce a binary or hex file with this data. This data is typically put into the external flash. The internal flash is then reserved for code.
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
The goal in this section is to select and develop a mechanism for loading data to the external flash.
Here are the verification points for this section:
|Data can be flashed||External flash can be used for image storage|
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
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 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. This is the recommended way.
The flash loader projects provided with STM32CubeProgrammer are found in the installation folder, typically here: C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\ExternalLoader
A few flash loaders are provided as source code projects for IAR Embedded Workbench, Keil, and TrueStudio.
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.
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.