hardware stack overflow

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

hardware stack overflow

Post by siliconchip »

hi can anybody explain to me what " hardware stack over flow" means, I have been searching around for answers but cannot get my head around the explanations thanks in advance

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: hardware stack overflow

Post by JonnyW »

Hi.

On an 8-bit PIC chip there is a hardware stack. This is the number of times you can call a macro to jump away from 'main' before it can cause your program to become corrupted. This also includes calls made by underlying components. You can turn this off and use a 'software stack', but I believe there is a speed-hit when you do this.

See the topic below for further details:
http://www.matrixmultimedia.com/mmforum ... 328#p35328

I hope this helps.

Jonny

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: hardware stack overflow

Post by Benj »

A really basic example is say you are in the main loop and call a subroutine. The stack is a place in memory which stores the return address so that when the microcontroller returns from the function it knows where in main to resume the execution. A standard 8-bit PIC16xxx device has 8-hardware levels of stack but this includes any interrupts and macros called by said interrupts.

The stack is completely managed by the C compiler so as long as you don't go over the limitations you don't have to worry about it.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

hi jonny/benj thanks for the info is this true of the pic18f as well in particular the 18f4550

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: hardware stack overflow

Post by Benj »

Hello,

The 18F4550 has a 31 level hardware stack, looking around it seems that all 18F devices might have this same setup.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

one final question how would you stop or correct a hardware stack overflow

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: hardware stack overflow

Post by Benj »

Hello,

I think most 18F devices have a config setting which allows the microcontroller to automatically reset itself if the stack does overflow.

The best option is to design your program in a way to match the limitations of the microcontroller and this way you can be assured that this problem won't happen.

If however you have a program which is warning you on compilation that there may be a overflow then you can switch on the software stack option in BoostC but as Johnny says there may be a slight performance hit.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

hi benj thanks, being a novice im not sure how to turn on the stack option in boost c :oops:

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: hardware stack overflow

Post by Benj »

Hello,

There is an example of how to do it here.
http://www.matrixmultimedia.com/mmforum ... 404#p38549

The two numbers that are used in the example are 6 2 which basically means use 6 hardware stack layers for the main program and 2 hardware stack layers for the interrupts. Any additional stack layers will be automatically performed using the BoostC software stack.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

hi benj I added -swcs 6 2 but used 20 11 instead of 6 2 because of there being 31 stacks on the pic 18f4550, not sure if 20 11 is the right combo but after compiling I got the following message

Error: failed to open :swcs.
Error: failed to open :20.
Error: failed to open :11

failure
return code = - 2.

have you anythoughts on this I added -swcs 6 2 in the linker/assembler parameter

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: hardware stack overflow

Post by medelec35 »

Hi Bob,
Try the attached flowchart and see if it sorts your issue out.
I have programmed flowchart to the following:
siliconchip wrote: basic flow chart involving using the RB0 interrupt so when pressed once to light a led for 2 seconds then go out and if pressed again to light another for 2 seconds then again go out
Pressing B0 during 2 second timing period should not affect the timing.

Don't forget that 2 seconds with Pic Sim IDE on ultimate settings will take a few minutes.

Martin
Attachments
RB0int LEDs.fcf
(17.84 KiB) Downloaded 231 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

hi martin thanks for the reply, I have tried your example in oshunsoft and it too is coming up with hardware stack overflow?? now im not sure if this matters if the program works on hardware in the real world ok, but the fact I want to look at registers changing as a learning point of view and seeing this in the oshunsoft software is confusing me a little, is the oshunsoft software in some way at fault or not set correctly by me either way this all points back to my original problem, I've tried putting in the extra text as mentioned earlier in the assenmbler/linker text but it will not compile, is the hardware stack over flow RB0 related or something completely different ?

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: hardware stack overflow

Post by medelec35 »

Program I have posted should not take it over the stack threshold.
If you compile, then compiler will flash up any stack overflow warnings:

Code: Select all

Building CASM file
Memory Usage Report
===================
RAM available:768 bytes, used:41 bytes (5.4%), free:727 bytes (94.6%), 
Heap size:471 bytes, Heap max single alloc:127 bytes
ROM available:16384 bytes, used:324 bytes (2.0%), free:16060 bytes (98.0%)



success
Return code = 0




FINISHED
Also not getting any stack errors with Pic sim:
pic sim.png
(288.43 KiB) Downloaded 5187 times
The above is running after 2 seconds has elapsed, pressed B0, stop watch reset to 0 and continued to run on ultimate.

Can you post your hex file so I can load into simulator.

Martin
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

hi martin mine compiles ok in flow code with no errors but as soon as its run in oshunsoft the hardware stack overflow appears I have added my hex file, one thought, I have is that I use -rb 0x1000 -ld at the front of my parameters in the linker assembler block of the compiler as im using a boot loader for the 4550, am I right that after an interrupt the stack goes to 0x0004 reset vector, ? could this be a problem in oshunsoft
Attachments
RB0int LEDs.hex
(988 Bytes) Downloaded 161 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: hardware stack overflow

Post by siliconchip »

martin I have removed-rb 0x1000 from the linker assembler parameter in the compiler and hex files are now working in oshunsoft, so now my question is that as I have a bootloader program installed on my 18f4450 will this affect the running/performance of any program, reason being that the oshunsoft simulator is not happy with this in the compiler parameter ??

Post Reply