当前位置:网站首页>ESP32_ FreeRTOS_ Arduino_ 1_ Create task

ESP32_ FreeRTOS_ Arduino_ 1_ Create task

2022-07-06 03:48:00 Colorful 2022

About FreeRTOS stay arduino Applications in the environment

One 、 About FreeRTOS

1、 What is? FreeRTOS

FreeRTOS It is a real-time operating system running on a microcontroller , Can effectively manage tasks , Allocate hardware resources reasonably .

for instance , When we are using Windows perhaps Linux when , You can open multiple processes and Applications , It seems to be running at the same time . But for single core computers , Only one process can be executed at a time . So the computer actually switches between multiple tasks very quickly , So that users feel that everything is running at the same time .

The operating system provides us with a reasonable task scheduler mechanism , And manage the resources needed for these tasks , To ensure that each thread can be executed correctly , And meet its resource needs .

Here we have a new concept . Task scheduling mechanism , Don't look at the scheduling mechanism first , Let's look at the task first .

2、 What is a mission

Task is the basic module of real-time operating system , They execute in their own context ( This part of the concept , If necessary, please refer to some of my articles on the operating system ).

PS: Task switching involves context switching , In the multithreading section of my operating system column, I touch on .

And the scheduling mechanism , Is responsible for the decision CPU Which task should be performed at a certain time . If multiple tasks are executed concurrently , If there is no scheduling mechanism , It will be very bad, very bad , Here are specific examples .

3、 Suggest

Knowledge of the operating system , I will make a special summary , Those who need it can learn , It is suggested that beginners learn , It can be downloaded from Arduino Of FreeRTOS Learn and experience first , Then go to learn the concept of operating system systematically .

Two 、 establish FreeRTOS Mission

Let's start with Arduino in FreeRTOS Simple application of , In this example, we will first create two prints "hello world" Message task , Then delete them .

1、xTaskCreate Task creation function

API Parameters

  • pvTaskCode
    • A function pointer , Used to pass the function responsible for task implementation
  • pcName
    • The name of the task
  • usStackDepth
    • Task stack size , In bytes , Usually a large enough value will be used
  • pvParameters
    • The pointer , Point to the parameters received by the task function , The type must be (void *)
  • uxPriority
    • Task priority
  • TaskHandle_t
    • Returns a handle , It is used for the reference to the task when calling the function in the future
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )

	static inline IRAM_ATTR BaseType_t xTaskCreate(
			TaskFunction_t pvTaskCode,
			const char * const pcName,
			const uint32_t usStackDepth,
			void * const pvParameters,
			UBaseType_t uxPriority,
			TaskHandle_t * const pvCreatedTask)
	{
    
		return xTaskCreatePinnedToCore( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pvCreatedTask, tskNO_AFFINITY );
	}

#endif

2、setup function and Loop code

Serial port settings

First of all we need to Setup Function , Open a serial connection , Used to output the running results of the test program .

Serial.begin(115200);
delay(1000);

Create a task example

#include<Arduino.h>

#include<FreeRTOS.h>

void taskOne( void * parameter )
{
    
  for( int i = 0;i<10;i++ ){
    
    Serial.println("Hello from task 1");
    delay(1000);
  }
  Serial.println("Ending task 1");
  vTaskDelete( NULL );
}

void taskTwo( void * parameter)
{
    
  for( int i = 0;i<10;i++ ){
    
    Serial.println("Hello from task 2");
    delay(1000);
  }
  Serial.println("Ending task 2");
  vTaskDelete( NULL );
}

void setup() {
    
  Serial.begin(112500);
  delay(1000);

  xTaskCreate(
    taskOne,          /* Task function. */
    "TaskOne",        /* String with name of task. */
    10000,            /* Stack size in bytes. */
    NULL,             /* Parameter passed as input of the task */
     1,                /* Priority of the task. */
    NULL);            /* Task handle. */

  xTaskCreate(
    taskTwo,          /* Task function. */
    "TaskTwo",        /* String with name of task. */
    10000,            /* Stack size in bytes. */
    NULL,             /* Parameter passed as input of the task */
    1,                /* Priority of the task. */
     NULL);            /* Task handle. */
}

void loop() {
    
  delay(1000);
}

Program execution result

 Insert picture description here

原网站

版权声明
本文为[Colorful 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132308102459.html