FreeRTOS10例程学习(一)

官网下载source-code-for-book-examples压缩包,解压后得到VS工程,打开source-code-for-book-examples\Win32-simulator-MSVC\RTOSDemo.sln后有Examples001-025。

Examples001,任务创建

包含头文件

/* FreeRTOS.org includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Demo includes. */
#include "supporting_functions.h"

supporting_functions.c包含了支持所有示例所使用的一些函数,一共有三类:

/*
* 1) IO functions:  vPrintString() and vPrintStringAndNumber().
* 2) RTOS hook functions: vApplicationMallocFailedHook(), vApplicationIdleHook()
* vApplicationIdleHook(), vApplicationStackOverflowHook() and
* vApplicationTickHook().
* 3) configASSERT() implementation: vAssertCalled()
*/

1)IO函数,为了允许最大的可移植性,例程不依赖于任何芯片的IO口,将其输出到控制台。然而直接输出到控制台是线程不安全的,所以将所有输出封装到函数中。
2)这些函数可以由用户进行定义,以获得执行中应用发生事件的通知。
3)FreeRTOS源代码使用assert()函数来捕获用户和其他错误。configASSERT()在FreeRTOSConfig.h中定义,调用vAssertCalled(),它是在这个.c文件中实现的。

在main.c中,预定义了一个变量

#define mainDELAY_LOOP_COUNT (0xffffff)

该变量作为一个粗糙的延时,用于任务运行时的睡眠。
定义了两个任务函数:

void vTask1( void *pvParameters );
void vTask2( void *pvParameters );

void vTask1( void *pvParameters )
{
const char *pcTaskName = "Task 1 is running\r\n";
volatile uint32_t ul;

    /* As per most tasks, this task is implemented in an infinite loop. */
    for( ;; )
    {
        /* Print out the name of this task. */
        vPrintString( pcTaskName );

        /* Delay for a period. */
        for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
        {
            /* This loop is just a very crude delay implementation.  There is
            nothing to do in here.  Later exercises will replace this crude
            loop with a proper delay/sleep function. */
        }
    }
}

void vTask2( void *pvParameters )
{
const char *pcTaskName = "Task 2 is running\r\n";
volatile uint32_t ul;

    for( ;; )
    {
        vPrintString( pcTaskName );

        for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
        {

        }
    }
}

main():

int main( void )
{
    /* Create one of the two tasks. */
    xTaskCreate(    vTask1,     /* Pointer to the function that implements the task. */
                    "Task 1",   /* Text name for the task.  This is to facilitate debugging only. */
                    1000,       /* Stack depth - most small microcontrollers will use much less stack than this. */
                    NULL,       /* We are not using the task parameter. */
                    1,          /* This task will run at priority 1. */
                    NULL );     /* We are not using the task handle. */

    /* Create the other task in exactly the same way. */
    xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL );

    /* Start the scheduler to start the tasks executing. */
    vTaskStartScheduler();  

    /* The following line should never be reached because vTaskStartScheduler() 
    will only return if there was not enough FreeRTOS heap memory available to
    create the Idle and (if configured) Timer tasks.  Heap management, and
    techniques for trapping heap exhaustion, are described in the book text. */
    for( ;; );
    return 0;
}

创建两个任务后运行vTaskStartScheduler()启动任务启动调度器,此时两个任务开始运行。

你可能感兴趣的:(FreeRTOS)