memory size

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

memory size

跳至解决方案
1,894 次查看
rahulkrishna
Contributor IV

Hi all,

 

We can set the size of the stack in the linker file in Codewarrior S12x. Let us say the RAM is from 0x2000 to 0x2100, if the stack size is 0x100. The first variable will be stored at 0x2100 and keeps decreasing. But due to my wrong calculations the the stack exceeds beyond 0x100. There is no RAM below 0x2000 then how does the microcontroller handle this. please explain me.

 

Thanks in advance.

标签 (1)
标记 (2)
0 项奖励
回复
1 解答
1,354 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

hello rahul,

yes we can read SP register in run time.

I made a small sample code that measures stack usage. see attachment.

Inside the code,

- Function checkStack() stores the current SP value in a global variable and determines what was the lowest value reached by SP in an application.

- Notice the convenient access to C variables from inline-assembly(asm(STS currentStack)). We are forced to use an assembly instruction because there is no access to the CPU registers in C.

- The idea is to have every function in the application call checkStack() to measure the lowest stack address. Thus you can calculate the total stack worst case to allocate.


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

在原帖中查看解决方案

0 项奖励
回复
5 回复数
1,354 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi rahul,

stack overflow error is always hard to detect.

If the stack grows into the heap/RAM, it will silently start to overwrite the data values in heap/RAM. that destroy contents in those memories.

if stack overflow overwrite some function pointer or PC pointer. the code may jump to unexpected address that we can't control.

does this answer your question?


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
回复
1,354 次查看
rahulkrishna
Contributor IV

Can we read the SP at run time?

0 项奖励
回复
1,355 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

hello rahul,

yes we can read SP register in run time.

I made a small sample code that measures stack usage. see attachment.

Inside the code,

- Function checkStack() stores the current SP value in a global variable and determines what was the lowest value reached by SP in an application.

- Notice the convenient access to C variables from inline-assembly(asm(STS currentStack)). We are forced to use an assembly instruction because there is no access to the CPU registers in C.

- The idea is to have every function in the application call checkStack() to measure the lowest stack address. Thus you can calculate the total stack worst case to allocate.


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
回复
1,354 次查看
rahulkrishna
Contributor IV

Thank you very much

0 项奖励
回复
1,354 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

hello rahul,

you are welcome!

Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
回复