分析main函数
第一句是关中断;
第二句是启动RT-Thread操作系统;
第三句:返回,如果系统正常运行起来,永远不会执行到这里。
int main(void)
{
/* disable interrupt first */
rt_hw_interrupt_disable();
/* startup RT-Thread RTOS */
rtthread_startup();
return 0;
}
分析rtthread_startup();
- rt_hw_board_init();电路板上的硬件初始化
- rt_show_version();显示操作系统的版本信息
- rt_system_heap_init(); 系统堆初始化
- rt_system_scheduler_init();系统调度器初始化
- rt_system_timer_init(); 系统定时器初始化
- rt_system_timer_thread_init();系统定时器线程初始化
- rt_application_init();应用程序初始化
- finsh_system_init(); finsh系统初始化
- finsh_set_device(RT_CONSOLE_DEVICE_NAME); 设置finsh交互的串口设备
- rt_thread_idle_init(); 空线程初始化
- rt_system_scheduler_start(); 系统调度器初始化
用户主要关注的几个函数:
- rt_hw_board_init();电路板上的硬件初始化
- rt_application_init();应用程序初始化
/**
* This function will startup RT-Thread RTOS.
*/
void rtthread_startup(void)
{
/* init board */
rt_hw_board_init();
/* show version */
rt_show_version();
#ifdef RT_USING_HEAP
#if STM32_EXT_SRAM
rt_system_heap_init((void*)STM32_EXT_SRAM_BEGIN, (void*)STM32_EXT_SRAM_END);
#else
#ifdef __CC_ARM
rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)STM32_SRAM_END);
#elif __ICCARM__
rt_system_heap_init(__segment_end("HEAP"), (void*)STM32_SRAM_END);
#else
/* init memory system */
rt_system_heap_init((void*)&__bss_end, (void*)STM32_SRAM_END);
#endif
#endif /* STM32_EXT_SRAM */
#endif /* RT_USING_HEAP */
/* init scheduler system */
rt_system_scheduler_init();
/* initialize timer */
rt_system_timer_init();
/* init timer thread */
rt_system_timer_thread_init();
/* init application */
rt_application_init();
#ifdef RT_USING_FINSH
finsh_system_init();
finsh_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
/* init idle thread */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return ;
}
rt_hw_board_init();电路板上的硬件初始化
- HAL_Init();
- SystemClock_Config();系统时钟配置
- rt_hw_usart_init();串口初始化
- rt_hw_pin_init(); I/O口初始化
- rt_console_set_device(RT_CONSOLE_DEVICE_NAME);控制台设备指定
/**
* This function will initial STM32 board.
*/
void rt_hw_board_init(void)
{
HAL_Init();
SystemClock_Config();
#ifdef RT_USING_SERIAL
rt_hw_usart_init();
#endif
#ifdef RT_USING_PIN
rt_hw_pin_init();
#endif
#ifdef RT_USING_CONSOLE
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
}
rt_application_init(void) 用户应用程序初始化函数
- init_thread = rt_thread_create();动态创建线程
- rt_thread_startup(init_thread);线程从初始态转就绪态
int rt_application_init(void)
{
rt_thread_t init_thread;
// rt_err_t result;
#if (RT_THREAD_PRIORITY_MAX == 32)
init_thread = rt_thread_create("init",
rt_init_thread_entry, RT_NULL,
1024, 8, 20);
#else
init_thread = rt_thread_create("init",
rt_init_thread_entry, RT_NULL,
1024, 80, 20);
#endif
if (init_thread != RT_NULL)
rt_thread_startup(init_thread);
return 0;
}
网友评论