Advertisement
mikroavr

esp32_multi_task

Oct 22nd, 2023
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TaskHandle_t Task1;
  2. TaskHandle_t Task2;
  3. TaskHandle_t Task3;
  4. unsigned long previousTime, previousTime1, previousTime2, previousTime3;
  5. void setup() {
  6.   Serial.begin(115200);
  7.   //create a task on core 0 that will be execute task1Func() with priority 10
  8.   xTaskCreatePinnedToCore(
  9.     task1Func,    /* Task function. */
  10.     "Task1",        /* name of task. */
  11.     10000,            /* Stack size of task */
  12.     NULL,               /* parameter of the task */
  13.     10,                     /* priority of the task */
  14.     &Task1,           /* Task handle to keep track of created task */
  15.     0);                   /* pin task to core 0 */
  16.   delay(500);
  17.   //create a task on core 1 that will be execute task2Func() with priority 9
  18.   xTaskCreatePinnedToCore(
  19.     task2Func,    /* Task function. */
  20.     "Task2",        /* name of task. */
  21.     10000,            /* Stack size of task */
  22.     NULL,               /* parameter of the task */
  23.     9,                    /* priority of the task */
  24.     &Task2,           /* Task handle to keep track of created task */
  25.     1);                   /* pin task to core 1 */
  26.   delay(500);
  27.  
  28.   xTaskCreatePinnedToCore(
  29.     task3Func,
  30.     "Task3",
  31.     10000,
  32.     NULL,
  33.     8,
  34.     &Task3,
  35.     0);
  36. }
  37. void task1Func( void * pvParameters ) {
  38.   for (;;) {
  39.     Serial.printf("%s running on core %d (priorite %d) - %dms\n", "Task1", xPortGetCoreID(), uxTaskPriorityGet( NULL ), millis() - previousTime1);
  40.     previousTime1 = millis();
  41.     delay(200);//vTaskDelay( pdMS_TO_TICKS( 200 ) );
  42.   }
  43. }
  44. void task2Func( void * pvParameters ) {
  45.   for (;;) {
  46.     Serial.printf("%s running on core %d (priorite %d) - %dms\n", "Task2", xPortGetCoreID(), uxTaskPriorityGet( NULL ), millis() - previousTime2);
  47.     previousTime2 = millis();
  48.     delay(600);
  49.     //vTaskDelay( pdMS_TO_TICKS( 600 ) );
  50.   }
  51. }
  52. void task3Func( void *pvParameters )
  53. {
  54.   for ( ;; )
  55.   {
  56.     Serial.printf("%s running on core %d (priorite %d) - %dms\n", "Task3", xPortGetCoreID(), uxTaskPriorityGet( NULL ), millis() - previousTime3);
  57.     previousTime3 = millis();
  58.     delay(100);
  59.     //vTaskDelay( pdMS_TO_TICKS( 100 ) );
  60.   }
  61. }
  62. void loop() {
  63.   Serial.printf("%s running on core %d (priorite %d) - %dms\n", "loop()", xPortGetCoreID(), uxTaskPriorityGet( NULL ), millis() - previousTime);
  64.   previousTime = millis();
  65.   delay(500);
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement