doc/flashloaders.md
The on-chip FLASH of STM32 needs to be written once a byte/half word/word/double word, which would lead to a unbearably long flashing time if the process is solely done by stlink from the host side. Flashloaders are introduced to cooperate with stlink so that the flashing process is divided into two stages. In the first stage, stlink loads flashloaders and flash data to SRAM where busy check is not applied. In the second stage, flashloaders are kick-started, writing data from SRAM to FLASH, where a busy check is applied. Thus the write-check_if_busy cycle of flashing is done solely by STM32 chip, which saves considerable time in communications between stlink and STM32.
As SRAM is usually less in size than FLASH, stlink only flashes one page (may be less if SRAM is insufficient) at a time. The whole flashing process may consist of server launches of flashloaders.
st-flash loads compiled binary of corresponding flashloader to SRAM by calling stlink_flash_loader_init in src/flash_loader.cst-flash erases corresponding flash page by calling stlink_erase_flash_page in common_legacy.c.st-flash calls stlink_flash_loader_run in flash_loader.c. In this function
r0r1r2r15 (pc)r2memcpy with busy check
Thus for developers who want to modify flashloaders, the following constraints should be satisfied.
r2 set to zero when halted.For devices that need to wait until the flash is not busy, check FLASH_SR_BUSY bit. For devices that need to check if there is any errors during flash, check FLASH_SR_(X)ERR where X can be any error state
FLASH_SR related offset and copy unit size may be found in ST official reference manuals and/or some header files in other open source projects. Clean room document provides some of them.
If you find some flashloaders to be broken or you need to write a new flashloader for new devices, the following tricks may help.
WAIT_ROUNDS marco to a bigger value so that you will have time to kill st-flash when it is waiting for a halted core.st-flash and kill it after the flashloader is loaded to SRAMst-util and gdb/lldbThe tricks work because by this means, most work (flash unlock, flash erase, load flashloader to SRAM) would have been done automatically, saving time to construct a debug environment.