可以通过初始化宏,如下:自动导入到初始化序列
/* 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 initialization) */
#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")
/* application initialization (rtgui application etc ...) */
#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
实现代码如下:
#ifndef __COMPONENTSINIT_H__
#define __COMPONENTSINIT_H__
/* initialization export */
#define RT_USING_COMPONENTS_INIT
/* Compiler Related Definitions */
#if defined(__ARMCC_VERSION) /* ARM Compiler */
#include <stdarg.h>
#define RT_SECTION(x) __attribute__((section(x)))
#define RT_USED __attribute__((used))
#define ALIGN(n) __attribute__((aligned(n)))
#define RT_WEAK __attribute__((weak))
#define rt_inline static __inline
/* module compiling */
#ifdef RT_USING_MODULE
#define RTT_API __declspec(dllimport)
#else
#define RTT_API __declspec(dllexport)
#endif /* RT_USING_MODULE */
#elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
#include <stdarg.h>
#define RT_SECTION(x) @ x
#define RT_USED __root
#define PRAGMA(x) _Pragma(#x)
#define ALIGN(n) PRAGMA(data_alignment=n)
#define RT_WEAK __weak
#define rt_inline static inline
#define RTT_API
#elif defined (__GNUC__) /* GNU GCC Compiler */
#ifdef RT_USING_NEWLIB
#include <stdarg.h>
#else
/* the version of GNU GCC must be greater than 4.x */
typedef __builtin_va_list __gnuc_va_list;
typedef __gnuc_va_list va_list;
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#endif /* RT_USING_NEWLIB */
#define RT_SECTION(x) __attribute__((section(x)))
#define RT_USED __attribute__((used))
#define ALIGN(n) __attribute__((aligned(n)))
#define RT_WEAK __attribute__((weak))
#define rt_inline static __inline
#define RTT_API
#elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
#include <stdarg.h>
#define RT_SECTION(x) __attribute__((section(x)))
#define RT_USED __attribute__((used))
#define ALIGN(n) __attribute__((aligned(n)))
#define RT_WEAK __attribute__((weak))
#define rt_inline static inline
#define RTT_API
#elif defined (_MSC_VER)
#include <stdarg.h>
#define RT_SECTION(x)
#define RT_USED
#define ALIGN(n) __declspec(align(n))
#define RT_WEAK
#define rt_inline static __inline
#define RTT_API
#elif defined (__TI_COMPILER_VERSION__)
#include <stdarg.h>
/* The way that TI compiler set section is different from other(at least
* GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more
* details. */
#define RT_SECTION(x)
#define RT_USED
#define PRAGMA(x) _Pragma(#x)
#define ALIGN(n)
#define RT_WEAK
#define rt_inline static inline
#define RTT_API
#elif defined (__TASKING__)
#include <stdarg.h>
#define RT_SECTION(x) __attribute__((section(x)))
#define RT_USED __attribute__((used, protect))
#define PRAGMA(x) _Pragma(#x)
#define ALIGN(n) __attribute__((__align(n)))
#define RT_WEAK __attribute__((weak))
#define rt_inline static inline
#define RTT_API
#else
#error not supported tool chain
#endif
#ifdef RT_USING_COMPONENTS_INIT
typedef int (*init_fn_t)(void);
#ifdef _MSC_VER
#pragma section("rti_fn$f",read)
#if RT_DEBUG_INIT
struct rt_init_desc
{
const char* level;
const init_fn_t fn;
const char* fn_name;
};
#define INIT_EXPORT(fn, level) \
const char __rti_level_##fn[] = ".rti_fn." level; \
const char __rti_##fn##_name[] = #fn; \
__declspec(allocate("rti_fn$f")) \
RT_USED const struct rt_init_desc __rt_init_msc_##fn = \
{__rti_level_##fn, fn, __rti_##fn##_name};
#else
struct rt_init_desc
{
const char* level;
const init_fn_t fn;
};
#define INIT_EXPORT(fn, level) \
const char __rti_level_##fn[] = ".rti_fn." level; \
__declspec(allocate("rti_fn$f")) \
RT_USED const struct rt_init_desc __rt_init_msc_##fn = \
{__rti_level_##fn, fn };
#endif
#else
#if RT_DEBUG_INIT
struct rt_init_desc
{
const char* fn_name;
const init_fn_t fn;
};
#define INIT_EXPORT(fn, level) \
const char __rti_##fn##_name[] = #fn; \
RT_USED const struct rt_init_desc __rt_init_desc_##fn RT_SECTION(".rti_fn." level) = \
{ __rti_##fn##_name, fn};
#else
#define INIT_EXPORT(fn, level) \
RT_USED const init_fn_t __rt_init_##fn RT_SECTION(".rti_fn." level) = fn
#endif
#endif
#else
#define INIT_EXPORT(fn, level)
#endif
/* 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 initialization) */
#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")
/* application initialization (rtgui application etc ...) */
#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
//需要在外部函数调用,所有导入的函数将在这个函数中执行
void rt_components_init(void);
#endif
#include "componentsInit.h"
#ifdef RT_USING_COMPONENTS_INIT
/*
* 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");
/**
* @brief Onboard components initialization. In this function, the board-level
* initialization function will be called to complete the initialization
* of the on-board peripherals.
*/
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
volatile 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_DEBUG_INIT */
}
/**
* @brief 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
volatile 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 /* RT_DEBUG_INIT */
}
#endif /* RT_USING_COMPONENTS_INIT */

Comments | NOTHING