Interrupt change has a 38us delay

italocjs
Posts: 11
Joined: Thu Mar 10, 2022 6:15 pm

Interrupt change has a 38us delay

Postby italocjs » Mon Oct 24, 2022 10:33 pm

I'm making a simple program to measure for how long a pin stays on, and i'm getting ~42us delay on interrupt change routine exemplified below, any ideas on how to improve this code? Outside the interrupt i'm running a calculation formula once every second, inside the main loop, which reads instant_ciclo_us and InstantON_uS. I've seen the same issue on Stackoverflow, but no solution there

Signal input: Toff = 117500us Ton = 2500us
esp32 reading: Toff 117539us Ton=2462us

Code: Select all

volatile unsigned long TotalON_uS, InstantON_uS, Instant_ciclo_uS; // volatile garante que a variavel é lida sempre, evita ler valor desatualizado
void IRAM_ATTR interrupt_pulso1()
{
  static unsigned long _micros1, _micros2, _last_micros1 = 1; 
  if (gpio_get_level(GPIO_NUM_27) == HIGH) 
  {
    _micros1 = micros();
    Instant_ciclo_uS = (_micros1 - _last_micros1);
    _last_micros1 = _micros1;
  }
  else
  {
    _micros2 = micros();
    if (_micros2 > _micros1)
    {
      InstantON_uS = (_micros2 - _micros1);
      TotalON_uS += InstantON_uS;
    }
  }
}
On my setup, i'm using:

Code: Select all

void setup(void)
{
    attachInterrupt(input1_pin, interrupt_pulso1, CHANGE); //input1 expand into GPIO27
}
Sorry if this has been posted before, i tried looking in the forum but the search function is returning error 500.

username
Posts: 532
Joined: Thu May 03, 2018 1:18 pm

Re: Interrupt change has a 38us delay

Postby username » Wed Oct 26, 2022 11:49 pm

You can knock off the long time it takes to read a pin state when using gpio_get_level(GPIO_NUM_27).

Try using this instead.
#define INPUT1_PIN GPIO_NUM_27
#define INPUT1_PIN_READ() (REG_READ(GPIO_IN1_REG) >> (INPUT1_PIN - 32))


if ((uint8_t)INPUT1_PIN_READ() )
{}

Who is online

Users browsing this forum: No registered users and 70 guests