FreeRTOS 任务创建和删除(静态)

#define configSUPPORT_STATIC_ALLOCATION 1   //打开静态方法

StackType_t TaskStackBuffer[50];    //任务堆栈大小
StaticTask_t TaskTCBBuffer;         //任务控制块大小

StackType_t TaskStackBuffer0[50];
StaticTask_t TaskTCBBuffer0;

StackType_t TaskStackBuffer1[50];
StaticTask_t TaskTCBBuffer1;

TaskHandle_t taskhandle0;
TaskHandle_t taskhandle1;

StaticTask_t pxIdleTaskTCBBuffer;
StackType_t pxIdleTaskStackBuffer[configMINIMAL_STACK_SIZE];

//需要用户添加定义
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, 
                                    StackType_t **ppxIdleTaskStackBuffer, 
                                    uint32_t *pulIdleTaskStackSize )
{
    *ppxIdleTaskTCBBuffer = &pxIdleTaskTCBBuffer;
    *ppxIdleTaskStackBuffer = pxIdleTaskStackBuffer;
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

void vTask0(void *t)
{
    int i = 0;

    while(1)
    {
        i++;
        if(i == 5)
        {
            vTaskDelete(taskhandle1);
            printf("vTask0: delete vTask1.\r\n");
        }
        printf("vTask: 0.\r\n");
        vTaskDelay(1000);
    }
}

void vTask1(void *t)
{
    while(1)
    {
        printf("vTask: 1.\r\n");
        vTaskDelay(1000);
    }
}

void start_vTask(void *t)
{
    taskhandle0 = xTaskCreateStatic( vTask0,
                                    "vTask0",
                                    50,
                                    ( void * ) NULL,
                                    2,
                                    (StackType_t *const)TaskStackBuffer0,
                                    (StaticTask_t *const)&TaskTCBBuffer0 );

    taskhandle1 = xTaskCreateStatic( vTask1,
                                    "vTask1",
                                    50,
                                    ( void * ) NULL,
                                    3,
                                    (StackType_t *const)TaskStackBuffer1,
                                    (StaticTask_t *const)&TaskTCBBuffer1 );
    vTaskDelete(NULL);
}

int main(void)
{
    xTaskCreateStatic( start_vTask,
                        "start_vTask",
                        50,
                        ( void * ) NULL,
                        1,
                        (StackType_t *const)TaskStackBuffer,
                        (StaticTask_t *const)&TaskTCBBuffer );

    vTaskStartScheduler(); 
}

运行结果:
1

posted @ 2017-10-24 22:25  thomas_blog  阅读(652)  评论(0编辑  收藏  举报