You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ESP32-S3 is based on the dual-core XTensa LX7, which can run code separately on two cores. This is enabled through FreeRTOS, by setting up tasks that run on each core (similarly to how `void loop()` is implemented). The cores available are `0` and `1`.
456
+
457
+
The example below is a modified version of the [BasicMultiThreading](https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples/FreeRTOS/BasicMultiThreading) example found in the Arduino ESP32 core, and demonstrates how to use two common operations simultaneously:
458
+
- Blink an LED using one task on a specific core (0),
459
+
- Read an analog pin using a second task on a specific core (1).
460
+
461
+
```arduino
462
+
/* Basic Multi Threading Arduino Example
463
+
464
+
Modified 16th October 2023 by Karl Söderby
465
+
466
+
Set up two tasks that run on each core of a Nano ESP32 (ESP32-S3 XTensa LX7 MCU),
467
+
one that blinks an LED, one that reads an analog signal.
468
+
469
+
These tasks will execute infinitely.
470
+
471
+
This example code is in the Public Domain (or CC0 licensed, at your option.)
472
+
Unless required by applicable law or agreed to in writing, this
473
+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
474
+
CONDITIONS OF ANY KIND, either express or implied.
475
+
*/
476
+
477
+
// Define the cores
478
+
#define CORE_0 0
479
+
#define CORE_1 1
480
+
481
+
#define ANALOG_INPUT_PIN A0 //Specify analog pin
482
+
#define LED_BUILTIN 13 // Specify the on which is your LED
483
+
484
+
485
+
int counter = 0;
486
+
// Define two tasks for Blink & AnalogRead.
487
+
void TaskBlink(void *pvParameters);
488
+
void TaskAnalogRead(void *pvParameters);
489
+
TaskHandle_t analog_read_task_handle; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
490
+
491
+
void setup() {
492
+
Serial.begin(115200);
493
+
uint32_t blink_delay = 1000; // Delay between changing state on LED pin
494
+
495
+
//create task for blinking an LED
496
+
xTaskCreatePinnedToCore(
497
+
TaskBlink, "Task Blink" // A name just for humans
498
+
,
499
+
2048 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
500
+
,
501
+
(void *)&blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
502
+
,
503
+
2 // Priority
504
+
,
505
+
NULL // Task handle is not used here - simply pass NULL
506
+
,
507
+
CORE_0 // Core on which the task will run
508
+
);
509
+
510
+
//create a task for reading analog signals
511
+
xTaskCreatePinnedToCore(
512
+
TaskAnalogRead, "Analog Read", 2048 // Stack size
513
+
,
514
+
NULL // When no parameter is used, simply pass NULL
515
+
,
516
+
1 // Priority
517
+
,
518
+
&analog_read_task_handle // With task handle we will be able to manipulate with this task.
519
+
,
520
+
CORE_1 // Core on which the task will run
521
+
);
522
+
}
523
+
524
+
void loop() {
525
+
//loop is empty, the tasks are instead looped infinitely
526
+
}
527
+
528
+
void TaskBlink(void *pvParameters) { // This is a task.
- The task is created in the `xTaskCreatePinnedToCore()`,
569
+
- inside `xTaskCreatePinnedToCore()` we specify a number of parameters, most importantly what **core** and what **function** to run,
570
+
- code inside task functions are placed inside the `for (;;){}` statement, that will loop infinitely.
571
+
572
+
***More information about dual-core on the ESP32 along with a detailed explanation of the example is available at [Basic Multi Threading Example](https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples/FreeRTOS/BasicMultiThreading).***
0 commit comments