|
本帖最后由 chinacool 于 2020-6-20 22:47 编辑
threadX 通过在main()中调用tx_kernel_enter()宏启动内核,注意此函数没有返回,因此要放在main()的最后面。
在系统函数_tx_initialize_kernel_enter()中调用 tx_application_define函数,而这个函数又由用户定义,因此可以放到main.c中:
/* Call the application provided initialization function. Pass the
first available memory address to it. */
tx_application_define(_tx_initialize_unused_memory);
在 tx_kernel_enter 到_tx_initialize_kernel_enter再到tx_application_define 之间,要经过一个map API 的操作,主要是控制是否进行错误检测。
预编译控制结构如下:
/* Define the system API mappings based on the error checking
selected by the user. Note: this section is only applicable to
application source code, hence the conditional that turns off this
stuff when the include file is processed by the ThreadX source. */
#ifndef TX_SOURCE_CODE
/* Determine if error checking is desired. If so, map API functions
to the appropriate error checking front-ends. Otherwise, map API
functions to the core functions that actually perform the work.
Note: error checking is enabled by default. */
#ifdef TX_DISABLE_ERROR_CHECKING
/* Services without error checking. */
#define tx_kernel_enter _tx_initialize_kernel_enter
#define tx_block_allocate _tx_block_allocate
。。。。此处省略部分代码
#else // -->TX_DISABLE_ERROR_CHECKING
/* Services with error checking. */
#define tx_kernel_enter _tx_initialize_kernel_enter
/* Define the system API mappings depending on the runtime error
checking behavior selected by the user. */
#ifdef TX_ENABLE_MULTI_ERROR_CHECKING
/* Services with MULTI runtime error checking ThreadX. */
#define tx_block_allocate _txr_block_allocate
。。。。此处省略部分代码
#else //-->TX_ENABLE_MULTI_ERROR_CHECKING
#define tx_block_allocate _txe_block_allocate
。。。。此处省略部分代码
#endif //-->TX_ENABLE_MULTI_ERROR_CHECKING
#endif //-->TX_DISABLE_ERROR_CHECKING
#endif //-->TX_SOURCE_CODE
由上面的结构可以知道分为三个层次:
1: 启用/不启用API映射代码。这要根据是否是应用程序代码,如果是OS代码则要通过定义TX_SOURCE_CODE宏关掉这部分,因此在系统代码文件中开头处总是能看到#define TX_SOURCE_CODE 这么一句代码,而在应用程序中则不加这句代码。注意看注释。
2. 当启用API map时,通过TX_DISABLE_ERROR_CHECKING 控制是否需要进行错误检查。
3. 当需要进行错误检测时,通过TX_ENABLE_MULTI_ERROR_CHECKING 控制是否需要进行运行时错误检查(runtime error checking)。
要注意的是运行时错误检查对应 的函数只有声明而没有定义。因此默认的映射函数就是进行错误检查的那部分函数(_txe_开头的)。这种套路只在threadX中看到,别的OS中没见过,每个OS都有自己的特点,像RTThread 中在启动程序执行后,程序跳到
os 自定义的$$main$$接口,将部分代码执行完后,再进入到main函数。
threadX os 还有个特点是一个API一个文件 ,文件很多,比较分散。命名方面比较讲究,单词是名词还是动词都考究,单词都是全拼而不是缩写。还有代码几乎每行都有注释,这个太好了。
继续研究。。。。
|
|