Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Stack Pointer - documentation conflict?

Stack Pointer - documentation conflict?

2006-04-05 by Don AE5K

As a longtime user of micros (since the Intel 4004), I know about the
necessity of initializing the stack pointer in those devices which have it.

However, some of the most recent Atmel datasheets (for example
doc2586.pdf, 2586F dated 4/06 on the tiny 25/45/85) lead me to believe
that Atmel has gone the way of initializing the stack pointer to the top
of SRAM at reset/powerup time.  In this doc (2586F), paragraph 5.6.1
shows the "Initial Value" to be "RAMEND".

This is great if true, BUT in the paragraph above it (par. 5.6) it says
"This Stack space in the data SRAM must be defined by the program before
any subroutine calls are executed or interrupts are enabled. The Stack
Pointer must be set to point above 0x60."

Also an answer to FAQ on the Atmel website that I read says "For all AVR
devices with SRAM the stack pointer must be set to point at the last
address in SRAM."  [I should note that this is not exactly true -- it
can be set anywhere in SRAM, just as long as there is enough room in
SRAM addresses below it to accommodate anything pushed on the stack!]

So the question is "Which is correct?"   Is the stack pointer in these
newer devices initialized as shown so that the programmer does not need
to do the initialization?

Regards,
Don

-- 
I Haven't Lost My Mind - It's Backed Up On Disk Somewhere

Re: [AVR-Chat] Stack Pointer - documentation conflict?

2006-04-05 by David VanHorn



On 4/5/06, Don AE5K <don@ae5k.us> wrote:
As a longtime user of micros (since the Intel 4004), I know about the
necessity of initializing the stack pointer in those devices which have it.
:) I used to work on 4004 systems.

However, some of the most recent Atmel datasheets (for example
doc2586.pdf, 2586F dated 4/06 on the tiny 25/45/85) lead me to believe
that Atmel has gone the way of initializing the stack pointer to the top
of SRAM at reset/powerup time. In this doc (2586F), paragraph 5.6.1
shows the "Initial Value" to be "RAMEND".
The smaller processors have hardware stack and can't be initted by the program.
On the larger ones, you must init the stack, and you're right you can set it anywhere you like.

This is great if true, BUT in the paragraph above it (par. 5.6) it says
"This Stack space in the data SRAM must be defined by the program before
any subroutine calls are executed or interrupts are enabled. The Stack
Pointer must be set to point above 0x60."
Actually, I'd set it SIGNIFICANTLY above 60, or you'll end up pushing data into the I/O with potentially hilarious consequences.

So the question is "Which is correct?" Is the stack pointer in these
newer devices initialized as shown so that the programmer does not need
to do the initialization?
If they've started preloading SP to the end of ram in the larger devices, that's news.
It's no big deal either way, four instructions that execute once isn't anything to worry about.



--
Feel the power of the dark side! Atmel AVR

Re: Re: [AVR-Chat] Stack Pointer - documentation conflict?

2006-04-05 by Don AE5K

David VanHorn wrote:

> If they've started preloading SP to the end of ram in the larger
> devices, that's news.
> It's no big deal either way, four instructions that execute once isn't
> anything to worry about.

Normally, I'd agree with you ... but in this particular instance I have
exactly 2 words left in some real tight code that I rewrote from a
AVRFreaks contribution -- the original author didn't init stack and uses
a rcall ;-)    The code works, but I started wondering if I was playing
Russian Roulette by no init of SP.

(And yes, I also agree that it is good and "normal" practice to init the
stack pointer to highest address in SRAM -- but even then you can easily
run into weird problems if you don't allocate enough space.  I was just
questioning the misleading "must" in Atmel's statement.)

I've submitted a query to Atmel support, so we'll see what is up.

Don

Re: Re: [AVR-Chat] Stack Pointer - documentation conflict?

2006-04-05 by David VanHorn


Normally, I'd agree with you ... but in this particular instance I have
exactly 2 words left in some real tight code that I rewrote from a
AVRFreaks contribution -- the original author didn't init stack and uses
a rcall ;-) The code works, but I started wondering if I was playing
Russian Roulette by no init of SP.
Hmm.. In a normal AVR, that would leave SP pointed at 0000, which is pretty much guaranteed not to work.

BTW: KC6ETE here.
--
Feel the power of the dark side! Atmel AVR

Re: [AVR-Chat] Stack Pointer - documentation conflict?

2006-04-05 by Henry Carl Ott

