RTT中的组建初始化

时间:2022-12-30 22:36:08
/* board init routines will be called in board_init() function */
#define INIT_BOARD_EXPORT(fn)           INIT_EXPORT(fn, "1")

/* pre/device/component/env/app init routines will be called in init_thread */
/* components pre-initialization (pure software initilization) */
#define INIT_PREV_EXPORT(fn)            INIT_EXPORT(fn, "2")
/* device initialization */
#define INIT_DEVICE_EXPORT(fn)          INIT_EXPORT(fn, "3")
/* components initialization (dfs, lwip, ...) */
#define INIT_COMPONENT_EXPORT(fn)       INIT_EXPORT(fn, "4")
/* environment initialization (mount disk, ...) */
#define INIT_ENV_EXPORT(fn)             INIT_EXPORT(fn, "5")
/* appliation initialization (rtgui application etc ...) */
#define INIT_APP_EXPORT(fn)             INIT_EXPORT(fn, "6")

 

#define INIT_EXPORT(fn, level)  RT_USED const init_fn_t __rt_init_##fn SECTION(".rti_fn."level) = fn

 

/*
 * Components Initialization will initialize some driver and components as following
 * order:
 * rti_start         --> 0
 * BOARD_EXPORT      --> 1
 * rti_board_end     --> 1.end
 *
 * DEVICE_EXPORT     --> 2
 * COMPONENT_EXPORT  --> 3
 * FS_EXPORT         --> 4
 * ENV_EXPORT        --> 5
 * APP_EXPORT        --> 6
 *
 * rti_end           --> 6.end
 *
 * These automatically initialization, the driver or component initial function must
 * be defined with:
 * INIT_BOARD_EXPORT(fn);
 * INIT_DEVICE_EXPORT(fn);
 * ...
 * INIT_APP_EXPORT(fn);
 * etc.
 */
static int rti_start(void)
{
    return 0;
}
INIT_EXPORT(rti_start, "0");

static int rti_board_start(void)
{
    return 0;
}
INIT_EXPORT(rti_board_start, "0.end");

static int rti_board_end(void)
{
    return 0;
}
INIT_EXPORT(rti_board_end, "1.end");

static int rti_end(void)
{
    return 0;
}
INIT_EXPORT(rti_end, "6.end")

 

//摘自ld文件
SECTIONS
{
    .text :
    {
        ...
        /* section information for initial. */
        . = ALIGN(4);
        __rt_init_start = .;
        KEEP(*(SORT(.rti_fn*)))
        __rt_init_end = .;
       ....
    } > CODE = 0
//摘自map文件
  *(SORT(.rti_fn*))
 .rti_fn.0      0x0806a968        0x4 build\kernel\src\components.o
                0x0806a968                __rt_init_rti_start
 .rti_fn.0.end  0x0806a96c        0x4 build\kernel\src\components.o
                0x0806a96c                __rt_init_rti_board_start
 .rti_fn.1      0x0806a970        0x4 build\drivers\drv_sdram.o
                0x0806a970                __rt_init_stm32_hw_0_sdram_init
 .rti_fn.1      0x0806a974        0x4 build\drivers\usart.o
                0x0806a974                __rt_init_stm32_hw_usart_init
 .rti_fn.1.end  0x0806a978        0x4 build\kernel\src\components.o
                0x0806a978                __rt_init_rti_board_end
 .rti_fn.4      0x0806a97c        0x4 build\packages\SystemView-v2.52a\SystemView_Src\Config\SEGGER_SYSVIEW_RTThread.o
                0x0806a97c                __rt_init_rt_trace_init
 .rti_fn.4      0x0806a980        0x4 build\kernel\components\libc\compilers\newlib\libc.o
                0x0806a980                __rt_init_libc_system_init
 .rti_fn.6      0x0806a984        0x4 build\drivers\drv_lcd.o
                0x0806a984                __rt_init_stm32_hw_lcd_init
 .rti_fn.6      0x0806a988        0x4 build\kernel\components\finsh\shell.o
                0x0806a988                __rt_init_finsh_system_init
 .rti_fn.6      0x0806a98c        0x4 build\packages\LittlevGL2RTT-v0.0.1\example\littlevgl2rtt_demo.o
                0x0806a98c                __rt_init_rt_lvgl_demo_init
 .rti_fn.6.end  0x0806a990        0x4 build\kernel\src\components.o
                0x0806a990                __rt_init_rti_end
                0x0806a994                __rt_init_end = .
                0x0806a994                . = ALIGN (0x4)
                0x0806a994                . = ALIGN (0x4)
                0x0806a994                _etext = .
                0x0806a994                __exidx_start = .

 

void rt_components_board_init(void)//INIT_BOARD_EXPORT在此初始化
{
    const init_fn_t *fn_ptr;

    for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
    {
        (*fn_ptr)();
    }
}

/**
 * RT-Thread Components Initialization
 */
void rt_components_init(void) //除INIT_BOARD_EXPORT以外的在此初始化
{
    const init_fn_t *fn_ptr;

    for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
    {
        (*fn_ptr)();
    }
}