How to Detect Stack Overflow in KEIL UVISION.
There is plenty of information about the stack on the internet, so I probably can't tell you anything new. If someone still doesn't know, the stack is an area of RAM that works like a stack of plates. What kind of principle is that?
The principle is that if there is a stack of identical plates in front of us and we need to take one, we take the top one, since it's the easiest to take. The same principle underlies how the stack works: at any moment you can only retrieve the data at the top of the stack.
In KEIL, the stack size is set in the startup file with the following line.
You can see the start and top addresses of the stack in the map file
Here __initial_sp is the pointer to the top of the stack.
Now that we know where the stack is located and how large it is, we can watch it by entering the right addresses in the Memory tab (View->Memory Windows->Memory).
In principle, we could stop here, but there's one inconvenience: every time you create a new project, you have to dig into the map file, look up the addresses and keep them in your head. It would be much more convenient to mark the boundaries of the stack somehow, so you don't have to do extra work.
How can this be done?
Initially the RAM and the stack are filled with zeros, so if you fill the stack with some data, for example the bytes CC, it will stand out visually and will be easy to find in RAM.
To do this, delete the following piece at the end of the startup file
And insert another one in its place

That's all; in the next article I'll describe how to flash a microcontroller using KEIL UVISION.
The principle is that if there is a stack of identical plates in front of us and we need to take one, we take the top one, since it's the easiest to take. The same principle underlies how the stack works: at any moment you can only retrieve the data at the top of the stack.
In KEIL, the stack size is set in the startup file with the following line.
Stack_Size EQU 0x00000400You can see the start and top addresses of the stack in the map file
STACK 0x20000260 Section 1024 startup_stm32f10x_hd.o(STACK)
Stack_Mem 0x20000260 Data 1024 startup_stm32f10x_hd.o(STACK)
__initial_sp 0x20000660 Data 0 startup_stm32f10x_hd.o(STACK)Here __initial_sp is the pointer to the top of the stack.
Now that we know where the stack is located and how large it is, we can watch it by entering the right addresses in the Memory tab (View->Memory Windows->Memory).
In principle, we could stop here, but there's one inconvenience: every time you create a new project, you have to dig into the map file, look up the addresses and keep them in your head. It would be much more convenient to mark the boundaries of the stack somehow, so you don't have to do extra work.
How can this be done?
Initially the RAM and the stack are filled with zeros, so if you fill the stack with some data, for example the bytes CC, it will stand out visually and will be easy to find in RAM.
To do this, delete the following piece at the end of the startup file
And insert another one in its place
__user_initial_stackheap
; debug - fill stack area with check pattern
LDR R0, =Stack_Mem ; Test stack
LDR R1, =Stack_Size ;
LDR R2, =0xCCCCCCCC ;
Fill STR R2, [R0], #4 ;
SUBS R1,#4 ;
BNE Fill ;
; end of debug
LDR R0, = Heap_Mem
LDR R1, =(Stack_Mem + Stack_Size)
LDR R2, = (Heap_Mem + Heap_Size)
LDR R3, = Stack_Mem
BX LR
ALIGN
ENDIF
END

That's all; in the next article I'll describe how to flash a microcontroller using KEIL UVISION.


