Stack Initialization in Attiny4313

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

Hi All,

Can anyone help in how to initialize Stack in C for Attiny4313 IC

IDE used Atmel studio7.0

compiler used GCC.

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

You don't need do anything. There is "hidden" code added by the C compiler that runs just before main() (known as the "C Run Time" or just "CRT") and that already does important jobs like setting the stack. For example:

C:\SysGCC\avr\bin>type avr.c
#include <avr/io.h>

int main(void) {
        while(1) {
        }
}

C:\SysGCC\avr\bin>avr-gcc -g -mmcu=attiny4313 -Os avr.c -o avr.elf

C:\SysGCC\avr\bin>avr-objdump -S avr.elf

avr.elf:     file format elf32-avr


Disassembly of section .text:

00000000 <__vectors>:
   0:   14 c0           rjmp    .+40            ; 0x2a <__ctors_end>
   2:   1b c0           rjmp    .+54            ; 0x3a <__bad_interrupt>
   4:   1a c0           rjmp    .+52            ; 0x3a <__bad_interrupt>
   6:   19 c0           rjmp    .+50            ; 0x3a <__bad_interrupt>
   8:   18 c0           rjmp    .+48            ; 0x3a <__bad_interrupt>
   a:   17 c0           rjmp    .+46            ; 0x3a <__bad_interrupt>
   c:   16 c0           rjmp    .+44            ; 0x3a <__bad_interrupt>
   e:   15 c0           rjmp    .+42            ; 0x3a <__bad_interrupt>
  10:   14 c0           rjmp    .+40            ; 0x3a <__bad_interrupt>
  12:   13 c0           rjmp    .+38            ; 0x3a <__bad_interrupt>
  14:   12 c0           rjmp    .+36            ; 0x3a <__bad_interrupt>
  16:   11 c0           rjmp    .+34            ; 0x3a <__bad_interrupt>
  18:   10 c0           rjmp    .+32            ; 0x3a <__bad_interrupt>
  1a:   0f c0           rjmp    .+30            ; 0x3a <__bad_interrupt>
  1c:   0e c0           rjmp    .+28            ; 0x3a <__bad_interrupt>
  1e:   0d c0           rjmp    .+26            ; 0x3a <__bad_interrupt>
  20:   0c c0           rjmp    .+24            ; 0x3a <__bad_interrupt>
  22:   0b c0           rjmp    .+22            ; 0x3a <__bad_interrupt>
  24:   0a c0           rjmp    .+20            ; 0x3a <__bad_interrupt>
  26:   09 c0           rjmp    .+18            ; 0x3a <__bad_interrupt>
  28:   08 c0           rjmp    .+16            ; 0x3a <__bad_interrupt>

0000002a <__ctors_end>:
  2a:   11 24           eor     r1, r1
  2c:   1f be           out     0x3f, r1        ; 63
  2e:   cf e5           ldi     r28, 0x5F       ; 95
  30:   d1 e0           ldi     r29, 0x01       ; 1
  32:   de bf           out     0x3e, r29       ; 62
  34:   cd bf           out     0x3d, r28       ; 61
  36:   02 d0           rcall   .+4             ; 0x3c <main>
  38:   02 c0           rjmp    .+4             ; 0x3e <_exit>

0000003a <__bad_interrupt>:
  3a:   e2 cf           rjmp    .-60            ; 0x0 <__vectors>

0000003c <main>:
#include <avr/io.h>

int main(void) {
  3c:   ff cf           rjmp    .-2             ; 0x3c <main>

0000003e <_exit>:
  3e:   f8 94           cli

00000040 <__stop_program>:
  40:   ff cf           rjmp    .-2             ; 0x40 <__stop_program>

C:\SysGCC\avr\bin>

I've highlighted the lines where it sets the stack.

 

As it happens, for modern AVRs this is just a "belts and braces" operation anyway. If you read the datasheet you will find that SP defaults to 0x15F (RAMEND) at power on anyway. But this CRT code is "safe" in that if control returns to location 0x0000 for some reason then this will reset the stack before it calls back into main().

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

And if you still want to change it look here

 

http://www.atmel.com/webdoc/avrl...

 

and change SPH SPL as the first thing (before any ISR's)