关于RT-Thread线程未启动的问题分析
RT-Thread线程未启动问题分析
发现问题
最近在学习RT-Thread相关的知识,运行创建线程的例程时发现程序没有跑起来。通过Debug发现,开启线程调度后,线程并没有成功的调度。main函数直接返回了。
int main(void)
{
led1_thread =
rt_thread_create( "led1",
led1_thread_entry,
RT_NULL,
512,
3,
20);
if (led1_thread != RT_NULL)
rt_thread_startup(led1_thread);
else
return -1;
}
分析问题
研究了一下RT-Thread启动流程,发现是先执行了rtthread_startup();再进入的main函数。
找到相应的代码如下。我是用的编译工具是keil5.38,按道理应该会进入到$Sub$$main()函数中,实际Debug发现并没有。而且使用__CC_ARM宏修饰的地方是灰色的,__GNUC__修饰的地方是高亮的。
#if defined (__CC_ARM)
extern int $Super$$main(void);
/* re-define main function */
int $Sub$$main(void)
{
rt_hw_interrupt_disable();
rtthread_startup();
return 0;
}
#elif defined(__ICCARM__)
extern int main(void);
/* __low_level_init will auto called by IAR cstartup */
extern void __iar_data_init3(void);
int __low_level_init(void)
{
// call IAR table copy function.
__iar_data_init3();
rt_hw_interrupt_disable();
rtthread_startup();
return 0;
}
#elif defined(__GNUC__)
extern int main(void);
/* Add -eentry to arm-none-eabi-gcc argument */
int entry(void)
{
rt_hw_interrupt_disable();
rtthread_startup();
return 0;
}
#endif
查找资料
查找了__CC_ARM相关的资料发现,在arm compiler 5中是有效的。但是arm compiler 6中是无效的。由于我安装的keil是5.38的版本,其中使用的是arm compiler 6。导致了 __CC_ARM失效的问题。
宏__CC_ARM 是MDK自带的, 在AC5中是__CC_ARM ,AC6中是__clang__。如下
AC5:
#if defined ( __ICCARM__ )
#elif defined ( __CC_ARM )
#endif
AC6 :
#if defined ( __ICCARM__ )
#pragma system_include /* treat file as system include file for MISRA check */
#elif defined (__clang__)
#pragma clang system_header /* treat file as system include file */
#endif
解决问题
两个思路吧,一个是用回原来的 arm compiler 5。一个是解决这个__CC_ARM失效的问题