cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong stack pointer for STM32F103C8

ASeib.2
Associate II

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".

1 ACCEPTED SOLUTION

Accepted Solutions
ASeib.2
Associate II

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

View solution in original post

2 REPLIES 2
ASeib.2
Associate II

Correction: The original line is of course

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

ASeib.2
Associate II

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