Custom external loader "failed to download segment[0]"
Why do I get an "failed to download Segment[0]" error when using a custom external loader in STM32CubeProgrammer version 2.7.0 or higher?
The trace shown below is only an example, some values may vary, but the main point is that an error is raised (and programming is interrupted) when programming an address in internal FLASH (0x080xxxxx):
(...) Memory Programming ... Opening and parsing file: ST-Link_GDB_server_a53460.srec File : ST-Link_GDB_server_a53460.srec Size : 1566744 Bytes Address : 0x08020000 Erasing memory corresponding to segment 0: Erasing internal memory sectors [16 41] Erasing memory corresponding to segment 1: Erasing internal memory sectors [0 330] Download in Progress: Error: failed to download Segment[0] Error: failed to download the File Debugger connection lost. Shutting down…
This error (from the STM32CubeProgrammer GUI log panel) can occur when:
- Forgetting to specify an external loader while the .elf or .hex file points to external memory.
- Specifying a custom external loader (new one or functional one on an older version of STM32CubeProgrammer.)
Since version 2.7.0, the use of interrupts is not allowed anymore in the custom external loaders.
The most common reason is that when using the ST HAL API (e.g. HAL_QSPI interface) you may transparently use the HAL_Delay() function which needs the Systick interrupt in its default implementation.
The solution is to overwrite the default HAL Tick implementation by adding the following code in the main.c file of the external loader project:
KeepInCompilation HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
return HAL_OK;
}
uint32_t HAL_GetTick(void)
{
return 1;
}
or (if you need HAL_Delay()):
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
return HAL_OK;
}
uint32_t HAL_GetTick(void)
{
return 1;
}
/** @defgroup STM32H7B3I_Discovery_OSPI_Private_Functions Private Functions
* @{
*/
void HAL_Delay(uint32_t Delay)
{
int i=0;
for (i=0; i<0x1000; i++);
}
This should fix your custom external loader for STM32CubeProgrammer versions higher than 2.7.0.
