Page 1 of 1

Calling function from inside ISR doesn't work

Posted: Fri Nov 30, 2018 8:36 pm
by DokHasenbein
Hi,

I am new to ESP32 and did some modifications to this ESP32-Radio project https://github.com/Edzelf/ESP32-Radio
What I am trying now is to add some functionality to the IR code interpretation which lacks some functionality. As it is Interrupt driven I found it quite hard to debug. So i added a function morseDebug to allow me to see what's going on on my oscilloscope.
However it seems that this function doesn't get executed when called from inside the ISR. I don't understand why? I stripped the code down to a minimal example:

Code: Select all

#define sv DRAM_ATTR static volatile
#define PIN_IR_DEBUG 21
#define IR_PIN 13

portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;  // DOESN'T SEEM TO MAKE ANY DIFFERENCE

void IRAM_ATTR morseDebug( int n )
{
  for (int i; i < n; i++)
  {
    (*((volatile uint32_t *) (0x3ff44000 + 0x8 ))) ^= 1 << PIN_IR_DEBUG ; // USED THIS AS A FASTER ALTERNATIVE TO
    (*((volatile uint32_t *) (0x3ff44000 + 0xC))) ^= 1 << PIN_IR_DEBUG ; // digitalWrite ( PIN_IR_DEBUG, HIGH/LOW )
  }
}
//**************************************************************************************************
//                                          I S R _ I R                                            *
//**************************************************************************************************
// Interrupts received from VS1838B on every change of the signal.                                 *
// Intervals are 640 or 1640 microseconds for data.  syncpulses are 3400 micros or longer.         *
// Input is complete after 65 level changes.                                                       *
// Only the last 32 level changes are significant and will be handed over to common data.          *
//**************************************************************************************************
void IRAM_ATTR isr_IR()
{
  portENTER_CRITICAL(&mux);
  (*((volatile uint32_t *) (0x3ff44000 + 0x8 ))) ^= 1 << PIN_IR_DEBUG ; // THIS GETS EXECUTED
  (*((volatile uint32_t *) (0x3ff44000 + 0xC))) ^= 1 << PIN_IR_DEBUG ; // THIS GETS EXECUTED

  morseDebug ( 5 );                                // THIS DOESNT GET EXECUTED

  portEXIT_CRITICAL(&mux);
}

//**************************************************************************************************
//                                           S E T U P                                             *
//**************************************************************************************************
// Setup for the program.                                                                          *
//**************************************************************************************************
void setup()
{
  pinMode ( PIN_IR_DEBUG, OUTPUT );                      // FOR IR TESTING
  (*((volatile uint32_t *) (0x3ff44000 + 0x8 ))) ^= 1 << PIN_IR_DEBUG ; // THIS GETS EXECUTED
  (*((volatile uint32_t *) (0x3ff44000 + 0xC))) ^= 1 << PIN_IR_DEBUG ; // THIS GETS EXECUTED

  morseDebug ( 20 ); // THIS GETS EXECUTED
  if ( IR_PIN >= 0 )
  {
    pinMode ( IR_PIN, INPUT ) ;                // Pin for IR receiver VS1838B
    attachInterrupt ( IR_PIN,                  // Interrupts will be handle by isr_IR
                      isr_IR, CHANGE ) ;
  }
}

void loop()
{
  delay(100);
}

Re: Calling function from inside ISR doesn't work

Posted: Sat Dec 01, 2018 11:18 am
by bobtidey
Difficult to see what is going on as code example seems to be badly formatted onto one line.

It does look like there might be a missing semicolon after the digitalWrite in morseDebug

Re: Calling function from inside ISR doesn't work

Posted: Sat Dec 01, 2018 11:41 am
by FreddyVictor
I think it's a bit more simple than that ...

for (int i; i < n; i++)
change to:
for (int i=0; i < n; i++)
might fix it

Re: Calling function from inside ISR doesn't work

Posted: Sat Dec 01, 2018 9:28 pm
by DokHasenbein
Thank you FreddyVictor. That was it. How embarrassing :oops: