cancel
Showing results for 
Search instead for 
Did you mean: 

Custom external loader "failed to download segment[0]"

N. SANTINI
ST Employee

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:

  1. Forgetting to specify an external loader while the .elf or .hex file points to external memory.
  2. 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.

Comments
sirhc79
Associate

Hi,

 

I have this Failed to download to segment issue. I wrote a custom loader for a stm32h725 with a qspi flash (yes qspi not octo). I have implemented the low level functies for the loader and tested them. They are woking fine. Also the memory mapping function is working. I can read the memory with the stm32programmer fine. I see the same correct data as when I use the memory viewer in the debugger. But for some reason I can only write the first 0x2200 bytes. After that I get the error message. I have tried several flash loader version all without any success. I also tried the lastest stm32programmer (2.14). The erasing part seems to be working fine. Any idea how to solve this issue? 

Kind regards,

Chris

I would suggest instrumenting so you understand the interaction and failure from the loaders perspective.

You can use any resources on your board, so out a workable UART.

What memory device are you using?

sirhc79
Associate

Hi,

Thanks for your reply. I'm using a mx25l512. I managed to get it some sort of working. For some reason sometimes I can program the chip many times after each other without any problem. But there are also moments I have to reconnect or even restart stm32cubeprogrammer many times before I'm able to reprogram the chip again. I'm also not able to flash from the cubeide with the same loader for some reason. Seems like it's not able to erase the flash of the stm32. Don't understand why yet. I will investigate later (and take your suggestion in account) because there are other priorities for this project. If you have any other suggestions please let me know.

Regards Chris

Version history
Last update:
‎2023-10-19 04:56 AM
Updated by:
Contributors