Simple Maths results in Guru Meditation Error

iamastic
Posts: 2
Joined: Fri Oct 15, 2021 8:05 pm

Simple Maths results in Guru Meditation Error

Postby iamastic » Fri Oct 15, 2021 8:24 pm

Hi,

I am working with the NodeMCU ESP32. I have a coin sensor built from two copper plates that detect when a coin rolls down the chute. I am using the interrupt function to detect the coin. It works well with the Arduino Uno R3, but when connected to the NodeMCU ESP32, a simple calculation in my function results in the following error:

Code: Select all

Guru Meditation Error: Core 1 panic'ed (Coprocessor exception)
It goes on to show me the Core 1 register dump, and then says:

Code: Select all

Core 1 was running in ISR context
Here is the breakdown of the code I am using:

1) I set up the interrupt on GPIO 17:

Code: Select all

attachInterrupt( digitalPinToInterrupt(INPUT_PIN_COIN), Interrupt_CoinDeposit, FALLING );
2) The Interrupt_CoinDeposit() calls another function:

Code: Select all

void IRAM_ATTR Interrupt_CoinDeposit()
{
    g_crOSCore.EnqueueCoin();
}
3) The Enqueue Coin function is supposed to check if one second has passed since the first time the coin struck the copper plates. This is to avoid the same coin being counted multiple times due to contact bounce:

Code: Select all

bool CCrowboxCore::EnqueueCoin()              
{
    //cros_time_t is a typedef of float
    cros_time_t n = GetUptimeSeconds();
    Serial.println(n);

    cros_time_t m = m_uptimeLastCoinDetected;
    Serial.println(m);

    cros_time_t x = n - m;
    Serial.println(x);

   if (x < 1.0) {      
      return false;
    }
  
    m_numEnqueuedDeposits++;    
    m_uptimeLastCoinDetected = GetUptimeSeconds();
    return true;
}
Notes:

Code: Select all

GetUptimeSeconds();
is a function that returns the time, in type float, that the NodeMCU has been running.

Code: Select all

m_uptimeLastCoinDetected
is another float that has been initiated in the setup() function.

My code runs into issues when I reach the

Code: Select all

x = n - m
line. I have tested this by using print lines as well as commenting out sections of the code. This code works with the Arduino Uno R3 but I can't seem to figure out why I am getting a segmentation fault with the ESP32.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: Simple Maths results in Guru Meditation Error

Postby ESP_Sprite » Sat Oct 16, 2021 3:35 am

You're trying to do floating point math in an interrupt handler. Don't do that.

iamastic
Posts: 2
Joined: Fri Oct 15, 2021 8:05 pm

Re: Simple Maths results in Guru Meditation Error

Postby iamastic » Sat Oct 16, 2021 7:23 am

I see, that's interesting! Do you know why that is? Is it specific to the ESP32 only? Because it appears to work on the Arduino Uno R3 / atmega328p.

I will try converting to an integer then instead and see if that works. Thanks!

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: Simple Maths results in Guru Meditation Error

Postby ESP_Sprite » Mon Oct 18, 2021 12:43 am

It has something to do with the fact that floating point numbers in the ESP32 are accelerated by a FPU that has registers that need to be saved and loaded every time there is a task switch, which is kind-of expensive in processing terms. That takes time and floating point numbers are generally only used in one or two tasks in an embedded system, so FreeRTOS uses a smart trick to only load and save those registers when needed. The logic used for that doesn't work in an interrupt, though, hence the exception. An Arduino Uno has no such hardware, so doesn't need logic like that.

Who is online

Users browsing this forum: Majestic-12 [Bot], StuartsProjects and 112 guests