4.9.3 The Powerup Routine
Some hardware configurations require special initialization, often within the first few instruction cycles after Reset. To achieve this you can have your own powerup routine executed during the runtime startup code.
Provided you write the required code in one of the
.initn
sections used by the runtime startup code, the compiler
will take care of linking your code to the appropriate location, without any need for you
to adjust the linker scripts. These sections are listed in 4.9.2 Runtime Startup Code.
For example, the following is a small assembly sequence that is placed in
the .init1
section and is executed soon after Reset and before
main()
is called.
#include <avr/io.h>
.section .init1,"ax",@progbits
ldi r16,_BV(SRE) | _BV(SRW)
out _SFR_IO_ADDR(MCUCR),r16
Place this routine in an assembly source file, assemble it, and link the output with other files in your program.
Remember that code in these sections is executed before all the runtime
startup code has been executed, so there is no usable stack and the
__zero_reg__
(r1) might not have been initialized. It is best to leave
__stack
at its default value (at the end of internal SRAM since this is
faster and required on some devices, like the ATmega161 to work around known errata), and
use the -Wl,-Tdata,0x801100
option to start the data section above the
stack.