Skip to content

Commit 1c6a435

Browse files
committed
Moved info from example to README
1 parent 1a20317 commit 1c6a435

File tree

2 files changed

+113
-48
lines changed

2 files changed

+113
-48
lines changed
Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,20 @@
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-
Task creation has few parameter you should understand:
22-
xTaskCreate(TaskFunction_t pxTaskCode,
23-
const char * const pcName,
24-
const uint16_t usStackDepth,
25-
void * const pvParameters,
26-
UBaseType_t uxPriority,
27-
TaskHandle_t * const pxCreatedTask )
28-
29-
- pxTaskCode is the name of your function which will run as a task
30-
- pcName is a string of human readable description for your task
31-
- usStackDepth is number of words (word = 4B) available to the task. If you see error similar to this "Debug exception reason: Stack canary watchpoint triggered (Task Blink)" you should increase it
32-
- pvParameters is a parameter which will be passed to the task function - it must be explicitly converted to (void*) and in your function explicitly converted back to the intended data type.
33-
- uxPriority is number from 0 to configMAX_PRIORITIES which determines how the FreeRTOS will allow the tasks to run. 0 is lowest priority.
34-
- pxCreatedTask task handle is basically a pointer to the task which allows you to manipulate with the task - delete it, suspend and resume.
35-
If you don't need to do anything special with you task, simply pass NULL for this parameter.
36-
You can read more about task control here: https://www.freertos.org/a00112.html
37-
*/
1+
// Please read file README.md in the folder containing this example.
382

393
#if CONFIG_FREERTOS_UNICORE
404
#define ARDUINO_RUNNING_CORE 0
415
#else
426
#define ARDUINO_RUNNING_CORE 1
437
#endif
448

9+
#define ANALOG_INPUT_PIN 27
10+
4511
#ifndef LED_BUILTIN
46-
#define LED_BUILTIN 13
12+
#define LED_BUILTIN 13 // Specify the on which is your LED
4713
#endif
4814

4915
// Define two tasks for Blink & AnalogRead.
5016
void TaskBlink( void *pvParameters );
51-
void TaskAnalogReadA3( void *pvParameters );
17+
void TaskAnalogRead( void *pvParameters );
5218
TaskHandle_t task_handle; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
5319

5420
// The setup function runs once when you press reset or power on the board.
@@ -67,10 +33,10 @@ void setup() {
6733
, NULL // Task handle is not used here - simply pass NULL
6834
);
6935

70-
// This variant of task creation can also for the task to run specified core
36+
// This variant of task creation can also specify on which core it will be run (only relevant for multi-core ESPs)
7137
xTaskCreatePinnedToCore(
72-
TaskAnalogReadA3
73-
, "Analog Read A3"
38+
TaskAnalogRead
39+
, "Analog Read"
7440
, 1024 // Stack size
7541
, NULL // When no parameter is used, simply pass NULL
7642
, 1 // Priority
@@ -82,13 +48,11 @@ void setup() {
8248
}
8349

8450
void loop(){
85-
/*
8651
if(task_handle != NULL){ // Make sure that the task actually exists
8752
delay(10000);
8853
vTaskDelete(task_handle); // Delete task
8954
task_handle = NULL; // prevent calling vTaskDelete on non-existing task
9055
}
91-
*/
9256
}
9357

9458
/*--------------------------------------------------*/
@@ -119,7 +83,7 @@ void TaskBlink(void *pvParameters){ // This is a task.
11983
}
12084
}
12185

