ATtiny13 stack initialisation

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
I am looking for any free space in my ATtiny13 programm and have such a question:
In datasheet p.9 is described stack pointer register and here is:

SPL
Initial Value 1 0 0 1 1 1 1 1

what correspond with RAMEND=0x9F from p.14 (memory map).
But on p.42 we have typical stack setup:

0x000A RESET: ldi r16, low(RAMEND); Main program start
0x000B out SPL,r16 ; Set Stack Pointer to top of RAM

Can I omit this 2 instructions pair?
I was testing my program without above lines and it works correctly, but I must be sure it is OK.

Szymon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Can I omit this 2 instructions pair?
Looks like you can with newer, small processors as the stack pointer is already intiliased for you. Just don't get into the habit if you have the room to initialise the stack pointer...just in case that processors you just started to use needs the stack pointer initialised. :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As far as I know the Stack pointer is uninitialized by default. If you don't need interrupts or things to push/pop from the stack your program will work correctly even if you don't set the SP.

Now if you have interrupts enabled how will the uC be able to store the last instruction before the interrupt fires, if you don't initialize it.

Summing up, if you don't need the stack pointer omit it, else make sure you initialize it first.

Edit: Bah I didn't read js' post. If it is already initialized for your model I guess you can omit it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If it is already initialized
Some new SMALL chip (T13,T25 family) do initialise the stack.

Initial Value RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND
                RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND

other do not.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I always adhere to 'better safe than sorry'. The few bytes of code to properly, and guarantee correct stack initialisation is worth it.

Sometimes it is better to not reinvent the wheel, think outside the box, or boldly go where no man has gone before.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Izotech wrote:

Can I omit this 2 instructions pair?

Yes.

All newer AVRs set the stack to a valid address after reset.

Only the older (ATtiny26,ATMega8,16,32,64,128) do it not.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If you don't need interrupts or things to push/pop from the stack your program will work correctly even if you don't set the SP.

If you try to use RCALL or CALL without a stackpointer there is nowhere to put return adress so your code is off to la-la land.
It's part of good programming practice to have stackpointer initialized at top of program.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lennart wrote:

If you try to use RCALL or CALL without a stackpointer there is nowhere to put return adress so your code is off to la-la land.

Of course, I should have noted that it won't work if you include subroutines on your code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks to all for confirmation.
But it is not clear from datasheet.
That same initial setup is used in ATmega48 document but I don't check is it really working.
Szymon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
But it is not clear from datasheet.
That same initial setup

That's general purpose stuff. Look at the actual reset state of the stack pointer for the M48 (or any chip).

Quote:
5.5.1 SPH and SPL – Stack Pointer High and Stack Pointer Low Register

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Izotech wrote:

But it is not clear from datasheet.
That same initial setup is used in ATmega48 document but I don't check is it really working.

On the ATtiny13 datasheet its clear:
Initial Value: 1 0 0 1 1 1 1 1

And on the ATmega48 datasheet also:
Initial Value: RAMEND

So there is no need to set the stack pointer twice.:wink:

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes but on p.55 in ATmega48 datasheet (typical interrupt setup) you can read:

0x01A RESET: ldi r16, high(RAMEND); Main program start
0x01B out SPH,r16 ; Set Stack Pointer to top of RAM
0x01C ldi r16, low(RAMEND)
0x01D out SPL,r16
0x01E sei ; Enable interrupts
0x01F xxx
... ... ... ...

So here is in my opinion unclear, it will be eventually not useless here RAMEND but another address if you set initial stack pointer to another place in ram.

Szymon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Izotech wrote:
you can read:

Unfortunately that just reflects the "cut and paste" nature of a lot of the AVR datasheets - some even refer to regsisters (like UDR instead of UDR0) that don't exist in the AVR that's being documented.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, thank you, so I have additional similar question, is still (like older models) undefined state after reset in working register r0...r31 , I can't find any information on it in ATtiny and ATmega48 manual?
Or maybe it is improved in new models. That same problem is with sram.

Szymon

Last Edited: Wed. Jan 9, 2008 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Curtvm did some recent experimentation and I think he found that all 32 registers defaulted to 0 but you may want to run a similar experiment on the specific AVR model you are using (have early code store them all somewhere then report these to LEDs or UART or something once the system has got going)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And Atmel confirmed this behaviour?
I had 'experience' with ATtiny26 some years ago, one of working register had uncleared state after reset and program was working correctly one month.
I think working register have some remaining memory, after power off.

Szymon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Curtvm did some recent experimentation and I think he found that all 32 registers defaulted to 0
Cliff is still recovering, so may be a little mixed up yet :) (nice to see you back)

The general registers r0-r31 have no initial state on power up (they can be anything, but doing some testing shows they were mostly the same, 'most' of the time- like 0x02 0x00 etc- but bits of the registers would change from time to time, in other words they can be anything. I was not seeing a lot of changes because I probably didn't wait a long enough time after powerdown, now that I think about it). The general registers (just like sram) will hold their state on an external reset or watchdog reset, but something like the brownout reset would be suspect (depending on where the brownout is set, I guess, but I wouldn't count on it when power is involved in the reset cause).

The datasheet doesn't say anything about the initial state of the general registers, but they are stating it by the fact that they do not mention any initial state. A statement by omission.

If you want the scoop on the stack pointer (or X/Y/Z pointers) when it points to invalid addresses, I tested a mega88 and a mega16- and the answer is in a thread somehwere (it ended up me just talking to myself). But I haven't forgotten it yet. I will try to find the thread.

here it is- it made sense when I wrote it, but now it sounds like gibberish-
https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The general registers r0-r31 have no initial state on power up
Of course C people have the compiler doing all the init, correct? So one does not really have a choice wether the stack is initialised or not, it's up to the master, the compiler writer :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Of course C people have the compiler doing all the init, correct?
Not if you don't want it. When I wrote my bootloader in C, I had no C startup stuff, or vector table. Easily turned off. You do have a choice. You are the master. Most of the time.

(you will happy to learn its now re-written in gcc assembly)