Memory programming and erasing ¶
This panel is dedicated to flash memory programming and erasing operations.
Note
STM32CubeProgrammer is able to write on aligned memory regions. Flash memory imposes a data alignment described in the STM32 reference manual. As an example, for STM32U5 devices, the reference manual indicates that this MCU supports “137 bits wide data read and write (128 effective bits plus 9 ECC bits)”, which means that data must be aligned on 16 bytes.
Internal flash memory programming ¶
All modifications involving the PC register need a board reset.
Memory programming ¶
To program a memory, go through the following steps:
- Click on the browse button and select the file to program. The supported
formats are binary files (.bin), ELF files (.elf, .axf, .out), Intel hex files (.hex), and Motorola S-record files (.Srec).
In case of a binary file, the address must be set.
Select the programming options:
Verify after programming: read back the programmed memory and compare it byte per byte with the file.
Skip flash memory erase before programming: if checked, the memory is not erased before programming. This option must be checked only when you are sure that the target memory is already erased.
Run after programming: start the application just after programming.
Click on the “Start programming” button.
The progress bar at the bottom of the window shows the progress of the operation.
The user can also edit the memory through the displayed memory grid in the Memory & File Editing tab by double-clicking on the ASCII field or on one of the memory grid cells. The value is padded if necessary.
Memory erasing ¶
Once connected to a target, the memory sectors are displayed in the right-hand panel, showing the start address and the size of each sector. To erase one or more sectors (flash or EEPROM), select them in the first column, and then click on the “Erase selected sectors” button.
Select “Full Flash memory checksum” to enable checksum calculation at the end of the download file.
The “Full chip erase” button erases the whole memory.
External flash memory programming ¶
To program an external memory connected to the microcontroller via any of the available interfaces, such as SPI, FMC, FSMC, QSPI, or OCTOSPI, you need an external loader.
STM32CubeProgrammer is delivered with external loaders for several STM32
evaluation and discovery boards. Refer to the
bin/ExternalLoader
directory.
If you need to create a new external loader, see the next section for more
details.
To program an external memory, select one or more external loaders from the “ExternalLoader” panel. They are then used by the tool to read, program, or erase external memories. Once selected, the external loader or loaders are used for any memory operation in their memory range.
The “External flash erasing” tab on the right of the “Erasing and Programming” panel displays the memory sectors for each selected loader, and enables sector or full-chip erase.
Developing customized loaders for external memory ¶
Based on the examples available on ST GitHub at https://github.com/STMicroelectronics/stm32-external-loader, users can develop their custom loaders for a given external memory.
The programming mechanism is the same one used by the STM32 ST-LINK Utility tool. Any flash loader developed for use with the ST-LINK Utility is compatible with STM32CubeProgrammer and can be used without any modification.
To create a new external memory loader, follow the steps below:
- Update the device information in the
StorageInfostructure in the Dev_Inf.cfile with the correct information concerning the external memory.
- Update the device information in the
Rewrite the corresponding function code in the
Loader_Src.cfile.Change the output file name.
Note
Some functions are mandatory and cannot be omitted. Refer to the function
descriptions in
Loader_Src.c. Linker or scatter files must not be
modified.
After building the external loader project, an ELF file is generated. The
extension of this file depends on the toolchain used:
.axf
for Keil,
.out
for EWARM, and
.elf
for TrueSTUDIO or any GCC-based toolchain.
The extension of the ELF file must be changed to
.stldr, and the file must
be copied under the
bin/ExternalLoader
directory.
Loader_Src.c file ¶
The development of an external loader for a memory, based on a specific IP, requires the following functions:
InitDefines the GPIO pins used to connect the external memory to the device, and initializes the clocks of the used IPs. Returns 1 if successful, and 0 if it fails.
WritePrograms a buffer defined by an address in the RAM range. Returns 1 if successful, and 0 if it fails.
SectorEraseErases the specified memory sectors. Returns 1 if successful, and 0 if it fails.
StartAddressequals the address of the first sector to be erased, andEndAddressequals the address of the last sector to be erased.
Note
SectorErase
is not used in the case of an external SRAM loader.
The functions mentioned above must be defined in an external loader. They are used by the tool to erase and program the external memory. For instance, if the user clicks on the program button from the external loader menu, the tool performs the following actions:
Calls
Initto initialize the interface, such as QSPI, and the flash memory.Calls
SectorErase()to erase the needed flash memory sectors.Calls
Write()to program the memory.
It is possible to define additional functions:
ReadThe
Readfunction is used to read a specific range of memory, and returns the data in a RAM buffer. It returns 1 if successful, and 0 if it fails.
Note
For Quad-SPI and Octo-SPI memories, the memory-mapped mode can be defined in
the
Init
function. In that case, the
Read
function is useless,
because data can be read directly from the JTAG or SWD interface.
VerifyThe
Verifyfunction is called when selecting the “verify while programming” mode. This function checks whether the programmed memory matches the buffer defined in RAM. It returns auint64value defined as follows:Return value = ((checksum << 32) + AddressFirstError)where
AddressFirstErroris the address of the first mismatch, andchecksumis the checksum value of the programmed buffer.MassEraseThe
MassErasefunction erases the full memory. It returns 1 if successful, and 0 if it fails.A checksum function.
All the functions described above return 1 in case of a successful operation, and 0 in case of a failure.
int Init (void)
int Write (uint32_t Address, uint32_t Size, uint8_t* buffer)
int SectorErase (uint32_t StartAddress, uint32_t EndAddress)
int Read (uint32_t Address, uint32_t Size, uint16_t* buffer)
uint64_t Verify (uint32_t FlashAddr, uint32_t RAMBufferAddr, uint32_t Size)
int MassErase (void)
Dev_Inf.c file ¶
The
StorageInfo
structure defined in this file provides information on the
external memory. An example of the type of information defined by this
structure is given below:
#if defined (__ICCARM__)
__root struct StorageInfo const StorageInfo = {
#else
struct StorageInfo const StorageInfo = {
#endif
"External_Loader_Name", // Device Name + version number
MCU_FLASH, // Device Type
0x08000000, // Device Start Address
0x00100000, // Device Size in Bytes (1 Mbyte / 8 Mbits)
0x00004000, // Programming Page Size 16 Kbytes
0xFF, // Initial Content of Erased Memory
0x00000004, 0x00004000, // Sector Num: 4, Sector Size: 16 Kbytes
0x00000001, 0x00010000, // Sector Num: 1, Sector Size: 64 Kbytes
0x00000007, 0x00020000, // Sector Num: 7, Sector Size: 128 Kbytes
0x00000000, 0x00000000,
};
External memory programming with bootloader interfaces on GUI ¶
This feature is supported by STM32H7Rx/7Sx products. Go through the sequence outlined below to successfully program the external memory using bootloader interfaces:
- Ensure that there is no external loader already selected before connecting
the board through STM32CubeProgrammer.
- Choose the bootloader interface. USB and USART are currently supported, then
connect.
- Select the external loader operating on your board. Only one can be
selected.
The open bootloader is loaded.
Write, read, and erase operations can be performed.
Note
If the board is disconnected, a hardware reset is needed to connect again to the interface. Go through step 1 again to perform another external memory programming via bootloader.