FreeRTOS的源代码个人分析(基于KEIL下STM32F103的Demo) 四

本文主要分析FreeRTOS中xPortStartScheduler()函数的实现,该函数在main函数调用vTaskStartScheduler后开始任务调度。首先创建低优先级的空闲任务IdleTask,接着设置系统时钟和调度器状态。xPortStartScheduler()函数设置中断优先级,初始化计时器,并启动第一个任务,使得FreeRTOS系统开始运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始任务的实现分析: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值