Startup: Reset_Handler and simila ( STM32F411-DISCO Arm Cortex-M4)

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
ales.coppelli
Posts: 34
Joined: Wed Aug 08, 2018 9:15 am

Startup: Reset_Handler and simila ( STM32F411-DISCO Arm Cortex-M4)

Post by ales.coppelli » Mon Feb 15, 2021 4:54 pm

Hi to all.

I'm trying to undestand the early stages after a power on in a
STM32F411-DISCO ( Arm Cortex-M4 ).

Immediatly after a reset ( or power on ) the program counter is
setting at 0x00000004 and called "Reset_Handler"

This function is in 'micropython/ports/stm32/resethandler.s'

Code: Select all

...
... At the end ...
    /* Initialise the system and jump to the main code */
    bl   SystemInit
    mov  r0, r4
    b    stm32_main

So, from Reset_Handler we have to jump ( with a return ) to "SystemInit".

Question_1

Where is this function?
I saw only a comment in 'micropython/ports/stm32/system_stm32.c'
but no implementation.

Code: Select all

  *   This file provides two functions and one global variable to be called from
  *   user application:
  *      - SystemInit(): This function is called at startup just after reset and
  *                      before branch to main program. This call is made inside
  *                      the "startup_stm32.s" file.
  *
  *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  *                                  by the user application to setup the SysTick
  *                                  timer or configure other parameters.
  *
Question 2

Then we have to jump (without return ) to "stm32_main":

'ports/stm32/mboot/main.c' or 'ports/stm32/main.c' ?

Which one ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Startup: Reset_Handler and simila ( STM32F411-DISCO Arm Cortex-M4)

Post by dhylands » Mon Feb 15, 2021 6:10 pm

The best way to answer this is to look in the .map file produced by the linker when it runs. For example, after doing

Code: Select all

make BOARD=STM32F411DISC
you'll find a firmware.map in the build-STM32F411DISC directory.

Searching for SystemInit, you'll see:

Code: Select all

 .text.SystemInit
                0x0000000008052fa8       0x4c build-STM32F411DISC/lib/stm32lib/CMSIS/STM32F4xx/Source/Templates/system_stm32f4xx.o
                0x0000000008052fa8                SystemInit
This tells us that the .o file was from lib/stm32lib/CMSIS/STM32F4xx/Source/Templates/system_stm32f4xx.o which means that you'll find the .c file in micropython/lib/stm32lib/CMSIS/STM32F4xx/Source/Templates/system_stm32f4xx.c

Similarly, for stm32_main:

Code: Select all

 .text.stm32_main
                0x000000000804a5ec      0x284 build-STM32F411DISC/main.o
                0x000000000804a5ec                stm32_main
which tells us it's coming from ports/stm32/main.c, and not insdie the mboot directory. The stm32_main from mboot would be the entry point if you installed the mboot bootloader, but that's normally not the case on the STM32F4 series.

ales.coppelli
Posts: 34
Joined: Wed Aug 08, 2018 9:15 am

Re: Startup: Reset_Handler and simila ( STM32F411-DISCO Arm Cortex-M4)

Post by ales.coppelli » Tue Feb 16, 2021 7:12 am

Thank you very much!!!

Post Reply