Skip to content

Commit f189822

Browse files
committed
Modified original example
1 parent dd2e474 commit f189822

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed
Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
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+
122
#if CONFIG_FREERTOS_UNICORE
223
#define ARDUINO_RUNNING_CORE 0
324
#else
@@ -8,50 +29,55 @@
829
#define LED_BUILTIN 13
930
#endif
1031

11-
// define two tasks for Blink & AnalogRead
32+
// Define two tasks for Blink & AnalogRead.
1233
void TaskBlink( void *pvParameters );
1334
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.
1436

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.
1638
void setup() {
17-
18-
// initialize serial communication at 115200 bits per second:
39+
// Initialize serial communication at 115200 bits per second:
1940
Serial.begin(115200);
2041

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(
2345
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.
2749
, 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+
);
3052

53+
// This variant of task creation can also for the task to run specified core
3154
xTaskCreatePinnedToCore(
3255
TaskAnalogReadA3
3356
, "AnalogReadA3"
3457
, 1024 // Stack size
35-
, NULL
58+
, NULL // When no parameter is used, simply pass NULL
3659
, 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+
);
3963

4064
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
4165
}
4266

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+
}
4673
}
4774

4875
/*--------------------------------------------------*/
4976
/*---------------------- Tasks ---------------------*/
5077
/*--------------------------------------------------*/
5178

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;
5581

5682
/*
5783
Blink
@@ -64,19 +90,17 @@ void TaskBlink(void *pvParameters) // This is a task.
6490
// initialize digital LED_BUILTIN on pin 13 as an output.
6591
pinMode(LED_BUILTIN, OUTPUT);
6692

67-
for (;;) // A Task shall never return or exit.
68-
{
93+
for (;;){ // A Task shall never return or exit.
6994
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
7095
// arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz and portTICK_PERIOD_MS
7196
// 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);
7398
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);
75100
}
76101
}
77102

78-
void TaskAnalogReadA3(void *pvParameters) // This is a task.
79-
{
103+
void TaskAnalogReadA3(void *pvParameters){ // This is a task.
80104
(void) pvParameters;
81105

82106
/*
@@ -88,12 +112,11 @@ void TaskAnalogReadA3(void *pvParameters) // This is a task.
88112
This example code is in the public domain.
89113
*/
90114

91-
for (;;)
92-
{
115+
for (;;){
93116
// read the input on analog pin A3:
94117
int sensorValueA3 = analogRead(A3);
95118
// print out the value you read:
96119
Serial.println(sensorValueA3);
97-
vTaskDelay(100 / portTICK_PERIOD_MS); // 100ms delay
120+
delay(100); // 100ms delay
98121
}
99122
}

0 commit comments

Comments
 (0)