Introduction: Starting with microcontrollers can feel like waking them up from sleep. Whether it’s through a reset button press or powering them on, this action kickstarts a series of steps to prepare the microcontroller for action. In this journey, one crucial element is the vector table, guiding the microcontroller’s path towards its ultimate destination—the main
function.
The Genesis: Powering On and Initialization: Imagine waking up in the morning->you need to get ready for the day. Similarly, when powered on or reset, a microcontroller undergoes initialization routines to ensure everything is in order. It checks its environment, sets up clocks, initializes memory, and primes its peripherals for use.
Encountering the Vector Table: As the microcontroller wakes up, it seeks guidance ->a roadmap to navigate the initialization process. Here, it encounters the vector table, a crucial map containing addresses pointing to various functions or handlers for interrupts and exceptions. This table serves as a guide, directing the microcontroller to the appropriate routines during its initialization. Below is an example of XMC4400 microcontroller’s vector table.
- When microcontroller resets, program counter or PC is loaded with the address 0x00000000, this first address contains the address of the top of the stack or the value that will be loaded to the main stack pointer.
- After that microcontroller loads the value stored at the address 0x00000000 to the main stack pointer or MSP.
- Now PC moves to the next address by increment its value by 4. Because the program counter always moves to the next address. The next address is 0x00000004 and the value at address 0x00000004 contains a pointer to a reset handler function.
Navigating the Startup Code: With the vector table as its guide, the microcontroller proceeds to execute the startup code. This code, residing at a predefined address pointed to by the vector table, orchestrates the crucial setup tasks necessary before reaching the main
function.
The Role of the Startup Code: The startup code embarks on a quest to lay the groundwork for program execution, undertaking tasks of paramount importance and it ensures that the microcontroller’s environment is properly configured and it performs below steps:-
- Disable all interrupts:- Firstly, it disables all interrupts so that microcontroller cannot be able to execute any other interrupt service routine such as watchdog time, etc. When you upload your code to a microcontroller, the binary image of code gets stored in flash memory. Because flash is a non-volatile memory. Therefore, code stores there permanently even without power. During the microcontroller booting process, some data which is stored in flash memory is moved to the RAM. But the question that might come to your mind is that what is the reason for copying data from flash to RAM of microcontroller? This is because RAM works faster than flash memory and hence it helps to reduce the latency of data accesses from memory. Another important reason is that the read and write operations to flash memory are limited unlike RAM. Therefore, it is preferred to perform all operations in RAM.
- Initialize data segment:– If you remember/know about the layout of a running C program then you must remember that the initialized global, static global and static local variables store in the .data section of memory. Reset handler assign memory region to .data section and moves the content of the initialized data section from flash to RAM memory region of the microcontroller.
- Initialize the .Bss segment:- In our embedded program, we use both initialized and uninitialized static and global variables. The uninitialized static, global static, and local static variables store in the .bss segment of memory. The .bss section also copies from flash to RAM and zero all uninitialized variables.
- Initialize stack segment:- The stack segment is a sub-segment of RAM memory. Reset handler reserves the stack space in data memory according to pre-compiled instructions given in the linker file. The stack memory region is used to hold temporary variables, input arguments, and return addresses of functions.
- Enable interrupts.
- Call the main function.
Conclusion:- Finally, Having navigated through the vector table and executed the startup code, the microcontroller transfers the control to the main function of your application. This is how microcontroller boots up and the reset sequence works in brief. To go into in-depth details of this process, we recommend you try to analyze the startup file of a microcontroller.
Leave a comment