Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESP32 - Interrupt on 2. core

Tags:

esp32

I just wanted to know if I did this right:

I created a task on the 2nd core and inserted an interrupt in it.

xTaskCreatePinnedToCore(coreTask1, "CPU_1", 1000, NULL, 0, &Core1TaskHnd, 1); //Creating Task on Core 1

void coreTask1(void *parameter) //The Task I call
{
  attachInterrupt(2, encoderEvent_Motor1, CHANGE); 

  //attachInterrupt(2, encoderEvent_Motor2, CHANGE); //TODO
  //attachInterrupt(2, encoderEvent_Motor3, CHANGE); //TODO
  //attachInterrupt(2, encoderEvent_Motor4, CHANGE); //TODO

  Serial.println(xPortGetCoreID());

  while (1)
  {
    /* code */
  }
}

My question is: Is the interrupt really running on the 2nd core.

like image 672
Teebow Avatar asked Oct 15 '25 21:10

Teebow


1 Answers

According to ESP-IDF's documentation, "External Peripheral Interrupts":

Allocating an external interrupt will always allocate it on the core that does the allocation.

So, yes, when you set up an interrupt handler, the interrupt will be handler by the core that set up the handler.

That said, it's unnecessary to create a task to pin it to the second core (core 1) as Arduino code automatically runs on that core. The first core (core 0) runs the network stack.

You can test this by calling xPortGetCoreID() in Setup() as you did in your task:

Serial.print("Current CPU core ");
Serial.println(xPortGetCoreID());

You should see "Current CPU core 1" as output (the cores are normally numbered 0 and 1).

You can also test that your interrupt handler is running on core 1 by calling this from it. For extra credit, have the interrupt handler increment a counter every time it runs on core 0 and then check the counter periodically from your main program and output a message if it's ever not zero.

like image 157
romkey Avatar answered Oct 18 '25 09:10

romkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!