1. Introduction to the xTaskCreate Task Creation Function
typedef long BaseType_t; BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName, configSTACK_DEPTH_TYPE usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask );
Function: Creates a new task and adds it to the list of tasks that are ready to run. Each task requires RAM to store its state and is used by the task as its stack. If a task is created using xTaskCreate(), the required RAM will be automatically allocated from the FreeRTOS heap.
Parameters:
- pvTaskCode: Function pointer to the C function corresponding to the task, of type TaskFunction_t, defined as
typedef void (*TaskFunction_t)( void *). - pcName: The name of the task, used only for debugging purposes and not utilized internally by FreeRTOS. The length of pcName is limited to
configMAX_TASK_NAME_LEN.#define configMAX_TASK_NAME_LEN ( 16 ) - usStackDepth: Each task has its own stack, and usStackDepth specifies the size of the stack in words. For example, if 100 is passed, the stack size is 100 words, which equals 400 bytes. The maximum value is the maximum value of uint16_t. Determining the stack size is not easy and is usually set based on estimates.
- pvParameters: The parameter used when calling the pvTaskCode function pointer:
pvTaskCode(pvParameters). - uxPriority: The priority of the task ranges from 0 to (configMAX_PRIORITIES – 1). The smaller the value, the lower the priority. If a value that is too large is passed, xTaskCreate will adjust it to (configMAX_PRIORITIES – 1).
#define configMAX_PRIORITIES ( 56 ) - pxCreatedTask: Used to store the output result of xTaskCreate, which is the task handle. If you need to perform operations on this task later, such as modifying its priority, you will need to use this handle. If you do not need to use this handle, you can pass NULL.
Return Value:
Returns pdPASS on success, or errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY on failure (the reason for failure is insufficient memory).
#define pdFALSE ( ( BaseType_t ) 0 )#define pdTRUE ( ( BaseType_t ) 1 )#define pdPASS ( pdTRUE )#define pdFAIL ( pdFALSE )#define errQUEUE_EMPTY ( ( BaseType_t ) 0 )#define errQUEUE_FULL ( ( BaseType_t ) 0 )/* FreeRTOS error definitions. */#define errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY ( -1 )#define errQUEUE_BLOCKED ( -4 )#define errQUEUE_YIELD ( -5 )
2. Example
Create a FreeRTOS_demo directory under the Application project, and create rtos_demo.c and rtos_demo.h files in that directory; also add them to the Keil project.



rtos_demo.c
#include "stdio.h"#include "FreeRTOS.h"#include "task.h"#include "cmsis_os.h"// Task functionvoid Task1(void *pvParameters){ while (1) { printf("Hello RTOS\r\n"); vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1000ms = 1s }}
rtos_demo.h
#ifndef _RTOS_DEMO_H_#define _RTOS_DEMO_H_#include "FreeRTOS.h"#include "task.h"#include "cmsis_os.h"#include "rtos_demo.h"// Task functionvoid Task1(void *pvParameters);#endif /* _RTOS_DEMO_H_ */
In the main function, use rtos_demo.h to call xTaskCreate to create a task.

3. Effect Display
Reply with the source code to get the Gitee link.If you find this article helpful, please give it a follow~