What is the "Startup Code" and where does it come

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

New guy here. :D I've been reading up on the AVR-GCC compiler toolchain and trying to get a complete understanding of the process. I understand most of it from a top-level except for the "Start-Up Code":

http://www.mygooglest.com/fni/el...

I've read references to how the start-up code is specific to a particular device, but what is it and where does it come from? What does it do?

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

It depends on what you are talking about.

There is "start up code" that is generated by the compiler that includes things like initialized variables.

There is also start-up code that YOU write. It does things like set up timers, configure I/O ports, set UART baud rate, and such.

By the way, in my opinion, that page you referenced does NOT give very good information. You want to use EMACS? We have a semi-great IDE called AVR Studio 6 that provides an editor, automatically generated make files, compilation with gcc, linking, code downloading to the target, simulation, and debugging in one complete package.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

The startup code referred to on that page comes from avr-libc. As Jim said, this is code that sets up global variables, ISR vectors and the stack so that it is all ready for main().

Regards,
Steve A.

The Board helps those that help themselves.

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

The startup code is doing several things. For example:
- It sets up a default interrupt handler so that if an interrupt occurs for which you have not created an ISR then the default one will be activated.
- It does what every C tool chain must do: It sets uninitialized statically allocated variables to zero, and sets initialized statically allocated variables to their respective init values.
- It initializes the stack pointer.
- It initializes the heap.
(and perhaps more that I've forgotten.)
- Finally it calls your main() function.

Just asa there is startup code, there is code that will be executed should your main() ever return. From memory, it
- Disables interrupts
- Goes into an endless loop

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Thanks everyone! Here's the page I was reading originally (I couldn't find it earlier):

https://www.avrfreaks.net/wiki/in...

The diagram shows two elements, Lib and startup code as input to the linker. That's what threw me. It makes sense for them to be the same file.

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

ka7ehk wrote:
By the way, in my opinion, that page you referenced does NOT give very good information. You want to use EMACS? We have a semi-great IDE called AVR Studio 6 that it provides an editor, automatically generated make files, compilation with gcc, linking, code downloading to the target, simulation, and debugging in one complete package.

Jim

Thanks Jim! I downloaded Studio 6 and started messing around with it. I'm designing into an ATmega2561 on a custom board. Also I'm very new with C and C++ so I'm still reading up on how to read and write registers and do bit-manipulation. I'm coming from a strong background in FPGA/digital design and trying my hand at embedded software. This is exciting! :D

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

Quote:

The diagram shows two elements, Lib and startup code as input to the linker. That's what threw me. It makes sense for them to be the same file.

The "lib" code it shows there are surely references to things like strcpy and printf or any standard or AVR (AVR-LibC) sepcific library code you use. That is generally optional while the "start up" code (usually referred to as the "C Run Time" or just CRT) almost always forms part of the e.fl you create (unless you use -nostartfiles or -nodefaultlibs options).

If you want to learn more search here for threads that include "CRT", "_do_copy_data" and "_do_clear_bss" which are two of the main components of the CRT. Maybe also search for "gcrt1.S" which is the single source file in AVR-LibC that builds about 100+ different crtXXX.o files in the run time library.