122-
void TaskAnalogReadA3(void *pvParameters){ // This is a task.
86+
void TaskAnalogRead(void *pvParameters){ // This is a task.
12387
(void) pvParameters;
12488

12589
/*
@@ -132,10 +96,10 @@ void TaskAnalogReadA3(void *pvParameters){ // This is a task.
13296
*/
13397

13498
for (;;){
135-
// read the input on analog pin A3:
136-
int sensorValueA3 = analogRead(A3);
99+
// read the input on analog pin:
100+
int sensorValue = analogRead(ANALOG_INPUT_PIN);
137101
// print out the value you read:
138-
Serial.println(sensorValueA3);
102+
Serial.println(sensorValue);
139103
delay(100); // 100ms delay
140104
}
141105
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Basic Multi Threading
2+
3+
This example demonstrates basic usage of FreeRTOS Tasks for multi threading.
4+
5+
Please refer to other examples in this folder to better utilize their the full potential and safe-guard potential problems.
6+
It is also advised to read documentation on FreeRTOS web pages:
7+
[https://www.freertos.org/a00106.html](https://www.freertos.org/a00106.html)
8+
9+
This example will blink builtin LED and read analog data.
10+
Additionally this example demonstrates usage of task handle, simply by deleting the analog
11+
read task after 10 seconds from main loop by calling the function `vTaskDelete`.
12+
13+
### Theory:
14+
A task is simply a function which runs when the operating system (FreeeRTOS) sees fit.
15+
This task can have an infinite loop inside if you want to do some work periodically for the entirety of the program run.
16+
This however can create a problem - no other task will ever run and also the Watch Dog will trigger and your program will restart.
17+
A nice behaving tasks knows when it is useless to keep the processor for itself and gives it away for other tasks to be used.
18+
This can be achieved by many ways, but the simplest is calling `delay(milliseconds)`.
19+
During that delay any other task may run and do it's job.
20+
When the delay runs out the Operating System gives the processor to the task which can continue.
21+
For other ways to yield the CPU in task please see other examples in this folder.
22+
It is also worth mentioning that two or more tasks running the exact same function will run them with separated stack, so if you want to run the same code (could be differentiated by the argument) there is no need to have multiple copies of the same function.
23+
24+
**Task creation has few parameters you should understand:**
25+
```
26+
xTaskCreate(TaskFunction_t pxTaskCode,
27+
const char * const pcName,
28+
const uint16_t usStackDepth,
29+
void * const pvParameters,
30+
UBaseType_t uxPriority,
31+
TaskHandle_t * const pxCreatedTask )
32+
```
33+
- **pxTaskCode** is the name of your function which will run as a task
34+
- **pcName** is a string of human readable description for your task
35+
- **usStackDepth** is number of words (word = 4B) available to the task. If you see error similar to this "Debug exception reason: Stack canary watchpoint triggered (Task Blink)" you should increase it
36+
- **pvParameters** is a parameter which will be passed to the task function - it must be explicitly converted to (void*) and in your function explicitly converted back to the intended data type.
37+
- **uxPriority** is number from 0 to configMAX_PRIORITIES which determines how the FreeRTOS will allow the tasks to run. 0 is lowest priority.
38+
- **pxCreatedTask** task handle is basically a pointer to the task which allows you to manipulate with the task - delete it, suspend and resume.
39+
If you don't need to do anything special with you task, simply pass NULL for this parameter.
40+
You can read more about task control here: https://www.freertos.org/a00112.html
41+
42+
# Supported Targets
43+
44+
This example supports all SoCs.
45+
46+
## How to Use Example
47+
48+
Read this documentation and the code and try to understand it.
49+
When you flash it to your ESP, open a Serial Plotter and observe the analog signal.
50+
51+
* How to install the Arduino IDE: [Install Arduino IDE](https://github.com/espressif/arduino-esp32/tree/master/docs/arduino-ide).
52+
53+
### Hardware Connection
54+
55+
If your board does not have built in LED, please connect one to the pin specified by the `LED_BUILTIN` in code (you can also change the number and connect it on pin you desire).
56+
57+
Optionally you can connect analog element to pin ? such as variable resistor, or analog input such as audio signal, or any signal generator. However if the pin is left unconnected it will receive background noise and you will also see change in the signal when the pin is touched by finger.
58+
Please refer to the ESP-IDF ADC documentation for specific SoC for info which pins are available:
59+
[ESP32](https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32/api-reference/peripherals/adc.html),
60+
[ESP32-S2](https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32s2/api-reference/peripherals/adc.html),
61+
[ESP32-S3](https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32s3/api-reference/peripherals/adc.html),
62+
[ESP32-C3](https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32c3/api-reference/peripherals/adc.html)
63+
64+
65+
#### Using Arduino IDE
66+
67+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
68+
69+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
70+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
71+
72+
#### Using Platform IO
73+
74+
* Select the COM port: `Devices` or set the `upload_port` option on the `platformio.ini` file.
75+
76+
## Troubleshooting
77+
78+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
79+
80+
* **LED not blinking:** Check the wiring connection and the IO selection.
81+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
82+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
83+
84+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
85+
86+
## Contribute
87+
88+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
89+
90+
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
91+
92+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
93+
94+
## Resources
95+
96+
* Official ESP32 Forum: [Link](https://esp32.com)
97+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
98+
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
99+
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
100+
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
101+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)

0 commit comments

Comments
 (0)