硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4123|回复: 1
收起左侧

[ThreadX全家桶] threadX 的api map操作

[复制链接]

27

主题

95

回帖

181

积分

初级会员

积分
181
发表于 2020-6-20 22:41:36 | 显示全部楼层 |阅读模式
本帖最后由 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一个文件 ,文件很多,比较分散。命名方面比较讲究,单词是名词还是动词都考究,单词都是全拼而不是缩写。还有代码几乎每行都有注释,这个太好了。

继续研究。。。。



回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115787
QQ
发表于 2020-6-21 00:20:08 | 显示全部楼层
谢谢楼主分享。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-5-19 02:55 , Processed in 0.212652 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表