Advertisement
pleasedontcode

ESP32 dual core tasks

Jan 26th, 2025
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.62 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2.  
  3. // Task handles
  4. TaskHandle_t Task1;
  5. TaskHandle_t Task2;
  6.  
  7. // Task function for Core 0
  8. void Task0code(void *parameter) {
  9.   while (true) {
  10.     Serial.print("Task 0 is running on core ");
  11.     Serial.println(xPortGetCoreID());
  12.     delay(2000); // Simulate a slower background task
  13.   }
  14. }
  15.  
  16. // Task function for Core 1
  17. void Task1code(void *parameter) {
  18.   while (true) {
  19.     Serial.print("Task 1 is running on core ");
  20.     Serial.println(xPortGetCoreID());
  21.     delay(1000); // Simulate a faster real-time task
  22.   }
  23. }
  24.  
  25. void setup() {
  26.   // Initialize Serial Monitor
  27.   Serial.begin(115200);
  28.   delay(1000); // Give Serial Monitor time to initialize
  29.   Serial.println("Starting dual-core tasks...");
  30.  
  31.   // Create Task for Core 0
  32.   xTaskCreatePinnedToCore(
  33.     Task0code,        // Task function
  34.     "Task0",          // Name of the task
  35.     10000,            // Stack size in bytes
  36.     NULL,             // Parameter to pass to the task
  37.     1,                // Task priority
  38.     &Task1,           // Task handle
  39.     0                 // Core 0
  40.   );
  41.  
  42.   // Create Task for Core 1
  43.   xTaskCreatePinnedToCore(
  44.     Task1code,        // Task function
  45.     "Task1",          // Name of the task
  46.     10000,            // Stack size in bytes
  47.     NULL,             // Parameter to pass to the task
  48.     1,                // Task priority
  49.     &Task2,           // Task handle
  50.     1                 // Core 1
  51.   );
  52. }
  53.  
  54. void loop() {
  55.   // Main loop runs on Core 1 by default
  56.   Serial.println("Loop task is running on core " + String(xPortGetCoreID()));
  57.   delay(3000); // Simulate a general task in the loop
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement