pierremolinaro / acan2515

MCP2515 CAN Controller Driver for Arduino
MIT License
77 stars 30 forks source link

ESP32 crashes when irq pin set to -1 and inInterruptServiceRoutine is NULL #40

Open ElektroBOXua opened 1 year ago

ElektroBOXua commented 1 year ago

When there are no IRQ pin specified, acan2515 requires inInterruptServiceRoutine to be NULL

There is no check for mINT != 255 thus this task is still created

    #ifdef ARDUINO_ARCH_ESP32
      xTaskCreate (myESP32Task, "ACAN2515Handler", 1024, this, 256, NULL) ;
    #endif

It also seems that 1024 is not enough stack size for the task on my esp32 because i've got stack cannary watchpoint triggered before noticing the main issue(Probably stack overflow).

After i fixed the problem it appeared that attachMCP2515InterruptPin attaches NULL to -1 pin which causes crash.

#ifdef ARDUINO_ARCH_ESP32
  static void myESP32Task (void * pData) {
    ACAN2515 * canDriver = (ACAN2515 *) pData ;
    while (1) {
      canDriver->attachMCP2515InterruptPin () ;
      xSemaphoreTake (canDriver->mISRSemaphore, portMAX_DELAY) ;
      bool loop = true ;
      while (loop) {
        loop = canDriver->isr_core () ;
      }
    }
  }
#endif

I tried to remove iterrupt task at all, but then i noticed that there are no mechanisms to poll messages, so it would not work anyway.

I didn't look more throught the code, so i am curious how to use this library without an IRQ pin on ESP32?

ElektroBOXua commented 1 year ago

Oh, i have found a solution for the issue by replacing:

#ifdef ARDUINO_ARCH_ESP32
  void ACAN2515::attachMCP2515InterruptPin (void) {
    attachInterrupt (digitalPinToInterrupt (mINT), mInterruptServiceRoutine, ONLOW) ;
  }
#endif

to

#ifdef ARDUINO_ARCH_ESP32
  void ACAN2515::attachMCP2515InterruptPin (void) {
    if (mINT != 255) {
    attachInterrupt (digitalPinToInterrupt (mINT), mInterruptServiceRoutine, ONLOW) ;
    }
  }
#endif

Also i've noticed that there's a poll function, which is actually what i've needed. Issue can be closed.