1. 程式人生 > >STM32F103ZE FreeRTOS任務建立與刪除

STM32F103ZE FreeRTOS任務建立與刪除

一、前期準備
微控制器:STM32F103ZET6
開發環境:MDK5.14
庫函式:標準庫V3.5
FreeRTOS:V9.0.0(網上下載,官網上目前是V10.0.1)

二、實驗效果
總共6個任務,串列埠1、4個RGB小燈以不同頻率閃爍、一個刪除任務,並通過串列埠2列印除錯資訊。刪除任務每秒進行一次自加。當加到3的時候刪除第一個小燈的任務,以此類推直到第四個小燈任務刪除,之後再自加3次刪除自身。這時候只有串列埠1任務在工作。
這裡寫圖片描述
這裡寫圖片描述

三、程式講解
任務建立函式:此為動態記憶體分配,由FreeRTOS的heap4.c分配
BaseType_t xTaskCreate(
TaskFunction_t pxTaskCode, /任務函式
const char * const pcName, //任務名稱
const uint16_t usStackDepth, //任務堆疊大小
void *const pvParameters, //傳遞給任務函式的引數
UBaseType_t uxPriority, //任務優先順序
TaskHandle_t * const pxCreateTask //任務控制代碼,刪除任務也是刪除任務控制代碼
);

任務刪除函式:
vTaskDelete(
TaskHandle_t xTaskToDelete //任務控制代碼
);

四、測試程式碼
完整工程請加QQ:1002521871,驗證:呵呵 。只附上與FreeRTOS相關的程式碼,其餘串列埠,LED初始化請參考STM32F103ZE FreeRTOS移植、測試
rtos_app.h

#ifndef __RTOS_APP_H__
#define __RTOS_APP_H__
#include "conf.h"
#include "FreeRTOS.h"
#include "task.h"

#define     START_TASK_PRIO         1
#define START_STK_SIZE 128 extern TaskHandle_t STARTTask_Handler; extern void START_task(void *pvParameters); #define LED0_TASK_PRIO 3 #define LED0_STK_SIZE 50 #define LED1_TASK_PRIO 4 #define LED1_STK_SIZE 50 #define LED2_TASK_PRIO 5
#define LED2_STK_SIZE 50 #define LED3_TASK_PRIO 6 #define LED3_STK_SIZE 50 #define UART1_TASK_PRIO 7 #define UART1_STK_SIZE 50 #define UART2_TASK_PRIO 7 #define UART2_STK_SIZE 50 #define Mess_TASK_PRIO 2 #define Mess_STK_SIZE 50 #define TEST_TASK_RPIO 7 #define TEST_STK_SIZE 128 #endif

rtos_app.c

#include "rtos_app.h"

TaskHandle_t    STARTTask_Handler;
TaskHandle_t    LED0Task_Handler;
TaskHandle_t    LED1Task_Handler;
TaskHandle_t    LED2Task_Handler;
TaskHandle_t    LED3Task_Handler;
TaskHandle_t    UART1Task_Handler;
TaskHandle_t    UART2Task_Handler;
TaskHandle_t    MessTask_Handler;
TaskHandle_t    TESTTask_Handler;

void LED0_task(void *pvParameters)
{
    while (1)
    {
        RGB_LED1 = ON;
        vTaskDelay(200 / portTICK_RATE_MS);
        RGB_LED1 = OFF;
        vTaskDelay(800 / portTICK_RATE_MS);
    }
}

void LED1_task(void *pvParameters)
{
    while (1)
    {
        RGB_LED2 = ON;
        vTaskDelay(300 / portTICK_RATE_MS);
        RGB_LED2 = OFF;
        vTaskDelay(700 / portTICK_RATE_MS);
    }
}

void LED2_task(void *pvParameters)
{
    while (1)
    {
        RGB_LED3 = ON;
        vTaskDelay(400 / portTICK_RATE_MS);
        RGB_LED3 = OFF;
        vTaskDelay(600 / portTICK_RATE_MS);
    }
}

void LED3_task(void *pvParameters)
{
    while (1)
    {
        RGB_LED4 = ON;
        vTaskDelay(500 / portTICK_RATE_MS);
        RGB_LED4 = OFF;
        vTaskDelay(500 / portTICK_RATE_MS);
    }
}

void UART1_task(void *pvParameters)
{
    int i = 0;
    while (1)
    {
        taskENTER_CRITICAL();
        USART_PRINTF_FLAG = 1;
        printf("UART1-%05d: FreeRTOS Test!\r\n", i);
        i ++;
        taskEXIT_CRITICAL();
        vTaskDelay(1000 / portTICK_RATE_MS);
    }
}

