Reset sequence of Micro-Controller

Padmanabh
3

In this article, you will learn
1. When you power on micro-controller how does control reach to main() function.
2. Why main() function is required.
3. Why uninitialized global variables have default value ?


1. Introduction :


What happens when micro-controller resets. when you reset micro-controller, it will start performing some initialization and starts executing from the main() function. Do you want to know how this all happens, then you are reading right article.



2. Startup Sequence : 


Have you ever thought how does micro-controller start execution from the main() function, when powered on? If yes then you are reading right article. Let's get started.

I assume you all are aware of the Program Counter of micro-controller. PC is a register which stores the address of an instruction  which is being executed at the current time. PC is hard-wired at specific address location. So when micro-controller starts from power up, PC fetches the 1st instruction from hard-wired address. This address is generally 0x00000000 in 32bit system.


Consider a example of Cortex M4 architecture (NXP S32K144 Series) :

1. When micro-controller resets PC is loaded with value 0x00000000. At address 0x00000000 vector table is located. In case of cortex M4, it reads first two words from vector table. Consider Fig.1 at address 0x00000000, value stored is 0x20008000, which is value of top of stack i.e MSP is pointing to 0x20008000. Fig. 2 Shows the Vector table of cortex M4 architecture (startup_S32K144.S) , _isr_vector: is start of vector table, and __StackTop is value of stack pointer stored at memory location 0x00000000. __StackTop value is defined in linker file (S32K144_100_flash.dld)


Fig.1 Startup Sequence of ARM Cortex M3

Fig.2 Vector Table
2. Then from address 0x00000004 PC get loaded with the address of Reset handler. Reset handler is defined in startup file(startup_S32K144.S). Reset handler will initialize registers i.e. r0 to r13 and stack pointer. Will call init_data_bss routine (Refer fig.3)

Fig.3 Reset Handler 




3. init_data_bss routine is defined in startup.c file. This routine will copy initialized data (.data section) and functions (.txt section) from rom to ram and zero initializes the .bss section .i.e uninitialized global variables will get initialized to 0 (fig.4).

Fig.4 init_data_bss Handler
4. After this reset handler will jump to the main function as shown in fig.2 instruction: bl main. This is why main is an entry function for c programs. Without the main function, system will not work. This is how control reach to main after reset of micro-controller.


Post a Comment

3 Comments
  1. when does the boot-loader execute?

    ReplyDelete
  2. In this blog I have explained reset sequence without bootloader.
    In case bootloader is present, then
    1.Reset vector is executed
    2. If firmware updateis required then bootloader will be executed, else jump to main .
    3. If bootloader executed i.e firmware is uodated, then after bootloader finishes execution, jump to main

    ReplyDelete
  3. You should consider the BTL as a normal software, with it's main function it depends also on what functionalities it's handling but the same sequence described by the article is followed to run the BTL.

    ReplyDelete
Post a Comment
To Top