STM32 Rtthread启动流程基于GNU交叉编译
Rtthread并不是直接从main函数开始启动的,而是从componets.c中通过entry函数启动的:
//需要在board.c中开启这个宏RT_USING_USER_MAIN
//我使用的时GNU编译器,所以入口函数在这,可以在ld命令中指定entry为入口地址,
//也可以在startup_stm32f411xe.s中指定入口地址为entry
/* Add -eentry to arm-none-eabi-gcc argument */
int entry(void)
{
rtthread_startup();
return 0;
}
int rtthread_startup(void)
{
rt_hw_interrupt_disable();
/* board level initialization
* NOTE: please initialize heap inside board initialization.
*/
rt_hw_board_init();
/* show RT-Thread version */
rt_show_version();
/* timer system initialization */
rt_system_timer_init();
/* scheduler system initialization */
rt_system_scheduler_init();
/* create init_thread */
rt_application_init();
/* timer thread initialization */
rt_system_timer_thread_init();
/* idle thread initialization */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return 0;
}
//rt_hw_board_init函数在board.c中定义,主要功能是做硬件初始化
void rt_hw_board_init()
{
USART1_Config(115200);//串口初始化
KEY_Init();//按键初始化
LED_init();//LED初始化
/* System Clock Update */
//该函数在system_stm32f4xx.c中定义,主要功能是获取SystemCoreClock 的值。
SystemCoreClockUpdate();
/* System Tick Configuration */
_SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);//RT_TICK_PER_SECOND 1000
/* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
//rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP) //初始化堆
rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}
static uint32_t _SysTick_Config(rt_uint32_t ticks)
{
if ((ticks - 1) > 0xFFFFFF)
{
return 1;
}
_SYSTICK_LOAD = ticks - 1; //RELOAD VALUE
_SYSTICK_PRI = 0xFF; //PRIORITY OF SYSTICK LOW
_SYSTICK_VAL = 0; //CLEAR CURRENT VALUE OF SYSTICK COUNT
//systick ctrl寄存器bit2 为1时使用AHB作为systic频率,不分频
_SYSTICK_CTRL = 0x07; //0x07 BIT2 ==1 使用AHB,0:使用内部时钟源,AHB/8
return 0;
}
//rt_components_board_init函数会初始化INIT_EXPORT(fn, level) 宏包起来的函数
//可以看看这篇文章:https://blog.csdn.net/qq_42370291/article/details/103639349
void rt_components_board_init(void)
{
#if RT_DEBUG_INIT //开了RT_DEBUG_INIT 将会打印出被调用的初始化的函数
int result;
const struct rt_init_desc *desc;
for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
{
rt_kprintf("initialize %s", desc->fn_name);
result = desc->fn();
rt_kprintf(":%d done\n", result);
}
#else
volatile const init_fn_t *fn_ptr;
for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
{
(*fn_ptr)();
}
#endif
}
在startup_stm32f411xe.s中指定入口地址为entry
/* Call the application's entry point.*/
bl entry //这里之前是main
bx lr