ESP32 Dual Core Programming
Introduction
The ESP32 is equipped with two 32-bit Tensilica Xtensa LX6 microprocessors, making it a powerful dual-core microcontroller. It features two independent CPU cores, Core 0 and Core 1, which can run concurrently. This dual-core architecture significantly enhances the ESP32's ability to handle complex tasks and support multitasking, providing improved performance for a wide range of applications.
While programming with Arduino IDE, the code only runs on Core1 because Core0 is already programmed for RF communication. In this tutorial we will show how to use both the cores of ESP32 to perform two operations simultaneously.
Core Features:
Cores: 2 × Tensilica Xtensa LX6 CPUs
Clock Speed: Up to 240 MHz (per core)
RAM: 520 KB SRAM (shared between cores), with external memory options like PSRAM available.
Flash Storage: Typically 4MB to 16MB (depending on the module).
Dual-core Parallelism: Both cores can run independent tasks, making the ESP32 suitable for handling multi-threaded applications or complex real-time systems.
Advantages of Multi-core processor -
Advantages of Dual-Core ESP32
1. Task Parallelism:
Improved Performance: By running different tasks on separate cores, you can improve overall system responsiveness. For example, one core can handle Wi-Fi communication, while the other handles sensor readings.
Multitasking: You can run multiple tasks simultaneously without them interfering with each other, which is ideal for applications with multiple sensors, communication protocols, or control loops.
2. Real-Time Processing:
The ESP32 can handle real-time tasks more effectively. Core 1 can be used for tasks that require low latency (like sensor data processing or motor control), while Core 0 can handle networking or background tasks.
3. Flexibility in Core Usage:
Both cores are equally powerful, so developer can configure the system as needed. For example, Core 0 can handle Wi-Fi/Bluetooth communications, and Core 1 can handle data processing or more computationally expensive tasks.
Low-Power Operation: You can also manage the power states of each core to save power in battery-powered applications.
4. Optimized for IoT Applications:
The ESP32’s dual-core architecture makes it especially useful for IoT projects where both communication and data processing are required simultaneously. For example, one core can manage Wi-Fi or Bluetooth communication with the cloud, while the other processes incoming sensor data.
Multi-core processors are useful when there are more than one processes to work simultaneously, as work is distributed among different cores, so multiple processes can be finished at the same time as Dual-core processors have to switch between different threads less often than single-core processors because they can handle two at once instead of one at a time.
Processor Cores:
Each core is based on the Xtensa LX6 architecture, which is a highly efficient 32-bit RISC (Reduced Instruction Set Computing) processor.
These cores can run at clock speeds of up to 240 MHz.
Processor Core 0 and Core 1:
While both cores are functionally equivalent, the way they are typically used can differ depending on the workload.
Core 0 (also referred to as the PRO CPU) handles lower-priority tasks, but it's the one that typically runs the FreeRTOS kernel, which controls task scheduling and management, it usually handles the main application logic, including Wi-Fi and Bluetooth operations.
Core 1 (also known as the APP CPU) is often dedicated to handling background tasks or real-time tasks (such as sensor data processing) to ensure that Core 0 is not overloaded. This core can be dedicated to running higher-level tasks, such as processing sensor data, managing complex logic, or handling time-sensitive operations.
The cores can either be run in parallel, performing independent tasks, or synchronously, where one core acts as a master and the other as a slave.
Both cores can operate independently, and software can assign tasks to either core, depending on how the application is designed.
Multitasking and Task Management:
The ESP32 uses FreeRTOS (a real-time operating system) for managing tasks and scheduling between the two cores. This allows for preemptive multitasking, where tasks can be prioritized and scheduled dynamically across the cores.
FreeRTOS enables efficient management of time-sensitive tasks by allowing one core to handle more important or urgent processes (e.g., network communication), while the other handles less time-critical tasks (e.g., sensor readings).
Communication Between Cores:
The cores can communicate with each other using inter-core interrupts or shared memory. The ESP32 provides IPC (Inter-Processor Communication) mechanisms for efficient communication.
Shared resources (like memory) must be managed carefully to avoid race conditions, which is crucial when using both cores in parallel.
Power Efficiency:
The dual-core design allows the ESP32 to balance performance and power consumption. When not in heavy use, one core can be put to sleep or deactivated to save power, leaving the other core active to handle minimal tasks.
This is especially useful for battery-powered applications.
Step 1: Connect Board to Laptop
Step 2: Dual Core Example Code
Select boar ESP32 DEV Module from Tools >> Boards menu, then select appropriate com port. Upload this program to ESP32.