ESP32 FreeRTOS-task input parameters (2)

Tip: A good memory is not as good as a bad pen. This blog is used as a study note, if there is a mistake, I hope to correct it

ESP32-FreeRTOS serial:

ESP32 FreeRTOS-Task Creation and Deletion(1)
ESP32 FreeRTOS-Task Input Parameters(2)
ESP32 FreeRTOS-Task Priority(3)
ESP32 FreeRTOS-Debugging Task Utility(4)

Foreword:

  Reference materials: In the first part of the FreeRTOS API reference
  , we introduced the creation of tasks. Careful friends will find that there are many parameters for creating tasks. Here I will start to learn and explain the task input parameters.
xTaskCreate API prototype:

BaseType_t xTaskCreate(    TaskFunction_t pvTaskCode,
                            const char * const pcName,
                            configSTACK_DEPTH_TYPE usStackDepth,
                            void *pvParameters,
                            UBaseType_t uxPriority,
                            TaskHandle_t *pxCreatedTask
                          );

  When creating a task, the above void *pvParameters is the input parameter of the task. You can see the void type pointer parameter of the type of our task input parameter, which means that we can pass in any type of data.
  The application scenario can be that after receiving a certain signal, some complex processing needs to be performed on the signal value, and then you can use this method if you do nothing after processing, receive the data, create a task, and then pass in the parameters to perform the value Operations, use queues to send data, and delete tasks. This kind of signal does not appear often, but it must be dealt with when it occurs.

1. Analysis of task incoming parameters

  The parameters are passed in here. I mainly introduce 4 data types here. They are integer data, integer array, string, and structure. Test experiments with different data can be realized through macro definitions. If the incoming parameter of the data is not a pointer, the address needs to be passed in, and then the address is converted into a (void *) null pointer data type, because the parameter for creating the task is of the void pointer type, which needs to be converted here, which is a pointer data type Just pass the data directly into the parameters, such as the following strings and arrays, if the array is directly passed into the array variable, it will be the first address of the array variable. In addition, it is necessary to perform data analysis on the received data, what type of data is the sent data, and then it needs to be converted into the corresponding data type when receiving.

/**
 * @file 2_TaskParameter.c
 * @author WSP
 * @brief 
 * @version 0.1
 * @date 2022-10-05
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "System_Include.h"

const static char * TAG = "Task_Parameter";
//  APP_task 任务的一些变量
TaskHandle_t APP_task_handle = NULL;;

#define PARAMETER_VALUE_STRUCT 1

#if PARAMETER_VALUE_NUMBER
    int Parameter_Value = 168;
#elif PARAMETER_VALUE_ARRAY
    int Parameter_Value[3] = {
    
    168,168,99};
#elif PARAMETER_VALUE_STR
    char * Parameter_Value = "My name is WSP";
#elif PARAMETER_VALUE_STRUCT
    typedef struct Parameter{
    
    
        int Value;
        char * str;
    }Parameter_Value_t;
    Parameter_Value_t Parameter_Value = {
    
    168,"My name is WSP"};
#endif
/**
 * @brief APP_task
 * @param arg 任务传入的参数
 * @return NULL
 */
void APP_task(void * arg)
{
    
    
#if PARAMETER_VALUE_NUMBER
    int Get_Parameter_Value = *(int *)arg;
    ESP_LOGI(TAG,"Get_Parameter_number:%d",Get_Parameter_Value);
#elif PARAMETER_VALUE_ARRAY
    int * Get_Parameter_Value = (int *)arg;
    ESP_LOGI(TAG,"Get_Parameter_array:%d %d %d",*Get_Parameter_Value, *(Get_Parameter_Value + 1), *(Get_Parameter_Value + 2));
#elif PARAMETER_VALUE_STR
    char * Get_Parameter_Value = (char *)arg;
    ESP_LOGI(TAG,"Get_Parameter_str:%s",Get_Parameter_Value);
#elif PARAMETER_VALUE_STRUCT
    Parameter_Value_t * Get_Parameter_Value = (Parameter_Value_t *)arg;
    ESP_LOGI(TAG,"Get_Parameter_Struct value:%d str:%s",Get_Parameter_Value->Value,Get_Parameter_Value->str);
#endif
    while (1){
    
    
        vTaskDelay(1000/portTICK_PERIOD_MS);
        ESP_LOGI(TAG,"APP_task");
    }
}
/**
 * @brief   创建函数初始化
 * @param   NULL
 * @return  NULL
 */
void Parameter_Task_Init(void)
{
    
    
    // 使用 freeREOS的标准API创建任务
    xTaskCreate(APP_task,                   // 创建任务
                "APP_task",                 // 创建任务名
                2048,                       // 任务堆栈
            #if PARAMETER_VALUE_NUMBER    | PARAMETER_VALUE_STRUCT  //传入数字或者结构体
                (void *)&Parameter_Value,   // 传入任务参数
            #elif PARAMETER_VALUE_ARRAY   | PARAMETER_VALUE_STR     //传入数组或者字符串
                (void *)Parameter_Value,    // 传入任务参数
            #endif
                2,                          // 任务优先级  
                &APP_task_handle);          // 任务句柄
}

Guess you like

Origin blog.csdn.net/believe666/article/details/127189240