Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and delete tasks in FreeRTOS real-time operating system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to create and delete tasks of the FreeRTOS real-time operating system". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to create and delete tasks of the FreeRTOS real-time operating system" can help you solve your doubts.

Preface

In the article on porting FreeRTOS to the Cortex-M3 hardware platform, we have seen task creation API, but that article focuses on how to migrate FreeRTOS, and this article focuses on task creation and deletion of API functions.

The task creation and deletion API functions are located in the file task.c and need to include the task.h header file.

1. Task creation 1.1 function description BaseType_t xTaskCreate (TaskFunction_t pvTaskCode, const char * const pcName, unsigned short usStackDepth, void * pvParameters, UBaseType_t uxPriority) TaskHandle_t * pvCreatedTask)

Create a new task and add to the task ready list.

If you use FreeRTOS-MPU (in the official download package, two migration schemes are written for the Cortex-M3 kernel, one is the normal FreeRTOS migration layer, and the other is the FreeRTOS-MPU migration layer. The latter includes full memory protection), so it is recommended to use the function xTaskCreateRestricted () instead of xTaskCreate (). In the case of FreeRTOS-MPU, use the xTaskCreate () function to create tasks that run in privileged mode or user mode (see the description of the function parameter uxPriority below). When running in privileged mode, the task can access the entire memory map; when in user mode, the task can only access its own stack. In any mode, MPU does not automatically catch stack overflows, so the standard FreeRTOS stack overflow detection mechanism is still used. The xTaskCreateRestricted () function is more flexible.

1.2 Parameter description

PvTaskCode: pointer to the entry of the task function. The task never returns (in an endless loop). The parameter type TaskFunction_t is defined in the file projdefs.h, defined as: typedefvoid (* TaskFunction_t) (void *).

PcName: task description. Mainly used for debugging. The maximum length of the string is specified by the macro configMax _ TASK_NAME_LEN, which is located in the FreeRTOSConfig.h file.

UsStackDepth: specifies the task stack size and the number of stack variables that can be supported instead of bytes. For example, under a 16-bit stack, where usStackDepth is defined as 100, 200 bytes of stack storage space is actually used. The width of the stack multiplied by the depth must not exceed the maximum that the size_t type can represent. For example, if size_t is 16 bits, the maximum value that can be expressed is 65535.

PvParameters: pointer, passed to the task as a parameter when the task is created.

UxPriority: priority of the task. Systems with MPU support can arbitrarily create tasks in privileged (system) mode by setting the portPRIVILEGE_BIT bit of the priority parameter. For example, create a privileged task with priority 2, and the parameter uxPriority can be set to (2 | portPRIVILEGE_BIT).

PvCreatedTask: used to return a handle (ID) that can be used to reference a task after it is created.

1.3 return value

& & return pdPASS if the task is successfully created and join the ready list function, otherwise the function returns an error code. For more information, please see projdefs.h.

1.4 example usage / * create a task. * / void vTaskCode (void * pvParameters) {for (;;) {/ * task code is put here * /}} / * create task function * / void vOtherFunction (void) {static unsigned char ucParameterToPass; xTaskHandlexHandle; / * create task and store handle. Note: the passed parameter ucParameterToPass must have the same lifetime as the task, so it is defined here as a static variable. If it is just an automatic variable, it may not have a long lifetime, because interrupts and high-priority tasks may use it. * / xTaskCreate (vTaskCode, "NAME", STACK_SIZE,&ucParameterToPass, tskIDLE_PRIORITY, & xHandle); / * use the handle to delete the task. * / if (xHandle! = NULL) {vTaskDelete (xHandle);} 2. Task deletion 2.1 Task description

VoidvTaskDelete (TaskHandle_t xTask)

Delete a task from the RTOS kernel manager. After the task is deleted, it will be removed from the ready, blocking, pause, and event list. In the file FreeRTOSConfig.h, the macro include _ vTaskDelete must be defined as 1 for this function to be valid.

Note: deleted tasks, the storage space allocated by the kernel when the task is created, will be released by idle tasks. If an application calls xTaskDelete (), you must ensure that idle tasks get a certain amount of microcontroller processing time. Memory allocated by the task code itself is not automatically freed, so it should be freed before the task is deleted.

2.2 Parameter description

XTask: the handle to the deleted task. NULL means to delete the current task.

After reading this, the article "how to create and delete tasks of the FreeRTOS real-time operating system" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report