Hint: STM32 HD stack problem

cgerlach5 wrote on Tuesday, June 16, 2009:

Hi All,

I just searched a while so I share it here before someone else is wasting time…

I am using a STM32 High-Density device with FreeRTOS and the ST FWlib 3.0 and had system crashes.
The supplied startup code in the FWlib sets the stack pointer in the vector table (1st entry) to the fixed address 0x20000400. After initializing the external memory interface (if any) it sets the MSP register to the desired value (_estack defined in the linker script).
Normaly this should be ok but FreeRTOS resets the stack pointer (MSP) to the value stored in the vector table (when staring the first thread). This is ok and saves stack space.

The too ideas are nice but do not play together very well…

My solution was to edit the startup code  (in $(FWLIB)/CMSIS/Core/CM3/startup) to place &_estack into the vector table (I am not using external RAM).


rtel wrote on Tuesday, June 16, 2009:

Hmm, interesting.  All CM3 devices are supposed to have the stack location in the first position within the vector table, FreeRTOS locates the vector table through the system control registers, and resets the stack back to its start position.  Are you saying the STM32 startup code defines a different stack in the linker script to that pointed to by the vector table?  I will need to look at that in more detail - thanks for the heads up.  What is at 0x20000400? 

This must be resurged in the linker script?


cgerlach5 wrote on Tuesday, June 16, 2009:

Hi Richard,

the problem only occurs with the STM32 startup code for the high-density  
devices. Since firmware lib 3.0.0 they are included. I think their intention
for using this scheme was to provide a stack for the startup code (RAM    
starts at 0x20000000), setup the external memory controller and then move 
the stack to the end of the ram (which may be inside the external memory).

I think FreeRTOS behavior is correct. It just collides with the (example)
routines from ST. But if someone (for whatever reason) wants to put the stack
into external RAM FreeRTOS’ way of resetting the stack may be a problem.

From $(fwlib)/Libraries/CMSIS/Core/CM3/startup/gcc/startup_stm32f10x_hd.c:

#define Initial_spTop      0x20000400     <- this is in internal ram (cg)


__attribute__ ((section(".isr_vector")))
void (* const g_pfnVectors[])(void) =
    (void *)Initial_spTop,      /* The initial stack pointer */


void Reset_Handler(void)

  /* restore original stack pointer */
  asm(" LDR r0, =_estack");             <- _estack defined by linker script
  asm(" MSR msp, r0");

  /* Initialize data and bss */

  /* Call the application’s entry point.*/

Maybe FreeRTOS’ stack reset behavior sould be documented in the port
information. Also the use of the dual stack pointers is not mentioned

BTW: Thank’s for the great OS!