In embedded purposes, since we’re working very conclude to the hardware and since we’re most regularly working with restricted situation, strict vitality requirements, and accurate timing restrictions, it is very critical to know exactly what occurs when we bustle a program from the starting to the conclude. Most developers (in particular these that approach from the non-embedded trend world) most regularly purchase that running a C program begins from the predominant() characteristic. Right here’s not correct.
There are a lot of steps that happen earlier than predominant() is executed and working out these steps may possibly possibly well help a lot in gaining a bigger working out of how the gadget works and in a roundabout procedure will possible be extremely worthwhile in some debug cases. Let’s reveal, in embedded systems it is possible to like your program wreck earlier than even reaching predominant(). Figuring out what occurs earlier than predominant() will receive you greater at debugging these form of crashes.
The procedure in which to study is to bustle a relatively easy program (eg. a program with a predominant() characteristic that merely returns zero) ideally on a microcontroller and expend gdb to step thru the code. While what occurs earlier than predominant(), is structure and platform dependent, there are some generic steps that most regularly happen. Right here they’re in no explicit account for:
- The hardware is initialized. The absolute most real looking section of this step is constructing the clock that the CPU needs to bustle the code. Right here’s most regularly performed by programming a PLL (if truth be told a programmable clock). If there is not clock going to the CPU, the CPU is in point of fact dead. The opposite section of hardware initialization is initializing the interrupt facing hardware.
- Memory segments are initialized. Memory segments a lot like .bss (for uninitialized records), .records (for initialized records a lot like static variables, global variables, native static variables, addresses of functions, and characteristic pointers), and .text (the place the actual code resides) are initialized and a legitimate stack is determined up.
- Expose line arguments are bought. This may possibly increasingly perchance well not be relevant in embedded systems as in embedded systems we don’t most regularly name predominant() with arguments
- The stack pointer is configured. Right here’s critical since the program needs to know the place to start up from.
Display that some microcontrollers may possibly possibly require a start up.c or cstart file that has the initialization code (whether or not this file is manually created or auto generated).
Now that we know what occurs earlier than predominant(), lets wonder if there’s a technique we can like watch over what occurs earlier than predominant, or if there is a technique we can write our grasp model of what occurs earlier than predominant(). In C, the acknowledge is basically no. Whatever occurs earlier than predominant() is basically dependent to your structure and compiler. However right here is that if truth be told that additionally, you will imagine in C++.
One procedure additionally, you will make this in C++ is by declaring a worldwide class object. Since global variables are initialized earlier than predominant(), the constructor of the category you like initialized as global will bustle earlier than predominant(). That you just may also attributable to this truth space the code you have to bustle earlier than predominant() in any such constructor.
A topic topic that is expounded to right here is the boot loader, but I will put that for one other put up.
Please follow and treasure us: