开始任务的实现分析:xPortStartScheduler()函数
FreeRTOS里开始任务是在main里调用vTaskStartScheduler函数来开始任务的,在调用这个函数后,系统会先自动的创建一个优先级最低(也就是0优先级)的空闲任务IdleTask,这个任务的作用是在所有用户的任务都被挂起,也就是当前没有用户所建立的任务在运行时,系统就会运行这个IdleTask。(但如果有用户任务的优先级也是0的话,那么用户任务会和IdleTask任务分时运行,所以一般设置任务优先级至少为1)。然后如果在config里设置了定时器,那么也会建立一个定时器的任务,定时器任务的优先级默认是最高优先级。
在完成这些准备工作之后,FreeRTOS就会调用xPortStartScheduler()函数来开启任务运行。
#else
{
/* The Idle task is being created using dynamically allocated RAM. */
xReturn = xTaskCreate( prvIdleTask,
"IDLE", configMINIMAL_STACK_SIZE,
( void * ) NULL,
( tskIDLE_PRIORITY | portPRIVILEGE_BIT ),
&xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
}
#endif /* configSUPPORT_STATIC_ALLOCATION */
#if ( configUSE_TIMERS == 1 )
{
if( xReturn == pdPASS )
{
xReturn = xTimerCreateTimerTask();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* configUSE_TIMERS */
if( xReturn == pdPASS )
{
/* Interrupts are turned off here, to ensure a tick does not occur
before or during the call to xPortStartScheduler(). The stacks of
the created tasks contain a status word with interrupts switched on
so interrupts will automatically get re-enabled when the first task
starts to run. */
portDISABLE_INTERRUPTS();
#if ( con