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