2025-12-02 6:44 AM
Hello ST Community,
I’m developing a custom external loader for STM32CubeProgrammer where the STM32 acts as a bridge to program an external MCU (not external flash). The STM32 uses UART5 as a debug port and also as the communication interface to the external MCU.
In testing, I see CubeProgrammer calling Write() repeatedly with non‑sector‑aligned sizes and addresses that don’t start at sector boundaries. Sample output from my loader:
Starting... Address: 0x90000000 Size: 0x0003C700 Size is not sector aligned:0x0003C700 Address: 0x9003C700 Size: 0x0003C700 Size is not sector aligned:0x0003C700 Address is not start of sector:0x9003C700 ... Address: 0x901E3800 Size: 0x00018E00 Size is not sector aligned:0x00018E00 Address is not start of sector:0x901E3800 Address: 0x901FC600 Size: 0x00018E60 Size is not sector aligned:0x00018E60 Address is not start of sector:0x901FC600
This looks like CubeProgrammer is splitting the image into chunks (e.g., 0x3C700, then smaller fragments near the end) and incrementing the Address, but not guaranteeing sector alignment. I understand this is expected for external flash loaders (since the loader handles alignment/erase internally), but in my case the target is an external MCU.
The external MCU requires sector-aligned writes because:
How can I enforce sector alignment in CubeProgrammer?
Is there a way to configure CubeProgrammer so that all Write() calls are sector-aligned (both Address and Size)?
int Init(void)
{
*(uint32_t *)0xE000EDF0 = 0xA05F0000; // enable interrupts in debug
SystemInit();
SCB->VTOR = 0x20000000 | 0x200;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
// For debug output
__HAL_RCC_UART5_FORCE_RESET();
__HAL_RCC_UART5_RELEASE_RESET();
MX_UART5_Init();
printf("Starting...\n");
/*
* Initialization function
*
*/
return LOADER_OK;
}
int Write(uint32_t Address, uint32_t Size, uint8_t *buffer)
{
printf("Address: 0x%08lX\n", Address);
printf("Size: 0x%08lX\n", Size);
// For external MCU programming, sector alignment required.
// Check that the size is sector aligned
if (Size % MEMORY_SECTOR_SIZE)
{
printf("Size is not sector aligned:0x%08lX\n", Size);
// return LOADER_FAIL;
}
// Check that the address is the start of a sector
if (Address % MEMORY_SECTOR_SIZE)
{
printf("Address is not start of sector:0x%08lX\n", Address);
// return LOADER_FAIL;
}
// Forward 'buffer' as-is to external MCU.
/*
* writing code
*/
return LOADER_OK;
}
Solved! Go to Solution.
2025-12-05 10:27 AM
You get to deblock / decompose the Write() into a form suitable for your memory
What does your StorageInfo structure look like?
The Buffer size tends to depend on the SRAM being used for the External Loader itself, and the remaining being split into Two ping/pong buffers which the JTAG/SWD can fill concurrently with your code executing.
2025-12-05 7:17 AM
Hello Macar,
STM32CubeProgrammer only calls functions (e.g. Init(), SectorErase(), Write()) and they must be defined in the external loader.
Currently, there is no setting that controls the alignement of Write() calls, the loader should handle any required sector logic internally.
Kouthair.
2025-12-05 10:27 AM
You get to deblock / decompose the Write() into a form suitable for your memory
What does your StorageInfo structure look like?
The Buffer size tends to depend on the SRAM being used for the External Loader itself, and the remaining being split into Two ping/pong buffers which the JTAG/SWD can fill concurrently with your code executing.