MightyPork
MightyPork

Reputation: 18891

AVR C - .init8 routine ignored by compiler

I wanted to add my custom routine to the init sequence, however the compiler seems to dislike it and it's not included in the generated hex.

// 8th init vector, naked
void init_io() __attribute__((naked)) __attribute__((section(".init8")));

void init_io() {
    DDRB    = OUT_PINS;
    PORTB   = IN_PINS; // pullup on input
}

void main() {
    // stuff
}

It's almost exactly copied from the manual, but it doesn't work.

Here's a piece of the asm listing where I expected my routine to end:

  10:   07 c0           rjmp    .+14        ; 0x20 <__bad_interrupt>
  12:   06 c0           rjmp    .+12        ; 0x20 <__bad_interrupt>

00000014 <__ctors_end>:
  14:   11 24           eor r1, r1
  16:   1f be           out 0x3f, r1    ; 63
  18:   cf e9           ldi r28, 0x9F   ; 159
  1a:   cd bf           out 0x3d, r28   ; 61
  1c:   02 d0           rcall   .+4         ; 0x22 <main>
  1e:   0c c0           rjmp    .+24        ; 0x38 <_exit>

// HERE IT SHOULD BE SOMEWHERE

00000020 <__bad_interrupt>:
  20:   ef cf           rjmp    .-34        ; 0x0 <__vectors>

00000022 <main>:

Upvotes: 1

Views: 99

Answers (1)

emacs drives me nuts
emacs drives me nuts

Reputation: 3993

The function should read something like:

__attribute__((used, unused, naked, section(".init8")))
[static] void init_io (void)
{
    DDRB    = OUT_PINS;
    PORTB   = IN_PINS; // pullup on input
}

Notice the used attribute. It tells the compiler to keep the function even when it concludes that it is never used.

The unused attribute silences warnings that the function is never used.

As an aside, also notice that according to GCC documentation, the only code that's supported in naked functions in inline asm.

Upvotes: 2

Related Questions