Topic : Why the Rkit-STM8 compiler does not set stack pointer in startup?

Forum : ST7/STM8

Original Post
Post Information Post
January 8, 2010 - 2:36am
Guest

I know the MCU sets the stack pointer to RAM top when MCU power on, if the stack pointer does not set in reset condition, this cause the firmware will not be stable in some rare case such as following:
((void (code *) (void))RESET_ADDRESS) ();
or assembly code
jp RESET_ADDRESS

the stack pointer does not change when use these code.

Replies
Post Information Post
+1
0
-1
January 8, 2010 - 10:49am
Raisonance Support Team

Hi Rod,

I do not exactly understand what you want to do with this "pseudo-reset".

If you are using an ST7 derivative, you can use the _rst_() intrinsic function. But this does not exist on STM8.
If you want to manually reset your STM8 device the best is to generate a (real) reset using the window watchdog as follows:

WWDG->CR = WWDG_CR_WDGA | 0x40;    // Activate watchdog counter to minimal value 0x40
disableInterrupts();               // Disconnect from the world
while(1);                          // After 12288 cycles the chip will be reset

I recommend NOT to reset the stack pointer manually, as there is no instruction for that on STM8, and the default SP value is not always the one referenced in the datasheets. So a manual SP reset may work now, but not with chips that you buy in few months.

This is why we do not reset the stack pointer in our startup code.

Regards,
Bruno

+1
0
-1
January 8, 2010 - 1:17pm
Guest

I think if add a stack pointer initialisation in startup.s, the firmware may be work well in any situation.

CST7_START_V:
CST7_START_Z:
CST7_START_V_Z:

ldw x,#CHIP_RAM_TOP
ldw sp,x

; Comment out the following line if your project does not use any
; 0-initialized C global variables initialization.
; ?C?ZeroData must be called BEFORE ?C?InitData
;-----------------------------------------------------------------------------
; WARNING: On ST7 projects, the ?C?ZeroData function will blank the stack
; if there are zero-initialized variables in both page0 and data
; memory spaces.
;-----------------------------------------------------------------------------
CALL ?C?ZeroData ; Initialize null global variables

; Comment out the following line if your project does not use any C global
; variables initialization. If you are unsure, just leave it as it is.
CALL ?C?InitData ; Initialize global variables.

JP main ; Call the user entry point (main)

+1
0
-1
January 8, 2010 - 4:47pm
Raisonance Support Team

It will, provided that you know the actual value for #CHIP_RAM_TOP.
But you don't: ST specs explain that SP is initialized "to the top of RAM", but you do not know the *real* RAM top address, which may vary.
For instance a STM8S207K6 chip with 2KB of (documented) RAM may have its stack pointer initilized to 0x1800 (which is not what you would expect).

Depending on your application's life cycle, avoid modifying SP manually, and try to find another algorithm to reinitialize.

Regards,
Bruno