最新消息:

移植RT-Thread的自动初始化功能到ESP32中

C ZmmFly 220浏览 0评论

===========================2021-05-29=========================

调试发现问题,ESP IDF将所有文件全部编译成静态库再链接,__used将不起作用,

也就是说如果没有显式的不可移除的调用,声明的初始化变量将在链接时被移除

===========================2021-05-29=========================

目的很简单。。

干掉app_main中一长条的init_***

首先提取RT-Thread中自动初始化部分的代码

typedef int (*init_fn_t)(void);

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

/* 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 ...) */

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");

void rt_components_board_init(void)
{
#if RT_DEBUG_INIT
    int result;
    const struct rt_init_desc *desc;
    for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++) { printf("initialize %s", desc->fn_name);
        result = desc->fn();
        printf(":%d done\n", result);
    }
#else
    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)();
    }
#endif
}

/**
 * RT-Thread Components Initialization
 */
void rt_components_init(void)
{
#if RT_DEBUG_INIT
    int result;
    const struct rt_init_desc *desc;

    printf("do components initialization.\n");
    for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++) { printf("initialize %s", desc->fn_name);
        result = desc->fn();
        printf(":%d done\n", result);
    }
#else
    const init_fn_t *fn_ptr;

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

提取链接脚本

/* section information for initial. */
. = ALIGN(4);
__rt_init_start = .;
KEEP(*(SORT(.rti_fn*)))
__rt_init_end = .;

置于ESP-IDF链接脚本的text段中, 再在app_main中只需要调用rt_components_board_initrt_components_init

剩下的,需要初始化就和RT-Thread中一样, 声明function, INIT_APP_EXPORT等等就OK了

转载请注明:神奇海域 » 移植RT-Thread的自动初始化功能到ESP32中

发表我的评论
取消评论

Protected by WP Anti Spam
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址