1
+ /*
2
+ This example demonstrates basic usage of FreeRTOS Task for multi threading.
3
+ Please refer to other examples in this folder to better utilize their the full potential and safe-guard potential problems.
4
+ It is also advised to read documentation on FreeRTOS web pages:
5
+ https://www.freertos.org/a00106.html
6
+
7
+ This example will blink builtin LED and read analog data.
8
+ Additionally this example demonstrates usage of task handle, simply by deleting the analog
9
+ read task after 10 seconds from main loop by calling the function `vTaskDelete`.
10
+
11
+ Theory:
12
+ A task is simply a function which runs when the operating system (FreeeRTOS) sees fit.
13
+ This task can have an infinite loop inside if you want to do some work periodically for the entirety of the program run.
14
+ This however can create a problem - no other task will ever run and also the Watch Dog will trigger and your program will restart.
15
+ A nice behaving tasks knows when it useless to keep the processor for itself and gives it away for other tasks to be used.
16
+ This can be achieved by many ways, but the simplest is calling `delay(milliseconds)`.
17
+ During that delay any other task may run and do it's job.
18
+ When the delay runs out the Operating System gives the processor to the task which can continue.
19
+ For other ways to yield the CPU in task please see other examples in this folder.
20
+ */
21
+
1
22
#if CONFIG_FREERTOS_UNICORE
2
23
#define ARDUINO_RUNNING_CORE 0
3
24
#else
8
29
#define LED_BUILTIN 13
9
30
#endif
10
31
11
- // define two tasks for Blink & AnalogRead
32
+ // Define two tasks for Blink & AnalogRead.
12
33
void TaskBlink ( void *pvParameters );
13
34
void TaskAnalogReadA3 ( void *pvParameters );
35
+ TaskHandle_t task_handle; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
14
36
15
- // the setup function runs once when you press reset or power the board
37
+ // The setup function runs once when you press reset or power on the board.
16
38
void setup () {
17
-
18
- // initialize serial communication at 115200 bits per second:
39
+ // Initialize serial communication at 115200 bits per second:
19
40
Serial.begin (115200 );
20
41
21
- // Now set up two tasks to run independently.
22
- xTaskCreatePinnedToCore (
42
+ // Set up two tasks to run independently.
43
+ uint32_t blink_delay = 1000 ; // Delay between changing state on LED pin
44
+ xTaskCreate (
23
45
TaskBlink
24
- , " TaskBlink" // A name just for humans
25
- , 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
26
- , NULL
46
+ , " TaskBlink" // A name just for humans
47
+ , 1024 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
48
+ , ( void *) &blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
27
49
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
28
- , NULL
29
- , ARDUINO_RUNNING_CORE );
50
+ , NULL // Task handle is not used here
51
+ );
30
52
53
+ // This variant of task creation can also for the task to run specified core
31
54
xTaskCreatePinnedToCore (
32
55
TaskAnalogReadA3
33
56
, " AnalogReadA3"
34
57
, 1024 // Stack size
35
- , NULL
58
+ , NULL // When no parameter is used, simply pass NULL
36
59
, 1 // Priority
37
- , NULL
38
- , ARDUINO_RUNNING_CORE);
60
+ , task_handle // With task handle we will be able to manipulate with this task.
61
+ , ARDUINO_RUNNING_CORE // Core on which the task will run
62
+ );
39
63
40
64
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
41
65
}
42
66
43
- void loop ()
44
- {
45
- // Empty. Things are done in Tasks.
67
+ void loop (){
68
+ if (task_handle != NULL ){ // Make sure that the task actually exists
69
+ delay (10000 );
70
+ vTaskDelete (task_handle); // Delete task
71
+ task_handle = NULL ; // prevent calling vTaskDelete on non-existing task
72
+ }
46
73
}
47
74
48
75
/* --------------------------------------------------*/
49
76
/* ---------------------- Tasks ---------------------*/
50
77
/* --------------------------------------------------*/
51
78
52
- void TaskBlink (void *pvParameters) // This is a task.
53
- {
54
- (void ) pvParameters;
79
+ void TaskBlink (void *pvParameters){ // This is a task.
80
+ uint32_t blink_delay = (uint32_t ) *pvParameters;
55
81
56
82
/*
57
83
Blink
@@ -64,19 +90,17 @@ void TaskBlink(void *pvParameters) // This is a task.
64
90
// initialize digital LED_BUILTIN on pin 13 as an output.
65
91
pinMode (LED_BUILTIN, OUTPUT);
66
92
67
- for (;;) // A Task shall never return or exit.
68
- {
93
+ for (;;){ // A Task shall never return or exit.
69
94
digitalWrite (LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
70
95
// arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz and portTICK_PERIOD_MS
71
96
// refers to how many milliseconds the period between each ticks is, ie. 1ms.
72
- vTaskDelay ( 1000 / portTICK_PERIOD_MS ); // vTaskDelay wants ticks, not milliseconds
97
+ delay (blink_delay);
73
98
digitalWrite (LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
74
- vTaskDelay ( 1000 / portTICK_PERIOD_MS); // 1 second delay
99
+ delay (blink_delay);
75
100
}
76
101
}
77
102
78
- void TaskAnalogReadA3 (void *pvParameters) // This is a task.
79
- {
103
+ void TaskAnalogReadA3 (void *pvParameters){ // This is a task.
80
104
(void ) pvParameters;
81
105
82
106
/*
@@ -88,12 +112,11 @@ void TaskAnalogReadA3(void *pvParameters) // This is a task.
88
112
This example code is in the public domain.
89
113
*/
90
114
91
- for (;;)
92
- {
115
+ for (;;){
93
116
// read the input on analog pin A3:
94
117
int sensorValueA3 = analogRead (A3);
95
118
// print out the value you read:
96
119
Serial.println (sensorValueA3);
97
- vTaskDelay (100 / portTICK_PERIOD_MS); // 100ms delay
120
+ delay (100 ); // 100ms delay
98
121
}
99
122
}
0 commit comments