void UART2_task(void *pvParameters)
{
    int i = 0;
    while (1)
    {
        taskENTER_CRITICAL();
        USART_PRINTF_FLAG = 2;
        printf("UART2-%05d: FreeRTOS Test!\r\n", i);
        i ++;
        taskEXIT_CRITICAL();
        vTaskDelay(1000 / portTICK_RATE_MS);    
    }
}

void Mess_task(void *pvParameters)
{
    while (1)
    {
        //taskENTER_CRITICAL();
        if (cmdDealFlag == 1)
        {           
            DealWithUARTMess(RecData);
            cmdDealFlag = 0;
        }
        //taskEXIT_CRITICAL();
    }
}

void TEST_task(void *pvParameters)
{
    uint8_t times = 0;
    while(1)
    {

        if (times == 3)
        {
            vTaskDelete(LED0Task_Handler);
            taskENTER_CRITICAL();
            USART_PRINTF_FLAG = 2;
            printf("Task_LED0 has already deleted!\r\n");
            taskEXIT_CRITICAL();
        }
        else if (times == 6)
        {
            vTaskDelete(LED1Task_Handler);
            taskENTER_CRITICAL();
            USART_PRINTF_FLAG = 2;
            printf("Task_LED1 has already deleted!\r\n");
            taskEXIT_CRITICAL();
        }
        else if (times == 9)
        {
            vTaskDelete(LED2Task_Handler);
            taskENTER_CRITICAL();
            USART_PRINTF_FLAG = 2;
            printf("Task_LED2 has already deleted!\r\n");
            taskEXIT_CRITICAL();
        }
        else if (times ==  12)
        {
            vTaskDelete(LED3Task_Handler);
            taskENTER_CRITICAL();
            USART_PRINTF_FLAG = 2;
            printf("Task_LED3 has already deleted!\r\n");
            taskEXIT_CRITICAL();
        }
        else if (times == 15)
        {
            taskENTER_CRITICAL();
            USART_PRINTF_FLAG = 2;
            printf("Test task has already deleted!\r\n");
            taskEXIT_CRITICAL();
            vTaskDelete(TESTTask_Handler);
            taskEXIT_CRITICAL();
        }
        taskENTER_CRITICAL();
        USART_PRINTF_FLAG = 2;
        printf("Times = %d\r\n", times);
        taskEXIT_CRITICAL();
        times ++;
        vTaskDelay(1000 / portTICK_RATE_MS);    
    }
    //vTaskDelete(NULL);
}

void START_task(void *pvParameters)
{
    taskENTER_CRITICAL();
    xTaskCreate(
        (TaskFunction_t) TEST_task,
        (const char *) "TEST_task",
        (uint16_t) TEST_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) TEST_TASK_RPIO,
        (TaskHandle_t *) &TESTTask_Handler
    );

    xTaskCreate(
        (TaskFunction_t) LED0_task,
        (const char *) "LED0_task",
        (uint16_t) LED0_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) LED0_TASK_PRIO,
        (TaskHandle_t *) &LED0Task_Handler
    );

    xTaskCreate(
        (TaskFunction_t) LED1_task,
        (const char *) "LED1_task",
        (uint16_t) LED1_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) LED1_TASK_PRIO,
        (TaskHandle_t *) &LED1Task_Handler
    );

    xTaskCreate(
        (TaskFunction_t) LED2_task,
        (const char *) "LED2_task",
        (uint16_t) LED2_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) LED2_TASK_PRIO,
        (TaskHandle_t *) &LED2Task_Handler
    );

    xTaskCreate(
        (TaskFunction_t) LED3_task,
        (const char *) "LED3_task",
        (uint16_t) LED3_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) LED3_TASK_PRIO,
        (TaskHandle_t *) &LED3Task_Handler
    );

    xTaskCreate(
        (TaskFunction_t) UART1_task,
        (const char *) "UART1_task",
        (uint16_t) UART1_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) UART1_TASK_PRIO,
        (TaskHandle_t *) &UART1Task_Handler
    );
//      
//  xTaskCreate(
//      (TaskFunction_t) UART2_task,
//      (const char *) "UART2_task",
//      (uint16_t) UART2_STK_SIZE,
//      (void *) NULL,
//      (UBaseType_t) UART2_TASK_PRIO,
//      (TaskHandle_t *) &UART2Task_Handler
//  );

    xTaskCreate(
        (TaskFunction_t) Mess_task,
        (const char *) "Mess_task",
        (uint16_t) Mess_STK_SIZE,
        (void *) NULL,
        (UBaseType_t) Mess_TASK_PRIO,
        (TaskHandle_t *) &MessTask_Handler
    );

    vTaskDelete(STARTTask_Handler);
    taskEXIT_CRITICAL();
}

由於作者能力有限,有不妥之處歡迎指正,郵箱[email protected]