At 03:43 PM 4/5/2006, you wrote:
>However, some of the most recent Atmel datasheets (for example
>doc2586.pdf, 2586F dated 4/06 on the tiny 25/45/85) lead me to believe
>that Atmel has gone the way of initializing the stack pointer to the top
>of SRAM at reset/powerup time.  In this doc (2586F), paragraph 5.6.1
>shows the "Initial Value" to be "RAMEND".
>
>This is great if true, BUT in the paragraph above it (par. 5.6) it says
>"This Stack space in the data SRAM must be defined by the program before
>any subroutine calls are executed or interrupts are enabled. The Stack
>Pointer must be set to point above 0x60."


The newer avr parts init the stack at ramend. Atmel has a tendency to copy 
and paste their data sheets, and these kind of conflicts creep in.
   The 0x60 is a bit of a giveaway, I'm pretty sure ram STARTS at 0x100 on 
most of the newer chips with an expanded i/o space.



carl

--------------------------------------------------------
Henry Carl Ott   N2RVQ
http://users.rcn.com/carlott/
--------------------------------------------------------

Re: [AVR-Chat] Stack Pointer - documentation conflict?

2006-04-05 by David VanHorn


The newer avr parts init the stack at ramend. Atmel has a tendency to copy
and paste their data sheets, and these kind of conflicts creep in.
The 0x60 is a bit of a giveaway, I'm pretty sure ram STARTS at 0x100 on
most of the newer chips with an expanded i/o space.
Hmm.. Do we know which chips?
I don't like exceptions. :( It's neat, but they COULD make this behaviour retroactive through all chips. I can't see why anyone would be bothered. If you're writing code that depends on SP=0000 on boot, that's pretty scary!

--
Feel the power of the dark side! Atmel AVR

Re: Stack Pointer - documentation conflict?

2006-04-06 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, Henry Carl Ott <carlott@...> wrote:
> The newer avr parts init the stack at ramend.

I just checked the mega644 via the JTAG Mk II.  SP is 0x10ff prior to 
executing any instructions.  In the mega32, on the other hand, SP 
comes up as 0x0000.

Don
ZBasic Microcontrollers
http://www.zbasic.net

Re: [AVR-Chat] Stack Pointer - documentation conflict? -- resolved

2006-04-06 by Don AE5K

I now have an "official" answer from avr@atmel.com as follows:

> On new devices is the stack pointer initialized to REMEND at reset, and you don't need to do that in your code.
> Please see the datasheet of the device you're using.
> I'll update the FAQ.
> 
> Best regards
> Jan HÃ¥vard Ugelstad
> Atmel AVR Technical Support
> 

As pointed out in my original query, the "datasheet" is in conflict with
itself and should be cleaned up considerably in this area.  Looking at
other recent AVR parts datasheets (mega48/88/168 series for example),
they are all incorrect and confusing.

Thanks to everyone on this list who replied and confirmed what I thought
was true.  And in my case of code for a tiny45 bootloader (fitting into
64 words), I don't need to worry about finding two more words (besides
the two I already have) to init the stack pointer.

Don

Don AE5K wrote:
Show quoted textHide quoted text
> As a longtime user of micros (since the Intel 4004), I know about the
> necessity of initializing the stack pointer in those devices which have it.
> 
> However, some of the most recent Atmel datasheets (for example
> doc2586.pdf, 2586F dated 4/06 on the tiny 25/45/85) lead me to believe
> that Atmel has gone the way of initializing the stack pointer to the top
> of SRAM at reset/powerup time.  In this doc (2586F), paragraph 5.6.1
> shows the "Initial Value" to be "RAMEND".
> 
> This is great if true, BUT in the paragraph above it (par. 5.6) it says
> "This Stack space in the data SRAM must be defined by the program before
> any subroutine calls are executed or interrupts are enabled. The Stack
> Pointer must be set to point above 0x60."
> 
> Also an answer to FAQ on the Atmel website that I read says "For all AVR
> devices with SRAM the stack pointer must be set to point at the last
> address in SRAM."  [I should note that this is not exactly true -- it
> can be set anywhere in SRAM, just as long as there is enough room in
> SRAM addresses below it to accommodate anything pushed on the stack!]
> 
> So the question is "Which is correct?"   Is the stack pointer in these
> newer devices initialized as shown so that the programmer does not need
> to do the initialization?
> 
> Regards,
> Don
>

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.