Skip to main content
ASeib.2
Associate
January 8, 2023
Solved

Wrong stack pointer for STM32F103C8

  • January 8, 2023
  • 2 replies
  • 2123 views

When creating a new project in the STM32CubeIDE for a STM32F103C8 (64k flash, 20k ram), the generated linker-script contains:

/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + (LENGTH(RAM) / 2); /* end of "RAM" Ram type memory */
 
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
 
/* Memories definition */
MEMORY
{
 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K
}

This places the stack (_estack) at 0x20005000 = 0x20000000 + 20k. When checking the memory at (_estack - 1) via the debugger, it can't be read. So while startup the STM32 immediately goes into a hard fault.

In fact, the last readable address is 0x200027ff, which suggests, that the address is actually in words rather than bytes : 0x20000000 + 10k = 0x20002800.

Changing the line to

_estack = ORIGIN(RAM) + (LENGTH(RAM) / 2); /* end of "RAM" Ram type memory */

places the stack at 0x20002800 and fixes the problem.

This can be reproduced with a "blue pill".

This topic has been closed for replies.
Best answer by ASeib.2

Please close - this is embarrassing - the board actually contains a STM32F103C6 with only 10k SRAM.

2 replies

ASeib.2
ASeib.2Author
Associate
January 8, 2023

Correction: The original line is of course

_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */

ASeib.2
ASeib.2AuthorBest answer
Associate
January 8, 2023

Please close - this is embarrassing - the board actually contains a STM32F103C6 with only 10k SRAM.