Write the boot file of stm32 by yourself

Project hardware verification environment: STM32F205
Project code cloud git address: https://gitee.com/liwandong/ShareRTOS

In general, you can use the file "startup" stm32f22xx. S provided by ST company directly to guide the startup of mcu, which is not very fond of assembly, so you can use C language to implement the startup process. First, analyze the startup process of the file "startup" stm32f22xx. S as follows:

Created with Rapha ë l 2.2.0 system power on setting SP,PC setting system clock software setting SP loading. data,. bss, and initialization stack area jump to main function

The general process is to obtain the interrupt vector table after the system is powered on, then run the rest? Handle function, then run the initialization clock interface, and finally call the main function. When using the IDE such as mdk, the data copying work is hidden. You can view the map file for analysis. Based on the above process, write our own startup file as follows: the file path is in M cudevice/src/stm32f205_startup.c

  1. Set up interrupt vector table, the code is as follows:
__attribute__ ((section("vectors"))) void (* const VectorArray[])(void)={
    //Kernel interrupt
    (void*)STACK_TOP,
    Reset_Handler,
    NMI_Handler,
    HardFault_Handler,
    MemManage_Handler,
    BusFault_Handler,
    UsageFault_Handler,
    0,  
    0,  
    0,  
    0,  
    SVC_Handler,
    DebugMon_Handler,
    0,  
    PendSV_Handler,
    SysTick_Handler
    //Peripheral interrupt
};

Two knowledge points are used here, one is "attribute" ((section ("vectors")) to define part of the content as a segment, the other is to use the function pointer array to contain multiple functions, so that their addresses are arranged according to the defined order.

  1. Copy data and data initialization functions
void stm32_copydata(unsigned int *form,unsigned int *section_begin,unsigned int *section_end)
{
    unsigned int *p; 
    p = section_begin;
    while(p<section_end)
    {
        *p++ = *form++;
    }
}

void stm32_bssinit(unsigned int *startbss,unsigned int *endbss)
{
  unsigned int *p;
  p = startbss;
  while(p<endbss)
  {
    *p++ = 0;
  }
}

  1. With these basic functions, copy bbs, ro, rw data, initialize the clock, and call the main function in the Reset_Handler function
void Reset_Handler(void){
    stm32_copydata(&_sidata,&_sdata,&_edata);
    stm32_bssinit(&__bss_start__,&__bss_end__);
    SystemInit();
    main();
}

  1. The data segment is from the linked script file. The script file is as follows:
/*
 Specify function entry
 */
ENTRY(Reset_Handler)
/*Define heap and stack sizes*/
_Min_Heap_Size =  0x0000;
_Min_Stack_Size  = 0x10000;

/*flash And ram distribution*/
MEMORY
{
    FLASH (rx):     ORIGIN = 0x08000000, LENGTH = 1024K
    RAM   (rwx):    ORIGIN = 0x20000000, LENGTH = 128K
}

SECTIONS
{
    /*Vector table at the top*/
    .vectors : 
    {
        . = ALIGN(4);
        KEEP(*(vectors))
        . = ALIGN(4);
    } > FLASH
    /*Code segment*/
    .text :
    {
        . = ALIGN(4);
        *(.text);
        *(.text*);
        *(.rodata);
        *(.rodata*);
        *(.glue_7)
        *(.glue_7t)
        *(.eh_frame)
        . = ALIGN(4);
        _etext = .; /*End of snippet*/
    }>FLASH

    /*Data segment*/
    _sidata = .;
    .data :  AT ( _sidata )
    {
        . = ALIGN(4);
        _sdata  = .;
        *(.data);
        *(.data*);
        . = ALIGN(4);
        _edata = .;
    }>RAM
    . = ALIGN(4);
    /*bss paragraph*/
    .bss : 
    {
        _sbss = .;
        __bss_start__ = _sbss;
        *(.bss);
        *(.bss*);
        *(COMMON);
        . = ALIGN(4);
        _ebss = .;
        __bss_end__ = _ebss;
    }>RAM
    /*Export symbols can be used in c*/
    PROVIDE ( end = _ebss );
    PROVIDE ( _end = _ebss );
    ._user_heap_stack :
    {
        . = ALIGN(4);
        . = . + _Min_Heap_Size;
        . = . + _Min_Stack_Size;
        . = ALIGN(4);
    } >RAM
     /DISCARD/ :
    {
        libc.a ( * )
        libm.a ( * )
        libgcc.a ( * )
    }
}

After the above steps, stm32 can be started.

Published 3 original articles, won praise 0, visited 157
Private letter follow

Tags: git REST Attribute C

Posted on Mon, 09 Mar 2020 03:45:30 -0400 by zardiw