0%

eCos 组件初始化

eCos 组件注册及初始化

eCos 组件初始化机制

eCos 组件初始化利用了 C++ 静态对象实例初始化的机制

C++ 对象在初始化时将会调用该对象类的构造函数来初始化对象

  • 对象在函数内声明,那么函数执行到对象的声明处调用类构造函数来初始化对象
  • 对象在函数外声明,也就是将对象声明为全局对象或静态对象,那么编译器会将该对象的初始化代码地址(函数指针)写到名为 .ctors 节,普通程序在进入 main 函数前由操作系统的运行时库调用存储在 .ctors 中的函数指针列表来初始化全局或静态对象

eCos HAL将调用 cyg_hal_invoke_constructors 函数,该函数调用存储在 .ctors 节中的初始化代码

组件初始化

IO 组件

externC void cyg_io_init(void);

class cyg_io_init_class {
public:
    cyg_io_init_class(void) {
        cyg_io_init();
    }
};

// And here's an instance of the class just to make the code run
static cyg_io_init_class _cyg_io_init CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO);

初始化顺序

初始化过程需要按照一定的次序进行。次序由 CYGBLD_ATTRIB_INIT_PRI 宏指定,该宏带有一个参数,该参数数值越小,那么越早调用该实例的初始化代码

例如 CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO) 展开为 __attribute__((init_priority(49000)))

组件注册

组件定义

#define MOD_IRR         cyg_irr_init_class        irr0       CYGBLD_ATTRIB_INIT_PRI( 60100 );

检查初始化代码

符号前缀 _GLOBAL__sub_I 表示这是静态对象初始化代码,符号中间部分的数字决定初始化次序

$ nm src/out.elf | grep "t _GLOBAL"
90039370 t _GLOBAL__D.11000_cyg_scheduler_sched_lock
900389a8 t _GLOBAL__D.12000__ZN13Cyg_Interrupt15disable_counterE
90070830 t _GLOBAL__D.14000__ZN9Cyg_Clock15real_time_clockE
90038704 t _GLOBAL__D.24100__ZN18Cyg_HardwareThread12thread_entryEP10Cyg_Thread
904382d8 t _GLOBAL__D.50000_cyg_mtab_extra
90045898 t _GLOBAL__D.50000_cyg_nstab_init
90044c60 t _GLOBAL__D.50000_cyg_timeval_to_ticks
900722b0 t _GLOBAL__D.50000_fdlock
90071cc0 t _GLOBAL__D.56000_cyg_libc_stdio_stdin
9003c6b0 t _GLOBAL__D.56000_strtok
9003ccd8 t _GLOBAL__D.56000__ZN20Cyg_libc_stdio_files5filesE
9003d1b8 t _GLOBAL__D.56001_cyg_libc_stdio_stdout
90071ba0 t _GLOBAL__D.56002_cyg_libc_stdio_stderr
9006b878 t _GLOBAL__D.58000_cyg_timespec_to_ticks
9006b5f4 t _GLOBAL__D.58000_pthread_mutex
9006bf64 t _GLOBAL__D.58000_signal_mutex
90039358 t _GLOBAL__I.11000_cyg_scheduler_sched_lock
90038738 t _GLOBAL__I.12000__Z26cyg_null_exception_handlerjij
90038998 t _GLOBAL__I.12000__ZN13Cyg_Interrupt15disable_counterE
90070818 t _GLOBAL__I.14000__ZN9Cyg_Clock15real_time_clockE
900386ec t _GLOBAL__I.24100__ZN18Cyg_HardwareThread12thread_entryEP10Cyg_Thread
9006c110 t _GLOBAL__I.32000__ZN12cyg_i2c_initC2Ev
90042c38 t _GLOBAL__I.36000__ZN13Cyg_WallClock9wallclockE
903e18ec t _GLOBAL__I.48000_char_dev_register
90436564 t _GLOBAL__I.49000_ioinit.cxx
904382c0 t _GLOBAL__I.50000_cyg_mtab_extra
90045880 t _GLOBAL__I.50000_cyg_nstab_init
90044c48 t _GLOBAL__I.50000_cyg_timeval_to_ticks
90072298 t _GLOBAL__I.50000_fdlock
90071ca8 t _GLOBAL__I.56000_cyg_libc_stdio_stdin
9003c698 t _GLOBAL__I.56000_strtok
9003ccc0 t _GLOBAL__I.56000__ZN20Cyg_libc_stdio_files5filesE
9003d1a0 t _GLOBAL__I.56001_cyg_libc_stdio_stdout
90071b88 t _GLOBAL__I.56002_cyg_libc_stdio_stderr
9006b860 t _GLOBAL__I.58000_cyg_timespec_to_ticks
9006b5dc t _GLOBAL__I.58000_pthread_mutex
9006bf4c t _GLOBAL__I.58000_signal_mutex
903e1100 t _GLOBAL__I.58000_startup.cxx
904554ac t _GLOBAL_OFFSET_TABLE_