下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

内核
最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

任务
[有关任务的更多信息……]

实现任务

任务结构应如下:
    void vATaskFunction( void *pvParameters )
    {
        for( ;; )
        {
            -- Task application code here. --
        }

        /* Tasks must not attempt to return from their implementing
        function or otherwise exit.  In newer FreeRTOS port
        attempting to do so will result in an configASSERT() being
        called if it is defined.  If it is necessary for a task to
        exit then have the task call vTaskDelete( NULL ) to ensure
        its exit is clean. */
        vTaskDelete( NULL );
    }
 
TaskFunction_t 类型是指返回 void 并将 void 指针作为其唯一参数的函数。所有实现任务的函数 都应该是这种类型的函数。该参数可用于将任何类型的信息传递到任务中, 如一些标准演示应用程序任务所示。

任务函数不应返回,因此通常实现为连续循环。但是,正如 RTOS 调度算法介绍页面所述, 通常最好创建事件驱动型任务,避免优先级较低的任务因缺少处理时间而饥饿,从而形成以下结构体:

    void vATaskFunction( void *pvParameters )
    {
        for( ;; )
        {
            /* Psudeo code showing a task waiting for an event 
            with a block time. If the event occurs, process it.  
            If the timeout expires before the event occurs, then 
            the system may be in an error state, so handle the
            error.  Here the pseudo code "WaitForEvent()" could 
            replaced with xQueueReceive(), ulTaskNotifyTake(), 
            xEventGroupWaitBits(), or any of the other FreeRTOS 
            communication and synchronisation primitives. */
            if( WaitForEvent( EventObject, TimeOut ) == pdPASS )
            {
                -- Handle event here. --
            }
            else
            {
                -- Clear errors, or take actions here. --
            }
        }

        /* As per the first code listing above. */
        vTaskDelete( NULL );
    }
 
再次提醒,如需查看更多示例,请参阅 RTOS 演示应用程序。

如需创建任务,请调用 xTaskCreate() 或 xTaskCreateStatic();如需删除任务,请调用 vTaskDelete()。

[返回顶部]



任务创建宏

可以使用 portTASK_FUNCTION portTASK_FUNCTION_PROTO 宏定义任务函数。 之所以提供这些宏, 是为了将编译器特定的语法分别添加到函数定义和原型中。 如果 所用端口(目前仅限 PIC18 fedC 端口)相关文档中未作具体说明,则无需使用这些宏。

上述函数的原型可以写为:

void vATaskFunction( void *pvParameters );
或者
portTASK_FUNCTION_PROTO( vATaskFunction, pvParameters );
同样,上述函数 可以相应写为:
    portTASK_FUNCTION( vATaskFunction, pvParameters )
    {
        for( ;; )
        {
            -- Task application code here. --
        }
    }
 



Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.